3535from django .core .validators import MaxValueValidator
3636from django .core .validators import MinValueValidator
3737from django .db import models
38- from django .db .models .signals import post_delete
39- from django .db .models .signals import post_save
40- from django .dispatch import receiver
4138from django .http import HttpResponseRedirect
4239from django .http import JsonResponse
4340from django .shortcuts import redirect
7673from wagtail .models import Site
7774from wagtail .search import index
7875from wagtail .utils .decorators import cached_classmethod
76+ from wagtail_flexible_forms .models import AbstractSessionFormSubmission
77+ from wagtail_flexible_forms .models import AbstractSubmissionRevision
78+ from wagtail_flexible_forms .models import StreamFormJSONEncoder
79+ from wagtail_flexible_forms .models import StreamFormMixin
7980from wagtailcache .cache import WagtailCacheMixin
8081from wagtailseo .models import SeoMixin
8182from wagtailseo .models import TwitterCard
9495from coderedcms .models .snippet_models import ClassifierTerm
9596from coderedcms .models .wagtailsettings_models import LayoutSettings
9697from coderedcms .settings import crx_settings
97- from coderedcms .wagtail_flexible_forms .blocks import FormFieldBlock
98- from coderedcms .wagtail_flexible_forms .blocks import FormStepBlock
99- from coderedcms .wagtail_flexible_forms .models import SessionFormSubmission
100- from coderedcms .wagtail_flexible_forms .models import Step
101- from coderedcms .wagtail_flexible_forms .models import Steps
102- from coderedcms .wagtail_flexible_forms .models import StreamFormJSONEncoder
103- from coderedcms .wagtail_flexible_forms .models import StreamFormMixin
104- from coderedcms .wagtail_flexible_forms .models import SubmissionRevision
10598from coderedcms .widgets import ClassifierSelectWidget
10699
107100
@@ -1793,46 +1786,18 @@ def get_submission_class(self):
17931786 return FormSubmission
17941787
17951788
1796- class CoderedSubmissionRevision (SubmissionRevision , models . Model ):
1789+ class CoderedSubmissionRevision (AbstractSubmissionRevision ):
17971790 pass
17981791
17991792
1800- class CoderedSessionFormSubmission (SessionFormSubmission ):
1801- INCOMPLETE = "incomplete"
1802- COMPLETE = "complete"
1803- REVIEWED = "reviewed"
1804- APPROVED = "approved"
1805- REJECTED = "rejected"
1806- STATUSES = (
1807- (INCOMPLETE , _ ("Not submitted" )),
1808- (COMPLETE , _ ("Complete" )),
1809- (REVIEWED , _ ("Under consideration" )),
1810- (APPROVED , _ ("Approved" )),
1811- (REJECTED , _ ("Rejected" )),
1812- )
1813- status = models .CharField (
1814- max_length = 10 , choices = STATUSES , default = INCOMPLETE
1815- )
1816-
1817- def create_normal_submission (self , delete_self = True ):
1818- submission_data = self .get_data ()
1819- if "user" in submission_data :
1820- submission_data ["user" ] = str (submission_data ["user" ])
1821- submission = FormSubmission .objects .create (
1822- form_data = submission_data ,
1823- page = self .page ,
1824- )
1825-
1826- if delete_self :
1827- CoderedSubmissionRevision .objects .filter (
1828- submission_id = self .id
1829- ).delete ()
1830- self .delete ()
1831-
1832- return submission
1793+ class CoderedSessionFormSubmission (AbstractSessionFormSubmission ):
1794+ """
1795+ Customize how certain fields are rendered.
1796+ """
18331797
1834- def render_email (self , value ):
1835- return value
1798+ @staticmethod
1799+ def get_revision_class ():
1800+ return CoderedSubmissionRevision
18361801
18371802 def render_link (self , value ):
18381803 return "{0}{1}" .format (crx_settings .CRX_PROTECTED_MEDIA_URL , value )
@@ -1844,75 +1809,10 @@ def render_file(self, value):
18441809 return "{0}{1}" .format (crx_settings .CRX_PROTECTED_MEDIA_URL , value )
18451810
18461811
1847- @receiver (post_save )
1848- def create_submission_changed_revision (sender , ** kwargs ):
1849- if not issubclass (sender , SessionFormSubmission ):
1850- return
1851- submission = kwargs ["instance" ]
1852- created = kwargs ["created" ]
1853- CoderedSubmissionRevision .create_from_submission (
1854- submission ,
1855- (
1856- CoderedSubmissionRevision .CREATED
1857- if created
1858- else CoderedSubmissionRevision .CHANGED
1859- ),
1860- )
1861-
1862-
1863- @receiver (post_delete )
1864- def create_submission_deleted_revision (sender , ** kwargs ):
1865- if not issubclass (sender , CoderedSessionFormSubmission ):
1866- return
1867- submission = kwargs ["instance" ]
1868- CoderedSubmissionRevision .create_from_submission (
1869- submission , SubmissionRevision .DELETED
1870- )
1871-
1872-
1873- class CoderedStep (Step ):
1874- def get_markups_and_bound_fields (self , form ):
1875- for struct_child in self .form_fields :
1876- block = struct_child .block
1877- if isinstance (block , FormFieldBlock ):
1878- struct_value = struct_child .value
1879- field_name = block .get_slug (struct_value )
1880- yield form [field_name ], "field" , struct_child
1881- else :
1882- yield mark_safe (struct_child ), "markup"
1883-
1884-
1885- class CoderedSteps (Steps ):
1886- def __init__ (self , page , request = None ):
1887- self .page = page
1888- # TODO: Make it possible to change the `form_fields` attribute.
1889- self .form_fields = page .form_fields
1890- self .request = request
1891- has_steps = any (
1892- isinstance (struct_child .block , FormStepBlock )
1893- for struct_child in self .form_fields
1894- )
1895- if has_steps :
1896- steps = [
1897- CoderedStep (self , i , form_field )
1898- for i , form_field in enumerate (self .form_fields )
1899- ]
1900- else :
1901- steps = [CoderedStep (self , 0 , self .form_fields )]
1902- super (Steps , self ).__init__ (steps )
1903-
1904-
19051812class CoderedStreamFormMixin (StreamFormMixin ):
1906- class Meta :
1907- abstract = True
1908-
1909- def get_steps (self , request = None ):
1910- if not hasattr (self , "steps" ):
1911- steps = CoderedSteps (self , request = request )
1912- if request is None :
1913- return steps
1914- self .steps = steps
1915- return self .steps
1813+ """
1814+ Customize the classes used to store submissions.
1815+ """
19161816
19171817 @staticmethod
19181818 def get_submission_class ():
@@ -1922,37 +1822,6 @@ def get_submission_class():
19221822 def get_session_submission_class ():
19231823 return CoderedSessionFormSubmission
19241824
1925- def get_submission (self , request ):
1926- Submission = self .get_session_submission_class ()
1927- if request .user .is_authenticated :
1928- user_submission = (
1929- Submission .objects .filter (user = request .user , page = self )
1930- .order_by ("-pk" )
1931- .first ()
1932- )
1933- if user_submission is None :
1934- return Submission (user = request .user , page = self , form_data = "[]" )
1935- return user_submission
1936-
1937- # Custom code to ensure that anonymous users get a session key.
1938- if not request .session .session_key :
1939- request .session .create ()
1940-
1941- user_submission = (
1942- Submission .objects .filter (
1943- session_key = request .session .session_key , page = self
1944- )
1945- .order_by ("-pk" )
1946- .first ()
1947- )
1948- if user_submission is None :
1949- return Submission (
1950- session_key = request .session .session_key ,
1951- page = self ,
1952- form_data = "[]" ,
1953- )
1954- return user_submission
1955-
19561825
19571826class CoderedStreamFormPage (
19581827 CoderedFormMixin , CoderedStreamFormMixin , CoderedWebPage
@@ -1981,15 +1850,20 @@ def process_form_post(self, form, request):
19811850 if form .is_valid ():
19821851 is_complete = self .steps .update_data ()
19831852 if is_complete :
1984- submission = self .get_submission (request )
1853+ # NOTE: normally this happens in
1854+ # ``wagtail_flexible_forms.StreamFormMixin.serve()``
1855+ # but we are overriding it in ``CoderedFormMixin``.
1856+ # This is a potentially confusing architecture.
1857+ form_submission = self .create_final_submission (
1858+ request , delete_session = True
1859+ )
19851860 self .process_form_submission (
19861861 request = request ,
19871862 form = form ,
1988- form_submission = submission ,
1989- processed_data = submission .get_data (),
1863+ form_submission = form_submission ,
1864+ processed_data = form_submission .get_data (),
19901865 )
1991- normal_submission = submission .create_normal_submission ()
1992- return self .render_landing_page (request , normal_submission )
1866+ return self .render_landing_page (request , form_submission )
19931867 return HttpResponseRedirect (self .url )
19941868 return self .process_form_get (form , request )
19951869
0 commit comments