<s> from __future__ import absolute_import , unicode_literals <EOL> import collections <EOL> from importlib import import_module <EOL> from django import forms <EOL> from django . core import checks <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . template . loader import render_to_string <EOL> from django . utils import six <EOL> from django . utils . encoding import force_text , python_2_unicode_compatible <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . text import capfirst <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class BaseBlock ( type ) : <EOL> def __new__ ( mcs , name , bases , attrs ) : <EOL> meta_class = attrs . pop ( '<STR_LIT:Meta>' , None ) <EOL> cls = super ( BaseBlock , mcs ) . __new__ ( mcs , name , bases , attrs ) <EOL> meta_class_bases = [ meta_class ] + [ getattr ( base , '<STR_LIT>' , None ) <EOL> for base in bases ] <EOL> meta_class_bases = tuple ( filter ( bool , meta_class_bases ) ) <EOL> cls . _meta_class = type ( str ( name + '<STR_LIT:Meta>' ) , meta_class_bases , { } ) <EOL> return cls <EOL> class Block ( six . with_metaclass ( BaseBlock , object ) ) : <EOL> name = '<STR_LIT>' <EOL> creation_counter = <NUM_LIT:0> <EOL> TEMPLATE_VAR = '<STR_LIT:value>' <EOL> class Meta ( object ) : <EOL> label = None <EOL> icon = "<STR_LIT>" <EOL> classname = None <EOL> """<STR_LIT>""" <EOL> dependencies = [ ] <EOL> def __new__ ( cls , * args , ** kwargs ) : <EOL> obj = super ( Block , cls ) . __new__ ( cls ) <EOL> obj . _constructor_args = ( args , kwargs ) <EOL> return obj <EOL> def all_blocks ( self ) : <EOL> """<STR_LIT>""" <EOL> result = [ self ] <EOL> for dep in self . dependencies : <EOL> result . extend ( dep . all_blocks ( ) ) <EOL> return result <EOL> def all_media ( self ) : <EOL> media = forms . Media ( ) <EOL> for block in self . all_blocks ( ) : <EOL> media += block . media <EOL> return media <EOL> def all_html_declarations ( self ) : <EOL> declarations = filter ( bool , [ block . html_declarations ( ) for block in self . all_blocks ( ) ] ) <EOL> return mark_safe ( '<STR_LIT:\n>' . join ( declarations ) ) <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . meta = self . _meta_class ( ) <EOL> for attr , value in kwargs . items ( ) : <EOL> setattr ( self . meta , attr , value ) <EOL> self . creation_counter = Block . creation_counter <EOL> Block . creation_counter += <NUM_LIT:1> <EOL> self . definition_prefix = '<STR_LIT>' % self . creation_counter <EOL> self . label = self . meta . label or '<STR_LIT>' <EOL> def set_name ( self , name ) : <EOL> self . name = name <EOL> if not self . meta . label : <EOL> self . label = capfirst ( force_text ( name ) . replace ( '<STR_LIT:_>' , '<STR_LIT:U+0020>' ) ) <EOL> @ property <EOL> def media ( self ) : <EOL> return forms . Media ( ) <EOL> def html_declarations ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' <EOL> def js_initializer ( self ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def render_form ( self , value , prefix = '<STR_LIT>' , errors = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' % self . __class__ ) <EOL> def value_from_datadict ( self , data , files , prefix ) : <EOL> raise NotImplementedError ( '<STR_LIT>' % self . __class__ ) <EOL> def bind ( self , value , prefix = None , errors = None ) : <EOL> """<STR_LIT>""" <EOL> return BoundBlock ( self , value , prefix = prefix , errors = errors ) <EOL> def get_default ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . meta . default <EOL> def prototype_block ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . bind ( self . get_default ( ) , '<STR_LIT>' ) <EOL> def clean ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return value <EOL> def to_python ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return value <EOL> def get_prep_value ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return value <EOL> def get_context ( self , value ) : <EOL> return { <EOL> '<STR_LIT>' : value , <EOL> self . TEMPLATE_VAR : value , <EOL> } <EOL> def render ( self , value ) : <EOL> """<STR_LIT>""" <EOL> template = getattr ( self . meta , '<STR_LIT>' , None ) <EOL> if template : <EOL> return render_to_string ( template , self . get_context ( value ) ) <EOL> else : <EOL> return self . render_basic ( value ) <EOL> def render_basic ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return force_text ( value ) <EOL> def get_searchable_content ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return [ ] <EOL> def check ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return [ ] <EOL> def _check_name ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> errors = [ ] <EOL> if not self . name : <EOL> errors . append ( checks . Error ( <EOL> "<STR_LIT>" % self . name , <EOL> hint = "<STR_LIT>" , <EOL> obj = kwargs . get ( '<STR_LIT>' , self ) , <EOL> id = '<STR_LIT>' , <EOL> ) ) <EOL> if '<STR_LIT:U+0020>' in self . name : <EOL> errors . append ( checks . Error ( <EOL> "<STR_LIT>" % self . name , <EOL> hint = "<STR_LIT>" , <EOL> obj = kwargs . get ( '<STR_LIT>' , self ) , <EOL> id = '<STR_LIT>' , <EOL> ) ) <EOL> if '<STR_LIT:->' in self . name : <EOL> errors . append ( checks . Error ( <EOL> "<STR_LIT>" % self . name , <EOL> "<STR_LIT>" , <EOL> obj = kwargs . get ( '<STR_LIT>' , self ) , <EOL> id = '<STR_LIT>' , <EOL> ) ) <EOL> if self . name and self . name [ <NUM_LIT:0> ] . isdigit ( ) : <EOL> errors . append ( checks . Error ( <EOL> "<STR_LIT>" % self . name , <EOL> "<STR_LIT>" , <EOL> obj = kwargs . get ( '<STR_LIT>' , self ) , <EOL> id = '<STR_LIT>' , <EOL> ) ) <EOL> return errors <EOL> def id_for_label ( self , prefix ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def deconstruct ( self ) : <EOL> module_name = self . __module__ <EOL> name = self . __class__ . __name__ <EOL> module = import_module ( module_name ) <EOL> if not hasattr ( module , name ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( name , module_name ) ) <EOL> try : <EOL> path = module . DECONSTRUCT_ALIASES [ self . __class__ ] <EOL> except ( AttributeError , KeyError ) : <EOL> path = '<STR_LIT>' % ( module_name , name ) <EOL> return ( <EOL> path , <EOL> self . _constructor_args [ <NUM_LIT:0> ] , <EOL> self . _constructor_args [ <NUM_LIT:1> ] , <EOL> ) <EOL> def __eq__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( other , Block ) : <EOL> return False <EOL> return ( self . name == other . name ) and ( self . deconstruct ( ) == other . deconstruct ( ) ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> __hash__ = None <EOL> @ python_2_unicode_compatible <EOL> class BoundBlock ( object ) : <EOL> def __init__ ( self , block , value , prefix = None , errors = None ) : <EOL> self . block = block <EOL> self . value = value <EOL> self . prefix = prefix <EOL> self . errors = errors <EOL> def render_form ( self ) : <EOL> return self . block . render_form ( self . value , self . prefix , errors = self . errors ) <EOL> def render ( self ) : <EOL> return self . block . render ( self . value ) <EOL> def id_for_label ( self ) : <EOL> return self . block . id_for_label ( self . prefix ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . block . render ( self . value ) <EOL> class DeclarativeSubBlocksMetaclass ( BaseBlock ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( mcs , name , bases , attrs ) : <EOL> current_blocks = [ ] <EOL> for key , value in list ( attrs . items ( ) ) : <EOL> if isinstance ( value , Block ) : <EOL> current_blocks . append ( ( key , value ) ) <EOL> value . set_name ( key ) <EOL> attrs . pop ( key ) <EOL> current_blocks . sort ( key = lambda x : x [ <NUM_LIT:1> ] . creation_counter ) <EOL> attrs [ '<STR_LIT>' ] = collections . OrderedDict ( current_blocks ) <EOL> new_class = ( super ( DeclarativeSubBlocksMetaclass , mcs ) . __new__ ( <EOL> mcs , name , bases , attrs ) ) <EOL> base_blocks = collections . OrderedDict ( ) <EOL> for base in reversed ( new_class . __mro__ ) : <EOL> if hasattr ( base , '<STR_LIT>' ) : <EOL> base_blocks . update ( base . declared_blocks ) <EOL> for attr , value in base . __dict__ . items ( ) : <EOL> if value is None and attr in base_blocks : <EOL> base_blocks . pop ( attr ) <EOL> new_class . base_blocks = base_blocks <EOL> return new_class <EOL> class BlockWidget ( forms . Widget ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , block_def , attrs = None ) : <EOL> super ( BlockWidget , self ) . __init__ ( attrs = attrs ) <EOL> self . block_def = block_def <EOL> def render_with_errors ( self , name , value , attrs = None , errors = None ) : <EOL> bound_block = self . block_def . bind ( value , prefix = name , errors = errors ) <EOL> js_initializer = self . block_def . js_initializer ( ) <EOL> if js_initializer : <EOL> js_snippet = """<STR_LIT>""" % ( js_initializer , name ) <EOL> else : <EOL> js_snippet = '<STR_LIT>' <EOL> return mark_safe ( bound_block . render_form ( ) + js_snippet ) <EOL> def render ( self , name , value , attrs = None ) : <EOL> return self . render_with_errors ( name , value , attrs = attrs , errors = None ) <EOL> @ property <EOL> def media ( self ) : <EOL> return self . block_def . all_media ( ) <EOL> def value_from_datadict ( self , data , files , name ) : <EOL> return self . block_def . value_from_datadict ( data , files , name ) <EOL> class BlockField ( forms . Field ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , block = None , ** kwargs ) : <EOL> if block is None : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" ) <EOL> self . block = block <EOL> if '<STR_LIT>' not in kwargs : <EOL> kwargs [ '<STR_LIT>' ] = BlockWidget ( block ) <EOL> super ( BlockField , self ) . __init__ ( ** kwargs ) <EOL> def clean ( self , value ) : <EOL> return self . block . clean ( value ) <EOL> DECONSTRUCT_ALIASES = { <EOL> Block : '<STR_LIT>' , <EOL> } </s>
