<s> import six <EOL> import django <EOL> from django import forms <EOL> from django . conf import settings <EOL> from django . core . exceptions import ValidationError <EOL> from django . forms . models import ModelChoiceIterator <EOL> from django . forms . models import ChoiceField , ModelMultipleChoiceField <EOL> from django . utils . html import escape , conditional_escape <EOL> try : <EOL> from django . forms . utils import flatatt <EOL> except ImportError : <EOL> from django . forms . util import flatatt <EOL> try : <EOL> from django . utils . encoding import force_unicode <EOL> except ImportError : <EOL> from django . utils . encoding import force_text as force_unicode <EOL> from generic_plus . forms import BaseGenericFileInlineFormSet , GenericForeignFileWidget <EOL> from . utils import json <EOL> __all__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class CropDusterWidget ( GenericForeignFileWidget ) : <EOL> sizes = None <EOL> template = "<STR_LIT>" <EOL> class Media : <EOL> css = { '<STR_LIT:all>' : ( u'<STR_LIT>' % settings . STATIC_URL , ) } <EOL> js = ( <EOL> u'<STR_LIT>' % settings . STATIC_URL , <EOL> u'<STR_LIT>' % settings . STATIC_URL , <EOL> ) <EOL> def get_context_data ( self , name , value , attrs = None , bound_field = None ) : <EOL> ctx = super ( CropDusterWidget , self ) . get_context_data ( name , value , attrs , bound_field ) <EOL> sizes = self . sizes <EOL> if six . callable ( sizes ) : <EOL> instance = getattr ( getattr ( bound_field , '<STR_LIT>' , None ) , '<STR_LIT>' , None ) <EOL> related_object = ctx [ '<STR_LIT>' ] <EOL> sizes_callable = getattr ( sizes , '<STR_LIT>' , sizes ) <EOL> sizes = sizes_callable ( instance , related = related_object ) <EOL> ctx . update ( { <EOL> '<STR_LIT>' : json . dumps ( sizes ) , <EOL> } ) <EOL> return ctx <EOL> class ThumbChoiceIterator ( ModelChoiceIterator ) : <EOL> def __iter__ ( self ) : <EOL> if self . field . empty_label is not None : <EOL> yield ( "<STR_LIT>" , self . field . empty_label ) <EOL> if getattr ( self . field , '<STR_LIT>' , None ) : <EOL> if self . field . choice_cache is None : <EOL> self . field . choice_cache = [ <EOL> self . choice ( obj ) for obj in self . queryset <EOL> ] <EOL> for choice in self . field . choice_cache : <EOL> yield choice <EOL> else : <EOL> for obj in self . queryset : <EOL> yield self . choice ( obj ) <EOL> def choice ( self , obj ) : <EOL> return ( obj , self . field . label_from_instance ( obj ) ) <EOL> class CropDusterThumbWidget ( forms . SelectMultiple ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> from cropduster . models import Thumb <EOL> super ( CropDusterThumbWidget , self ) . __init__ ( * args , ** kwargs ) <EOL> self . model = Thumb <EOL> def render_option ( self , selected_choices , option_value , option_label ) : <EOL> attrs = { } <EOL> if isinstance ( option_value , self . model ) : <EOL> thumb = option_value <EOL> option_value = thumb . pk <EOL> else : <EOL> try : <EOL> thumb = self . model . objects . get ( pk = option_value ) <EOL> except ( TypeError , self . model . DoesNotExist ) : <EOL> thumb = None <EOL> if thumb : <EOL> use_tmp_file = not ( thumb . image_id ) <EOL> attrs = { <EOL> '<STR_LIT>' : thumb . width , <EOL> '<STR_LIT>' : thumb . height , <EOL> '<STR_LIT>' : json . dumps ( use_tmp_file ) , <EOL> } <EOL> option_value = force_unicode ( option_value ) <EOL> if option_value in selected_choices : <EOL> selected_html = u'<STR_LIT>' <EOL> if not self . allow_multiple_selected : <EOL> selected_choices . remove ( option_value ) <EOL> else : <EOL> selected_html = '<STR_LIT>' <EOL> return ( <EOL> u'<STR_LIT>' ) % { <EOL> '<STR_LIT:value>' : escape ( option_value ) , <EOL> '<STR_LIT>' : selected_html , <EOL> '<STR_LIT>' : flatatt ( attrs ) , <EOL> '<STR_LIT:label>' : conditional_escape ( force_unicode ( option_label ) ) , <EOL> } <EOL> class CropDusterThumbFormField ( ModelMultipleChoiceField ) : <EOL> widget = CropDusterThumbWidget <EOL> def clean ( self , value ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> value = super ( CropDusterThumbFormField , self ) . clean ( value ) <EOL> except ValidationError as e : <EOL> if self . error_messages [ '<STR_LIT>' ] in e . messages : <EOL> raise <EOL> elif self . error_messages [ '<STR_LIT:list>' ] in e . messages : <EOL> raise <EOL> return value <EOL> def _get_choices ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . _choices <EOL> return ThumbChoiceIterator ( self ) <EOL> choices = property ( _get_choices , ChoiceField . _set_choices ) <EOL> def get_cropduster_field_on_model ( model , field_identifier ) : <EOL> from cropduster . fields import CropDusterField <EOL> opts = model . _meta <EOL> if hasattr ( opts , '<STR_LIT>' ) : <EOL> m2m_fields = [ f for f in opts . get_fields ( ) if f . many_to_many and not f . auto_created ] <EOL> else : <EOL> m2m_fields = opts . many_to_many <EOL> m2m_related_fields = set ( m2m_fields + opts . virtual_fields ) <EOL> field_match = lambda f : ( isinstance ( f , CropDusterField ) <EOL> and f . field_identifier == field_identifier ) <EOL> try : <EOL> return [ f for f in m2m_related_fields if field_match ( f ) ] [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> return None <EOL> class CropDusterInlineFormSet ( BaseGenericFileInlineFormSet ) : <EOL> fields = ( '<STR_LIT:image>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( CropDusterInlineFormSet , self ) . __init__ ( * args , ** kwargs ) <EOL> if self . instance and not self . data : <EOL> cropduster_field = get_cropduster_field_on_model ( self . instance . __class__ , self . field_identifier ) <EOL> if cropduster_field : <EOL> self . queryset = self . queryset . order_by ( '<STR_LIT>' ) <EOL> field_file = getattr ( self . instance , cropduster_field . name ) <EOL> self . queryset . _result_cache = list ( filter ( None , [ field_file . related_object ] ) ) <EOL> def _construct_form ( self , i , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> from cropduster . models import Thumb <EOL> form = super ( CropDusterInlineFormSet , self ) . _construct_form ( i , ** kwargs ) <EOL> field_identifier_field = form . fields [ '<STR_LIT>' ] <EOL> field_identifier_field . widget = forms . HiddenInput ( ) <EOL> field_identifier_field . initial = self . field_identifier <EOL> thumbs_field = form . fields [ '<STR_LIT>' ] <EOL> if form . instance and form . instance . pk : <EOL> if django . VERSION < ( <NUM_LIT:1> , <NUM_LIT:7> ) : <EOL> thumbs_field . queryset = form . instance . thumbs . get_query_set ( ) <EOL> else : <EOL> thumbs_field . queryset = form . instance . thumbs . get_queryset ( ) <EOL> else : <EOL> thumbs_field . queryset = Thumb . objects . none ( ) <EOL> if form . data : <EOL> try : <EOL> thumb_pks = [ int ( v ) for v in form [ '<STR_LIT>' ] . value ( ) ] <EOL> except ( TypeError , ValueError ) : <EOL> pass <EOL> else : <EOL> if thumb_pks and thumb_pks != [ o . pk for o in thumbs_field . queryset ] : <EOL> thumbs_field . queryset = Thumb . objects . filter ( pk__in = thumb_pks ) <EOL> return form </s>
