Commit 04541cfc by Dudás Ádám

school: prettification and tests

parent a123cd4a
...@@ -59,7 +59,7 @@ msgid "default group" ...@@ -59,7 +59,7 @@ msgid "default group"
msgstr "alapértelmezett csoport" msgstr "alapértelmezett csoport"
#: models.py:71 #: models.py:71
msgid "New users will automatically get to this group." msgid "New users will be automatically assigned to this group."
msgstr "Az új hallgatók automatikusan ebbe a csoportba kerülnek." msgstr "Az új hallgatók automatikusan ebbe a csoportba kerülnek."
#: models.py:73 models.py:115 models.py:149 models.py:162 #: models.py:73 models.py:115 models.py:149 models.py:162
......
...@@ -22,7 +22,7 @@ def create_user_profile(sender, instance, created, **kwargs): ...@@ -22,7 +22,7 @@ def create_user_profile(sender, instance, created, **kwargs):
@param sender: The model class. @param sender: The model class.
@type instance: User @type instance: User
@param instance: The instance being saved. @param instance: The user to create a profile for (if necessary).
@type created: Boolean @type created: Boolean
@param created: True if a new record was created. @param created: True if a new record was created.
""" """
...@@ -48,10 +48,14 @@ class Person(models.Model): ...@@ -48,10 +48,14 @@ class Person(models.Model):
code = models.CharField(_('code'), max_length=30, unique=True) code = models.CharField(_('code'), max_length=30, unique=True)
def get_owned_shares(self): def get_owned_shares(self):
return one.models.Share.objects.filter(group__in=self.owned_groups.all()) """Get the shares of the groups which the person owns."""
return one.models.Share.objects.filter(
group__in=self.owned_groups.all())
def get_shares(self): def get_shares(self):
return one.models.Share.objects.filter(group__in=self.course_groups.all()) """Get the shares of the groups which the person is a member of."""
return one.models.Share.objects.filter(
group__in=self.course_groups.all())
def short_name(self): def short_name(self):
if self.user: if self.user:
...@@ -63,15 +67,16 @@ class Person(models.Model): ...@@ -63,15 +67,16 @@ class Person(models.Model):
return self.code return self.code
def __unicode__(self): def __unicode__(self):
u = self.user if self.user:
if not u: if self.user.last_name and self.user.first_name:
return self.code
if u.last_name and u.first_name:
# TRANSLATORS: full name format used in enumerations # TRANSLATORS: full name format used in enumerations
return _("%(first)s %(last)s") % {'first': u.first_name, return _("%(first)s %(last)s") % {
'last': u.last_name} 'first': self.user.first_name,
'last': self.user.last_name}
else:
return self.user.username
else: else:
return u.username return self.code
class Meta: class Meta:
verbose_name = _('person') verbose_name = _('person')
...@@ -86,7 +91,7 @@ class Course(models.Model): ...@@ -86,7 +91,7 @@ class Course(models.Model):
verbose_name=_('name')) verbose_name=_('name'))
default_group = models.ForeignKey('Group', null=True, blank=True, default_group = models.ForeignKey('Group', null=True, blank=True,
related_name='default_group_of', verbose_name=_('default group'), related_name='default_group_of', verbose_name=_('default group'),
help_text=_('New users will automatically get to this group.')) help_text=_('New users will be automatically assigned to this group.'))
owners = models.ManyToManyField(Person, blank=True, null=True, owners = models.ManyToManyField(Person, blank=True, null=True,
verbose_name=_('owners')) verbose_name=_('owners'))
...@@ -162,10 +167,14 @@ class Semester(models.Model): ...@@ -162,10 +167,14 @@ class Semester(models.Model):
class Group(models.Model): class Group(models.Model):
name = models.CharField(max_length=80, verbose_name=_('name')) name = models.CharField(max_length=80, verbose_name=_('name'))
course = models.ForeignKey('Course', null=True, blank=True, verbose_name=_('course')) course = models.ForeignKey('Course', null=True, blank=True,
semester = models.ForeignKey('Semester', null=False, blank=False, verbose_name=_('semester')) verbose_name=_('course'))
owners = models.ManyToManyField(Person, blank=True, null=True, related_name='owned_groups', verbose_name=_('owners')) semester = models.ForeignKey('Semester', null=False, blank=False,
members = models.ManyToManyField(Person, blank=True, null=True, related_name='course_groups', verbose_name=_('members')) verbose_name=_('semester'))
owners = models.ManyToManyField(Person, blank=True, null=True,
related_name='owned_groups', verbose_name=_('owners'))
members = models.ManyToManyField(Person, blank=True, null=True,
related_name='course_groups', verbose_name=_('members'))
class Meta: class Meta:
unique_together = (('name', 'course', 'semester', ), ) unique_together = (('name', 'course', 'semester', ), )
...@@ -173,10 +182,10 @@ class Group(models.Model): ...@@ -173,10 +182,10 @@ class Group(models.Model):
verbose_name_plural = _('groups') verbose_name_plural = _('groups')
def owner_list(self): def owner_list(self):
if self.owners: if self.owners and self.owners.count() > 0:
return ", ".join([p.short_name() for p in self.owners.all()]) return ", ".join([p.short_name() for p in self.owners.all()])
else: else:
return _("n/a") return _("(none)")
owner_list.verbose_name = _('owners') owner_list.verbose_name = _('owners')
def member_count(self): def member_count(self):
...@@ -190,4 +199,4 @@ class Group(models.Model): ...@@ -190,4 +199,4 @@ class Group(models.Model):
@models.permalink @models.permalink
def get_absolute_url(self): def get_absolute_url(self):
return ('group_show', None, {'gid':self.id}) return ('group_show', None, {'gid': self.id})
...@@ -6,29 +6,47 @@ class MockUser: ...@@ -6,29 +6,47 @@ class MockUser:
class CreateUserProfileTestCase(TestCase): class CreateUserProfileTestCase(TestCase):
def setUp(self): def setUp(self):
self.user = MockUser()
for p in Person.objects.all(): for p in Person.objects.all():
p.delete() p.delete()
def test_new_profile(self): def test_new_profile(self):
"""Test profile creation functionality for new user.""" """Test profile creation functionality for new user."""
user = MockUser() create_user_profile(self.user.__class__, self.user, True)
create_user_profile(user.__class__, user, True) self.assertEqual(Person.objects.filter(
self.assertEqual(Person.objects.filter(code=user.username).count(), 1) code=self.user.username).count(), 1)
def test_existing_profile(self): def test_existing_profile(self):
"""Test profile creation functionality when it already exists.""" """Test profile creation functionality when it already exists."""
user = MockUser() Person.objects.create(code=self.user.username)
Person.objects.create(code=user.username) create_user_profile(self.user.__class__, self.user, True)
create_user_profile(user.__class__, user, True) self.assertEqual(Person.objects.filter(
self.assertEqual(Person.objects.filter(code=user.username).count(), 1) code=self.user.username).count(), 1)
class PersonTestCase(TestCase): class PersonTestCase(TestCase):
def setUp(self): def setUp(self):
Person.objects.create() self.testperson = Person.objects.create(code='testperson')
def test_language_code_in_choices(self): def test_language_code_in_choices(self):
"""Test whether the default value for language is a valid choice.""" """Test whether the default value for language is a valid choice."""
language_field = Person.objects.all()[0]._meta.get_field('language') # TODO
language_field = self.testperson._meta.get_field('language')
choice_codes = [code for (code, _) in language_field.choices] choice_codes = [code for (code, _) in language_field.choices]
self.assertIn(language_field.default, choice_codes) self.assertIn(language_field.default, choice_codes)
def test_get_owned_shares(self):
# TODO
self.testperson.get_owned_shares()
def test_get_shares(self):
# TODO
self.testperson.get_shares()
def test_short_name(self):
# TODO
self.testperson.short_name()
def test_unicode(self):
# TODO
self.testperson.__unicode__()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment