Project

General

Profile

Download (3.85 KB) Statistics
| Branch: | Tag: | Revision:

root / uauth / organization / views.py @ 7b499930

1
from django.utils.translation import ugettext as _
2
from django.core.urlresolvers import reverse_lazy
3
from django.http import HttpResponseRedirect
4

    
5
from django.views.generic.base import TemplateView
6
from django.views.generic.list import ListView
7
from django.views.generic.edit import FormView, UpdateView
8
from django.views.generic import DetailView
9
from django.contrib import messages
10

    
11
from django_tables2 import RequestConfig
12

    
13
from .utils import create_user
14
from .models import LocalAccount, Organization
15
from .forms import LocalAccountCreateForm, LocalAccountForm
16
from .tables import AccountTable
17

    
18

    
19
class OrganizationMixin(object):
20

    
21
    def get_queryset(self):
22
        qs = super(OrganizationMixin, self).get_queryset()
23
        return qs.filter(organization__slug=self.kwargs['organization_slug'])
24

    
25
    def get_success_url(self):
26
        return reverse_lazy('manage-users', kwargs={'organization_slug': self.kwargs['organization_slug']})
27

    
28
    def get_context_data(self, *args, **kwargs):
29
        ctx = super(OrganizationMixin, self).get_context_data(*args, **kwargs)
30
        ctx['organization'] = Organization.objects.get(slug=self.kwargs['organization_slug'])
31
        return ctx
32

    
33
class ManageView(TemplateView):
34
    template_name = 'organization/manage.html'
35

    
36
manage = ManageView.as_view()
37

    
38
class UsersPageView(OrganizationMixin, ListView):
39
    template_name = 'organization/users.html'
40
    model = LocalAccount
41

    
42
    def get_context_data(self, *args, **kwargs):
43
        context = super(UsersPageView, self).get_context_data(*args, **kwargs)
44
        table = AccountTable(context['object_list'])
45
        RequestConfig(self.request).configure(table)
46
        context['table'] = table
47
        return context
48

    
49
users = UsersPageView.as_view()
50

    
51

    
52
class UsersCreateView(OrganizationMixin, FormView):
53
    template_name = 'organization/create-users.html'
54
    form_class = LocalAccountCreateForm
55

    
56
    def form_valid(self, form):
57
        data = form.cleaned_data
58
        data['organization'] = Organization.objects.get(slug=self.kwargs['organization_slug'])
59
        accounts_number = data.pop('accounts_number')
60
        accounts_number_start = data.pop('accounts_number_start')
61
        if accounts_number_start < 1:
62
            accounts_number_start = 0
63

    
64
        username = data['username']
65
        if accounts_number > 1:
66
            for index in xrange(accounts_number_start, accounts_number + accounts_number_start):
67
                data['username'] = '%s-%s' % (username, index)
68
                if not create_user(data):
69
                    messages.error(self, request, _('Error while creating user %s' % data['username']))
70
                    break
71
            messages.info(self.request, _('%s users added successfully' % accounts_number))
72
        else:
73
            if create_user(data):
74
                messages.info(self.request, _('User "%s" successfully created' % data['username']))
75
            else:
76
                messages.error(self.request, _('Error occured while creating user "%s"' % data['username']))
77
        return super(UsersCreateView, self).form_valid(form)
78

    
79
create_users = UsersCreateView.as_view()
80

    
81

    
82
class ShowUserView(OrganizationMixin, DetailView):
83
    model = LocalAccount
84
    template_name = 'organization/view_user.html'
85

    
86
view_user = ShowUserView.as_view()
87

    
88

    
89
class UserEditView(OrganizationMixin, UpdateView):
90
    template_name = 'organization/edit_user.html'
91
    model = LocalAccount
92
    form_class = LocalAccountForm
93

    
94
    def form_valid(self, form):
95
        username = self.object.username
96
        if 'delete' in self.request.POST:
97
            self.object.delete()
98
            messages.info(self.request, _('Account "%s" successfully deleted' % username))
99
            return HttpResponseRedirect(self.get_success_url())
100
        else:
101
            messages.info(self.request, _('Account "%s" successfully updated' % username))
102
            return super(UserEditView, self).form_valid(form)
103

    
104
edit_user = UserEditView.as_view()
(9-9/9)