<s> from __future__ import unicode_literals <EOL> from future . builtins import int , range , str <EOL> from datetime import date , datetime <EOL> from os . path import join , split <EOL> from uuid import uuid4 <EOL> from django import forms <EOL> from django . forms . extras import SelectDateWidget <EOL> from django . core . files . storage import FileSystemStorage <EOL> from django . core . urlresolvers import reverse <EOL> from django . template import Template <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . translation import ugettext as _ <EOL> from django . utils . timezone import now <EOL> from mezzanine . conf import settings <EOL> from mezzanine . forms import fields <EOL> from mezzanine . forms . models import FormEntry , FieldEntry <EOL> from mezzanine . utils . email import split_addresses as split_choices <EOL> fs = FileSystemStorage ( location = settings . FORMS_UPLOAD_ROOT ) <EOL> FILTER_CHOICE_CONTAINS = "<STR_LIT:1>" <EOL> FILTER_CHOICE_DOESNT_CONTAIN = "<STR_LIT:2>" <EOL> FILTER_CHOICE_EQUALS = "<STR_LIT:3>" <EOL> FILTER_CHOICE_DOESNT_EQUAL = "<STR_LIT:4>" <EOL> FILTER_CHOICE_BETWEEN = "<STR_LIT:5>" <EOL> FILTER_CHOICE_CONTAINS_ANY = "<STR_LIT>" <EOL> FILTER_CHOICE_CONTAINS_ALL = "<STR_LIT>" <EOL> FILTER_CHOICE_DOESNT_CONTAIN_ANY = "<STR_LIT>" <EOL> FILTER_CHOICE_DOESNT_CONTAIN_ALL = "<STR_LIT>" <EOL> TEXT_FILTER_CHOICES = ( <EOL> ( "<STR_LIT>" , _ ( "<STR_LIT>" ) ) , <EOL> ( FILTER_CHOICE_CONTAINS , _ ( "<STR_LIT>" ) ) , <EOL> ( FILTER_CHOICE_DOESNT_CONTAIN , _ ( "<STR_LIT>" ) ) , <EOL> ( FILTER_CHOICE_EQUALS , _ ( "<STR_LIT>" ) ) , <EOL> ( FILTER_CHOICE_DOESNT_EQUAL , _ ( "<STR_LIT>" ) ) , <EOL> ) <EOL> CHOICE_FILTER_CHOICES = ( <EOL> ( "<STR_LIT>" , _ ( "<STR_LIT>" ) ) , <EOL> ( FILTER_CHOICE_CONTAINS_ANY , _ ( "<STR_LIT>" ) ) , <EOL> ( FILTER_CHOICE_DOESNT_CONTAIN_ANY , _ ( "<STR_LIT>" ) ) , <EOL> ) <EOL> MULTIPLE_FILTER_CHOICES = ( <EOL> ( "<STR_LIT>" , _ ( "<STR_LIT>" ) ) , <EOL> ( FILTER_CHOICE_CONTAINS_ANY , _ ( "<STR_LIT>" ) ) , <EOL> ( FILTER_CHOICE_CONTAINS_ALL , _ ( "<STR_LIT>" ) ) , <EOL> ( FILTER_CHOICE_DOESNT_CONTAIN_ANY , _ ( "<STR_LIT>" ) ) , <EOL> ( FILTER_CHOICE_DOESNT_CONTAIN_ALL , _ ( "<STR_LIT>" ) ) , <EOL> ) <EOL> DATE_FILTER_CHOICES = ( <EOL> ( "<STR_LIT>" , _ ( "<STR_LIT>" ) ) , <EOL> ( FILTER_CHOICE_BETWEEN , _ ( "<STR_LIT>" ) ) , <EOL> ) <EOL> FILTER_FUNCS = { <EOL> FILTER_CHOICE_CONTAINS : <EOL> lambda val , field : val . lower ( ) in field . lower ( ) , <EOL> FILTER_CHOICE_DOESNT_CONTAIN : <EOL> lambda val , field : val . lower ( ) not in field . lower ( ) , <EOL> FILTER_CHOICE_EQUALS : <EOL> lambda val , field : val . lower ( ) == field . lower ( ) , <EOL> FILTER_CHOICE_DOESNT_EQUAL : <EOL> lambda val , field : val . lower ( ) != field . lower ( ) , <EOL> FILTER_CHOICE_BETWEEN : <EOL> lambda val_from , val_to , field : ( <EOL> ( not val_from or val_from <= field ) and <EOL> ( not val_to or val_to >= field ) <EOL> ) , <EOL> FILTER_CHOICE_CONTAINS_ANY : <EOL> lambda val , field : set ( val ) & set ( split_choices ( field ) ) , <EOL> FILTER_CHOICE_CONTAINS_ALL : <EOL> lambda val , field : set ( val ) == set ( split_choices ( field ) ) , <EOL> FILTER_CHOICE_DOESNT_CONTAIN_ANY : <EOL> lambda val , field : not set ( val ) & set ( split_choices ( field ) ) , <EOL> FILTER_CHOICE_DOESNT_CONTAIN_ALL : <EOL> lambda val , field : set ( val ) != set ( split_choices ( field ) ) , <EOL> } <EOL> text_filter_field = forms . ChoiceField ( label = "<STR_LIT:U+0020>" , required = False , <EOL> choices = TEXT_FILTER_CHOICES ) <EOL> choice_filter_field = forms . ChoiceField ( label = "<STR_LIT:U+0020>" , required = False , <EOL> choices = CHOICE_FILTER_CHOICES ) <EOL> multiple_filter_field = forms . ChoiceField ( label = "<STR_LIT:U+0020>" , required = False , <EOL> choices = MULTIPLE_FILTER_CHOICES ) <EOL> date_filter_field = forms . ChoiceField ( label = "<STR_LIT:U+0020>" , required = False , <EOL> choices = DATE_FILTER_CHOICES ) <EOL> class FormForForm ( forms . ModelForm ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> model = FormEntry <EOL> exclude = ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def __init__ ( self , form , context , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . form = form <EOL> self . form_fields = form . fields . visible ( ) <EOL> initial = kwargs . pop ( "<STR_LIT>" , { } ) <EOL> field_entries = { } <EOL> if kwargs . get ( "<STR_LIT>" ) : <EOL> for field_entry in kwargs [ "<STR_LIT>" ] . fields . all ( ) : <EOL> field_entries [ field_entry . field_id ] = field_entry . value <EOL> super ( FormForForm , self ) . __init__ ( * args , ** kwargs ) <EOL> for field in self . form_fields : <EOL> field_key = "<STR_LIT>" % field . id <EOL> field_class = fields . CLASSES [ field . field_type ] <EOL> field_widget = fields . WIDGETS . get ( field . field_type ) <EOL> field_args = { "<STR_LIT:label>" : field . label , "<STR_LIT>" : field . required , <EOL> "<STR_LIT>" : field . help_text } <EOL> if field . required and not field . help_text : <EOL> field_args [ "<STR_LIT>" ] = _ ( "<STR_LIT>" ) <EOL> arg_names = field_class . __init__ . __code__ . co_varnames <EOL> if "<STR_LIT:max_length>" in arg_names : <EOL> field_args [ "<STR_LIT:max_length>" ] = settings . FORMS_FIELD_MAX_LENGTH <EOL> if "<STR_LIT>" in arg_names : <EOL> choices = list ( field . get_choices ( ) ) <EOL> if ( field . field_type == fields . SELECT and <EOL> field . default not in [ c [ <NUM_LIT:0> ] for c in choices ] ) : <EOL> choices . insert ( <NUM_LIT:0> , ( "<STR_LIT>" , field . placeholder_text ) ) <EOL> field_args [ "<STR_LIT>" ] = choices <EOL> if field_widget is not None : <EOL> field_args [ "<STR_LIT>" ] = field_widget <EOL> initial_val = None <EOL> try : <EOL> initial_val = field_entries [ field . id ] <EOL> except KeyError : <EOL> try : <EOL> initial_val = initial [ field_key ] <EOL> except KeyError : <EOL> initial_val = Template ( field . default ) . render ( context ) <EOL> if initial_val : <EOL> if field . is_a ( * fields . MULTIPLE ) : <EOL> initial_val = split_choices ( initial_val ) <EOL> elif field . field_type == fields . CHECKBOX : <EOL> initial_val = initial_val != "<STR_LIT:False>" <EOL> self . initial [ field_key ] = initial_val <EOL> self . fields [ field_key ] = field_class ( ** field_args ) <EOL> if field . field_type == fields . DOB : <EOL> _now = datetime . now ( ) <EOL> years = list ( range ( _now . year , _now . year - <NUM_LIT> , - <NUM_LIT:1> ) ) <EOL> self . fields [ field_key ] . widget . years = years <EOL> setattr ( self . fields [ field_key ] , "<STR_LIT:type>" , <EOL> field_class . __name__ . lower ( ) ) <EOL> if ( field . required and settings . FORMS_USE_HTML5 and <EOL> field . field_type != fields . CHECKBOX_MULTIPLE ) : <EOL> self . fields [ field_key ] . widget . attrs [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> if field . placeholder_text and not field . default : <EOL> text = field . placeholder_text <EOL> self . fields [ field_key ] . widget . attrs [ "<STR_LIT>" ] = text <EOL> def save ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> entry = super ( FormForForm , self ) . save ( commit = False ) <EOL> entry . form = self . form <EOL> entry . entry_time = now ( ) <EOL> entry . save ( ) <EOL> entry_fields = entry . fields . values_list ( "<STR_LIT>" , flat = True ) <EOL> new_entry_fields = [ ] <EOL> for field in self . form_fields : <EOL> field_key = "<STR_LIT>" % field . id <EOL> value = self . cleaned_data [ field_key ] <EOL> if value and self . fields [ field_key ] . widget . needs_multipart_form : <EOL> value = fs . save ( join ( "<STR_LIT>" , str ( uuid4 ( ) ) , value . name ) , value ) <EOL> if isinstance ( value , list ) : <EOL> value = "<STR_LIT:U+002CU+0020>" . join ( [ v . strip ( ) for v in value ] ) <EOL> if field . id in entry_fields : <EOL> field_entry = entry . fields . get ( field_id = field . id ) <EOL> field_entry . value = value <EOL> field_entry . save ( ) <EOL> else : <EOL> new = { "<STR_LIT>" : entry , "<STR_LIT>" : field . id , "<STR_LIT:value>" : value } <EOL> new_entry_fields . append ( FieldEntry ( ** new ) ) <EOL> if new_entry_fields : <EOL> FieldEntry . objects . bulk_create ( new_entry_fields ) <EOL> return entry <EOL> def email_to ( self ) : <EOL> """<STR_LIT>""" <EOL> for field in self . form_fields : <EOL> if field . is_a ( fields . EMAIL ) : <EOL> return self . cleaned_data [ "<STR_LIT>" % field . id ] <EOL> return None <EOL> class EntriesForm ( forms . Form ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , form , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . form = form <EOL> self . request = request <EOL> self . form_fields = form . fields . all ( ) <EOL> self . entry_time_name = str ( FormEntry . _meta . get_field ( <EOL> "<STR_LIT>" ) . verbose_name ) <EOL> super ( EntriesForm , self ) . __init__ ( * args , ** kwargs ) <EOL> for field in self . form_fields : <EOL> field_key = "<STR_LIT>" % field . id <EOL> self . fields [ "<STR_LIT>" % field_key ] = forms . BooleanField ( <EOL> label = field . label , initial = True , required = False ) <EOL> if field . is_a ( * fields . CHOICES ) : <EOL> if field . is_a ( fields . CHECKBOX ) : <EOL> choices = ( ( True , _ ( "<STR_LIT>" ) ) , ( False , _ ( "<STR_LIT>" ) ) ) <EOL> else : <EOL> choices = field . get_choices ( ) <EOL> contains_field = forms . MultipleChoiceField ( label = "<STR_LIT:U+0020>" , <EOL> choices = choices , widget = forms . CheckboxSelectMultiple ( ) , <EOL> required = False ) <EOL> self . fields [ "<STR_LIT>" % field_key ] = choice_filter_field <EOL> self . fields [ "<STR_LIT>" % field_key ] = contains_field <EOL> elif field . is_a ( * fields . MULTIPLE ) : <EOL> contains_field = forms . MultipleChoiceField ( label = "<STR_LIT:U+0020>" , <EOL> choices = field . get_choices ( ) , <EOL> widget = forms . CheckboxSelectMultiple ( ) , <EOL> required = False ) <EOL> self . fields [ "<STR_LIT>" % field_key ] = multiple_filter_field <EOL> self . fields [ "<STR_LIT>" % field_key ] = contains_field <EOL> elif field . is_a ( * fields . DATES ) : <EOL> self . fields [ "<STR_LIT>" % field_key ] = date_filter_field <EOL> self . fields [ "<STR_LIT>" % field_key ] = forms . DateField ( <EOL> label = "<STR_LIT:U+0020>" , widget = SelectDateWidget ( ) , required = False ) <EOL> self . fields [ "<STR_LIT>" % field_key ] = forms . DateField ( <EOL> label = _ ( "<STR_LIT>" ) , widget = SelectDateWidget ( ) , required = False ) <EOL> else : <EOL> contains_field = forms . CharField ( label = "<STR_LIT:U+0020>" , required = False ) <EOL> self . fields [ "<STR_LIT>" % field_key ] = text_filter_field <EOL> self . fields [ "<STR_LIT>" % field_key ] = contains_field <EOL> field_key = "<STR_LIT>" <EOL> self . fields [ "<STR_LIT>" % field_key ] = forms . BooleanField ( initial = True , <EOL> label = FormEntry . _meta . get_field ( "<STR_LIT>" ) . verbose_name , <EOL> required = False ) <EOL> self . fields [ "<STR_LIT>" % field_key ] = date_filter_field <EOL> self . fields [ "<STR_LIT>" % field_key ] = forms . DateField ( <EOL> label = "<STR_LIT:U+0020>" , widget = SelectDateWidget ( ) , required = False ) <EOL> self . fields [ "<STR_LIT>" % field_key ] = forms . DateField ( <EOL> label = _ ( "<STR_LIT>" ) , widget = SelectDateWidget ( ) , required = False ) <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> for field_id in [ f . id for f in self . form_fields ] + [ <NUM_LIT:0> ] : <EOL> prefix = "<STR_LIT>" % field_id <EOL> fields = [ f for f in super ( EntriesForm , self ) . __iter__ ( ) <EOL> if f . name . startswith ( prefix ) ] <EOL> yield fields [ <NUM_LIT:0> ] , fields [ <NUM_LIT:1> ] , fields [ <NUM_LIT:2> : ] <EOL> def columns ( self ) : <EOL> """<STR_LIT>""" <EOL> fields = [ f . label for f in self . form_fields <EOL> if self . cleaned_data [ "<STR_LIT>" % f . id ] ] <EOL> if self . cleaned_data [ "<STR_LIT>" ] : <EOL> fields . append ( self . entry_time_name ) <EOL> return fields <EOL> def rows ( self , csv = False ) : <EOL> """<STR_LIT>""" <EOL> field_indexes = { } <EOL> file_field_ids = [ ] <EOL> date_field_ids = [ ] <EOL> for field in self . form_fields : <EOL> if self . cleaned_data [ "<STR_LIT>" % field . id ] : <EOL> field_indexes [ field . id ] = len ( field_indexes ) <EOL> if field . is_a ( fields . FILE ) : <EOL> file_field_ids . append ( field . id ) <EOL> elif field . is_a ( * fields . DATES ) : <EOL> date_field_ids . append ( field . id ) <EOL> num_columns = len ( field_indexes ) <EOL> include_entry_time = self . cleaned_data [ "<STR_LIT>" ] <EOL> if include_entry_time : <EOL> num_columns += <NUM_LIT:1> <EOL> field_entries = FieldEntry . objects . filter ( <EOL> entry__form = self . form ) . order_by ( <EOL> "<STR_LIT>" ) . select_related ( "<STR_LIT>" ) <EOL> if self . cleaned_data [ "<STR_LIT>" ] == FILTER_CHOICE_BETWEEN : <EOL> time_from = self . cleaned_data [ "<STR_LIT>" ] <EOL> time_to = self . cleaned_data [ "<STR_LIT>" ] <EOL> if time_from and time_to : <EOL> field_entries = field_entries . filter ( <EOL> entry__entry_time__range = ( time_from , time_to ) ) <EOL> current_entry = None <EOL> current_row = None <EOL> valid_row = True <EOL> for field_entry in field_entries : <EOL> if field_entry . entry_id != current_entry : <EOL> if valid_row and current_row is not None : <EOL> if not csv : <EOL> current_row . insert ( <NUM_LIT:0> , current_entry ) <EOL> yield current_row <EOL> current_entry = field_entry . entry_id <EOL> current_row = [ "<STR_LIT>" ] * num_columns <EOL> valid_row = True <EOL> if include_entry_time : <EOL> current_row [ - <NUM_LIT:1> ] = field_entry . entry . entry_time <EOL> field_value = field_entry . value or "<STR_LIT>" <EOL> field_id = field_entry . field_id <EOL> filter_type = self . cleaned_data . get ( "<STR_LIT>" % field_id ) <EOL> filter_args = None <EOL> if filter_type : <EOL> if filter_type == FILTER_CHOICE_BETWEEN : <EOL> f , t = "<STR_LIT>" % field_id , "<STR_LIT>" % field_id <EOL> filter_args = [ self . cleaned_data [ f ] , self . cleaned_data [ t ] ] <EOL> else : <EOL> field_name = "<STR_LIT>" % field_id <EOL> filter_args = self . cleaned_data [ field_name ] <EOL> if filter_args : <EOL> filter_args = [ filter_args ] <EOL> if filter_args : <EOL> if field_id in date_field_ids : <EOL> y , m , d = field_value . split ( "<STR_LIT:U+0020>" ) [ <NUM_LIT:0> ] . split ( "<STR_LIT:->" ) <EOL> dte = date ( int ( y ) , int ( m ) , int ( d ) ) <EOL> filter_args . append ( dte ) <EOL> else : <EOL> filter_args . append ( field_value ) <EOL> filter_func = FILTER_FUNCS [ filter_type ] <EOL> if not filter_func ( * filter_args ) : <EOL> valid_row = False <EOL> if field_entry . value and field_id in file_field_ids : <EOL> url = reverse ( "<STR_LIT>" , args = ( field_entry . id , ) ) <EOL> field_value = self . request . build_absolute_uri ( url ) <EOL> if not csv : <EOL> parts = ( field_value , split ( field_entry . value ) [ <NUM_LIT:1> ] ) <EOL> field_value = mark_safe ( "<STR_LIT>" % parts ) <EOL> try : <EOL> current_row [ field_indexes [ field_id ] ] = field_value <EOL> except KeyError : <EOL> pass <EOL> if valid_row and current_row is not None : <EOL> if not csv : <EOL> current_row . insert ( <NUM_LIT:0> , current_entry ) <EOL> yield current_row </s>
