<s> from __future__ import absolute_import , unicode_literals <EOL> import json <EOL> import re <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . core . serializers . json import DjangoJSONEncoder <EOL> from django . db import models <EOL> from django . shortcuts import render <EOL> from django . utils . encoding import python_2_unicode_compatible <EOL> from django . utils . six import text_type <EOL> from django . utils . text import slugify <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from unidecode import unidecode <EOL> from wagtail . wagtailadmin . edit_handlers import FieldPanel <EOL> from wagtail . wagtailadmin . utils import send_mail <EOL> from wagtail . wagtailcore . models import Orderable , Page , UserPagePermissionsProxy , get_page_models <EOL> from . forms import FormBuilder <EOL> FORM_FIELD_CHOICES = ( <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT:email>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT:url>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT:date>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ) <EOL> HTML_EXTENSION_RE = re . compile ( r"<STR_LIT>" ) <EOL> @ python_2_unicode_compatible <EOL> class FormSubmission ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> form_data = models . TextField ( ) <EOL> page = models . ForeignKey ( Page , on_delete = models . CASCADE ) <EOL> submit_time = models . DateTimeField ( verbose_name = _ ( '<STR_LIT>' ) , auto_now_add = True ) <EOL> def get_data ( self ) : <EOL> return json . loads ( self . form_data ) <EOL> def __str__ ( self ) : <EOL> return self . form_data <EOL> class Meta : <EOL> verbose_name = _ ( '<STR_LIT>' ) <EOL> class AbstractFormField ( Orderable ) : <EOL> """<STR_LIT>""" <EOL> label = models . CharField ( <EOL> verbose_name = _ ( '<STR_LIT:label>' ) , <EOL> max_length = <NUM_LIT:255> , <EOL> help_text = _ ( '<STR_LIT>' ) <EOL> ) <EOL> field_type = models . CharField ( verbose_name = _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:16> , choices = FORM_FIELD_CHOICES ) <EOL> required = models . BooleanField ( verbose_name = _ ( '<STR_LIT>' ) , default = True ) <EOL> choices = models . CharField ( <EOL> verbose_name = _ ( '<STR_LIT>' ) , <EOL> max_length = <NUM_LIT> , <EOL> blank = True , <EOL> help_text = _ ( '<STR_LIT>' ) <EOL> ) <EOL> default_value = models . CharField ( <EOL> verbose_name = _ ( '<STR_LIT>' ) , <EOL> max_length = <NUM_LIT:255> , <EOL> blank = True , <EOL> help_text = _ ( '<STR_LIT>' ) <EOL> ) <EOL> help_text = models . CharField ( verbose_name = _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:255> , blank = True ) <EOL> @ property <EOL> def clean_name ( self ) : <EOL> return str ( slugify ( text_type ( unidecode ( self . label ) ) ) ) <EOL> panels = [ <EOL> FieldPanel ( '<STR_LIT:label>' ) , <EOL> FieldPanel ( '<STR_LIT>' ) , <EOL> FieldPanel ( '<STR_LIT>' ) , <EOL> FieldPanel ( '<STR_LIT>' , classname = "<STR_LIT>" ) , <EOL> FieldPanel ( '<STR_LIT>' , classname = "<STR_LIT>" ) , <EOL> FieldPanel ( '<STR_LIT>' , classname = "<STR_LIT>" ) , <EOL> ] <EOL> class Meta : <EOL> abstract = True <EOL> ordering = [ '<STR_LIT>' ] <EOL> _FORM_CONTENT_TYPES = None <EOL> def get_form_types ( ) : <EOL> global _FORM_CONTENT_TYPES <EOL> if _FORM_CONTENT_TYPES is None : <EOL> form_models = [ <EOL> model for model in get_page_models ( ) <EOL> if issubclass ( model , AbstractForm ) <EOL> ] <EOL> _FORM_CONTENT_TYPES = list ( <EOL> ContentType . objects . get_for_models ( * form_models ) . values ( ) <EOL> ) <EOL> return _FORM_CONTENT_TYPES <EOL> def get_forms_for_user ( user ) : <EOL> """<STR_LIT>""" <EOL> editable_pages = UserPagePermissionsProxy ( user ) . editable_pages ( ) <EOL> return editable_pages . filter ( content_type__in = get_form_types ( ) ) <EOL> class AbstractForm ( Page ) : <EOL> """<STR_LIT>""" <EOL> form_builder = FormBuilder <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( AbstractForm , self ) . __init__ ( * args , ** kwargs ) <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> template_wo_ext = re . match ( HTML_EXTENSION_RE , self . template ) . group ( <NUM_LIT:1> ) <EOL> self . landing_page_template = template_wo_ext + '<STR_LIT>' <EOL> class Meta : <EOL> abstract = True <EOL> def get_form_class ( self ) : <EOL> fb = self . form_builder ( self . form_fields . all ( ) ) <EOL> return fb . get_form_class ( ) <EOL> def get_form_parameters ( self ) : <EOL> return { } <EOL> def get_form ( self , * args , ** kwargs ) : <EOL> form_class = self . get_form_class ( ) <EOL> form_params = self . get_form_parameters ( ) <EOL> form_params . update ( kwargs ) <EOL> return form_class ( * args , ** form_params ) <EOL> def process_form_submission ( self , form ) : <EOL> FormSubmission . objects . create ( <EOL> form_data = json . dumps ( form . cleaned_data , cls = DjangoJSONEncoder ) , <EOL> page = self , <EOL> ) <EOL> def serve ( self , request ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = self . get_form ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> self . process_form_submission ( form ) <EOL> return render ( <EOL> request , <EOL> self . landing_page_template , <EOL> self . get_context ( request ) <EOL> ) <EOL> else : <EOL> form = self . get_form ( ) <EOL> context = self . get_context ( request ) <EOL> context [ '<STR_LIT>' ] = form <EOL> return render ( <EOL> request , <EOL> self . template , <EOL> context <EOL> ) <EOL> preview_modes = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] <EOL> def serve_preview ( self , request , mode ) : <EOL> if mode == '<STR_LIT>' : <EOL> return render ( <EOL> request , <EOL> self . landing_page_template , <EOL> self . get_context ( request ) <EOL> ) <EOL> else : <EOL> return super ( AbstractForm , self ) . serve_preview ( request , mode ) <EOL> class AbstractEmailForm ( AbstractForm ) : <EOL> """<STR_LIT>""" <EOL> to_address = models . CharField ( <EOL> verbose_name = _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:255> , blank = True , <EOL> help_text = _ ( "<STR_LIT>" ) <EOL> ) <EOL> from_address = models . CharField ( verbose_name = _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:255> , blank = True ) <EOL> subject = models . CharField ( verbose_name = _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:255> , blank = True ) <EOL> def process_form_submission ( self , form ) : <EOL> super ( AbstractEmailForm , self ) . process_form_submission ( form ) <EOL> if self . to_address : <EOL> content = '<STR_LIT:\n>' . join ( [ x [ <NUM_LIT:1> ] . label + '<STR_LIT>' + text_type ( form . data . get ( x [ <NUM_LIT:0> ] ) ) for x in form . fields . items ( ) ] ) <EOL> send_mail ( self . subject , content , [ self . to_address ] , self . from_address , ) <EOL> class Meta : <EOL> abstract = True </s>
