<s> from __future__ import unicode_literals <EOL> from future . builtins import bytes , open <EOL> from csv import writer <EOL> from mimetypes import guess_type <EOL> from os . path import join <EOL> from datetime import datetime <EOL> from io import BytesIO , StringIO <EOL> from django . conf . urls import patterns , url <EOL> from django . contrib import admin <EOL> from django . core . files . storage import FileSystemStorage <EOL> from django . core . urlresolvers import reverse <EOL> from django . db . models import Count <EOL> from django . http import HttpResponse , HttpResponseRedirect <EOL> from django . shortcuts import render_to_response , get_object_or_404 <EOL> from django . template import RequestContext <EOL> from django . utils . translation import ungettext , ugettext_lazy as _ <EOL> from forms_builder . forms . forms import EntriesForm <EOL> from forms_builder . forms . models import Form , Field , FormEntry , FieldEntry <EOL> from forms_builder . forms . settings import CSV_DELIMITER , UPLOAD_ROOT <EOL> from forms_builder . forms . settings import USE_SITES , EDITABLE_SLUGS <EOL> from forms_builder . forms . utils import now , slugify <EOL> try : <EOL> import xlwt <EOL> XLWT_INSTALLED = True <EOL> XLWT_DATETIME_STYLE = xlwt . easyxf ( num_format_str = '<STR_LIT>' ) <EOL> except ImportError : <EOL> XLWT_INSTALLED = False <EOL> fs = FileSystemStorage ( location = UPLOAD_ROOT ) <EOL> form_admin_filter_horizontal = ( ) <EOL> form_admin_fieldsets = [ <EOL> ( None , { "<STR_LIT>" : ( "<STR_LIT:title>" , ( "<STR_LIT:status>" , "<STR_LIT>" , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , ) , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) } ) , <EOL> ( _ ( "<STR_LIT>" ) , { "<STR_LIT>" : ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ) } ) , ] <EOL> if EDITABLE_SLUGS : <EOL> form_admin_fieldsets . append ( <EOL> ( _ ( "<STR_LIT>" ) , { "<STR_LIT>" : ( "<STR_LIT>" , ) , "<STR_LIT>" : ( "<STR_LIT>" , ) } ) ) <EOL> if USE_SITES : <EOL> form_admin_fieldsets . append ( ( _ ( "<STR_LIT>" ) , { "<STR_LIT>" : ( "<STR_LIT>" , ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , ) } ) ) <EOL> form_admin_filter_horizontal = ( "<STR_LIT>" , ) <EOL> class FieldAdmin ( admin . TabularInline ) : <EOL> model = Field <EOL> exclude = ( '<STR_LIT>' , ) <EOL> class FormAdmin ( admin . ModelAdmin ) : <EOL> formentry_model = FormEntry <EOL> fieldentry_model = FieldEntry <EOL> inlines = ( FieldAdmin , ) <EOL> list_display = ( "<STR_LIT:title>" , "<STR_LIT:status>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> list_display_links = ( "<STR_LIT:title>" , ) <EOL> list_editable = ( "<STR_LIT:status>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> list_filter = ( "<STR_LIT:status>" , ) <EOL> filter_horizontal = form_admin_filter_horizontal <EOL> search_fields = ( "<STR_LIT:title>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> radio_fields = { "<STR_LIT:status>" : admin . HORIZONTAL } <EOL> fieldsets = form_admin_fieldsets <EOL> def get_queryset ( self , request ) : <EOL> """<STR_LIT>""" <EOL> qs = super ( FormAdmin , self ) . get_queryset ( request ) <EOL> return qs . annotate ( total_entries = Count ( "<STR_LIT>" ) ) <EOL> def get_urls ( self ) : <EOL> """<STR_LIT>""" <EOL> urls = super ( FormAdmin , self ) . get_urls ( ) <EOL> extra_urls = [ <EOL> url ( "<STR_LIT>" , <EOL> self . admin_site . admin_view ( self . entries_view ) , <EOL> name = "<STR_LIT>" ) , <EOL> url ( "<STR_LIT>" , <EOL> self . admin_site . admin_view ( self . entries_view ) , <EOL> { "<STR_LIT>" : True } , name = "<STR_LIT>" ) , <EOL> url ( "<STR_LIT>" , <EOL> self . admin_site . admin_view ( self . entries_view ) , <EOL> { "<STR_LIT>" : True } , name = "<STR_LIT>" ) , <EOL> url ( "<STR_LIT>" , <EOL> self . admin_site . admin_view ( self . file_view ) , <EOL> name = "<STR_LIT>" ) , <EOL> ] <EOL> return extra_urls + urls <EOL> def entries_view ( self , request , form_id , show = False , export = False , <EOL> export_xls = False ) : <EOL> """<STR_LIT>""" <EOL> if request . POST . get ( "<STR_LIT>" ) : <EOL> bits = ( self . model . _meta . app_label , self . model . __name__ . lower ( ) ) <EOL> change_url = reverse ( "<STR_LIT>" % bits , args = ( form_id , ) ) <EOL> return HttpResponseRedirect ( change_url ) <EOL> form = get_object_or_404 ( self . model , id = form_id ) <EOL> post = request . POST or None <EOL> args = form , request , self . formentry_model , self . fieldentry_model , post <EOL> entries_form = EntriesForm ( * args ) <EOL> delete = "<STR_LIT>" % self . formentry_model . _meta . app_label <EOL> can_delete_entries = request . user . has_perm ( delete ) <EOL> submitted = entries_form . is_valid ( ) or show or export or export_xls <EOL> export = export or request . POST . get ( "<STR_LIT>" ) <EOL> export_xls = export_xls or request . POST . get ( "<STR_LIT>" ) <EOL> if submitted : <EOL> if export : <EOL> response = HttpResponse ( content_type = "<STR_LIT>" ) <EOL> fname = "<STR_LIT>" % ( form . slug , slugify ( now ( ) . ctime ( ) ) ) <EOL> attachment = "<STR_LIT>" % fname <EOL> response [ "<STR_LIT>" ] = attachment <EOL> queue = StringIO ( ) <EOL> try : <EOL> csv = writer ( queue , delimiter = CSV_DELIMITER ) <EOL> writerow = csv . writerow <EOL> except TypeError : <EOL> queue = BytesIO ( ) <EOL> delimiter = bytes ( CSV_DELIMITER , encoding = "<STR_LIT:utf-8>" ) <EOL> csv = writer ( queue , delimiter = delimiter ) <EOL> writerow = lambda row : csv . writerow ( [ c . encode ( "<STR_LIT:utf-8>" ) <EOL> if hasattr ( c , "<STR_LIT>" ) else c for c in row ] ) <EOL> writerow ( entries_form . columns ( ) ) <EOL> for row in entries_form . rows ( csv = True ) : <EOL> writerow ( row ) <EOL> data = queue . getvalue ( ) <EOL> response . write ( data ) <EOL> return response <EOL> elif XLWT_INSTALLED and export_xls : <EOL> response = HttpResponse ( content_type = "<STR_LIT>" ) <EOL> fname = "<STR_LIT>" % ( form . slug , slugify ( now ( ) . ctime ( ) ) ) <EOL> attachment = "<STR_LIT>" % fname <EOL> response [ "<STR_LIT>" ] = attachment <EOL> queue = BytesIO ( ) <EOL> workbook = xlwt . Workbook ( encoding = '<STR_LIT:utf8>' ) <EOL> sheet = workbook . add_sheet ( form . title [ : <NUM_LIT> ] ) <EOL> for c , col in enumerate ( entries_form . columns ( ) ) : <EOL> sheet . write ( <NUM_LIT:0> , c , col ) <EOL> for r , row in enumerate ( entries_form . rows ( csv = True ) ) : <EOL> for c , item in enumerate ( row ) : <EOL> if isinstance ( item , datetime ) : <EOL> item = item . replace ( tzinfo = None ) <EOL> sheet . write ( r + <NUM_LIT:2> , c , item , XLWT_DATETIME_STYLE ) <EOL> else : <EOL> sheet . write ( r + <NUM_LIT:2> , c , item ) <EOL> workbook . save ( queue ) <EOL> data = queue . getvalue ( ) <EOL> response . write ( data ) <EOL> return response <EOL> elif request . POST . get ( "<STR_LIT>" ) and can_delete_entries : <EOL> selected = request . POST . getlist ( "<STR_LIT>" ) <EOL> if selected : <EOL> try : <EOL> from django . contrib . messages import info <EOL> except ImportError : <EOL> def info ( request , message , fail_silently = True ) : <EOL> request . user . message_set . create ( message = message ) <EOL> entries = self . formentry_model . objects . filter ( id__in = selected ) <EOL> count = entries . count ( ) <EOL> if count > <NUM_LIT:0> : <EOL> entries . delete ( ) <EOL> message = ungettext ( "<STR_LIT>" , <EOL> "<STR_LIT>" , count ) <EOL> info ( request , message % { "<STR_LIT:count>" : count } ) <EOL> template = "<STR_LIT>" <EOL> context = { "<STR_LIT:title>" : _ ( "<STR_LIT>" ) , "<STR_LIT>" : entries_form , <EOL> "<STR_LIT>" : self . model . _meta , "<STR_LIT>" : form , <EOL> "<STR_LIT>" : can_delete_entries , <EOL> "<STR_LIT>" : submitted , <EOL> "<STR_LIT>" : XLWT_INSTALLED } <EOL> return render_to_response ( template , context , RequestContext ( request ) ) <EOL> def file_view ( self , request , field_entry_id ) : <EOL> """<STR_LIT>""" <EOL> model = self . fieldentry_model <EOL> field_entry = get_object_or_404 ( model , id = field_entry_id ) <EOL> path = join ( fs . location , field_entry . value ) <EOL> response = HttpResponse ( content_type = guess_type ( path ) [ <NUM_LIT:0> ] ) <EOL> f = open ( path , "<STR_LIT>" ) <EOL> response [ "<STR_LIT>" ] = "<STR_LIT>" % f . name <EOL> response . write ( f . read ( ) ) <EOL> f . close ( ) <EOL> return response <EOL> admin . site . register ( Form , FormAdmin ) </s>
