fixed update of person_api

This commit is contained in:
Oskar Hahn 2013-03-14 22:18:56 +01:00
parent 73723feb74
commit 545d4be04f
8 changed files with 131 additions and 6 deletions

1
.gitignore vendored
View File

@ -4,6 +4,7 @@
*~ *~
settings.py settings.py
database.sqlite database.sqlite
!tests/settings.py
# Package building # Package building
docs/_build/* docs/_build/*

View File

@ -6,5 +6,6 @@ install:
- pip install -r requirements.txt --use-mirrors - pip install -r requirements.txt --use-mirrors
- python extras/scripts/create_local_settings.py - python extras/scripts/create_local_settings.py
script: script:
- export DJANGO_SETTINGS_MODULE=tests.settings
- coverage run ./manage.py test tests && coverage report -m - coverage run ./manage.py test tests && coverage report -m
- pep8 --max-line-length=150 --exclude="urls.py," --statistics openslides - pep8 --max-line-length=150 --exclude="urls.py," --statistics openslides

View File

@ -12,6 +12,7 @@ from django.core.management import execute_from_command_line
from openslides.main import get_user_config_path, setup_django_environment from openslides.main import get_user_config_path, setup_django_environment
if __name__ == "__main__": if __name__ == "__main__":
setup_django_environment( if 'DJANGO_SETTINGS_MODULE' not in os.environ:
get_user_config_path('openslides', 'settings.py')) setup_django_environment(
get_user_config_path('openslides', 'settings.py'))
execute_from_command_line(sys.argv) execute_from_command_line(sys.argv)

View File

@ -12,7 +12,7 @@
from django.db import models from django.db import models
from .forms import PersonFormField from .forms import PersonFormField
from .api import get_person, generate_person_id, Person from .api import get_person, generate_person_id
class PersonField(models.fields.Field): class PersonField(models.fields.Field):
@ -29,7 +29,7 @@ class PersonField(models.fields.Field):
""" """
Convert string value to a User Object. Convert string value to a User Object.
""" """
if isinstance(value, Person): if isinstance(value, PersonMixin):
return value return value
elif value is None: elif value is None:
return None return None
@ -60,5 +60,11 @@ class PersonMixin(object):
raise AttributeError("%s has to have a attribute 'person_prefix'" raise AttributeError("%s has to have a attribute 'person_prefix'"
% self) % self)
def __repr__(self): def __unicode__(self):
return 'Person: %s' % self.person_id return 'MyPerson: %s' % self.person_id
def prepare_database_save(self, field):
if type(field) is PersonField:
return self.person_id
else:
return super(PersonMixin, self).prepare_database_save(field)

View File

View File

@ -0,0 +1,46 @@
from django.db import models
from django.dispatch import receiver
from openslides.utils.person.models import PersonMixin, PersonField
from openslides.utils.person.signals import receive_persons
from openslides.utils.person.api import Person
class TestPerson(PersonMixin, models.Model):
person_prefix = 'test'
name = models.CharField(max_length='255')
def __unicode__(self):
return self.name
class TestPersonToPerson(object):
def __init__(self, person_prefix_filter=None, id_filter=None):
self.person_prefix_filter = person_prefix_filter
self.id_filter = id_filter
def __iter__(self):
if (not self.person_prefix_filter or
self.person_prefix_filter == TestPerson.person_prefix):
if self.id_filter:
try:
yield TestPerson.objects.get(pk=self.id_filter)
except TestPerson.DoesNotExist:
pass
else:
for user in TestPerson.objects.all():
yield user
@receiver(receive_persons, dispatch_uid="test_person")
def receive_persons(sender, **kwargs):
return TestPersonToPerson(
person_prefix_filter=kwargs['person_prefix_filter'],
id_filter=kwargs['id_filter'])
class TestModel(models.Model):
person = PersonField()
def __unicode__(self):
return self.person

34
tests/person_api/tests.py Normal file
View File

@ -0,0 +1,34 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
openslides.person_api.tests
~~~~~~~~~~~~~~~~~~~~~~~
Unit test for the person api.
:copyright: 2011 - 2013 by OpenSlides team, see AUTHORS.
:license: GNU GPL, see LICENSE for more details.
"""
from django.test.client import Client
from django.db.models.query import EmptyQuerySet
from openslides.utils.test import TestCase
from .models import TestPerson, TestModel
class ItemTest(TestCase):
def setUp(self):
self.person1 = TestPerson.objects.create(name='test1')
def test_update_of_person_field(self):
self.assertEqual(self.person1.person_id, 'test:1')
# save person field
test_object = TestModel.objects.create(person=self.person1)
self.assertEqual(test_object.person, self.person1)
# update person field
test_object.save()
self.assertEqual(TestModel.objects.get(pk=test_object.pk).person, self.person1)

36
tests/settings.py Normal file
View File

@ -0,0 +1,36 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import openslides.main
from openslides.global_settings import *
# Use 'DEBUG = True' to get more details for server errors
# (Default for releases: 'False')
DEBUG = True
TEMPLATE_DEBUG = DEBUG
DBPATH = u'database.sqlite'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': DBPATH,
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
# Set timezone
TIME_ZONE = 'Europe/Berlin'
# Make this unique, and don't share it with anybody.
SECRET_KEY = 'secred'
# Add OpenSlides plugins to this list (see example entry in comment)
INSTALLED_PLUGINS = (
'tests.person_api',
)
INSTALLED_APPS += INSTALLED_PLUGINS