Révision 64438aee
Ajouté par Serghei Mihai (congés, retour 15/05) il y a environ 9 ans
uauth/organization/views.py | ||
---|---|---|
1 |
import csv |
|
2 |
import datetime |
|
3 |
|
|
1 | 4 |
from django.utils.translation import ugettext as _ |
2 | 5 |
from django.core.urlresolvers import reverse_lazy |
3 |
from django.http import HttpResponseRedirect
|
|
6 |
from django.shortcuts import render, redirect
|
|
4 | 7 |
|
5 | 8 |
from django.views.generic.base import TemplateView |
6 | 9 |
from django.views.generic.list import ListView |
7 | 10 |
from django.views.generic.edit import FormView, UpdateView |
8 |
from django.views.generic import DetailView |
|
11 |
from django.views.generic import DetailView, View
|
|
9 | 12 |
from django.contrib import messages |
10 | 13 |
|
11 | 14 |
from django_tables2 import RequestConfig |
12 | 15 |
|
13 |
from .utils import create_user |
|
16 |
from .utils import create_user, create_or_update_users
|
|
14 | 17 |
from .models import LocalAccount, Organization |
15 |
from .forms import LocalAccountCreateForm, LocalAccountForm |
|
18 |
from .forms import LocalAccountCreateForm, LocalAccountForm, UsersImportForm
|
|
16 | 19 |
from .tables import AccountTable |
17 | 20 |
|
18 | 21 |
|
... | ... | |
96 | 99 |
if 'delete' in self.request.POST: |
97 | 100 |
self.object.delete() |
98 | 101 |
messages.info(self.request, _('Account "%s" successfully deleted' % username)) |
99 |
return HttpResponseRedirect(self.get_success_url())
|
|
102 |
return redirect(self.get_success_url())
|
|
100 | 103 |
else: |
101 | 104 |
messages.info(self.request, _('Account "%s" successfully updated' % username)) |
102 | 105 |
return super(UserEditView, self).form_valid(form) |
103 | 106 |
|
104 | 107 |
edit_user = UserEditView.as_view() |
108 |
|
|
109 |
|
|
110 |
class ImportUsersView(OrganizationMixin, TemplateView): |
|
111 |
form_class = UsersImportForm |
|
112 |
template_name = 'organization/import_users.html' |
|
113 |
|
|
114 |
def get_context_data(self, **kwargs): |
|
115 |
ctx = super(ImportUsersView, self).get_context_data(**kwargs) |
|
116 |
ctx['form'] = self.form_class() |
|
117 |
return ctx |
|
118 |
|
|
119 |
def post(self, request, *args, **kwargs): |
|
120 |
form = self.form_class(request.POST, request.FILES) |
|
121 |
context = self.get_context_data(**kwargs) |
|
122 |
context['form'] = form |
|
123 |
if form.is_valid(): |
|
124 |
data = form.cleaned_data['users_file'] |
|
125 |
dialect = csv.Sniffer().sniff(data.read(1024)) |
|
126 |
data.seek(0) |
|
127 |
reader = csv.reader(data, dialect) |
|
128 |
reader.next() |
|
129 |
users = [] |
|
130 |
for row in reader: |
|
131 |
try: |
|
132 |
user = {'username': row[0].strip(), |
|
133 |
'first_name': row[1].strip(), |
|
134 |
'last_name': row[2].strip(), |
|
135 |
'password': row[4].strip(), |
|
136 |
'organization': context['organization'] |
|
137 |
} |
|
138 |
except IndexError: |
|
139 |
# ignore wrong lines |
|
140 |
continue |
|
141 |
try: |
|
142 |
user['expiration_date'] = datetime.datetime.strptime(row[3], '%Y-%m-%d') |
|
143 |
except ValueError: |
|
144 |
pass |
|
145 |
users.append(user) |
|
146 |
created, updated = create_or_update_users(users) |
|
147 |
if created: |
|
148 |
messages.info(request, _('%s accounts added' % created)) |
|
149 |
if updated: |
|
150 |
messages.info(request, _('%s accounts updated' % updated)) |
|
151 |
return redirect(self.get_success_url()) |
|
152 |
else: |
|
153 |
return self.render_to_response(context) |
|
154 |
|
|
155 |
import_users = ImportUsersView.as_view() |
Formats disponibles : Unified diff
import users view(#7065)