<s> from django import forms <EOL> from django . core . exceptions import ValidationError <EOL> from django . core . validators import validate_slug <EOL> from django . db import models <EOL> from django . utils import simplejson as json <EOL> from django . utils . text import capfirst <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from philo . forms . fields import JSONFormField <EOL> from philo . utils . registry import RegistryIterator <EOL> from philo . validators import TemplateValidator , json_validator <EOL> class TemplateField ( models . TextField ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , allow = None , disallow = None , secure = True , * args , ** kwargs ) : <EOL> super ( TemplateField , self ) . __init__ ( * args , ** kwargs ) <EOL> self . validators . append ( TemplateValidator ( allow , disallow , secure ) ) <EOL> class JSONDescriptor ( object ) : <EOL> def __init__ ( self , field ) : <EOL> self . field = field <EOL> def __get__ ( self , instance , owner ) : <EOL> if instance is None : <EOL> raise AttributeError <EOL> if self . field . name not in instance . __dict__ : <EOL> json_string = getattr ( instance , self . field . attname ) <EOL> instance . __dict__ [ self . field . name ] = json . loads ( json_string ) <EOL> return instance . __dict__ [ self . field . name ] <EOL> def __set__ ( self , instance , value ) : <EOL> instance . __dict__ [ self . field . name ] = value <EOL> setattr ( instance , self . field . attname , json . dumps ( value ) ) <EOL> def __delete__ ( self , instance ) : <EOL> del ( instance . __dict__ [ self . field . name ] ) <EOL> setattr ( instance , self . field . attname , json . dumps ( None ) ) <EOL> class JSONField ( models . TextField ) : <EOL> """<STR_LIT>""" <EOL> default_validators = [ json_validator ] <EOL> def get_attname ( self ) : <EOL> return "<STR_LIT>" % self . name <EOL> def contribute_to_class ( self , cls , name ) : <EOL> super ( JSONField , self ) . contribute_to_class ( cls , name ) <EOL> setattr ( cls , name , JSONDescriptor ( self ) ) <EOL> models . signals . pre_init . connect ( self . fix_init_kwarg , sender = cls ) <EOL> def fix_init_kwarg ( self , sender , args , kwargs , ** signal_kwargs ) : <EOL> if self . name in kwargs : <EOL> value = kwargs . pop ( self . name ) <EOL> if value is None : <EOL> value = '<STR_LIT:null>' <EOL> kwargs [ self . attname ] = value <EOL> def formfield ( self , * args , ** kwargs ) : <EOL> kwargs [ "<STR_LIT>" ] = JSONFormField <EOL> return super ( JSONField , self ) . formfield ( * args , ** kwargs ) <EOL> class SlugMultipleChoiceField ( models . Field ) : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = models . SubfieldBase <EOL> description = _ ( "<STR_LIT>" ) <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def to_python ( self , value ) : <EOL> if not value : <EOL> return [ ] <EOL> if isinstance ( value , list ) : <EOL> return value <EOL> return value . split ( '<STR_LIT:U+002C>' ) <EOL> def get_prep_value ( self , value ) : <EOL> return '<STR_LIT:U+002C>' . join ( value ) <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { <EOL> '<STR_LIT>' : forms . CheckboxSelectMultiple , <EOL> '<STR_LIT>' : self . get_choices ( include_blank = False ) , <EOL> '<STR_LIT:label>' : capfirst ( self . verbose_name ) , <EOL> '<STR_LIT>' : not self . blank , <EOL> '<STR_LIT>' : self . help_text <EOL> } <EOL> if self . has_default ( ) : <EOL> if callable ( self . default ) : <EOL> defaults [ '<STR_LIT>' ] = self . default <EOL> defaults [ '<STR_LIT>' ] = True <EOL> else : <EOL> defaults [ '<STR_LIT>' ] = self . get_default ( ) <EOL> for k in kwargs . keys ( ) : <EOL> if k not in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:label>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) : <EOL> del kwargs [ k ] <EOL> defaults . update ( kwargs ) <EOL> form_class = forms . TypedMultipleChoiceField <EOL> return form_class ( ** defaults ) <EOL> def validate ( self , value , model_instance ) : <EOL> invalid_values = [ ] <EOL> for val in value : <EOL> try : <EOL> validate_slug ( val ) <EOL> except ValidationError : <EOL> invalid_values . append ( val ) <EOL> if invalid_values : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] % invalid_values ) <EOL> def _get_choices ( self ) : <EOL> if isinstance ( self . _choices , RegistryIterator ) : <EOL> return self . _choices . copy ( ) <EOL> elif hasattr ( self . _choices , '<STR_LIT>' ) : <EOL> choices , self . _choices = itertools . tee ( self . _choices ) <EOL> return choices <EOL> else : <EOL> return self . _choices <EOL> choices = property ( _get_choices ) <EOL> try : <EOL> from south . modelsinspector import add_introspection_rules <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> add_introspection_rules ( [ ] , [ "<STR_LIT>" ] ) <EOL> add_introspection_rules ( [ ] , [ "<STR_LIT>" ] ) <EOL> add_introspection_rules ( [ ] , [ "<STR_LIT>" ] ) </s>
