2
0

models.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. from collections import OrderedDict
  2. from importlib import import_module
  3. from itertools import zip_longest
  4. import json
  5. import os
  6. from pathlib import Path
  7. from PIL import Image
  8. import datetime
  9. from django.apps import apps
  10. from django.conf import settings
  11. from django.contrib import messages
  12. from django.contrib.contenttypes.fields import GenericForeignKey
  13. from django.contrib.contenttypes.models import ContentType
  14. from django.core.files.storage import default_storage
  15. from django.core.serializers.json import DjangoJSONEncoder
  16. from django.db.models import (
  17. CharField, TextField, DateTimeField, Model, ForeignKey, PROTECT, CASCADE,
  18. QuerySet,
  19. )
  20. from django.db.models.fields.files import FieldFile
  21. from django.db.models.signals import post_delete
  22. from django.dispatch import receiver
  23. from django.forms import Form, ImageField, FileField, URLField, EmailField
  24. from django.http import HttpResponseRedirect
  25. from django.template.response import TemplateResponse
  26. from django.utils.safestring import SafeData, mark_safe
  27. from django.utils.timezone import now
  28. from django.utils.translation import gettext_lazy as _
  29. from wagtail.models import Page
  30. from wagtail.contrib.forms.models import (
  31. AbstractForm, AbstractEmailForm, AbstractFormSubmission)
  32. from .blocks import FormStepBlock, FormFieldBlock
  33. class Step:
  34. def __init__(self, steps, index, struct_child):
  35. self.steps = steps
  36. self.index = index
  37. block = getattr(struct_child, 'block', None)
  38. if block is None:
  39. struct_child = []
  40. if isinstance(block, FormStepBlock):
  41. self.name = struct_child.value['name']
  42. self.form_fields = struct_child.value['form_fields']
  43. else:
  44. self.name = ''
  45. self.form_fields = struct_child
  46. @property
  47. def index1(self):
  48. return self.index + 1
  49. @property
  50. def url(self):
  51. return '%s?step=%s' % (self.steps.page.url, self.index1)
  52. def get_form_fields(self):
  53. form_fields = OrderedDict()
  54. field_blocks = self.form_fields
  55. for struct_child in field_blocks:
  56. block = struct_child.block
  57. if isinstance(block, FormFieldBlock):
  58. struct_value = struct_child.value
  59. field_name = block.get_slug(struct_value)
  60. form_fields[field_name] = block.get_field(struct_value)
  61. return form_fields
  62. def get_form_class(self):
  63. return type('WagtailForm', self.steps.page.get_form_class_bases(),
  64. self.get_form_fields())
  65. def get_markups_and_bound_fields(self, form):
  66. for struct_child in self.form_fields:
  67. block = struct_child.block
  68. if isinstance(block, FormFieldBlock):
  69. struct_value = struct_child.value
  70. field_name = block.get_slug(struct_value)
  71. yield form[field_name], 'field'
  72. else:
  73. yield mark_safe(struct_child), 'markup'
  74. def __str__(self):
  75. if self.name:
  76. return self.name
  77. return _('Step %s') % self.index1
  78. @property
  79. def badge(self):
  80. return (mark_safe('<span class="badge">%s/%s</span>')
  81. % (self.index1, len(self.steps)))
  82. def __html__(self):
  83. return '%s %s' % (self, self.badge)
  84. @property
  85. def is_active(self):
  86. return self.index == self.steps.current_index
  87. @property
  88. def is_last(self):
  89. return self.index1 == len(self.steps)
  90. @property
  91. def has_prev(self):
  92. return self.index > 0
  93. @property
  94. def has_next(self):
  95. return self.index1 < len(self.steps)
  96. @property
  97. def prev(self):
  98. if self.has_prev:
  99. return self.steps[self.index-1]
  100. @property
  101. def next(self):
  102. if self.has_next:
  103. return self.steps[self.index+1]
  104. def get_existing_data(self, raw=False):
  105. data = self.steps.get_existing_data()[self.index]
  106. fields = self.get_form_fields()
  107. if not raw:
  108. class FakeField:
  109. storage = self.steps.get_storage()
  110. for field_name, value in data.items():
  111. if field_name in fields and isinstance(fields[field_name],
  112. FileField):
  113. data[field_name] = FieldFile(None, FakeField, value)
  114. return data
  115. @property
  116. def is_available(self):
  117. return self.prev is None or self.prev.get_existing_data(raw=True)
  118. class StreamFormJSONEncoder(DjangoJSONEncoder):
  119. def default(self, o):
  120. try:
  121. from phonenumber_field.phonenumber import PhoneNumber
  122. except ImportError:
  123. pass
  124. else:
  125. if isinstance(o, PhoneNumber):
  126. return str(o)
  127. return super().default(o)
  128. class Steps(list):
  129. def __init__(self, page, request=None):
  130. self.page = page
  131. # TODO: Make it possible to change the `form_fields` attribute.
  132. self.form_fields = page.form_fields
  133. self.request = request
  134. has_steps = any(isinstance(struct_child.block, FormStepBlock)
  135. for struct_child in self.form_fields)
  136. if has_steps:
  137. steps = [Step(self, i, form_field)
  138. for i, form_field in enumerate(self.form_fields)]
  139. else:
  140. steps = [Step(self, 0, self.form_fields)]
  141. super().__init__(steps)
  142. def clamp_index(self, index: int):
  143. if index < 0:
  144. index = 0
  145. if index >= len(self):
  146. index = len(self) - 1
  147. while not self[index].is_available:
  148. index -= 1
  149. return index
  150. @property
  151. def current_index(self):
  152. return self.request.session.get(self.page.current_step_session_key, 0)
  153. @property
  154. def current(self):
  155. return self[self.current_index]
  156. @current.setter
  157. def current(self, new_index: int):
  158. if not isinstance(new_index, int):
  159. raise TypeError('Use an integer to set the new current step.')
  160. self.request.session[self.page.current_step_session_key] = \
  161. self.clamp_index(new_index)
  162. def forward(self, increment: int = 1):
  163. self.current = self.current_index + increment
  164. def backward(self, increment: int = 1):
  165. self.current = self.current_index - increment
  166. def get_submission(self):
  167. return self.page.get_submission(self.request)
  168. def get_existing_data(self):
  169. submission = self.get_submission()
  170. data = [] if submission is None else json.loads(submission.form_data)
  171. length_difference = len(self) - len(data)
  172. if length_difference > 0:
  173. data.extend([{}] * length_difference)
  174. return data
  175. def get_current_form(self):
  176. request = self.request
  177. if request.method == 'POST':
  178. step_value = request.POST.get('step', 'next')
  179. if step_value == 'prev':
  180. self.backward()
  181. else:
  182. return self.current.get_form_class()(
  183. request.POST, request.FILES,
  184. initial=self.current.get_existing_data())
  185. return self.current.get_form_class()(
  186. initial=self.current.get_existing_data())
  187. def get_storage(self):
  188. return self.page.get_storage()
  189. def save_files(self, form):
  190. submission = self.get_submission()
  191. for name, field in form.fields.items():
  192. if isinstance(field, FileField):
  193. file = form.cleaned_data[name]
  194. if file == form.initial.get(name, ''): # Nothing submitted.
  195. form.cleaned_data[name] = file.name
  196. continue
  197. if submission is not None:
  198. submission.delete_file(name)
  199. if not file: # 'Clear' was checked.
  200. form.cleaned_data[name] = ''
  201. continue
  202. directory = self.request.session.session_key
  203. storage = self.get_storage()
  204. Path(storage.path(directory)).mkdir(parents=True,
  205. exist_ok=True)
  206. path = storage.get_available_name(
  207. str(Path(directory) / file.name))
  208. with storage.open(path, 'wb+') as destination:
  209. for chunk in file.chunks():
  210. destination.write(chunk)
  211. form.cleaned_data[name] = path
  212. def update_data(self):
  213. form = self.get_current_form()
  214. if form.is_valid():
  215. form_data = self.get_existing_data()
  216. self.save_files(form)
  217. form_data[self.current_index] = form.cleaned_data
  218. form_data = json.dumps(form_data, cls=StreamFormJSONEncoder)
  219. is_complete = self.current.is_last
  220. submission = self.get_submission()
  221. submission.form_data = form_data
  222. if not submission.is_complete and is_complete:
  223. submission.status = submission.COMPLETE
  224. submission.save()
  225. if is_complete:
  226. self.current = 0
  227. else:
  228. self.forward()
  229. return is_complete
  230. return False
  231. class SessionFormSubmission(AbstractFormSubmission):
  232. session_key = CharField(max_length=40, null=True, default=None)
  233. user = ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True,
  234. related_name='+', on_delete=PROTECT)
  235. thumbnails_by_path = TextField(default=json.dumps({}))
  236. last_modification = DateTimeField(_('last modification'), auto_now=True)
  237. INCOMPLETE = 'incomplete'
  238. COMPLETE = 'complete'
  239. REVIEWED = 'reviewed'
  240. APPROVED = 'approved'
  241. REJECTED = 'rejected'
  242. STATUSES = (
  243. (INCOMPLETE, _('Not submitted')),
  244. (COMPLETE, _('In progress')),
  245. (REVIEWED, _('Under consideration')),
  246. (APPROVED, _('Approved')),
  247. (REJECTED, _('Rejected')),
  248. )
  249. status = CharField(max_length=10, choices=STATUSES, default=INCOMPLETE)
  250. class Meta:
  251. verbose_name = _('form submission')
  252. verbose_name_plural = _('form submissions')
  253. unique_together = (('page', 'session_key'),
  254. ('page', 'user'))
  255. abstract = True
  256. @property
  257. def is_complete(self):
  258. return self.status != self.INCOMPLETE
  259. @property
  260. def form_page(self):
  261. return self.page.specific
  262. def get_session(self):
  263. return import_module(settings.SESSION_ENGINE).SessionStore(
  264. session_key=self.session_key)
  265. def reset_step(self):
  266. session = self.get_session()
  267. try:
  268. del session[self.form_page.current_step_session_key]
  269. except KeyError:
  270. pass
  271. else:
  272. session.save()
  273. def get_storage(self):
  274. return self.form_page.get_storage()
  275. def get_thumbnail_path(self, path, width=64, height=64):
  276. if not path:
  277. return ''
  278. variant = '%s×%s' % (width, height)
  279. thumbnails_by_path = json.loads(self.thumbnails_by_path)
  280. thumbnails_paths = thumbnails_by_path.get(path)
  281. if thumbnails_paths is None:
  282. thumbnails_by_path[path] = {}
  283. else:
  284. thumbnail_path = thumbnails_paths.get(variant)
  285. if thumbnail_path is not None:
  286. return thumbnail_path
  287. path = Path(path)
  288. thumbnail_path = str(path.with_suffix('.%s%s'
  289. % (variant, path.suffix)))
  290. storage = self.get_storage()
  291. thumbnail_path = storage.get_available_name(thumbnail_path)
  292. thumbnail = Image.open(storage.path(path))
  293. thumbnail.thumbnail((width, height))
  294. thumbnail.save(storage.path(thumbnail_path))
  295. thumbnails_by_path[str(path)][variant] = thumbnail_path
  296. self.thumbnails_by_path = json.dumps(thumbnails_by_path,
  297. cls=StreamFormJSONEncoder)
  298. self.save()
  299. return thumbnail_path
  300. def get_fields(self, by_step=False):
  301. return self.form_page.get_form_fields(by_step=by_step)
  302. def get_existing_thumbnails(self, path):
  303. thumbnails_paths = json.loads(self.thumbnails_by_path).get(path, {})
  304. for thumbnail_path in thumbnails_paths.values():
  305. yield thumbnail_path
  306. def get_files_by_field(self):
  307. data = self.get_data(raw=True)
  308. files = {}
  309. for name, field in self.get_fields().items():
  310. if isinstance(field, FileField):
  311. path = data.get(name)
  312. if path:
  313. files[name] = [path] + list(
  314. self.get_existing_thumbnails(path))
  315. return files
  316. def get_all_files(self):
  317. for paths in self.get_files_by_field().values():
  318. for path in paths:
  319. yield path
  320. def delete_file(self, field_name):
  321. thumbnails_by_path = json.loads(self.thumbnails_by_path)
  322. for path in self.get_files_by_field().get(field_name, ()):
  323. self.get_storage().delete(path)
  324. if path in thumbnails_by_path:
  325. del thumbnails_by_path[path]
  326. self.thumbnails_by_path = json.dumps(thumbnails_by_path,
  327. cls=StreamFormJSONEncoder)
  328. self.save()
  329. def render_email(self, value):
  330. return (mark_safe('<a href="mailto:%s" target="_blank">%s</a>')
  331. % (value, value))
  332. def render_link(self, value):
  333. return (mark_safe('<a href="%s" target="_blank">%s</a>')
  334. % (value, value))
  335. def render_image(self, value):
  336. storage = self.get_storage()
  337. return (mark_safe('<a href="%s" target="_blank"><img src="%s" /></a>')
  338. % (storage.url(value),
  339. storage.url(self.get_thumbnail_path(value))))
  340. def render_file(self, value):
  341. return mark_safe('<a href="%s" target="_blank">%s</a>') % (
  342. self.get_storage().url(value),
  343. Path(value).name
  344. )
  345. def format_value(self, field, value):
  346. if value is None or value == '':
  347. return '-'
  348. new_value = self.form_page.format_value(field, value)
  349. if new_value != value:
  350. return new_value
  351. if value is True:
  352. return 'Yes'
  353. if value is False:
  354. return 'No'
  355. if isinstance(value, (list, tuple)):
  356. return ', '.join([self.format_value(field, item)
  357. for item in value])
  358. if isinstance(value, datetime.date):
  359. return value
  360. if isinstance(field, EmailField):
  361. return self.render_email(value)
  362. if isinstance(field, URLField):
  363. return self.render_link(value)
  364. if isinstance(field, ImageField):
  365. return self.render_image(value)
  366. if isinstance(field, FileField):
  367. return self.render_file(value)
  368. if isinstance(value, SafeData) or hasattr(value, '__html__'):
  369. return value
  370. return str(value)
  371. def format_db_field(self, field_name, raw=False):
  372. method = getattr(self, 'get_%s_display' % field_name, None)
  373. if method is not None:
  374. return method()
  375. value = getattr(self, field_name)
  376. if raw:
  377. return value
  378. return self.format_value(self._meta.get_field(field_name).formfield(),
  379. value)
  380. def get_steps_data(self, raw=False):
  381. steps_data = json.loads(self.form_data)
  382. if raw:
  383. return steps_data
  384. fields_and_data_iterator = zip_longest(self.get_fields(by_step=True),
  385. steps_data, fillvalue={})
  386. return [
  387. OrderedDict([(name, self.format_value(field, step_data.get(name)))
  388. for name, field in step_fields.items()])
  389. for step_fields, step_data in fields_and_data_iterator]
  390. def get_extra_data(self, raw=False):
  391. return self.form_page.get_extra_data(self, raw=raw)
  392. def get_data(self, raw=False, add_metadata=True):
  393. steps_data = self.get_steps_data(raw=raw)
  394. form_data = {}
  395. form_data.update(self.get_extra_data(raw=raw))
  396. for step_data in steps_data:
  397. form_data.update(step_data)
  398. if add_metadata:
  399. form_data.update(
  400. status=self.format_db_field('status', raw=raw),
  401. user=self.format_db_field('user', raw=raw),
  402. submit_time=self.format_db_field('submit_time', raw=raw),
  403. last_modification=self.format_db_field('last_modification',
  404. raw=raw),
  405. )
  406. return form_data
  407. def steps_with_data_iterator(self, raw=False):
  408. for step, step_data_fields, step_data in zip(
  409. self.form_page.get_steps(),
  410. self.form_page.get_data_fields(by_step=True),
  411. self.get_steps_data(raw=raw)):
  412. yield step, [(field_name, field_label, step_data[field_name])
  413. for field_name, field_label in step_data_fields]
  414. @receiver(post_delete, sender=SessionFormSubmission)
  415. def delete_files(sender, **kwargs):
  416. instance = kwargs['instance']
  417. instance.reset_step()
  418. storage = instance.get_storage()
  419. for path in instance.get_all_files():
  420. storage.delete(path)
  421. # Automatically deletes ancestor folders if empty.
  422. directory = Path(path)
  423. while directory.parent != Path(directory.root):
  424. directory = directory.parent
  425. try:
  426. subdirectories, files = storage.listdir(directory)
  427. except FileNotFoundError:
  428. continue
  429. if not subdirectories and not files:
  430. Path(storage.path(directory)).rmdir()
  431. class SubmissionRevisionQuerySet(QuerySet):
  432. def for_submission(self, submission):
  433. return self.filter(**self.model.get_filters_for(submission))
  434. def created(self):
  435. return self.filter(type=self.model.CREATED)
  436. def changed(self):
  437. return self.filter(type=self.model.CHANGED)
  438. def deleted(self):
  439. return self.filter(type=self.model.DELETED)
  440. class SubmissionRevision(Model):
  441. CREATED = 'created'
  442. CHANGED = 'changed'
  443. DELETED = 'deleted'
  444. TYPES = (
  445. (CREATED, _('Created')),
  446. (CHANGED, _('Changed')),
  447. (DELETED, _('Deleted')),
  448. )
  449. type = CharField(max_length=7, choices=TYPES)
  450. created_at = DateTimeField(auto_now_add=True)
  451. submission_ct = ForeignKey('contenttypes.ContentType', on_delete=CASCADE)
  452. submission_id = TextField()
  453. submission = GenericForeignKey('submission_ct', 'submission_id')
  454. data = TextField()
  455. summary = TextField()
  456. objects = SubmissionRevisionQuerySet.as_manager()
  457. class Meta:
  458. ordering = ('-created_at',)
  459. abstract = True
  460. @staticmethod
  461. def get_filters_for(submission):
  462. return {
  463. 'submission_ct':
  464. ContentType.objects.get_for_model(submission._meta.model),
  465. 'submission_id': str(submission.pk),
  466. }
  467. @classmethod
  468. def diff_summary(cls, page, data1, data2):
  469. diff = []
  470. data_fields = page.get_data_fields()
  471. hidden_types = (tuple, list, dict)
  472. for k, label in data_fields:
  473. value1 = data1.get(k)
  474. value2 = data2.get(k)
  475. if value2 == value1 or not value1 and not value2:
  476. continue
  477. is_hidden = (isinstance(value1, hidden_types)
  478. or isinstance(value2, hidden_types))
  479. # Escapes newlines as they are used as separator inside summaries.
  480. if isinstance(value1, str):
  481. value1 = value1.replace('\n', r'\n')
  482. if isinstance(value2, str):
  483. value2 = value2.replace('\n', r'\n')
  484. if value2 and not value1:
  485. diff.append(
  486. ((_('“%s” set.') % label) if is_hidden
  487. else (_('“%s” set to “%s”.')) % (label, value2)))
  488. elif value1 and not value2:
  489. diff.append(_('“%s” unset.') % label)
  490. else:
  491. diff.append(((_('“%s” changed.') % label) if is_hidden
  492. else (_('“%s” changed from “%s” to “%s”.')
  493. % (label, value1, value2))))
  494. return '\n'.join(diff)
  495. @classmethod
  496. def create_from_submission(cls, submission, revision_type):
  497. page = submission.form_page
  498. try:
  499. previous = cls.objects.for_submission(
  500. submission).latest('created_at')
  501. except cls.DoesNotExist:
  502. previous_data = {}
  503. else:
  504. previous_data = previous.get_data()
  505. filters = cls.get_filters_for(submission)
  506. data = submission.get_data(raw=True, add_metadata=False)
  507. data['status'] = submission.status
  508. if revision_type == cls.CREATED:
  509. summary = _('Submission created.')
  510. elif revision_type == cls.DELETED:
  511. summary = _('Submission deleted.')
  512. else:
  513. summary = cls.diff_summary(page, previous_data, data)
  514. if not summary: # Nothing changed.
  515. return
  516. filters.update(
  517. type=revision_type, data=json.dumps(data, cls=StreamFormJSONEncoder), summary=summary
  518. )
  519. return cls.objects.create(**filters)
  520. def get_data(self):
  521. return json.loads(self.data)
  522. # ORIGINAL NORIPYT CODE.
  523. # We don't want these receivers triggering.
  524. # @receiver(post_save)
  525. # def create_submission_changed_revision(sender, **kwargs):
  526. # if not issubclass(sender, SessionFormSubmission):
  527. # return
  528. # submission = kwargs['instance']
  529. # created = kwargs['created']
  530. # SubmissionRevision.create_from_submission(
  531. # submission, (SubmissionRevision.CREATED if created
  532. # else SubmissionRevision.CHANGED))
  533. # @receiver(post_delete)
  534. # def create_submission_deleted_revision(sender, **kwargs):
  535. # if not issubclass(sender, SessionFormSubmission):
  536. # return
  537. # submission = kwargs['instance']
  538. # SubmissionRevision.create_from_submission(submission,
  539. # SubmissionRevision.DELETED)
  540. class StreamFormMixin:
  541. preview_modes = Page.DEFAULT_PREVIEW_MODES
  542. @property
  543. def current_step_session_key(self):
  544. return '%s:step' % self.pk
  545. def get_steps(self, request=None):
  546. if not hasattr(self, 'steps'):
  547. steps = Steps(self, request=request)
  548. if request is None:
  549. return steps
  550. self.steps = steps
  551. return self.steps
  552. def get_form_fields(self, by_step=False):
  553. if by_step:
  554. return [step.get_form_fields() for step in self.get_steps()]
  555. form_fields = OrderedDict()
  556. for step_fields in self.get_form_fields(by_step=True):
  557. form_fields.update(step_fields)
  558. return form_fields
  559. def get_context(self, request, *args, **kwargs):
  560. context = super().get_context(request, *args, **kwargs)
  561. self.steps = self.get_steps(request)
  562. step_value = request.GET.get('step')
  563. if step_value is not None and step_value.isdigit():
  564. self.steps.current = int(step_value) - 1
  565. form = self.steps.get_current_form()
  566. context.update(
  567. steps=self.steps,
  568. step=self.steps.current,
  569. form=form,
  570. markups_and_bound_fields=list(
  571. self.steps.current.get_markups_and_bound_fields(form)),
  572. )
  573. return context
  574. def get_storage(self):
  575. return default_storage
  576. @staticmethod
  577. def get_form_class_bases():
  578. return Form,
  579. @staticmethod
  580. def get_submission_class():
  581. return SessionFormSubmission
  582. def get_submission(self, request):
  583. Submission = self.get_submission_class()
  584. if request.user.is_authenticated:
  585. user_submission = Submission.objects.filter(
  586. user=request.user, page=self).order_by('-pk').first()
  587. if user_submission is None:
  588. return Submission(user=request.user, page=self, form_data='[]')
  589. return user_submission
  590. user_submission = Submission.objects.filter(
  591. session_key=request.session.session_key, page=self
  592. ).order_by('-pk').first()
  593. if user_submission is None:
  594. return Submission(session_key=request.session.session_key,
  595. page=self, form_data='[]')
  596. return user_submission
  597. def get_success_url(self):
  598. form_complete_models = [model for model in apps.get_models()
  599. if issubclass(model, FormCompleteMixin)]
  600. cts = (ContentType.objects
  601. .get_for_models(*form_complete_models).values())
  602. first_child = self.get_children().filter(content_type__in=cts).first()
  603. if first_child is None:
  604. return self.url
  605. return first_child.url
  606. def serve_success(self, request, *args, **kwargs):
  607. url = self.get_success_url()
  608. if url == self.url:
  609. messages.success(request,
  610. _('Successfully submitted the form.'))
  611. return HttpResponseRedirect(url)
  612. def serve(self, request, *args, **kwargs):
  613. context = self.get_context(request)
  614. form = context['form']
  615. if request.method == 'POST' and form.is_valid():
  616. is_complete = self.steps.update_data()
  617. if is_complete:
  618. return self.serve_success(request, *args, **kwargs)
  619. return HttpResponseRedirect(self.url)
  620. return Page.serve(self, request, *args, **kwargs)
  621. def get_data_fields(self, by_step=False, add_metadata=True):
  622. if by_step:
  623. return [[(field_name, field.label)
  624. for field_name, field in step_fields.items()]
  625. for step_fields in self.get_form_fields(by_step=True)]
  626. data_fields = []
  627. data_fields.extend(self.get_extra_data_fields())
  628. if add_metadata:
  629. data_fields.extend((
  630. ('status', _('Status')),
  631. ('user', _('User')),
  632. ('submit_time', _('First modification')),
  633. ('last_modification', _('Last modification'))))
  634. data_fields.extend([
  635. (field_name, field_label)
  636. for step_data_fields in self.get_data_fields(by_step=True)
  637. for field_name, field_label in step_data_fields])
  638. return data_fields
  639. def get_extra_data_fields(self):
  640. return ()
  641. def get_extra_data(self, submission, raw=False):
  642. return {}
  643. def format_value(self, field, value):
  644. return value
  645. class ClosingFormMixin(Model):
  646. closing_at = DateTimeField()
  647. closed_template = None
  648. class Meta:
  649. abstract = True
  650. @property
  651. def is_closed(self):
  652. return now() > self.closing_at
  653. def get_closed_template(self, request, *args, **kwargs):
  654. if self.closed_template is None:
  655. template = self.get_template(request, *args, **kwargs)
  656. base, ext = os.path.splitext(template)
  657. return '%s_closed%s' % (base, ext)
  658. return self.closed_template
  659. def serve_closed(self, request, *args, **kwargs):
  660. return TemplateResponse(
  661. request,
  662. self.get_closed_template(request, *args, **kwargs),
  663. self.get_context(request, *args, **kwargs),
  664. )
  665. def serve(self, request, *args, **kwargs):
  666. if self.is_closed:
  667. return self.serve_closed(request, *args, **kwargs)
  668. return super().serve(request, *args, **kwargs)
  669. class FormCompleteMixin:
  670. def get_form_page(self):
  671. return self.get_parent().specific
  672. def serve(self, request, *args, **kwargs):
  673. form_page = self.get_form_page()
  674. if isinstance(form_page, LoginRequiredMixin) \
  675. and not request.user.is_authenticated():
  676. return HttpResponseRedirect(form_page.url)
  677. self.submission = form_page.get_submission(request)
  678. if self.submission is not None and self.submission.is_complete \
  679. or getattr(request, 'is_preview', False):
  680. return super().serve(request, *args, **kwargs)
  681. return HttpResponseRedirect(form_page.url)
  682. def get_context(self, *args, **kwargs):
  683. context = super().get_context(*args, **kwargs)
  684. if hasattr(self, 'submission'):
  685. context['submission'] = self.submission
  686. return context
  687. class LoginRequiredMixin:
  688. login_required_template = None
  689. def get_login_required_template(self, request, *args, **kwargs):
  690. if self.login_required_template is None:
  691. template = self.get_template(request, *args, **kwargs)
  692. base, ext = os.path.splitext(template)
  693. return '%s_login_required%s' % (base, ext)
  694. return self.login_required_template
  695. def serve_login_required(self, request, *args, **kwargs):
  696. return TemplateResponse(
  697. request,
  698. self.get_login_required_template(request, *args, **kwargs),
  699. self.get_context(request, *args, **kwargs),
  700. )
  701. def serve(self, request, *args, **kwargs):
  702. if not request.user.is_authenticated():
  703. return self.serve_login_required(request, *args, **kwargs)
  704. return super().serve(request, *args, **kwargs)
  705. class AbstractStreamForm(StreamFormMixin, AbstractForm):
  706. class Meta:
  707. abstract = True
  708. class AbstractEmailStreamForm(StreamFormMixin, AbstractEmailForm):
  709. class Meta:
  710. abstract = True