<s> from __future__ import unicode_literals <EOL> import json <EOL> from django import forms <EOL> from django . contrib import admin <EOL> from django . forms . utils import flatatt <EOL> from django . utils import six <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 django . utils . translation import ugettext_lazy as _ <EOL> from dynamic_forms . formfields import formfield_registry <EOL> from dynamic_forms . models import FormFieldModel , FormModel , FormModelData <EOL> class ReadOnlyWidget ( forms . Widget ) : <EOL> def __init__ ( self , attrs = None , ** kwargs ) : <EOL> self . show_text = kwargs . pop ( '<STR_LIT>' , None ) <EOL> super ( ReadOnlyWidget , self ) . __init__ ( attrs ) <EOL> def render ( self , name , value , attrs = None ) : <EOL> content = '<STR_LIT>' <EOL> if value is not None : <EOL> content = value <EOL> if self . show_text is not None : <EOL> content = self . show_text <EOL> final_attrs = self . build_attrs ( attrs ) <EOL> return format_html ( '<STR_LIT>' , <EOL> flatatt ( final_attrs ) , <EOL> force_text ( content ) , <EOL> ) <EOL> class OptionsWidget ( forms . MultiWidget ) : <EOL> def __init__ ( self , option_names , widgets , attrs = None ) : <EOL> self . option_names = option_names <EOL> super ( OptionsWidget , self ) . __init__ ( widgets , attrs ) <EOL> def decompress ( self , value ) : <EOL> mapping = json . loads ( value ) if value else { } <EOL> return [ mapping . get ( key ) for key in self . option_names ] <EOL> def format_output ( self , rendered_widgets , id_ ) : <EOL> output = [ ] <EOL> i = <NUM_LIT:0> <EOL> for n , ( r , w ) in six . moves . zip ( self . option_names , rendered_widgets ) : <EOL> output . append ( <EOL> format_html ( <EOL> '<STR_LIT>' , <EOL> w . id_for_label ( id_ ) , i , n , r <EOL> ) <EOL> ) <EOL> i += <NUM_LIT:1> <EOL> return mark_safe ( '<STR_LIT>' + <EOL> ( '<STR_LIT>' . join ( output ) ) + '<STR_LIT>' ) <EOL> def render ( self , name , value , attrs = None ) : <EOL> if self . is_localized : <EOL> for widget in self . widgets : <EOL> widget . is_localized = self . is_localized <EOL> if not isinstance ( value , list ) : <EOL> value = self . decompress ( value ) <EOL> output = [ ] <EOL> final_attrs = self . build_attrs ( attrs ) <EOL> id_ = final_attrs . get ( '<STR_LIT:id>' ) <EOL> for i , widget in enumerate ( self . widgets ) : <EOL> try : <EOL> widget_value = value [ i ] <EOL> except IndexError : <EOL> widget_value = None <EOL> if id_ : <EOL> final_attrs = dict ( final_attrs , id = '<STR_LIT>' % ( id_ , i ) ) <EOL> rendered = widget . render ( name + '<STR_LIT>' % i , widget_value , <EOL> final_attrs ) <EOL> output . append ( ( rendered , widget ) ) <EOL> return mark_safe ( self . format_output ( output , id_ ) ) <EOL> class OptionsField ( forms . MultiValueField ) : <EOL> def __init__ ( self , meta , * args , ** kwargs ) : <EOL> self . option_names = [ ] <EOL> self . option_fields = [ ] <EOL> self . option_widgets = [ ] <EOL> initial = { } <EOL> for name , option in sorted ( meta . items ( ) ) : <EOL> self . option_names . append ( name ) <EOL> initial [ name ] = option [ <NUM_LIT:1> ] <EOL> formfield = option [ <NUM_LIT:2> ] <EOL> if isinstance ( formfield , forms . Field ) : <EOL> self . option_fields . append ( formfield ) <EOL> self . option_widgets . append ( formfield . widget ) <EOL> elif isinstance ( formfield , ( tuple , list ) ) : <EOL> if isinstance ( formfield [ <NUM_LIT:0> ] , forms . Field ) : <EOL> self . option_fields . append ( formfield [ <NUM_LIT:0> ] ) <EOL> else : <EOL> self . option_fields . append ( formfield [ <NUM_LIT:0> ] ( ) ) <EOL> if isinstance ( formfield [ <NUM_LIT:1> ] , forms . Widget ) : <EOL> self . option_widgets . append ( formfield [ <NUM_LIT:1> ] ) <EOL> else : <EOL> self . option_widgets . append ( formfield [ <NUM_LIT:1> ] ( ) ) <EOL> elif isinstance ( formfield , type ) : <EOL> self . option_fields . append ( formfield ( ) ) <EOL> self . option_widgets . append ( formfield . widget ) <EOL> kwargs [ '<STR_LIT>' ] = OptionsWidget ( self . option_names , <EOL> self . option_widgets ) <EOL> if '<STR_LIT>' in kwargs : <EOL> kwargs [ '<STR_LIT>' ] . update ( initial ) <EOL> else : <EOL> kwargs [ '<STR_LIT>' ] = initial <EOL> super ( OptionsField , self ) . __init__ ( self . option_fields , * args , ** kwargs ) <EOL> def compress ( self , data_list ) : <EOL> data = { } <EOL> for name , value in six . moves . zip ( self . option_names , data_list ) : <EOL> if value is not None : <EOL> data [ name ] = value <EOL> return json . dumps ( data ) <EOL> class AdminFormModelForm ( forms . ModelForm ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( AdminFormModelForm , self ) . __init__ ( * args , ** kwargs ) <EOL> choices = self . fields [ '<STR_LIT>' ] . choices <EOL> self . fields [ '<STR_LIT>' ] . choices = sorted ( choices , key = lambda x : x [ <NUM_LIT:1> ] ) <EOL> class AdminFormFieldInlineForm ( forms . ModelForm ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> instance = kwargs . get ( '<STR_LIT>' ) <EOL> meta = None <EOL> if instance : <EOL> df = formfield_registry . get ( instance . field_type ) <EOL> if df : <EOL> meta = df . _meta <EOL> super ( AdminFormFieldInlineForm , self ) . __init__ ( * args , ** kwargs ) <EOL> choices = self . fields [ '<STR_LIT>' ] . choices <EOL> self . fields [ '<STR_LIT>' ] . choices = sorted ( choices , key = lambda x : x [ <NUM_LIT:1> ] ) <EOL> if meta is not None : <EOL> self . fields [ '<STR_LIT>' ] = OptionsField ( meta , required = False , <EOL> label = _ ( '<STR_LIT>' ) ) <EOL> else : <EOL> self . fields [ '<STR_LIT>' ] . widget = ReadOnlyWidget ( show_text = _ ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) ) <EOL> class FormFieldModelInlineAdmin ( admin . StackedInline ) : <EOL> extra = <NUM_LIT:3> <EOL> form = AdminFormFieldInlineForm <EOL> list_display = ( '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT:label>' ) <EOL> model = FormFieldModel <EOL> prepopulated_fields = { "<STR_LIT:name>" : ( "<STR_LIT:label>" , ) } <EOL> class FormModelAdmin ( admin . ModelAdmin ) : <EOL> form = AdminFormModelForm <EOL> inlines = ( FormFieldModelInlineAdmin , ) <EOL> list_display = ( '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> model = FormModel <EOL> admin . site . register ( FormModel , FormModelAdmin ) <EOL> class FormModelDataAdmin ( admin . ModelAdmin ) : <EOL> fields = ( '<STR_LIT>' , '<STR_LIT:value>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> list_display = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> model = FormModelData <EOL> readonly_fields = ( '<STR_LIT>' , '<STR_LIT>' , ) <EOL> admin . site . register ( FormModelData , FormModelDataAdmin ) </s>
