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

View File

@ -124,7 +124,7 @@
{% endif %}
</th>
{% 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>
<a href='{% url assignment_gen_poll assignment.id %}'>
<span class="button">
@ -138,9 +138,9 @@
{% for candidate, poll_list in vote_results.items %}
<tr class="{% cycle 'odd' '' %}">
<td class="candidate">
{% if candidate in assignment.elected.all %}
{% if candidate in assignment.elected %}
{% 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 %}
<a class="elected">
<img src="{% static 'images/icons/voting-yes.png' %}" title="{% trans 'Candidate is elected' %}">
@ -148,7 +148,7 @@
{% endif %}
{% else %}
{% 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 %}
{{ candidate }}
@ -168,7 +168,7 @@
{% endif %}
</td>
{% 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>
{% endif %}
</tr>
@ -186,7 +186,7 @@
</td>
{% endif %}
{% 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>
{% endif %}
</tr>
@ -203,7 +203,7 @@
</td>
{% endif %}
{% 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>
{% endif %}
</tr>
@ -213,7 +213,7 @@
<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 %}'>
<span class="button">
<span class="icon statistics">{% trans 'New ballot' %}</span>

View File

@ -105,13 +105,13 @@ urlpatterns = patterns('openslides.assignment.views',
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',
{'elected': True},
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',
{'elected': False},
name='assignment_user_not_elected',

View File

@ -266,21 +266,19 @@ def set_publish_status(request, poll_id):
@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)
profile = Profile.objects.get(pk=profile_id)
assignment.set_elected(profile, elected)
user = get_user(user_id)
assignment.set_elected(user, elected)
if request.is_ajax():
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')
else:
link = reverse('assignment_user_elected', args=[assignment.id, profile.id])
link = reverse('assignment_user_elected', args=[assignment.id, user.uid])
text = _('elected')
return ajax_request({'elected': elected,
'link': link,
'text': text})
return ajax_request({'elected': elected, 'link': link, 'text': text})
return redirect(reverse('assignment_view', args=[assignment_id]))