1
|
import os
|
2
|
import csv
|
3
|
import datetime
|
4
|
|
5
|
from django.conf import settings
|
6
|
from django.utils.translation import ugettext as _
|
7
|
from django.core.urlresolvers import reverse_lazy
|
8
|
from django.shortcuts import render, redirect
|
9
|
|
10
|
from django.views.generic.base import TemplateView
|
11
|
from django.views.generic.list import ListView
|
12
|
from django.views.generic.edit import FormView, UpdateView
|
13
|
from django.views.generic import DetailView, View
|
14
|
from django.contrib import messages
|
15
|
|
16
|
from django_tables2 import RequestConfig
|
17
|
|
18
|
from .utils import create_user, create_or_update_users
|
19
|
from .models import LocalAccount, Organization
|
20
|
from .forms import *
|
21
|
from .tables import AccountTable
|
22
|
|
23
|
|
24
|
class OrganizationMixin(object):
|
25
|
|
26
|
def get_queryset(self):
|
27
|
qs = super(OrganizationMixin, self).get_queryset()
|
28
|
return qs.filter(organization__slug=self.kwargs['organization_slug'])
|
29
|
|
30
|
def get_success_url(self):
|
31
|
return reverse_lazy('manage-users', kwargs={'organization_slug': self.kwargs['organization_slug']})
|
32
|
|
33
|
def get_context_data(self, *args, **kwargs):
|
34
|
ctx = super(OrganizationMixin, self).get_context_data(*args, **kwargs)
|
35
|
ctx['organization'] = Organization.objects.get(slug=self.kwargs['organization_slug'])
|
36
|
return ctx
|
37
|
|
38
|
class ManageView(OrganizationMixin, TemplateView):
|
39
|
template_name = 'organization/manage.html'
|
40
|
|
41
|
manage = ManageView.as_view()
|
42
|
|
43
|
class UsersPageView(OrganizationMixin, ListView):
|
44
|
template_name = 'organization/users.html'
|
45
|
model = LocalAccount
|
46
|
|
47
|
def get_context_data(self, *args, **kwargs):
|
48
|
context = super(UsersPageView, self).get_context_data(*args, **kwargs)
|
49
|
table = AccountTable(context['object_list'])
|
50
|
RequestConfig(self.request).configure(table)
|
51
|
context['table'] = table
|
52
|
return context
|
53
|
|
54
|
users = UsersPageView.as_view()
|
55
|
|
56
|
|
57
|
class UsersCreateView(OrganizationMixin, FormView):
|
58
|
template_name = 'organization/create-users.html'
|
59
|
form_class = LocalAccountCreateForm
|
60
|
|
61
|
def form_valid(self, form):
|
62
|
data = form.cleaned_data
|
63
|
data['organization'] = Organization.objects.get(slug=self.kwargs['organization_slug'])
|
64
|
accounts_number = data.pop('accounts_number')
|
65
|
accounts_number_start = data.pop('accounts_number_start')
|
66
|
if accounts_number_start < 1:
|
67
|
accounts_number_start = 0
|
68
|
|
69
|
username = data['username']
|
70
|
if accounts_number > 1:
|
71
|
for index in xrange(accounts_number_start, accounts_number + accounts_number_start):
|
72
|
data['username'] = '%s-%s' % (username, index)
|
73
|
if not create_user(data):
|
74
|
messages.error(self, request, _('Error while creating user %s') % data['username'])
|
75
|
break
|
76
|
messages.info(self.request, _('%s users added successfully') % accounts_number)
|
77
|
else:
|
78
|
if create_user(data):
|
79
|
messages.info(self.request, _('User "%s" successfully created') % data['username'])
|
80
|
else:
|
81
|
messages.error(self.request, _('Error occured while creating user "%s"') % data['username'])
|
82
|
return super(UsersCreateView, self).form_valid(form)
|
83
|
|
84
|
create_users = UsersCreateView.as_view()
|
85
|
|
86
|
|
87
|
class ShowUserView(OrganizationMixin, DetailView):
|
88
|
model = LocalAccount
|
89
|
template_name = 'organization/view_user.html'
|
90
|
|
91
|
view_user = ShowUserView.as_view()
|
92
|
|
93
|
|
94
|
class UserEditView(OrganizationMixin, UpdateView):
|
95
|
template_name = 'organization/edit_user.html'
|
96
|
model = LocalAccount
|
97
|
form_class = LocalAccountForm
|
98
|
|
99
|
def form_valid(self, form):
|
100
|
username = self.object.username
|
101
|
if 'delete' in self.request.POST:
|
102
|
self.object.delete()
|
103
|
messages.info(self.request, _('Account "%s" successfully deleted') % username)
|
104
|
return redirect(self.get_success_url())
|
105
|
else:
|
106
|
messages.info(self.request, _('Account "%s" successfully updated') % username)
|
107
|
return super(UserEditView, self).form_valid(form)
|
108
|
|
109
|
edit_user = UserEditView.as_view()
|
110
|
|
111
|
|
112
|
class ImportUsersView(OrganizationMixin, TemplateView):
|
113
|
form_class = UsersImportForm
|
114
|
template_name = 'organization/import_users.html'
|
115
|
|
116
|
def get_context_data(self, **kwargs):
|
117
|
ctx = super(ImportUsersView, self).get_context_data(**kwargs)
|
118
|
ctx['form'] = self.form_class()
|
119
|
return ctx
|
120
|
|
121
|
def post(self, request, *args, **kwargs):
|
122
|
form = self.form_class(request.POST, request.FILES)
|
123
|
context = self.get_context_data(**kwargs)
|
124
|
context['form'] = form
|
125
|
if form.is_valid():
|
126
|
data = form.cleaned_data['users_file']
|
127
|
dialect = csv.Sniffer().sniff(data.read(1024))
|
128
|
data.seek(0)
|
129
|
reader = csv.reader(data, dialect)
|
130
|
reader.next()
|
131
|
users = []
|
132
|
for row in reader:
|
133
|
try:
|
134
|
user = {'username': row[0].strip(),
|
135
|
'first_name': row[1].strip(),
|
136
|
'last_name': row[2].strip(),
|
137
|
'password': row[4].strip(),
|
138
|
'organization': context['organization']
|
139
|
}
|
140
|
except IndexError:
|
141
|
# ignore wrong lines
|
142
|
continue
|
143
|
try:
|
144
|
user['expiration_date'] = datetime.datetime.strptime(row[3], '%Y-%m-%d')
|
145
|
except ValueError:
|
146
|
pass
|
147
|
users.append(user)
|
148
|
created, updated = create_or_update_users(users)
|
149
|
if created:
|
150
|
messages.info(request, _('%s accounts added') % created)
|
151
|
if updated:
|
152
|
messages.info(request, _('%s accounts updated') % updated)
|
153
|
return redirect(self.get_success_url())
|
154
|
else:
|
155
|
return self.render_to_response(context)
|
156
|
|
157
|
import_users = ImportUsersView.as_view()
|
158
|
|
159
|
|
160
|
class ThemeView(OrganizationMixin, TemplateView):
|
161
|
template_name = 'organization/theme.html'
|
162
|
|
163
|
def get_success_url(self):
|
164
|
return reverse_lazy('manage-theme', kwargs={'organization_slug': self.kwargs['organization_slug']})
|
165
|
|
166
|
def get_context_data(self, **kwargs):
|
167
|
ctx = super(ThemeView, self).get_context_data(**kwargs)
|
168
|
organization = ctx['organization']
|
169
|
templates_dir = os.path.join(settings.ORGANIZATIONS_DIR,
|
170
|
organization.slug, 'templates')
|
171
|
statics_dir = os.path.join(settings.ORGANIZATIONS_DIR,
|
172
|
organization.slug, 'static')
|
173
|
ctx['templates'] = []
|
174
|
ctx['statics'] = []
|
175
|
if os.path.exists(templates_dir):
|
176
|
ctx['templates'] = os.listdir(templates_dir)
|
177
|
if os.path.exists(statics_dir):
|
178
|
ctx['statics'] = os.listdir(statics_dir)
|
179
|
ctx['templates_dir'] = templates_dir
|
180
|
ctx['statics_dir'] = statics_dir
|
181
|
return ctx
|
182
|
|
183
|
theme = ThemeView.as_view()
|
184
|
|
185
|
class UploadMixin(object):
|
186
|
template_name = "organization/upload.html"
|
187
|
|
188
|
def get_context_data(self, **kwargs):
|
189
|
ctx = super(UploadMixin, self).get_context_data(**kwargs)
|
190
|
ctx['form'] = self.form_class()
|
191
|
return ctx
|
192
|
|
193
|
def post(self, request, *args, **kwargs):
|
194
|
form = self.form_class(request.POST, request.FILES)
|
195
|
context = self.get_context_data(**kwargs)
|
196
|
context['form'] = form
|
197
|
organization = context['organization']
|
198
|
destination_dir = os.path.join(settings.ORGANIZATIONS_DIR,
|
199
|
organization.slug, self.upload_dir)
|
200
|
if form.is_valid():
|
201
|
data = form.cleaned_data[self.filename_param]
|
202
|
if not os.path.exists(destination_dir):
|
203
|
os.makedirs(destination_dir)
|
204
|
try:
|
205
|
with open(os.path.join(destination_dir, data.name), 'w') as template:
|
206
|
template.write(data.read())
|
207
|
messages.info(request, _('File "%s" successfully uploaded') % data.name)
|
208
|
except OSError:
|
209
|
messages.error(request, _('An error occured while uploading file "%s"') % data.name)
|
210
|
return redirect(self.get_success_url())
|
211
|
else:
|
212
|
return self.render_to_response(context)
|
213
|
|
214
|
|
215
|
class TemplateUpload(UploadMixin, ThemeView):
|
216
|
form_class = TemplateForm
|
217
|
filename_param = 'template_file'
|
218
|
upload_dir = 'templates'
|
219
|
|
220
|
template_upload = TemplateUpload.as_view()
|
221
|
|
222
|
|
223
|
class TemplateDelete(ThemeView):
|
224
|
|
225
|
def get(self, request, *args, **kwargs):
|
226
|
ctx = self.get_context_data(**kwargs)
|
227
|
template = request.GET.get('template')
|
228
|
if os.path.exists(os.path.join(ctx['templates_dir'], template)):
|
229
|
try:
|
230
|
os.remove(os.path.join(ctx['templates_dir'], template))
|
231
|
messages.info(request, _('Template %s successfully removed') % template)
|
232
|
except IOError:
|
233
|
messages.error(request, _('An error occured while removing file %s') % template)
|
234
|
else:
|
235
|
messages.error(request, _('Unknown template %s') % template)
|
236
|
return redirect(self.get_success_url())
|
237
|
|
238
|
|
239
|
template_delete = TemplateDelete.as_view()
|
240
|
|
241
|
|
242
|
class StaticUpload(UploadMixin, ThemeView):
|
243
|
form_class = StaticForm
|
244
|
filename_param = 'static_file'
|
245
|
upload_dir = 'static'
|
246
|
|
247
|
static_upload = StaticUpload.as_view()
|
248
|
|
249
|
|
250
|
class StaticDelete(ThemeView):
|
251
|
|
252
|
def get(self, request, *args, **kwargs):
|
253
|
ctx = self.get_context_data(**kwargs)
|
254
|
static = request.GET.get('static')
|
255
|
if os.path.exists(os.path.join(ctx['statics_dir'], static)):
|
256
|
try:
|
257
|
os.remove(os.path.join(ctx['statics_dir'], static))
|
258
|
messages.info(request, _('Static file %s successfully removed') % static)
|
259
|
except IOError:
|
260
|
messages.error(request, _('An error occured while removing file %s') % static)
|
261
|
else:
|
262
|
messages.error(request, _('Unknown static %s') % static)
|
263
|
return redirect(self.get_success_url())
|
264
|
|
265
|
static_delete = StaticDelete.as_view()
|
266
|
|
267
|
|
268
|
class FederationsView(OrganizationMixin, TemplateView):
|
269
|
template_name = 'organization/federations.html'
|
270
|
|
271
|
def get_success_url(self):
|
272
|
return reverse_lazy('manage-federations', kwargs={'organization_slug': self.kwargs['organization_slug']})
|
273
|
|
274
|
def get_context_data(self, **kwargs):
|
275
|
ctx = super(FederationsView, self).get_context_data(**kwargs)
|
276
|
org = ctx['organization']
|
277
|
federations_dir = os.path.join(settings.ORGANIZATIONS_DIR,
|
278
|
org.slug, 'federations')
|
279
|
ctx['federations_dir'] = federations_dir
|
280
|
if os.path.exists(federations_dir):
|
281
|
ctx['federations'] = os.listdir(federations_dir)
|
282
|
else:
|
283
|
ctx['federations'] = []
|
284
|
return ctx
|
285
|
|
286
|
federations = FederationsView.as_view()
|
287
|
|
288
|
|
289
|
class FederationsAdd(UploadMixin, FederationsView):
|
290
|
form_class = FederationForm
|
291
|
filename_param = 'federation_file'
|
292
|
upload_dir = 'federations'
|
293
|
|
294
|
federations_add = FederationsAdd.as_view()
|
295
|
|
296
|
|
297
|
class FederationsDelete(FederationsView):
|
298
|
def get(self, request, *args, **kwargs):
|
299
|
ctx = self.get_context_data(**kwargs)
|
300
|
f = request.GET.get('federation')
|
301
|
if os.path.exists(os.path.join(ctx['federations_dir'], f)):
|
302
|
try:
|
303
|
os.remove(os.path.join(ctx['federations_dir'], f))
|
304
|
messages.info(request, _('Federation file %s successfully removed') % f)
|
305
|
except IOError:
|
306
|
messages.error(request, _('An error occured while removing file %s') % f)
|
307
|
else:
|
308
|
messages.error(request, _('Unknown federation file %s') % f)
|
309
|
return redirect(self.get_success_url())
|
310
|
|
311
|
federations_delete = FederationsDelete.as_view()
|