jetpack models with signals + default settings

This commit is contained in:
zalun 2010-02-09 13:30:17 +00:00
Родитель 55568d07b5
Коммит 4d9a6fc4b8
9 изменённых файлов: 281 добавлений и 24 удалений

Просмотреть файл

@ -0,0 +1,21 @@
# Taken from feincms
# http://github.com/matthiask/feincms/blob/master/feincms/__init__.py
VERSION = (0, 0, 1)
__version__ = '.'.join(map(str, VERSION))
# Do not use Django settings at module level as recommended
from django.utils.functional import LazyObject
class LazySettings(LazyObject):
def _setup(self):
from jetpack import default_settings
self._wrapped = Settings(default_settings)
class Settings(object):
def __init__(self, settings_module):
for setting in dir(settings_module):
if setting == setting.upper():
setattr(self, setting, getattr(settings_module, setting))
settings = LazySettings()

Просмотреть файл

@ -0,0 +1,7 @@
from django.conf import settings
# ------------------------------------------------------------------------
JETPACK_SLUG_LENGTH = getattr(settings, 'JETPACK_SLUG_LENGTH', 10)
# TODO: make things work if True
JETPACK_NEW_IS_BASE = False

Просмотреть файл

@ -1,21 +1,49 @@
from django.db.models.signals import pre_save, post_save
from django.db import models
from django.contrib.auth.models import User
from jetpack import settings
class Jetpack(models.Model):
PERMISSIONS_CHOICEiS = (
PERMISSIONS_CHOICES = (
(0, 'denied'),
(1, 'view'),
(2, 'edit'),
)
slug = models.CharField(max_length=20)
name = models.CharField(max_length=255)
decription = models.TextField(blank=True, null=True)
author = models.ForeignKey(User, related_name="authored_jetpacks")
managers = models.ManyToManyField(User, related_name="managed_jetpacks")
developers = models.ManyToManyField(User, related_name="developed_jetpacks")
public_permission = models.IntegerField(choices=PERMISSIONS_CHOICEiS)
group_permission = models.IntegerField(choices=PERMISSIONS_CHOICEiS)
public_permission = models.IntegerField(choices=PERMISSIONS_CHOICES, default=2, blank=True)
group_permission = models.IntegerField(choices=PERMISSIONS_CHOICES, default=2, blank=True)
def set_slug(self):
from utils import random_string
check_slug = True
while check_slug:
self.slug = random_string(settings.JETPACK_SLUG_LENGTH)
try:
Jetpack.objects.get(slug=self.slug)
except:
check_slug = False
@property
def base_version(self):
try:
return Version.objects.get(jetpack__id=self.id, is_base=True)
except:
return None
def make_slug_on_create(instance, **kwargs):
if kwargs.get('raw',False): return
if not instance.id and not instance.slug:
instance.set_slug()
pre_save.connect(make_slug_on_create, sender=Jetpack)
@ -27,10 +55,67 @@ STATUS_CHOICES = (
class Version(models.Model):
jetpack = models.ForeignKey(Jetpack, related_name="versions")
name = models.CharField(max_length=255)
name = models.CharField(max_length=255, blank=True)
decription = models.TextField(blank=True, null=True)
code = models.TextField(blank=True, null=True)
status = models.CharField(max_length=1, choices=STATUS_CHOICES)
status = models.CharField(max_length=1, choices=STATUS_CHOICES, default='a', blank=True)
published = models.BooleanField(default=False, blank=True)
is_base = models.BooleanField(default=False, blank=True)
def default_name(instance, **kwargs):
"""
If name not given set from Jetpack
"""
if kwargs.get('raw', False): return
if not instance.name:
instance.name = instance.jetpack.name
pre_save.connect(default_name, sender=Version)
def first_is_base(instance, **kwargs):
"""
There is always a base version
"""
if kwargs.get('raw', False): return
if not instance.is_base:
try:
base_version = Version.objects.get(jetpack__id=instance.jetpack.id, is_base=True)
return
except:
instance.is_base = True
pre_save.connect(first_is_base, sender=Version)
def new_is_base(instance, **kwargs):
"""
Depending on settings new option has to be a base one one automatically
"""
if kwargs.get('raw', False): return
if instance.is_base: return
if not settings.JETPACK_NEW_IS_BASE: return
# TODO: something is bad here
instance.is_base = True
pre_save.connect(new_is_base, sender=Version)
def unbase_other_base_version(instance, **kwargs):
"""
There may be only one base version
"""
if kwargs.get('raw', False): return
if instance.is_base:
try:
base_versions = Version.objects.filter(jetpack__id=instance.jetpack.id, is_base=True)
except:
return
for base_version in base_versions:
try:
if instance.id and base_version.id == instance.id: return
base_version.is_base = False
base_version.save()
except:
return
pre_save.connect(unbase_other_base_version, sender=Version)

Просмотреть файл

@ -0,0 +1,74 @@
from django.db.models.signals import pre_save, post_save
from jetpack import settings
from jetpack.models import Jetpack, Version
# ------------------------------------------
# Jetpack signals
def make_slug_on_create(instance, **kwargs):
if kwargs.get('raw',False): return
if not instance.id and not instance.slug:
instance.set_slug()
pre_save.connect(make_slug_on_create, sender=Jetpack)
# ------------------------------------------
# Version signals
def default_name(instance, **kwargs):
"""
If name not given set from Jetpack
"""
if kwargs.get('raw', False): return
if not instance.name:
instance.name = instance.jetpack.name
pre_save.connect(default_name, sender=Version)
def first_is_base(instance, **kwargs):
"""
There is always a base version
"""
if kwargs.get('raw', False): return
if not instance.is_base:
try:
base_version = Version.objects.get(jetpack__id=instance.jetpack.id, is_base=True)
return
except:
instance.is_base = True
pre_save.connect(first_is_base, sender=Version)
def new_is_base(instance, **kwargs):
"""
Depending on settings new option has to be a base one one automatically
"""
if kwargs.get('raw', False): return
if instance.is_base: return
if not settings.JETPACK_NEW_IS_BASE: return
# TODO: something is bad here
instance.is_base = True
pre_save.connect(new_is_base, sender=Version)
def unbase_other_base_version(instance, **kwargs):
"""
There may be only one base version
"""
if kwargs.get('raw', False): return
if instance.is_base:
try:
base_versions = Version.objects.filter(jetpack__id=instance.jetpack.id, is_base=True)
except:
return
for base_version in base_versions:
try:
if instance.id and base_version.id == instance.id: return
base_version.is_base = False
base_version.save()
except:
return
pre_save.connect(unbase_other_base_version, sender=Version)

Просмотреть файл

@ -1,23 +1,55 @@
"""
This file demonstrates two different styles of tests (one doctest and one
unittest). These will both pass when you run "manage.py test".
Replace these with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.failUnlessEqual(1 + 1, 2)
from test_utils import create_test_user
from jetpack.models import Jetpack, Version
from jetpack import settings
__test__ = {"doctest": """
Another way to test that 1 + 1 is equal to 2.
TEST_USERNAME = 'test_user'
TEST_JETPACK_NAME = 'test Jetpack'
>>> 1 + 1 == 2
True
"""}
class JetpackTest(TestCase):
def setUp(self):
self.to_delete = []
self.user = create_test_user(username=TEST_USERNAME)
self.jetpack = Jetpack(name=TEST_JETPACK_NAME, author=self.user)
self.jetpack.save()
self.version = Version(jetpack=self.jetpack, name='first')
self.version.save()
def tearDown(self):
self.user.delete()
for o in self.to_delete:
try:
o.delete()
except:
print "Object %s can't be deleted" % str(o)
def test_jetpack_creation(self):
jetpack = Jetpack.objects.get(name=TEST_JETPACK_NAME)
self.failUnless(jetpack)
self.assertEqual(len(self.jetpack.slug), settings.JETPACK_SLUG_LENGTH);
def test_first_as_base(self):
"""
First is base
"""
# first created version is base one
self.failUnless(self.jetpack.base_version)
self.assertEqual(self.jetpack.base_version.name, 'first')
def test_switch_base(self):
"""
There is only one base version per Jetpack
"""
# next base version
first_base_id = self.jetpack.base_version.id
second_base = Version(jetpack=self.jetpack, name='second', is_base=True)
second_base.save()
jetpack = Jetpack.objects.get(name=TEST_JETPACK_NAME)
self.assertEqual(second_base.name, self.jetpack.base_version.name)
first_base = Version.objects.get(pk=first_base_id)
self.assertEqual(first_base.is_base, False)

Просмотреть файл

@ -107,4 +107,7 @@ AUTHENTICATION_BACKENDS = (
)
# overwrite default settings with the ones from settings_local.py
from settings_local import *
try:
from settings_local import *
except:
pass

17
flightdeck/test_utils.py Normal file
Просмотреть файл

@ -0,0 +1,17 @@
def create_test_user(username="test_username", password="password", email="test@example.com"):
from django.contrib.auth.models import User
from person.models import Profile
user = User(
username=username,
password=password,
email=email
)
user.save()
Profile(
user=user,
amo_session=""
).save()
return user

10
flightdeck/utils.py Normal file
Просмотреть файл

@ -0,0 +1,10 @@
from random import choice
CHARS='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'
def random_string(length=10, prefix=''):
random_length = length - len(prefix)
if random_length <= 0:
return prefix
return (prefix + ''.join([choice(CHARS) for i in range(random_length)]))[0:length]

8
scripts/manage.py Executable file
Просмотреть файл

@ -0,0 +1,8 @@
#!/bin/bash
source scripts/environment.sh
# run server
cd $PROJECT_DIR/$PROJECT_NAME/
$PYTHON_COMMAND ./manage.py $@