views.py 6.13 KB
Newer Older
1
# -*- coding: utf-8 -*-
2 3 4 5 6 7 8 9
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.views.generic import TemplateView
from django.views.generic.edit import FormView
from django.core.urlresolvers import reverse_lazy
from django.shortcuts import redirect
from django.forms.models import formset_factory
from django.contrib import messages
Mathilde Espinasse's avatar
Mathilde Espinasse committed
10
from django.utils.translation import ugettext_lazy as _
11

12
from forms import ImprimeurAddForm, ImprimeurDelForm, ResponsableEditForm
13 14
from tools import get_clubs_for_user, get_imprimeurs_for_club

15
from intranet import conn_pool
16

17 18 19
class IndexView(TemplateView):
    template_name = 'club/index.html'

20
    def get_context_data(self, cid = None, *args, **kwargs):
21 22
        context = super(IndexView, self).get_context_data(*args, **kwargs)
        clubs = []
23 24 25 26 27 28 29
        if cid and self.request.user.has_perm('auth.crans_cableur'):
            club = conn_pool.get_conn(self.request.user).search(u'cid=%s' % cid)[0]
            user_clubs = [(unicode(club['nom'][0]),club)]
            context.update({'cablage' : True})
        else:
            user_clubs = get_clubs_for_user(self.request.user)
        for (club_name, club) in user_clubs:
30 31
            imprimeurs = get_imprimeurs_for_club(club)
            club_dict = {
32 33
                'club_name': club_name,
                'cid': int(club['cid'][0]),
34 35 36 37
                }
            if imprimeurs:
                club_dict.update({
                    'imprimeurs': zip(*get_imprimeurs_for_club(club))[0],
38
                })
39 40 41 42
            else:
                club_dict.update({
                    'imprimeurs': [],
                })
43 44 45 46
            responsable = club['responsable'][0].get_respo()
            club_dict.update({
                'responsable': unicode(responsable['prenom'][0]) +u' ' + unicode(responsable['nom'][0])
            })
47
            clubs.append(club_dict)
48 49 50 51 52 53 54
        context.update({'clubs': clubs})
        return context

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(IndexView, self).dispatch(*args, **kwargs)

55 56

class EditClubView(FormView):
57 58
    success_url = reverse_lazy('club:index')

59 60 61 62 63 64
    def get_context_data(self, *args, **kwargs):
        context = super(EditClubView, self).get_context_data(*args, **kwargs)
        if 'cablage' in self.kwargs:
            context.update({'cablage':True,'cid':self.cid})
        return context

65 66 67
    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        self.cid = int(self.kwargs['cid'])
68 69 70 71 72 73 74
        if 'cablage' in self.kwargs:
            self.success_url = reverse_lazy('club:index', args=[(self.cid)])
        if not self.request.user.has_perm('auth.crans_cableur'):
            for club_name, club in get_clubs_for_user(self.request.user):
                if int(club['cid'][0]) == self.cid:
                    break
            else: # Ce else est pour le for et non pour le if
Mathilde Espinasse's avatar
Mathilde Espinasse committed
75
                messages.error(self.request, _("Vous n'avez pas le droit de modifier ce club !"))
76 77
                return redirect('club:index')
        return super(EditClubView, self).dispatch(*args, **kwargs)
78

79
class ImprimeurAddView(EditClubView):
80 81 82 83 84 85 86 87
    template_name = 'club/imprimeur_add.html'
    form_class = ImprimeurAddForm

    def form_valid(self, form):
        ldap_user = conn_pool.get_conn(self.request.user)
        club = ldap_user.search(u'cid=%s' % self.cid, mode='w')[0]
        with club as club:
            new_imprimeur = form.cleaned_data['new_imprimeur']
88 89 90
            try:
                new_imprimeur_ldap = ldap_user.search(u'uid=%s' % new_imprimeur)[0]
            except IndexError:
Mathilde Espinasse's avatar
Mathilde Espinasse committed
91
                messages.error(self.request, _("Ce login n'est pas valide"))
92
                return redirect(self.request.path)
93 94 95 96 97 98 99
            try:
                club['imprimeurClub'].append(unicode(new_imprimeur_ldap['aid'][0]))
                club.history_gen()
                club.save()
            except ValueError as e:
                messages.error(self.request, e)
                return redirect(self.request.path)
100 101
        return super(ImprimeurAddView, self).form_valid(form)

102
class ImprimeurDelView(EditClubView):
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    template_name = 'club/imprimeur_del.html'
    form_class = ImprimeurDelForm

    def get_form(self, form_class):
        ldap_user = conn_pool.get_conn(self.request.user)
        club = ldap_user.search(u'cid=%s' % self.cid, mode='w')[0]
        post = self.request.POST
        if post:
            return form_class(club, post)
        else:
            return form_class(club)

    def form_invalid(self, form):
        print self.request.POST
        print form.non_field_errors()
        print form.errors
        print form.__dict__
        return super(ImprimeurDelView, self).form_invalid(form)

    def form_valid(self, form):
        ldap_user = conn_pool.get_conn(self.request.user)
        club = ldap_user.search(u'cid=%s' % self.cid, mode='w')[0]
        with club as club:
            for old_imprimeur_aid in form.cleaned_data['old_imprimeurs']:
                club['imprimeurClub'].remove(unicode(old_imprimeur_aid))
                club.history_gen()
                club.save()
        return super(ImprimeurDelView, self).form_valid(form)
131

132 133 134 135 136 137 138 139 140 141 142 143
class ResponsableEditView(EditClubView):
    template_name = 'club/responsable_edit.html'
    form_class = ResponsableEditForm

    def form_valid(self, form):
        ldap_user = conn_pool.get_conn(self.request.user)
        club = ldap_user.search(u'cid=%s' % self.cid, mode='w')[0]
        with club as club:
            new_responsable = form.cleaned_data['new_responsable']
            try:
                new_responsable_ldap = ldap_user.search(u'uid=%s' % new_responsable)[0]
            except IndexError:
Mathilde Espinasse's avatar
Mathilde Espinasse committed
144
                messages.error(self.request, _("Ce login n'est pas valide"))
145 146 147 148 149 150 151 152 153 154
                return redirect(self.request.path)
            try:
                club['responsable'] = unicode(new_responsable_ldap['aid'][0])
                club.history_gen()
                club.save()
            except ValueError as e:
                messages.error(self.request, e)
                return redirect(self.request.path)
        return super(ResponsableEditView, self).form_valid(form)