Commit 2f3b4810 authored by Sli's avatar Sli

Improve deletion and add ordering on roles

parent 625b5132
Pipeline #1425 passed with stage
in 6 minutes and 11 seconds
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('election', '0002_election_archived'),
]
operations = [
migrations.AlterModelOptions(
name='role',
options={'ordering': ('order',)},
),
migrations.AddField(
model_name='role',
name='order',
field=models.PositiveIntegerField(editable=False, default=0, db_index=True),
preserve_default=False,
),
]
from django.db import models
from ordered_model.models import OrderedModel
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
......@@ -81,10 +82,15 @@ class Election(models.Model):
results[role.title] = role.results(total_vote)
return results
def delete(self):
for election_list in self.election_lists.all():
election_list.delete()
super(Election, self).delete()
# Permissions
class Role(models.Model):
class Role(OrderedModel):
"""
This class allows to create a new role avaliable for a candidature
"""
......@@ -149,6 +155,10 @@ class Candidature(models.Model):
program = models.TextField(_('description'), null=True, blank=True)
election_list = models.ForeignKey(ElectionList, related_name='candidatures', verbose_name=_('election list'))
def delete(self):
for vote in self.votes.all():
vote.delete()
def can_be_edited_by(self, user):
return (user == self.user) or user.can_edit(self.role.election)
......
......@@ -273,7 +273,8 @@ th {
</th>
{%- endfor %}
</thead>
{%- for role in election.roles.all() %}
{%- set role_list = election.roles.order_by('order').all() %}
{%- for role in role_list %}
{%- set count = [0] %}
{%- set role_data = election_form.data.getlist(role.title) if role.title in election_form.data else [] %}
<tbody data-max-choice="{{role.max_choice}}" class="role{{ ' role_error' if role.title in election_form.errors else '' }}{{ ' role__multiple-choices' if role.max_choice > 1 else ''}}">
......@@ -283,6 +284,22 @@ th {
{% if user.can_edit(role) and election.is_vote_editable -%}
<a href="{{url('election:update_role', role_id=role.id)}}">{% trans %}Edit{% endtrans %}</a>
<a href="{{url('election:delete_role', role_id=role.id)}}">{% trans %}Delete{% endtrans %}</a>
<span style="float:right">
{%- if role == role_list.last() %}
<button disabled><i class="fa fa-arrow-down"></i></button>
<button disabled><i class="fa fa-caret-down"></i></button>
{%- else %}
<button type="button" onclick="window.location.replace('?role={{ role.id }}&action=bottom');"><i class="fa fa-arrow-down"></i></button>
<button type="button" onclick="window.location.replace('?role={{ role.id }}&action=down');"><i class="fa fa-caret-down"></i></button>
{%- endif %}
{% if role == role_list.first() %}
<button disabled><i class="fa fa-caret-up"></i></button>
<button disabled><i class="fa fa-arrow-up"></i></button>
{% else %}
<button type="button" onclick="window.location.replace('?role={{ role.id }}&action=up');"><i class="fa fa-caret-up"></i></button>
<button type="button" onclick="window.location.replace('?role={{ role.id }}&action=top');"><i class="fa fa-arrow-up"></i></button>
{% endif %}
</span>
{%- endif -%}
<br><span class='role__description'><p>{{ role.description }}</p></span>
{%- if role.max_choice > 1 and not election.has_voted(user) and election.can_vote(user) %}
......
......@@ -177,6 +177,23 @@ class ElectionDetailView(CanViewMixin, DetailView):
template_name = 'election/election_detail.jinja'
pk_url_kwarg = "election_id"
def get(self, request, *arg, **kwargs):
r = super(ElectionDetailView, self).get(request, *arg, **kwargs)
election = self.get_object()
if request.user.can_edit(election) and election.is_vote_editable:
action = request.GET.get('action', None)
role = request.GET.get('role', None)
if action and role and Role.objects.filter(id=role).exists():
if action == "up":
Role.objects.get(id=role).up()
elif action == "down":
Role.objects.get(id=role).down()
elif action == "bottom":
Role.objects.get(id=role).bottom()
elif action == "top":
Role.objects.get(id=role).top()
return r
def get_context_data(self, **kwargs):
""" Add additionnal data to the template """
kwargs = super(ElectionDetailView, self).get_context_data(**kwargs)
......
This diff is collapsed.
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 to comment