user-api: make the assignments work with the new user-api

This commit is contained in:
Oskar Hahn 2012-08-03 18:56:00 +02:00
parent c16d38d9a3
commit 8fcf65e829
4 changed files with 56 additions and 53 deletions

View File

@ -33,7 +33,7 @@ from openslides.agenda.models import Item
class AssignmentCandidate(models.Model): class AssignmentCandidate(models.Model):
assignment = models.ForeignKey("Assignment") assignment = models.ForeignKey("Assignment")
user = UserField(unique=True, db_index=True) user = UserField(db_index=True)
elected = models.BooleanField(default=False) elected = models.BooleanField(default=False)
def __unicode__(self): def __unicode__(self):
@ -57,14 +57,6 @@ class Assignment(models.Model, SlideMixin):
verbose_name=_("Comment on the ballot paper")) verbose_name=_("Comment on the ballot paper"))
status = models.CharField(max_length=3, choices=STATUS, default='sea') status = models.CharField(max_length=3, choices=STATUS, default='sea')
@property
def profile(self):
return AssignmentCandidate.objects.filter(assignment=self).filter(elected=False)
@property
def elected(self):
return AssignmentCandidate.objects.filter(assignment=self).filter(elected=True)
def set_status(self, status): def set_status(self, status):
error = True error = True
for a, b in Assignment.STATUS: for a, b in Assignment.STATUS:
@ -97,45 +89,59 @@ class Assignment(models.Model, SlideMixin):
# TODO: Use an OpenSlides Error # TODO: Use an OpenSlides Error
raise Exception(_('The candidate list is already closed.')) raise Exception(_('The candidate list is already closed.'))
if self.is_candidate(user): if self.is_candidate(user):
AssignmentCandidate.objects.get(user=user).delete() assignment_candidats.get(user=user).delete()
else: else:
# TODO: Use an OpenSlides Error # TODO: Use an OpenSlides Error
raise Exception(_('%s is no candidate') % user) raise Exception(_('%s is no candidate') % user)
def is_candidate(self, user): def is_candidate(self, user):
if AssignmentCandidate.objects.filter(user=user).exists(): if self.assignment_candidats.filter(user=user).exists():
return True return True
else: else:
return False return False
@property
def assignment_candidats(self):
return AssignmentCandidate.objects.filter(assignment=self)
@property @property
def candidates(self): def candidates(self):
for candidate in AssignmentCandidate.objects.filter(assignment=self).filter(elected=False): return self.get_participants(only_candidate=True)
yield candidate.user
@property
def elected(self):
return self.get_participants(only_elected=True)
def get_participants(self, only_elected=False, only_candidate=False):
candidates = self.assignment_candidats
if only_elected and only_candidate:
# TODO: Use right Exception
raise Exception("only_elected and only_candidate can not both be Treu")
if only_elected:
candidates = candidates.filter(elected=True)
if only_candidate:
candidates = candidates.filter(elected=False)
return [candidate.user for candidate in candidates]
#for candidate in candidates:
# yield candidate.user
def set_elected(self, profile, value=True): def set_elected(self, user, value=True):
if profile in self.candidates: candidate = AssignmentCandidate.objects.get(user=user)
if value and not self.is_elected(profile): candidate.elected = value
self.elected.add(profile) candidate.save()
elif not value:
self.elected.remove(profile)
def is_elected(self, profile): def is_elected(self, user):
if profile in self.elected.all(): return user in self.elected
return True
return False
def gen_poll(self): def gen_poll(self):
poll = AssignmentPoll(assignment=self) poll = AssignmentPoll(assignment=self)
poll.save() poll.save()
candidates = list(self.profile.all()) poll.set_options([{'candidate': user} for user in self.candidates])
for elected in self.elected.all():
try:
candidates.remove(elected)
except ValueError:
pass
poll.set_options([{'candidate': profile} for profile in candidates])
return poll return poll
@ -177,6 +183,7 @@ class Assignment(models.Model, SlideMixin):
return self.name return self.name
def delete(self): def delete(self):
# Remove any Agenda-Item, which is related to this application.
for item in Item.objects.filter(related_sid=self.sid): for item in Item.objects.filter(related_sid=self.sid):
item.delete() item.delete()
super(Assignment, self).delete() super(Assignment, self).delete()
@ -224,7 +231,7 @@ class AssignmentVote(BaseVote):
class AssignmentOption(BaseOption): class AssignmentOption(BaseOption):
poll = models.ForeignKey('AssignmentPoll') poll = models.ForeignKey('AssignmentPoll')
candidate = models.ForeignKey(Profile) candidate = UserField()
vote_class = AssignmentVote vote_class = AssignmentVote
def __unicode__(self): def __unicode__(self):
@ -248,8 +255,8 @@ class AssignmentPoll(BasePoll, CountInvalid, CountVotesCast, PublishPollMixin):
self.yesnoabstain = True self.yesnoabstain = True
else: else:
# candidates <= available posts -> yes/no/abstain # candidates <= available posts -> yes/no/abstain
if self.assignment.candidates.count() <= (self.assignment.posts if self.assignment.assignment_candidats.filter(elected=False).count() <= (self.assignment.posts
- self.assignment.elected.count()): - self.assignment.assignment_candidats.filter(elected=True).count()):
self.yesnoabstain = True self.yesnoabstain = True
else: else:
self.yesnoabstain = False self.yesnoabstain = False
@ -278,8 +285,6 @@ class AssignmentPoll(BasePoll, CountInvalid, CountVotesCast, PublishPollMixin):
return _("Ballot %d") % self.get_ballot() return _("Ballot %d") % self.get_ballot()
@receiver(default_config_value, dispatch_uid="assignment_default_config") @receiver(default_config_value, dispatch_uid="assignment_default_config")
def default_config(sender, key, **kwargs): def default_config(sender, key, **kwargs):
return { return {

View File

@ -124,7 +124,7 @@
{% endif %} {% endif %}
</th> </th>
{% endfor %} {% endfor %}
{% if assignment.profile.exists and perms.assignment.can_manage_assignment and assignment.status == "vot" %} {% if assignment.candidates and perms.assignment.can_manage_assignment and assignment.status == "vot" %}
<th> <th>
<a href='{% url assignment_gen_poll assignment.id %}'> <a href='{% url assignment_gen_poll assignment.id %}'>
<span class="button"> <span class="button">
@ -138,9 +138,9 @@
{% for candidate, poll_list in vote_results.items %} {% for candidate, poll_list in vote_results.items %}
<tr class="{% cycle 'odd' '' %}"> <tr class="{% cycle 'odd' '' %}">
<td class="candidate"> <td class="candidate">
{% if candidate in assignment.elected.all %} {% if candidate in assignment.elected %}
{% if perms.assignment.can_manage_assignment %} {% if perms.assignment.can_manage_assignment %}
<a class="election_link elected" href='{% url assignment_user_not_elected assignment.id candidate.id %}'></a> <a class="election_link elected" href='{% url assignment_user_not_elected assignment.id candidate.uid %}'></a>
{% else %} {% else %}
<a class="elected"> <a class="elected">
<img src="{% static 'images/icons/voting-yes.png' %}" title="{% trans 'Candidate is elected' %}"> <img src="{% static 'images/icons/voting-yes.png' %}" title="{% trans 'Candidate is elected' %}">
@ -148,7 +148,7 @@
{% endif %} {% endif %}
{% else %} {% else %}
{% if perms.assignment.can_manage_assignment %} {% if perms.assignment.can_manage_assignment %}
<a class="election_link" href='{% url assignment_user_elected assignment.id candidate.id %}'></a> <a class="election_link" href='{% url assignment_user_elected assignment.id candidate.uid %}'></a>
{% endif %} {% endif %}
{% endif %} {% endif %}
{{ candidate }} {{ candidate }}
@ -168,7 +168,7 @@
{% endif %} {% endif %}
</td> </td>
{% endfor %} {% endfor %}
{% if assignment.profile.exists and perms.assignment.can_manage_assignment and assignment.status == "vot" %} {% if assignment.candidates and perms.assignment.can_manage_assignment and assignment.status == "vot" %}
<td></td> <td></td>
{% endif %} {% endif %}
</tr> </tr>
@ -186,7 +186,7 @@
</td> </td>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
{% if assignment.profile.exists and perms.assignment.can_manage_assignment and assignment.status == "vot" %} {% if assignment.candidates and perms.assignment.can_manage_assignment and assignment.status == "vot" %}
<td></td> <td></td>
{% endif %} {% endif %}
</tr> </tr>
@ -203,7 +203,7 @@
</td> </td>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
{% if assignment.profile.exists and perms.assignment.can_manage_assignment and assignment.status == "vot" %} {% if assignment.candidates and perms.assignment.can_manage_assignment and assignment.status == "vot" %}
<td></td> <td></td>
{% endif %} {% endif %}
</tr> </tr>
@ -213,7 +213,7 @@
<i>{% trans "No ballots available." %}</i> <i>{% trans "No ballots available." %}</i>
{% if assignment.profile.count > 0 and perms.assignment.can_manage_assignment and assignment.status == "vot" %} {% if assignment.candidates and perms.assignment.can_manage_assignment and assignment.status == "vot" %}
<p><a href='{% url assignment_gen_poll assignment.id %}'> <p><a href='{% url assignment_gen_poll assignment.id %}'>
<span class="button"> <span class="button">
<span class="icon statistics">{% trans 'New ballot' %}</span> <span class="icon statistics">{% trans 'New ballot' %}</span>

View File

@ -105,13 +105,13 @@ urlpatterns = patterns('openslides.assignment.views',
name='assignment_poll_publish_status', name='assignment_poll_publish_status',
), ),
url(r'^(?P<assignment_id>\d+)/elected/(?P<profile_id>\d+)/$', url(r'^(?P<assignment_id>\d+)/elected/(?P<user_id>[^/]+)/$',
'set_elected', 'set_elected',
{'elected': True}, {'elected': True},
name='assignment_user_elected', name='assignment_user_elected',
), ),
url(r'^(?P<assignment_id>\d+)/notelected/(?P<profile_id>\d+)/$', url(r'^(?P<assignment_id>\d+)/notelected/(?P<user_id>[^/]+)/$',
'set_elected', 'set_elected',
{'elected': False}, {'elected': False},
name='assignment_user_not_elected', name='assignment_user_not_elected',

View File

@ -266,21 +266,19 @@ def set_publish_status(request, poll_id):
@permission_required('assignment.can_manage_assignment') @permission_required('assignment.can_manage_assignment')
def set_elected(request, assignment_id, profile_id, elected=True): def set_elected(request, assignment_id, user_id, elected=True):
assignment = Assignment.objects.get(pk=assignment_id) assignment = Assignment.objects.get(pk=assignment_id)
profile = Profile.objects.get(pk=profile_id) user = get_user(user_id)
assignment.set_elected(profile, elected) assignment.set_elected(user, elected)
if request.is_ajax(): if request.is_ajax():
if elected: if elected:
link = reverse('assignment_user_not_elected', args=[assignment.id, profile.id]) link = reverse('assignment_user_not_elected', args=[assignment.id, user.uid])
text = _('not elected') text = _('not elected')
else: else:
link = reverse('assignment_user_elected', args=[assignment.id, profile.id]) link = reverse('assignment_user_elected', args=[assignment.id, user.uid])
text = _('elected') text = _('elected')
return ajax_request({'elected': elected, return ajax_request({'elected': elected, 'link': link, 'text': text})
'link': link,
'text': text})
return redirect(reverse('assignment_view', args=[assignment_id])) return redirect(reverse('assignment_view', args=[assignment_id]))