PageRenderTime 39ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/archaeological_files/views.py

https://gitlab.com/cyrilbrulebois/ishtar
Python | 261 lines | 210 code | 34 blank | 17 comment | 12 complexity | f117cc4fdad993908d09a6fe66270c03 MD5 | raw file
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright (C) 2010-2015 √Čtienne Loks <etienne.loks_AT_peacefrogsDOTnet>
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as
  6. # published by the Free Software Foundation, either version 3 of the
  7. # License, or (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU Affero General Public License for more details.
  12. # You should have received a copy of the GNU Affero General Public License
  13. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. # See the file COPYING for details.
  15. import json
  16. import re
  17. from django.core.urlresolvers import reverse
  18. from django.db.models import Q
  19. from django.http import HttpResponse
  20. from django.shortcuts import render_to_response, redirect
  21. from django.utils.translation import ugettext_lazy as _
  22. from ishtar_common.views import get_item, show_item, revert_item
  23. from ishtar_common.models import Person
  24. from archaeological_operations.models import Operation
  25. import models
  26. from ishtar_common.wizards import SearchWizard
  27. from archaeological_operations.wizards import AdministrativeActDeletionWizard, \
  28. is_preventive, is_not_preventive
  29. from wizards import *
  30. from ishtar_common.forms_common import TownFormset
  31. from archaeological_operations.forms import ParcelFormSet, \
  32. FinalAdministrativeActDeleteForm
  33. from ishtar_common.forms import ClosingDateFormSelection
  34. from forms import *
  35. RE_YEAR_INDEX = re.compile(r"([1-2][0-9]{3})-([0-9]+)") # eg.: 2014-123
  36. def autocomplete_file(request):
  37. if not request.user.has_perm('ishtar_common.view_file', models.File) and \
  38. not request.user.has_perm('ishtar_common.view_own_file', models.File) \
  39. and not request.user.ishtaruser.has_right('file_search',
  40. session=request.session):
  41. return HttpResponse(mimetype='text/plain')
  42. if not request.GET.get('term'):
  43. return HttpResponse(mimetype='text/plain')
  44. q = request.GET.get('term')
  45. query = Q()
  46. for q in q.split(' '):
  47. extra = Q(internal_reference__icontains=q) | \
  48. Q(towns__name__icontains=q) | \
  49. Q(address__icontains=q)
  50. try:
  51. int(q)
  52. extra = extra | Q(year=q) | Q(numeric_reference=q)
  53. except ValueError:
  54. pass
  55. m = RE_YEAR_INDEX.match(q)
  56. if m:
  57. yr, idx = m.groups()
  58. extra = extra | Q(year=yr, numeric_reference=idx)
  59. query = query & extra
  60. limit = 20
  61. files = models.File.objects.filter(query)[:limit]
  62. data = json.dumps([{'id': file.pk, 'value': unicode(file)}
  63. for file in files])
  64. return HttpResponse(data, mimetype='text/plain')
  65. get_file = get_item(
  66. models.File, 'get_file', 'file',
  67. bool_fields=['end_date__isnull'],
  68. extra_request_keys={
  69. 'parcel_0': ('parcels__section',
  70. 'operations__parcels__section'),
  71. 'parcel_1': ('parcels__parcel_number',
  72. 'operations__parcels__parcel_number'),
  73. 'end_date': 'end_date__isnull',
  74. 'towns__numero_insee__startswith':
  75. 'towns__numero_insee__startswith',
  76. 'name': 'name__icontains',
  77. 'comment': 'comment__icontains',
  78. 'permit_reference': 'permit_reference__icontains',
  79. 'general_contractor__attached_to':
  80. 'general_contractor__attached_to__pk',
  81. 'history_creator': 'history_creator__ishtaruser__person__pk',
  82. 'history_modifier': 'history_modifier__ishtaruser__person__pk',
  83. },)
  84. revert_file = revert_item(models.File)
  85. def extra_file_dct(request, item):
  86. dct = {}
  87. if (request.user.has_perm('ishtar_common.add_operation', Operation)
  88. or request.user.ishtaruser.has_right('add_operation')):
  89. dct['can_add_operation'] = True
  90. return dct
  91. show_file = show_item(models.File, 'file', extra_dct=extra_file_dct)
  92. get_administrativeactfile = get_item(
  93. AdministrativeAct, 'get_administrativeactfile', 'administrativeactfile',
  94. associated_models=[
  95. (models.File, 'associated_file'),
  96. (Person, 'associated_file__general_contractor')],
  97. dated_fields=['signature_date__lte', 'signature_date__gte'],
  98. extra_request_keys={
  99. 'year': 'signature_date__year',
  100. 'associated_file__towns': 'associated_file__towns__pk',
  101. 'history_creator': 'history_creator__ishtaruser__person__pk',
  102. 'associated_file__operations__code_patriarche':
  103. 'associated_file__operations__code_patriarche',
  104. 'act_type__intented_to': 'act_type__intented_to',
  105. 'act_object': 'act_object__icontains',
  106. 'signature_date_before': 'signature_date__lte',
  107. 'signature_date_after': 'signature_date__gte',
  108. 'associated_file__general_contractor__attached_to':
  109. 'associated_file__general_contractor__attached_to__pk',
  110. 'associated_file__name': 'associated_file__name__icontains',
  111. 'associated_file__towns__numero_insee__startswith':
  112. 'associated_file__towns__numero_insee__startswith',
  113. 'indexed': 'index__isnull',
  114. 'parcel_0': ('operation__parcels__section',
  115. 'associated_file__parcels__section'),
  116. 'parcel_1': (
  117. 'operation__parcels__parcel_number',
  118. 'associated_file__parcels__parcel_number'),
  119. 'associated_file__permit_reference':
  120. 'associated_file__permit_reference__icontains'},
  121. reversed_bool_fields=['index__isnull'],
  122. base_request={"associated_file__pk__isnull": False},
  123. relative_session_names={'file': 'associated_file__pk'})
  124. def dashboard_file(request, *args, **kwargs):
  125. """
  126. Main dashboard
  127. """
  128. dct = {'dashboard': models.FileDashboard()}
  129. return render_to_response('ishtar/dashboards/dashboard_file.html', dct,
  130. context_instance=RequestContext(request))
  131. file_search_wizard = SearchWizard.as_view(
  132. [('general-file_search', FileFormSelection)],
  133. label=_(u"File search"), url_name='file_search',)
  134. file_creation_wizard = FileWizard.as_view(
  135. [('general-file_creation', FileFormGeneral),
  136. ('towns-file_creation', TownFormset),
  137. ('parcels-file_creation', ParcelFormSet),
  138. ('preventive-file_creation', FileFormPreventive),
  139. ('research-file_creation', FileFormResearch),
  140. ('final-file_creation', FinalForm)],
  141. label=_(u"New file"),
  142. condition_dict={
  143. 'preventive-file_creation':
  144. is_preventive('general-file_creation', models.FileType,
  145. type_key='file_type'),
  146. 'research-file_creation':
  147. is_not_preventive('general-file_creation', models.FileType,
  148. type_key='file_type'),
  149. },
  150. url_name='file_creation',)
  151. file_modification_wizard = FileModificationWizard.as_view(
  152. [('selec-file_modification', FileFormSelection),
  153. ('general-file_modification', FileFormGeneralRO),
  154. ('towns-file_modification', TownFormset),
  155. ('parcels-file_modification', ParcelFormSet),
  156. ('preventive-file_modification', FileFormPreventive),
  157. ('research-file_modification', FileFormResearch),
  158. ('final-file_modification', FinalForm)],
  159. label=_(u"File modification"),
  160. condition_dict={
  161. 'preventive-file_modification':
  162. is_preventive('general-file_modification',
  163. models.FileType, type_key='file_type'),
  164. 'research-file_modification':
  165. is_not_preventive('general-file_modification',
  166. models.FileType, type_key='file_type'),
  167. },
  168. url_name='file_modification',)
  169. def file_modify(request, pk):
  170. file_modification_wizard(request)
  171. FileModificationWizard.session_set_value(
  172. request, 'selec-file_modification', 'pk', pk, reset=True)
  173. return redirect(reverse('file_modification',
  174. kwargs={'step': 'general-file_modification'}))
  175. file_closing_wizard = FileClosingWizard.as_view(
  176. [('selec-file_closing', FileFormSelection),
  177. ('date-file_closing', ClosingDateFormSelection),
  178. ('final-file_closing', FinalFileClosingForm)],
  179. label=_(u"File closing"), url_name='file_closing',)
  180. file_deletion_wizard = FileDeletionWizard.as_view(
  181. [('selec-file_deletion', FileFormSelection),
  182. ('final-file_deletion', FinalFileDeleteForm)],
  183. label=_(u"File deletion"),
  184. url_name='file_deletion',)
  185. file_administrativeactfile_search_wizard = \
  186. SearchWizard.as_view([
  187. ('selec-file_administrativeactfile_search',
  188. AdministrativeActFileFormSelection)],
  189. label=_(u"File: search administrative act"),
  190. url_name='file_administrativeactfile_search',)
  191. file_administrativeactfile_wizard = \
  192. FileAdministrativeActWizard.as_view([
  193. ('selec-file_administrativeactfile', FileFormSelection),
  194. ('administrativeact-file_administrativeactfile',
  195. AdministrativeActFileForm),
  196. ('final-file_administrativeactfile', FinalForm)],
  197. label=_(u"File: new administrative act"),
  198. url_name='file_administrativeactfile',)
  199. file_administrativeactfile_modification_wizard = \
  200. FileEditAdministrativeActWizard.as_view([
  201. ('selec-file_administrativeactfile_modification',
  202. AdministrativeActFileModifyFormSelection),
  203. ('administrativeact-file_administrativeactfile_modification',
  204. AdministrativeActFileForm),
  205. ('final-file_administrativeactfile_modification', FinalForm)],
  206. label=_(u"File: administrative act modification"),
  207. url_name='file_administrativeactfile_modification',)
  208. file_administrativeactfile_deletion_wizard = \
  209. AdministrativeActDeletionWizard.as_view([
  210. ('selec-file_administrativeactfile_deletion',
  211. AdministrativeActFileFormSelection),
  212. ('final-file_administrativeactfile_deletion',
  213. FinalAdministrativeActDeleteForm)],
  214. label=_(u"File: administrative act deletion"),
  215. url_name='file_administrativeactfile_deletion',)
  216. def reset_wizards(request):
  217. for wizard_class, url_name in (
  218. (FileWizard, 'file_creation'),
  219. (FileModificationWizard, 'file_modification'),
  220. (FileClosingWizard, 'file_modification'),
  221. (FileDeletionWizard, 'file_deletion'),
  222. (FileAdministrativeActWizard, 'file_administrativeactfile'),
  223. (FileEditAdministrativeActWizard,
  224. 'file_administrativeactfile_modification_wizard'),
  225. (AdministrativeActDeletionWizard,
  226. 'file_administrativeactfile_deletion_wizard'),):
  227. wizard_class.session_reset(request, url_name)