Merge pull request #52 from ostcar/sort_person_api

Sort person api
This commit is contained in:
Oskar Hahn 2012-11-22 08:01:30 -08:00
commit 12554c71e6
6 changed files with 19 additions and 5 deletions

View File

@ -160,6 +160,7 @@ class Assignment(models.Model, SlideMixin):
participants = []
for candidate in candidates.all():
participants.append(candidate.person)
participants.sort(key=lambda person: person.sort_name)
return participants
#return candidates.values_list('person', flat=True)
@ -178,7 +179,6 @@ class Assignment(models.Model, SlideMixin):
poll.set_options([{'candidate': person} for person in self.candidates])
return poll
def vote_results(self, only_published):
"""
returns a table represented as a list with all candidates from all

View File

@ -539,7 +539,7 @@ class AssignmentPollPDF(PDFView):
stylesheet['Ballot_title']))
cell.append(Paragraph(self.poll.assignment.polldescription,
stylesheet['Ballot_subtitle']))
options = self.poll.get_options().order_by('candidate')
options = self.poll.get_options()
ballot_string = _("%d. ballot") % self.poll.get_ballot()
candidate_string = ungettext("%d candidate", "%d candidates",

View File

@ -162,8 +162,8 @@ class Motion(models.Model, SlideMixin):
@property
def supporters(self):
for object in self.motionsupporter_set.all():
yield object.person
return sorted([object.person for object in self.motionsupporter_set.all()],
key=lambda person: person.sort_name)
def is_supporter(self, person):
try:

View File

@ -83,6 +83,12 @@ class User(DjangoUser, PersonMixin, Person, SlideMixin):
self.set_password(password)
self.save()
@property
def sort_name(self):
if config['participant_sort_users_by_first_name']:
return self.first_name.lower()
return self.last_name.lower()
@models.permalink
def get_absolute_url(self, link='view'):
"""

View File

@ -29,6 +29,14 @@ class Person(object):
"""
return str(self.person_id)
@property
def sort_name(self):
"""
Return the part of the name, which is used for sorting.
For example the pre-name or the last-name
"""
return self.clean_name.lower()
@property
def clean_name(self):
"""

View File

@ -22,7 +22,7 @@ class PersonChoices(object):
def __iter__(self):
if self.field.empty_label is not None:
yield (u"", self.field.empty_label)
for person in Persons():
for person in sorted(Persons(), key=lambda person: person.sort_name):
yield (person.person_id, person)