<s> from subscribe . models import Event , EventQuestion , EventOption , Answer , Registration <EOL> from django . contrib import admin <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponse <EOL> from django . utils . html import format_html <EOL> from xlwt import Workbook <EOL> import io as BytesIO <EOL> def export_events ( eventadmin , request , queryset ) : <EOL> """<STR_LIT>""" <EOL> wb = Workbook ( ) <EOL> for event in queryset : <EOL> s = wb . add_sheet ( event . slug [ : <NUM_LIT:30> ] ) <EOL> s . write ( <NUM_LIT:0> , <NUM_LIT:0> , "<STR_LIT>" ) <EOL> s . write ( <NUM_LIT:0> , <NUM_LIT:1> , "<STR_LIT>" ) <EOL> s . write ( <NUM_LIT:0> , <NUM_LIT:2> , "<STR_LIT>" ) <EOL> s . write ( <NUM_LIT:0> , <NUM_LIT:3> , "<STR_LIT>" ) <EOL> s . write ( <NUM_LIT:0> , <NUM_LIT:4> , "<STR_LIT>" ) <EOL> s . write ( <NUM_LIT:0> , <NUM_LIT:5> , "<STR_LIT>" ) <EOL> col_count = <NUM_LIT:6> <EOL> q_to_col = { } <EOL> for question in event . eventquestion_set . all ( ) : <EOL> if question . question_type == "<STR_LIT>" : <EOL> continue <EOL> q_to_col [ question . id ] = col_count <EOL> s . write ( <NUM_LIT:0> , col_count , question . name ) <EOL> col_count += <NUM_LIT:1> <EOL> row = <NUM_LIT:1> <EOL> for reg in event . registrations . all ( ) : <EOL> s . write ( row , <NUM_LIT:0> , reg . first_name ) <EOL> s . write ( row , <NUM_LIT:1> , reg . last_name ) <EOL> s . write ( row , <NUM_LIT:2> , reg . email ) <EOL> s . write ( row , <NUM_LIT:3> , reg . paid ) <EOL> s . write ( row , <NUM_LIT:4> , float ( reg . price ) / <NUM_LIT:100> ) <EOL> s . write ( row , <NUM_LIT:5> , reg . id ) <EOL> for ans in reg . answers . all ( ) : <EOL> s . write ( row , q_to_col [ ans . question . id ] , '<STR_LIT:{}>' . format ( ans . get_answer ( ) ) ) <EOL> row += <NUM_LIT:1> <EOL> out = BytesIO . BytesIO ( ) <EOL> wb . save ( out ) <EOL> response = HttpResponse ( out . getvalue ( ) , content_type = "<STR_LIT>" ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return response <EOL> export_events . short_description = "<STR_LIT>" <EOL> class EventQuestionInline ( admin . TabularInline ) : <EOL> model = EventQuestion <EOL> extra = <NUM_LIT:1> <EOL> fields = [ '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , ] <EOL> readonly_fields = ( '<STR_LIT>' , ) <EOL> show_change_link = True <EOL> ordering = ( '<STR_LIT>' , ) <EOL> def admin_link ( self , instance ) : <EOL> url = reverse ( '<STR_LIT>' , args = ( instance . id , ) ) <EOL> return format_html ( '<STR_LIT>' , url ) <EOL> class EventAdmin ( admin . ModelAdmin ) : <EOL> fieldsets = [ <EOL> ( "<STR_LIT>" , { <EOL> '<STR_LIT>' : [ <EOL> ( '<STR_LIT:name>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT:description>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> ] } ) , <EOL> ( "<STR_LIT>" , { "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" ] } ) , <EOL> ] <EOL> prepopulated_fields = { '<STR_LIT>' : ( '<STR_LIT:name>' , ) } <EOL> date_hierarchy = '<STR_LIT>' <EOL> inlines = [ EventQuestionInline ] <EOL> actions = [ export_events , ] <EOL> list_display = [ '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> search_fields = [ "<STR_LIT:name>" , ] <EOL> class EventOptionInline ( admin . TabularInline ) : <EOL> model = EventOption <EOL> extra = <NUM_LIT:1> <EOL> fields = [ '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , ] <EOL> readonly_fields = ( '<STR_LIT>' , ) <EOL> ordering = ( '<STR_LIT>' , ) <EOL> def has_delete_permission ( self , request , obj = None ) : <EOL> return False <EOL> def num_registrations ( self , instance ) : <EOL> return instance . num_registrations ( ) <EOL> num_registrations . short_description = "<STR_LIT>" <EOL> class EventQuestionAdmin ( admin . ModelAdmin ) : <EOL> readonly_fields = ( '<STR_LIT>' , ) <EOL> list_display = [ "<STR_LIT:name>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , ] <EOL> list_filter = [ "<STR_LIT>" ] <EOL> inlines = [ EventOptionInline ] <EOL> class AnswerInline ( admin . TabularInline ) : <EOL> model = Answer <EOL> fields = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> readonly_fields = [ '<STR_LIT>' , ] <EOL> extra = <NUM_LIT:0> <EOL> def get_formset ( self , request , obj = None , ** kwargs ) : <EOL> self . parent_obj = obj <EOL> return super ( AnswerInline , self ) . get_formset ( request , obj , ** kwargs ) <EOL> def formfield_for_foreignkey ( self , db_field , request , ** kwargs ) : <EOL> if db_field . name == "<STR_LIT>" : <EOL> kwargs [ '<STR_LIT>' ] = db_field . rel . to . objects . filter ( question__event = self . parent_obj . event ) <EOL> return super ( AnswerInline , self ) . formfield_for_foreignkey ( db_field , request , ** kwargs ) <EOL> class RegistrationAdmin ( admin . ModelAdmin ) : <EOL> readonly_fields = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> list_display = [ "<STR_LIT:id>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:status>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , ] <EOL> list_filter = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> search_fields = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> inlines = [ AnswerInline ] <EOL> admin . site . register ( EventQuestion , EventQuestionAdmin ) <EOL> admin . site . register ( Event , EventAdmin ) <EOL> admin . site . register ( Registration , RegistrationAdmin ) </s>
