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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2010-2016 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# See the file COPYING for details.
import json
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
from django.db.models import Q
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import redirect
from django.utils.translation import ugettext_lazy as _
from django.views.generic import TemplateView
from django.views.generic.edit import CreateView, FormView
from ishtar_common.forms import FinalForm
from ishtar_common.forms_common import SourceForm, AuthorFormset, \
SourceDeletionForm
from ishtar_common.models import IshtarUser
from archaeological_context_records.forms \
import RecordFormSelection as RecordFormSelectionTable
from ishtar_common.views import get_item, show_item, revert_item, \
get_autocomplete_generic, IshtarMixin, LoginRequiredMixin
from ishtar_common.wizards import SearchWizard
from wizards import *
from forms import *
import models
get_find = get_item(models.Find, 'get_find', 'find')
get_find_for_ope = get_item(models.Find, 'get_find', 'find',
own_table_cols=models.Find.TABLE_COLS_FOR_OPE)
get_find_for_treatment = get_item(
models.Find, 'get_find', 'find',
own_table_cols=models.Find.TABLE_COLS_FOR_OPE, base_request={})
show_treatment = show_item(models.Treatment, 'treatment')
get_treatment = get_item(models.Treatment, 'get_treatement', 'treatment')
show_treatmentfile = show_item(models.TreatmentFile, 'treatmentfile')
get_treatmentfile = get_item(models.TreatmentFile, 'get_treatementfile',
'treatmentfile')
def autocomplete_treatmentfile(request):
if not request.user.has_perm('ishtar_common.view_treatment',
models.Treatment) and \
not request.user.has_perm('ishtar_common.view_own_treatment',
models.Treatment) \
and not request.user.ishtaruser.has_right('treatmentfile_search',
session=request.session):
return HttpResponse(mimetype='text/plain')
if not request.GET.get('term'):
return HttpResponse(mimetype='text/plain')
q = request.GET.get('term')
query = Q()
for q1 in q.split(' '):
for q in q1.split(' '):
extra = Q(internal_reference__icontains=q) | \
Q(external_id__icontains=q) | \
Q(name__icontains=q)
try:
int(q)
extra = extra | Q(year=q) | Q(index=q)
except ValueError:
pass
query = query & extra
limit = 20
files = models.TreatmentFile.objects.filter(query)[:limit]
data = json.dumps([{'id': file.pk, 'value': unicode(file)}
for file in files])
return HttpResponse(data, mimetype='text/plain')
show_findsource = show_item(models.FindSource, 'findsource')
get_findsource = get_item(models.FindSource, 'get_findsource', 'findsource')
show_find = show_item(models.Find, 'find')
revert_find = revert_item(models.Find)
show_findbasket = show_item(models.FindBasket, 'findbasket')
find_creation_wizard = FindWizard.as_view([
('selecrecord-find_creation', RecordFormSelectionTable),
('find-find_creation', FindForm),
('dating-find_creation', DatingFormSet),
('final-find_creation', FinalForm)],
label=_(u"New find"),
url_name='find_creation',)
find_search_wizard = SearchWizard.as_view([
('general-find_search', FindFormSelection)],
label=_(u"Find search"),
url_name='find_search',)
find_modification_wizard = FindModificationWizard.as_view([
('selec-find_modification', FindFormSelection),
('selecrecord-find_modification', RecordFormSelection),
('find-find_modification', FindForm),
('dating-find_modification', DatingFormSet),
('final-find_modification', FinalForm)],
label=_(u"Find modification"),
url_name='find_modification',)
def find_modify(request, pk):
# view = find_modification_wizard(request)
FindModificationWizard.session_set_value(
request, 'selec-find_modification', 'pk', pk, reset=True)
return redirect(
reverse('find_modification',
kwargs={'step': 'selecrecord-find_modification'}))
find_deletion_wizard = FindDeletionWizard.as_view([
('selec-find_deletion', FindFormSelection),
('final-find_deletion', FindDeletionForm)],
label=_(u"Find deletion"),
url_name='find_deletion',)
find_source_search_wizard = SearchWizard.as_view([
('selec-find_source_search', FindSourceFormSelection)],
label=_(u"Find: source search"),
url_name='find_source_search',)
find_source_creation_wizard = FindSourceWizard.as_view([
('selec-find_source_creation', SourceFindFormSelection),
('source-find_source_creation', SourceForm),
('authors-find_source_creation', AuthorFormset),
('final-find_source_creation', FinalForm)],
label=_(u"Find: new source"),
url_name='find_source_creation',)
find_source_modification_wizard = FindSourceWizard.as_view([
('selec-find_source_modification', FindSourceFormSelection),
('source-find_source_modification', SourceForm),
('authors-find_source_modification', AuthorFormset),
('final-find_source_modification', FinalForm)],
label=_(u"Find: source modification"),
url_name='find_source_modification',)
def find_source_modify(request, pk):
find_source_modification_wizard(request)
FindSourceWizard.session_set_value(
request, 'selec-find_source_modification', 'pk', pk, reset=True)
return redirect(reverse(
'find_source_modification',
kwargs={'step': 'source-find_source_modification'}))
find_source_deletion_wizard = FindSourceDeletionWizard.as_view([
('selec-find_source_deletion', FindSourceFormSelection),
('final-find_source_deletion', SourceDeletionForm)],
label=_(u"Find: source deletion"),
url_name='find_source_deletion',)
autocomplete_objecttype = get_autocomplete_generic(models.ObjectType)
autocomplete_materialtype = get_autocomplete_generic(models.MaterialType)
autocomplete_preservationtype = get_autocomplete_generic(
models.PreservationType)
autocomplete_integritytype = get_autocomplete_generic(models.IntegrityType)
class NewFindBasketView(IshtarMixin, LoginRequiredMixin, CreateView):
template_name = 'ishtar/form.html'
model = models.FindBasket
form_class = NewFindBasketForm
page_name = _(u"New basket")
def get_form_kwargs(self):
kwargs = super(NewFindBasketView, self).get_form_kwargs()
kwargs['user'] = IshtarUser.objects.get(pk=self.request.user.pk)
return kwargs
def get_success_url(self):
return reverse('select_itemsinbasket',
kwargs={'pk': self.object.pk})
def form_valid(self, form):
self.object = form.save()
return HttpResponseRedirect(self.get_success_url())
class SelectBasketForManagement(IshtarMixin, LoginRequiredMixin, FormView):
template_name = 'ishtar/form.html'
form_class = SelectFindBasketForm
page_name = _(u"Manage items in basket")
def get_form_kwargs(self):
kwargs = super(SelectBasketForManagement, self).get_form_kwargs()
kwargs['user'] = IshtarUser.objects.get(pk=self.request.user.pk)
if 'pk' in self.kwargs:
kwargs['initial'].update({'basket': self.kwargs['pk']})
return kwargs
def get_success_url(self, basket):
return reverse('select_itemsinbasket',
kwargs={'pk': basket})
def form_valid(self, form):
return HttpResponseRedirect(self.get_success_url(
form.cleaned_data['basket']))
class SelectItemsInBasket(IshtarMixin, LoginRequiredMixin, TemplateView):
template_name = 'ishtar/manage_basket.html'
page_name = _(u"Manage basket")
def get_context_data(self, *args, **kwargs):
context = super(SelectItemsInBasket, self).get_context_data(
*args, **kwargs)
self.user = IshtarUser.objects.get(pk=self.request.user.pk)
try:
self.basket = models.FindBasket.objects.get(
pk=self.kwargs['pk'], user=self.user)
except models.FindBasket.DoesNotExist:
raise PermissionDenied
context['basket'] = self.basket
context['form'] = MultipleFindFormSelection()
context['add_url'] = reverse('add_iteminbasket')
context['list_url'] = reverse('list_iteminbasket',
kwargs={'pk': self.basket.pk})
return context
def form_valid(self, form):
return HttpResponseRedirect(self.get_success_url())
class FindBasketAddItemView(IshtarMixin, LoginRequiredMixin, FormView):
template_name = 'ishtar/simple_form.html'
form_class = FindBasketAddItemForm
def get_success_url(self, basket):
return reverse('list_iteminbasket', kwargs={'pk': basket.pk})
def form_valid(self, form):
user = IshtarUser.objects.get(pk=self.request.user.pk)
# rights are checked on the form
basket = form.save(user)
return HttpResponseRedirect(self.get_success_url(basket))
class FindBasketListView(IshtarMixin, LoginRequiredMixin, TemplateView):
template_name = 'ishtar/basket_list.html'
def get_context_data(self, *args, **kwargs):
context = super(FindBasketListView, self).get_context_data(
*args, **kwargs)
self.user = IshtarUser.objects.get(pk=self.request.user.pk)
try:
self.basket = models.FindBasket.objects.get(
pk=self.kwargs['pk'], user=self.user)
except models.FindBasket.DoesNotExist:
raise PermissionDenied
context['basket'] = self.basket
context['item_url'] = '/'.join(
reverse(models.Find.SHOW_URL, args=[1]).split('/')[:-1])
context['delete_url'] = '/'.join(
reverse('delete_iteminbasket', args=[1, 1]).split('/')[:-3])
return context
class FindBasketDeleteItemView(IshtarMixin, LoginRequiredMixin, TemplateView):
template_name = 'ishtar/simple_form.html'
def get_success_url(self, basket):
return reverse('list_iteminbasket', kwargs={'pk': basket.pk})
def get(self, *args, **kwargs):
user = self.request.user
ishtaruser = IshtarUser.objects.get(pk=self.request.user.pk)
try:
find = models.Find.objects.get(
pk=self.kwargs['find_pk'])
except models.Find.DoesNotExist:
raise PermissionDenied
try:
basket = models.FindBasket.objects.get(
pk=self.kwargs['basket'], user=ishtaruser)
except models.FindBasket.DoesNotExist:
raise PermissionDenied
if not user.is_superuser and \
not ishtaruser.has_right('change_find') and \
not (ishtaruser.has_right('change_own_find')
and find.is_own(user)):
raise PermissionDenied
basket.items.remove(find)
return HttpResponseRedirect(self.get_success_url(basket))
class DeleteFindBasketView(IshtarMixin, LoginRequiredMixin, FormView):
template_name = 'ishtar/form_delete.html'
form_class = DeleteFindBasketForm
success_url = '/'
page_name = _(u"Delete basket")
def get_form_kwargs(self):
kwargs = super(DeleteFindBasketView, self).get_form_kwargs()
kwargs['user'] = IshtarUser.objects.get(pk=self.request.user.pk)
return kwargs
def form_valid(self, form):
form.save()
return HttpResponseRedirect(self.get_success_url())
get_upstreamtreatment = get_item(
models.FindUpstreamTreatments, 'get_upstreamtreatment', 'uptreatment')
get_downstreamtreatment = get_item(
models.FindDownstreamTreatments, 'get_downstreamtreatment',
'downtreatment')
treatment_wizard_steps = [
('file-treatment_creation', TreatmentFormFileChoice),
('basetreatment-treatment_creation', BaseTreatmentForm),
('selecfind-treatment_creation', UpstreamFindFormSelection),
('selecbasket-treatment_creation', SelectFindBasketForm),
# ('resultfind-treatment_creation', ResultFindForm),
# ('resultfinds-treatment_creation', ResultFindFormSet),
('final-treatment_creation', FinalForm)]
treatment_search_wizard = SearchWizard.as_view([
('general-treatment_search', TreatmentFormSelection)],
label=_(u"Treatment search"),
url_name='treatment_search',)
treatment_creation_wizard = TreatmentWizard.as_view(
treatment_wizard_steps,
condition_dict={
'selecfind-treatment_creation':
check_value('basetreatment-treatment_creation',
'target_is_basket', False),
'selecbasket-treatment_creation':
check_value('basetreatment-treatment_creation',
'target_is_basket', True),
# 'resultfinds-treatment_creation':
# check_type_field('basetreatment-treatment_creation',
# 'treatment_type', models.TreatmentType,
# 'downstream_is_many'),
# 'resultfind-treatment_creation':
# check_type_field('basetreatment-treatment_creation',
# 'treatment_type', models.TreatmentType,
# 'upstream_is_many')
},
label=_(u"New treatment"),
url_name='treatment_creation',)
treatment_modification_wizard = TreatmentModificationWizard.as_view(
[('selec-treatment_modification', TreatmentFormSelection),
('basetreatment-treatment_modification', BaseTreatmentForm),
('final-treatment_modification', FinalForm)],
label=_(u"Modify"),
url_name='treatment_modification',
)
treatment_deletion_wizard = TreatmentDeletionWizard.as_view([
('selec-treatment_deletion', TreatmentFormSelection),
('final-treatment_deletion', TreatmentDeletionForm)],
label=_(u"Treatment deletion"),
url_name='treatment_deletion',)
treatmentfile_search_wizard = SearchWizard.as_view([
('general-treatmentfile_search', TreatmentFileFormSelection)],
label=_(u"Treatment file search"),
url_name='treatmentfile_search',)
treatmentfile_wizard_steps = [
('treatmentfile-treatmentfile_creation', TreatmentFileForm),
('final-treatmentfile_creation', FinalForm)]
treatmentfile_creation_wizard = TreatmentFileWizard.as_view(
treatmentfile_wizard_steps,
label=_(u"New treatment file"),
url_name='treatmentfile_creation',)
treatmentfile_modification_wizard = TreatmentFileModificationWizard.as_view(
[('selec-treatmentfile_modification', TreatmentFileFormSelection),
('treatmentfile-treatmentfile_modification', TreatmentFileForm),
('final-treatmentfile_modification', FinalForm)],
label=_(u"Modify"),
url_name='treatmentfile_modification',
)
treatmentfile_deletion_wizard = TreatmentFileDeletionWizard.as_view([
('selec-treatmentfile_deletion', TreatmentFileFormSelection),
('final-treatmentfile_deletion', TreatmentFileDeletionForm)],
label=_(u"Treatment file deletion"),
url_name='treatmentfile_deletion',)
"""
treatment_source_creation_wizard = TreatmentSourceWizard.as_view([
('selec-treatment_source_creation', SourceTreatmentFormSelection),
('source-treatment_source_creation', SourceForm),
('authors-treatment_source_creation', AuthorFormset),
('final-treatment_source_creation', FinalForm)],
url_name='treatment_source_creation',)
"""
|