summaryrefslogtreecommitdiffstats
path: root/uauth/organization/views.py
blob: 3175cd6ee940649bfa366dabab0f8e5ae9272fa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from django.utils.translation import ugettext as _
from django.core.urlresolvers import reverse_lazy

from django.views.generic.base import TemplateView
from django.views.generic.list import ListView
from django.views.generic.edit import FormView
from django.contrib import messages

from django_tables2 import RequestConfig

from .utils import create_user
from .models import LocalAccount, Organization
from .forms import LocalAccountCreateForm
from .tables import AccountTable


class OrganizationMixin(object):

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

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

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

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

manage = ManageView.as_view()

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

    def get_context_data(self, *args, **kwargs):
        context = super(UsersPageView, self).get_context_data(*args, **kwargs)
        table = AccountTable(context['object_list'])
        RequestConfig(self.request).configure(table)
        context['table'] = table
        return context

users = UsersPageView.as_view()


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

    def form_valid(self, form):
        data = form.cleaned_data
        data['organization'] = Organization.objects.get(slug=self.kwargs['organization_slug'])
        accounts_number = data.pop('accounts_number')
        accounts_number_start = data.pop('accounts_number_start')
        if accounts_number_start < 1:
            accounts_number_start = 0

        username = data['username']
        if accounts_number > 1:
            for index in xrange(accounts_number_start, accounts_number + accounts_number_start):
                data['username'] = '%s-%s' % (username, index)
                if not create_user(data):
                    messages.error(self, request, _('Error while creating user %s' % data['username']))
                    break
            messages.info(self.request, _('%s users added successfully' % accounts_number))
        else:
            if create_user(data):
                messages.info(self.request, _('User "%s" successfully created' % data['username']))
            else:
                messages.error(self.request, _('Error occured while creating user "%s"' % data['username']))
        return super(UsersCreateView, self).form_valid(form)

create_users = UsersCreateView.as_view()