<s> import bleach <EOL> from django import forms <EOL> from django . db import transaction <EOL> from django . db . models import Q <EOL> from django . utils . encoding import force_text <EOL> from django . utils . html import format_html <EOL> from django . utils . safestring import mark_safe <EOL> from subscribe . models import Answer , Registration , AFDELINGEN <EOL> setattr ( forms . fields . Field , '<STR_LIT>' , lambda self : isinstance ( self . widget , forms . CheckboxInput ) ) <EOL> class RadioChoiceInputDisabled ( forms . widgets . RadioChoiceInput ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> disabledset = kwargs . pop ( '<STR_LIT>' , None ) <EOL> super ( RadioChoiceInputDisabled , self ) . __init__ ( * args , ** kwargs ) <EOL> self . disabledset = set ( [ str ( x . pk ) for x in disabledset ] ) <EOL> if self . choice_value in self . disabledset : <EOL> self . attrs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> class RadioFieldDisabledRenderer ( forms . widgets . RadioFieldRenderer ) : <EOL> def choice_input_class ( self , * args , ** kwargs ) : <EOL> kwargs = dict ( kwargs , disabledset = self . disabledset ) <EOL> return RadioChoiceInputDisabled ( * args , ** kwargs ) <EOL> class RadioSelectDisabled ( forms . widgets . RadioSelect ) : <EOL> def renderer ( self , * args , ** kwargs ) : <EOL> if getattr ( self , '<STR_LIT>' , None ) is not None : <EOL> instance = RadioFieldDisabledRenderer ( * args , ** kwargs ) <EOL> instance . disabledset = self . disabledset <EOL> return instance <EOL> else : <EOL> return super ( RadioSelectDisabled , self ) . renderer ( * args , ** kwargs ) <EOL> class SelectDisabled ( forms . widgets . Select ) : <EOL> def render_option ( self , selected_choices , option_value , option_label ) : <EOL> if option_value is None : <EOL> option_value = '<STR_LIT>' <EOL> option_value = force_text ( option_value ) <EOL> if getattr ( self , '<STR_LIT>' , None ) is not None and getattr ( self , '<STR_LIT>' , None ) is None : <EOL> self . disabledstr = set ( [ str ( x . pk ) for x in self . disabledset ] ) <EOL> if getattr ( self , '<STR_LIT>' , None ) is not None and option_value in self . disabledstr : <EOL> disabled_html = mark_safe ( '<STR_LIT>' ) <EOL> else : <EOL> disabled_html = '<STR_LIT>' <EOL> if option_value in selected_choices : <EOL> selected_html = mark_safe ( '<STR_LIT>' ) <EOL> if not self . allow_multiple_selected : <EOL> selected_choices . remove ( option_value ) <EOL> else : <EOL> selected_html = '<STR_LIT>' <EOL> return format_html ( '<STR_LIT>' , <EOL> option_value , <EOL> selected_html , <EOL> disabled_html , <EOL> force_text ( option_label ) ) <EOL> class SubscribeForm ( forms . Form ) : <EOL> def __init__ ( self , event , * args , ** kwargs ) : <EOL> if len ( args ) and '<STR_LIT>' in args [ <NUM_LIT:0> ] : <EOL> self . preview = True <EOL> else : <EOL> self . preview = False <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> super ( SubscribeForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . event = event <EOL> closed_options = [ ] <EOL> for question in event . eventquestion_set . all ( ) : <EOL> for option in question . options . all ( ) : <EOL> if option . is_full ( ) : <EOL> closed_options += [ option . pk ] <EOL> self . _elements = [ ] <EOL> inserted_default_fields = False <EOL> for question in event . eventquestion_set . order_by ( '<STR_LIT>' ) : <EOL> if question . order >= <NUM_LIT:0> and not inserted_default_fields : <EOL> self . fields [ "<STR_LIT>" ] = forms . CharField ( max_length = <NUM_LIT:64> , required = True , label = "<STR_LIT>" ) <EOL> self . _elements += [ ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> self . fields [ "<STR_LIT>" ] = forms . CharField ( max_length = <NUM_LIT:64> , required = True , label = "<STR_LIT>" ) <EOL> self . _elements += [ ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> self . fields [ "<STR_LIT:email>" ] = forms . EmailField ( required = True , label = "<STR_LIT>" ) <EOL> self . _elements += [ ( '<STR_LIT>' , '<STR_LIT:email>' ) ] <EOL> inserted_default_fields = True <EOL> name = question . form_id ( ) <EOL> if question . question_type == "<STR_LIT>" : <EOL> self . fields [ name ] = forms . IntegerField ( label = question . name , required = question . required ) <EOL> self . _elements += [ ( '<STR_LIT>' , name ) ] <EOL> elif question . question_type == "<STR_LIT>" : <EOL> self . fields [ name ] = forms . CharField ( max_length = <NUM_LIT> , label = question . name , required = question . required ) <EOL> self . _elements += [ ( '<STR_LIT>' , name ) ] <EOL> elif question . question_type == "<STR_LIT>" : <EOL> if question . radio : <EOL> self . fields [ name ] = forms . CharField ( max_length = <NUM_LIT> , label = question . name , required = question . required , <EOL> widget = forms . RadioSelect ( choices = AFDELINGEN ) ) <EOL> else : <EOL> self . fields [ name ] = forms . CharField ( max_length = <NUM_LIT> , label = question . name , required = question . required , <EOL> widget = forms . Select ( choices = AFDELINGEN ) ) <EOL> self . _elements += [ ( '<STR_LIT>' , name ) ] <EOL> elif question . question_type == "<STR_LIT>" : <EOL> options = question . options . all ( ) <EOL> active = True <EOL> if len ( options ) and options [ <NUM_LIT:0> ] . price != <NUM_LIT:0> : <EOL> if options [ <NUM_LIT:0> ] . price < <NUM_LIT:0> : <EOL> label = "<STR_LIT>" % ( question . name , float ( - options [ <NUM_LIT:0> ] . price ) / <NUM_LIT:100> ) <EOL> else : <EOL> label = "<STR_LIT>" % ( question . name , float ( options [ <NUM_LIT:0> ] . price ) / <NUM_LIT:100> ) <EOL> if not options [ <NUM_LIT:0> ] . active or options [ <NUM_LIT:0> ] . is_full ( ) : <EOL> label += "<STR_LIT>" <EOL> active = False <EOL> else : <EOL> label = question . name <EOL> self . fields [ name ] = forms . BooleanField ( label = label , required = question . required ) <EOL> if not active : <EOL> self . fields [ name ] . widget . attrs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . _elements += [ ( '<STR_LIT>' , name ) ] <EOL> elif question . question_type == "<STR_LIT>" : <EOL> if question . radio : <EOL> self . fields [ name ] = forms . ModelChoiceField ( widget = RadioSelectDisabled ( ) , label = question . name , required = question . required , queryset = question . options . exclude ( active = False ) . order_by ( '<STR_LIT>' ) , empty_label = None ) <EOL> else : <EOL> self . fields [ name ] = forms . ModelChoiceField ( widget = SelectDisabled ( ) , label = question . name , required = question . required , queryset = question . options . exclude ( active = False ) . order_by ( '<STR_LIT>' ) , empty_label = None ) <EOL> self . _elements += [ ( '<STR_LIT>' , name ) ] <EOL> self . fields [ name ] . widget . disabledset = question . options . filter ( Q ( pk__in = closed_options ) | Q ( active = False ) ) <EOL> elif question . question_type == "<STR_LIT>" : <EOL> allowed_tags = [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:code>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:i>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:p>' , '<STR_LIT>' ] <EOL> allowed_attrs = { <EOL> '<STR_LIT:*>' : [ '<STR_LIT:class>' , '<STR_LIT>' ] , <EOL> '<STR_LIT:a>' : [ '<STR_LIT>' , '<STR_LIT:target>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:src>' , '<STR_LIT>' ] , <EOL> } <EOL> text = mark_safe ( bleach . clean ( question . text , tags = allowed_tags , attributes = allowed_attrs ) ) <EOL> self . _elements += [ ( '<STR_LIT:text>' , text ) ] <EOL> if not inserted_default_fields : <EOL> self . fields [ "<STR_LIT>" ] = forms . CharField ( max_length = <NUM_LIT:64> , required = True , label = "<STR_LIT>" ) <EOL> self . _elements += [ ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> self . fields [ "<STR_LIT>" ] = forms . CharField ( max_length = <NUM_LIT:64> , required = True , label = "<STR_LIT>" ) <EOL> self . _elements += [ ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> self . fields [ "<STR_LIT:email>" ] = forms . EmailField ( required = True , label = "<STR_LIT>" ) <EOL> self . _elements += [ ( '<STR_LIT>' , '<STR_LIT:email>' ) ] <EOL> def is_valid ( self ) : <EOL> res = super ( SubscribeForm , self ) . is_valid ( ) <EOL> if not res : <EOL> return False <EOL> if self . preview : <EOL> self . confirm_page ( ) <EOL> self . full_clean ( ) <EOL> res = super ( SubscribeForm , self ) . is_valid ( ) <EOL> return res <EOL> def confirm_page ( self ) : <EOL> self . preview = True <EOL> for f in self . fields : <EOL> self . fields [ f ] . widget . attrs [ '<STR_LIT>' ] = True <EOL> str = '<STR_LIT>' <EOL> price = self . event . price <EOL> if price != <NUM_LIT:0> : <EOL> str += '<STR_LIT>' . format ( float ( price ) / <NUM_LIT> ) <EOL> for question in self . event . eventquestion_set . order_by ( '<STR_LIT>' ) : <EOL> if question . question_type == '<STR_LIT>' : <EOL> option = self . cleaned_data [ question . form_id ( ) ] <EOL> if option is not None : <EOL> if option . price != <NUM_LIT:0> : <EOL> price += option . price <EOL> str += '<STR_LIT>' . format ( option . name , float ( option . price ) / <NUM_LIT> ) <EOL> if question . question_type == '<STR_LIT>' : <EOL> if self . cleaned_data [ question . form_id ( ) ] : <EOL> option = list ( question . options . all ( ) [ : <NUM_LIT:1> ] ) <EOL> if option : <EOL> option = option [ <NUM_LIT:0> ] <EOL> if option . price != <NUM_LIT:0> : <EOL> price += option . price <EOL> str += '<STR_LIT>' . format ( option . name , float ( option . price ) / <NUM_LIT> ) <EOL> str += '<STR_LIT>' . format ( float ( price ) / <NUM_LIT> ) <EOL> str += '<STR_LIT>' <EOL> self . price = price <EOL> self . price_description = str <EOL> def visible_fields ( self ) : <EOL> fields = [ self [ f ] for f in self . fields ] <EOL> return [ field for field in fields if not field . is_hidden ] <EOL> def elements ( self ) : <EOL> res = [ ] <EOL> for element_type , element_value in self . _elements : <EOL> if element_type == "<STR_LIT:text>" : <EOL> res += [ ( element_type , element_value ) ] <EOL> elif element_type == "<STR_LIT>" : <EOL> res += [ ( element_type , self [ element_value ] ) ] <EOL> return res <EOL> @ transaction . atomic <EOL> def fill_subscription ( form , event ) : <EOL> reg = Registration ( event = event ) <EOL> reg . first_name = form . cleaned_data [ "<STR_LIT>" ] <EOL> reg . last_name = form . cleaned_data [ "<STR_LIT>" ] <EOL> reg . email = form . cleaned_data [ "<STR_LIT:email>" ] <EOL> reg . save ( ) <EOL> for question in event . eventquestion_set . all ( ) : <EOL> if question . form_id ( ) in form . cleaned_data : <EOL> ans = Answer ( registration = reg , question = question ) <EOL> ans . set_answer ( form . cleaned_data [ question . form_id ( ) ] ) <EOL> ans . save ( ) <EOL> reg . calculate_price ( ) <EOL> reg . save ( ) <EOL> return reg </s>
