<s> from __future__ import unicode_literals <EOL> import re <EOL> from django . core . validators import RegexValidator <EOL> from django . db import models <EOL> from django . db . models import signals <EOL> from django . utils . encoding import python_2_unicode_compatible <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from . bbcode import get_parser <EOL> __all__ = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> _rendered_content_field_name = lambda name : '<STR_LIT>' . format ( name ) <EOL> _smiley_code_re = re . compile ( r'<STR_LIT>' ) <EOL> validate_smiley_code = RegexValidator ( _smiley_code_re , _ ( "<STR_LIT>" ) , '<STR_LIT>' ) <EOL> @ python_2_unicode_compatible <EOL> class BBCodeContent ( object ) : <EOL> def __init__ ( self , raw , rendered = None ) : <EOL> self . raw = raw <EOL> self . rendered = mark_safe ( rendered ) if rendered else rendered <EOL> def __str__ ( self ) : <EOL> return self . raw <EOL> class BBCodeTextCreator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , field ) : <EOL> self . field = field <EOL> self . rendered_field_name = _rendered_content_field_name ( self . field . name ) <EOL> def __get__ ( self , instance , type = None ) : <EOL> if instance is None : <EOL> return self . field <EOL> raw_content = instance . __dict__ [ self . field . name ] <EOL> if raw_content is None : <EOL> return None <EOL> else : <EOL> return BBCodeContent ( raw_content , rendered = getattr ( instance , self . rendered_field_name ) ) <EOL> def __set__ ( self , instance , value ) : <EOL> if isinstance ( value , BBCodeContent ) : <EOL> instance . __dict__ [ self . field . name ] = value . raw <EOL> setattr ( instance , self . rendered_field_name , value . rendered ) <EOL> else : <EOL> instance . __dict__ [ self . field . name ] = self . field . to_python ( value ) <EOL> class BBCodeTextField ( models . TextField ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . add_rendered_field = not kwargs . pop ( '<STR_LIT>' , False ) <EOL> super ( BBCodeTextField , self ) . __init__ ( * args , ** kwargs ) <EOL> def deconstruct ( self ) : <EOL> """<STR_LIT>""" <EOL> name , import_path , args , kwargs = super ( BBCodeTextField , self ) . deconstruct ( ) <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> return name , import_path , args , kwargs <EOL> def contribute_to_class ( self , cls , name ) : <EOL> self . raw_name = name <EOL> if self . add_rendered_field and not cls . _meta . abstract : <EOL> self . rendered_field_name = _rendered_content_field_name ( name ) <EOL> rendered = models . TextField ( editable = False , null = True , blank = True ) <EOL> rendered . creation_counter = self . creation_counter <EOL> cls . add_to_class ( self . rendered_field_name , rendered ) <EOL> signals . pre_save . connect ( self . process_bbcodes , sender = cls ) <EOL> super ( BBCodeTextField , self ) . contribute_to_class ( cls , name ) <EOL> self . set_descriptor_class ( cls ) <EOL> def set_descriptor_class ( self , cls ) : <EOL> setattr ( cls , self . name , BBCodeTextCreator ( self ) ) <EOL> def get_db_prep_save ( self , value , connection ) : <EOL> if isinstance ( value , BBCodeContent ) : <EOL> value = value . raw <EOL> return super ( BBCodeTextField , self ) . get_db_prep_save ( value , connection ) <EOL> def process_bbcodes ( self , signal , sender , instance = None , ** kwargs ) : <EOL> bbcode_text = getattr ( instance , self . raw_name ) <EOL> if isinstance ( bbcode_text , BBCodeContent ) : <EOL> bbcode_text = bbcode_text . raw <EOL> rendered = '<STR_LIT>' <EOL> if bbcode_text : <EOL> parser = get_parser ( ) <EOL> rendered = parser . render ( bbcode_text ) <EOL> setattr ( instance , self . rendered_field_name , rendered ) <EOL> class SmileyCodeField ( models . CharField ) : <EOL> default_validators = [ validate_smiley_code ] <EOL> description = _ ( "<STR_LIT>" ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> kwargs [ '<STR_LIT:max_length>' ] = kwargs . get ( '<STR_LIT:max_length>' , <NUM_LIT:50> ) <EOL> if '<STR_LIT>' not in kwargs : <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> super ( SmileyCodeField , self ) . __init__ ( * args , ** kwargs ) <EOL> try : <EOL> from south . modelsinspector import add_introspection_rules <EOL> add_introspection_rules ( rules = [ ( ( BBCodeTextField , ) , <EOL> [ ] , <EOL> { '<STR_LIT>' : ( '<STR_LIT>' , <EOL> { } ) } ) ] , <EOL> patterns = [ '<STR_LIT>' ] ) <EOL> add_introspection_rules ( [ ] , [ '<STR_LIT>' ] ) <EOL> except ImportError : <EOL> pass </s>
