<s> import codecs <EOL> import csv <EOL> from io import BytesIO <EOL> import json <EOL> from django import forms <EOL> from django . conf import settings <EOL> from django . conf . urls import patterns , url <EOL> from django . contrib import admin <EOL> from django . db . models import Model <EOL> from django . http import HttpResponse <EOL> from django . shortcuts import get_object_or_404 <EOL> from django . utils import six <EOL> from django . utils . encoding import smart_text <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from form_designer import models <EOL> if six . PY3 : <EOL> UnicodeWriter = csv . writer <EOL> else : <EOL> class UnicodeWriter : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , f , dialect = csv . excel , encoding = "<STR_LIT:utf-8>" , ** kwds ) : <EOL> self . queue = BytesIO ( ) <EOL> self . writer = csv . writer ( self . queue , dialect = dialect , ** kwds ) <EOL> self . stream = f <EOL> self . encoder = codecs . getincrementalencoder ( encoding ) ( '<STR_LIT:replace>' ) <EOL> def writerow ( self , row ) : <EOL> row = [ smart_text ( s ) for s in row ] <EOL> self . writer . writerow ( [ s . encode ( "<STR_LIT:utf-8>" ) for s in row ] ) <EOL> data = self . queue . getvalue ( ) <EOL> data = data . decode ( "<STR_LIT:utf-8>" ) <EOL> data = self . encoder . encode ( data ) <EOL> self . stream . write ( data ) <EOL> self . queue . truncate ( <NUM_LIT:0> ) <EOL> def writerows ( self , rows ) : <EOL> for row in rows : <EOL> self . writerow ( row ) <EOL> def jsonize ( v ) : <EOL> if isinstance ( v , dict ) : <EOL> return dict ( ( i1 , jsonize ( i2 ) ) for i1 , i2 in v . items ( ) ) <EOL> if hasattr ( v , '<STR_LIT>' ) and not isinstance ( v , six . string_types ) : <EOL> return [ jsonize ( i ) for i in v ] <EOL> if isinstance ( v , Model ) : <EOL> return v . pk <EOL> return v <EOL> class FormAdminForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> widgets = { <EOL> '<STR_LIT>' : forms . Textarea ( attrs = { '<STR_LIT>' : <NUM_LIT:3> } ) , <EOL> } <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( FormAdminForm , self ) . __init__ ( * args , ** kwargs ) <EOL> choices = ( <EOL> ( key , cfg . get ( '<STR_LIT:title>' , key ) ) <EOL> for key , cfg in self . _meta . model . CONFIG_OPTIONS ) <EOL> self . fields [ '<STR_LIT>' ] = forms . MultipleChoiceField ( <EOL> choices = choices , <EOL> label = _ ( '<STR_LIT>' ) , <EOL> help_text = _ ( '<STR_LIT>' ) , <EOL> widget = forms . CheckboxSelectMultiple , <EOL> ) <EOL> config_fieldsets = [ ] <EOL> try : <EOL> selected = self . data . getlist ( '<STR_LIT>' ) <EOL> except AttributeError : <EOL> if self . instance . pk : <EOL> selected = self . instance . config . keys ( ) <EOL> else : <EOL> selected = None <EOL> selected = selected or ( ) <EOL> self . fields [ '<STR_LIT>' ] . initial = selected <EOL> for s in selected : <EOL> cfg = dict ( self . _meta . model . CONFIG_OPTIONS ) [ s ] <EOL> fieldset = [ <EOL> _ ( '<STR_LIT>' ) % cfg . get ( '<STR_LIT:title>' , s ) , <EOL> { '<STR_LIT>' : [ ] } , <EOL> ] <EOL> for k , f in cfg . get ( '<STR_LIT>' , [ ] ) : <EOL> self . fields [ '<STR_LIT>' % ( s , k ) ] = f <EOL> if k in self . instance . config . get ( s , { } ) : <EOL> f . initial = self . instance . config [ s ] . get ( k ) <EOL> fieldset [ <NUM_LIT:1> ] [ '<STR_LIT>' ] . append ( '<STR_LIT>' % ( s , k ) ) <EOL> config_fieldsets . append ( fieldset ) <EOL> self . request . _formdesigner_config_fieldsets = config_fieldsets <EOL> def clean ( self ) : <EOL> data = self . cleaned_data <EOL> if '<STR_LIT>' in self . changed_data : <EOL> return data <EOL> selected = data . get ( '<STR_LIT>' , [ ] ) <EOL> config_options = { } <EOL> for s in selected : <EOL> cfg = dict ( self . _meta . model . CONFIG_OPTIONS ) [ s ] <EOL> option_item = { } <EOL> for k , f in cfg . get ( '<STR_LIT>' , [ ] ) : <EOL> key = '<STR_LIT>' % ( s , k ) <EOL> if key in data : <EOL> option_item [ k ] = data . get ( key ) <EOL> config_options [ s ] = option_item <EOL> data [ '<STR_LIT>' ] = json . dumps ( jsonize ( config_options ) ) <EOL> return data <EOL> class FormFieldAdmin ( admin . TabularInline ) : <EOL> extra = <NUM_LIT:0> <EOL> model = models . FormField <EOL> prepopulated_fields = { '<STR_LIT:name>' : ( '<STR_LIT:title>' , ) } <EOL> class FormAdmin ( admin . ModelAdmin ) : <EOL> form = FormAdminForm <EOL> inlines = [ FormFieldAdmin ] <EOL> list_display = ( '<STR_LIT:title>' , ) <EOL> save_as = True <EOL> def get_form ( self , request , obj = None , ** kwargs ) : <EOL> form_class = super ( FormAdmin , self ) . get_form ( request , obj , ** kwargs ) <EOL> return type ( form_class . __name__ , ( form_class , ) , { '<STR_LIT>' : request } ) <EOL> def get_fieldsets ( self , request , obj = None ) : <EOL> fieldsets = super ( FormAdmin , self ) . get_fieldsets ( request , obj ) <EOL> if not hasattr ( request , '<STR_LIT>' ) : <EOL> return fieldsets <EOL> fieldsets [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ '<STR_LIT>' ] . remove ( '<STR_LIT>' ) <EOL> fieldsets . append ( ( <EOL> _ ( '<STR_LIT>' ) , <EOL> { <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> } <EOL> ) ) <EOL> fieldsets . extend ( request . _formdesigner_config_fieldsets ) <EOL> return fieldsets <EOL> def export_submissions ( self , request , form_id ) : <EOL> form = get_object_or_404 ( models . Form , pk = form_id ) <EOL> rows = [ ] <EOL> for submission in form . submissions . all ( ) : <EOL> data = submission . sorted_data ( include = ( '<STR_LIT:date>' , '<STR_LIT:time>' , '<STR_LIT:path>' ) ) <EOL> if not rows : <EOL> rows . append ( list ( data . keys ( ) ) ) <EOL> rows . append ( [ data . get ( field_name ) for field_name in rows [ <NUM_LIT:0> ] ] ) <EOL> response = HttpResponse ( content_type = '<STR_LIT>' ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> writer = UnicodeWriter ( <EOL> response , ** getattr ( settings , '<STR_LIT>' , { } ) ) <EOL> writer . writerows ( rows ) <EOL> return response <EOL> def get_urls ( self ) : <EOL> return patterns ( <EOL> '<STR_LIT>' , <EOL> url ( <EOL> r'<STR_LIT>' , <EOL> self . admin_site . admin_view ( self . export_submissions ) , <EOL> ) <EOL> ) + super ( FormAdmin , self ) . get_urls ( ) <EOL> class FormSubmissionAdmin ( admin . ModelAdmin ) : <EOL> list_display = ( '<STR_LIT>' , '<STR_LIT:path>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> list_filter = ( '<STR_LIT>' , ) <EOL> fields = ( '<STR_LIT>' , '<STR_LIT:path>' , '<STR_LIT>' ) <EOL> readonly_fields = fields <EOL> def data_summary ( self , submission ) : <EOL> data = submission . formatted_data ( ) <EOL> if len ( data ) > <NUM_LIT:100> : <EOL> return u'<STR_LIT>' % data [ : <NUM_LIT> ] <EOL> return data <EOL> def has_add_permission ( self , request ) : <EOL> return False <EOL> admin . site . register ( models . Form , FormAdmin ) <EOL> admin . site . register ( models . FormSubmission , FormSubmissionAdmin ) </s>
