views.py 2.48 KB
Newer Older
Sli's avatar
Sli committed
1
2
3
4
5
from django.shortcuts import render
from django.views.generic import ListView, DetailView, RedirectView
from django.views.generic.edit import UpdateView, CreateView, DeleteView, FormView
from django.core.urlresolvers import reverse_lazy, reverse
from django.utils.translation import ugettext_lazy as _
6
7
from django.forms.models import modelform_factory
from django.forms import CheckboxSelectMultiple
Sli's avatar
Sli committed
8
from django.utils import timezone
Sli's avatar
Sli committed
9
from django.conf import settings
10
from django import forms
Sli's avatar
Sli committed
11
12

from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, CanCreateMixin
13
14
from core.views.forms import SelectDateTime
from core.widgets import ChoiceWithOtherField
Sli's avatar
Sli committed
15
from election.models import Election, Role, Candidature
Sli's avatar
Sli committed
16

17
18
19
20
21
22
23
24
from ajax_select.fields import AutoCompleteSelectField

# Forms


class CandidateForm(forms.Form):
    user = AutoCompleteSelectField('users', label=_('Refound this account'), help_text=None, required=True)

Sli's avatar
Sli committed
25
26
27
28
29
30
31
32
33
# Display elections


class ElectionsListView(CanViewMixin, ListView):
    """
    A list with all responsabilities and their candidates
    """
    model = Election
    template_name = 'election/election_list.jinja'
Sli's avatar
Sli committed
34

Sli's avatar
Sli committed
35
36
37
38
39
40
    def get_queryset(self):
        qs = super(ElectionsListView, self).get_queryset()
        today = timezone.now()
        qs = qs.filter(end_date__gte=today, start_date__lte=today)
        return qs

Sli's avatar
Sli committed
41
42
43
44
45
46
47
48
49

class ElectionDetailView(CanViewMixin, DetailView):
    """
    Details an election responsability by responsability
    """
    model = Election
    template_name = 'election/election_detail.jinja'
    pk_url_kwarg = "election_id"

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

class PageCreateView(CanCreateMixin, CreateView):
    model = Election
    form_class = modelform_factory(Election,
        fields=['title', 'description', 'start_candidature', 'end_candidature', 'start_date', 'end_date',
                'edit_groups', 'view_groups', 'vote_groups', 'candidature_groups'],
        widgets={
            'edit_groups': CheckboxSelectMultiple,
            'view_groups': CheckboxSelectMultiple,
            'edit_groups': CheckboxSelectMultiple,
            'vote_groups': CheckboxSelectMultiple,
            'candidature_groups': CheckboxSelectMultiple,
            'start_date': SelectDateTime,
            'end_date': SelectDateTime,
            'start_candidature': SelectDateTime,
            'end_candidature': SelectDateTime,
        })
    template_name = 'core/page_prop.jinja'

    def get_success_url(self, **kwargs):
        return reverse_lazy('election:detail', kwargs={'election_id': self.object.id})