<s> import random <EOL> import os <EOL> import decimal <EOL> import logging <EOL> import datetime <EOL> from django . conf import settings <EOL> from django . contrib . webdesign import lorem_ipsum <EOL> from django . core . files . base import ContentFile <EOL> from django . db . models import URLField , get_model <EOL> from django . template . defaultfilters import slugify <EOL> from dilla import spam <EOL> try : <EOL> from cStringIO import StringIO <EOL> except ImportError : <EOL> from StringIO import StringIO <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> dictionary = getattr ( settings , '<STR_LIT>' , "<STR_LIT>" ) <EOL> if os . path . exists ( dictionary ) and not getattr ( settings , '<STR_LIT>' , False ) : <EOL> d = open ( dictionary , "<STR_LIT:r>" ) . readlines ( ) <EOL> _random_words = lambda n : "<STR_LIT:U+0020>" . join ( [ random . choice ( d ) . lower ( ) . rstrip ( ) for i in range ( n ) ] ) <EOL> _random_paragraph = lambda : _random_words ( <NUM_LIT:30> ) . capitalize ( ) <EOL> _random_paragraphs = lambda n : "<STR_LIT>" . join ( [ _random_paragraph ( ) for i in range ( n ) ] ) <EOL> else : <EOL> _random_words = lorem_ipsum . words <EOL> _random_paragraphs = lambda n : "<STR_LIT>" . join ( lorem_ipsum . paragraphs ( n ) ) <EOL> @ spam . global_handler ( '<STR_LIT>' ) <EOL> def random_words ( record , field ) : <EOL> if isinstance ( field , URLField ) : <EOL> return "<STR_LIT>" % tuple ( _random_words ( <NUM_LIT:4> ) . split ( "<STR_LIT:U+0020>" ) ) <EOL> max_length = field . max_length <EOL> words = _random_words ( <NUM_LIT:3> ) . decode ( '<STR_LIT:utf8>' ) . encode ( '<STR_LIT:utf8>' ) <EOL> if max_length < len ( words ) : <EOL> return words [ : max_length ] <EOL> return words <EOL> @ spam . global_handler ( '<STR_LIT>' ) <EOL> def random_text ( record , field ) : <EOL> text_content = _random_paragraphs ( <NUM_LIT:4> ) <EOL> if field . max_length : <EOL> text_content = text_content [ : field . max_length ] <EOL> return text_content <EOL> @ spam . global_handler ( '<STR_LIT>' ) <EOL> def random_ip ( record , field ) : <EOL> return "<STR_LIT:.>" . join ( [ str ( random . randrange ( <NUM_LIT:0> , <NUM_LIT:255> ) ) for i in range ( <NUM_LIT:0> , <NUM_LIT:4> ) ] ) <EOL> @ spam . global_handler ( '<STR_LIT>' ) <EOL> def random_slug ( record , field ) : <EOL> slug = random_words ( record , field ) . replace ( "<STR_LIT:U+0020>" , "<STR_LIT:->" ) <EOL> return '<STR_LIT>' . join ( ch for ch in slug if ch . isalnum ( ) or ch == '<STR_LIT:->' ) <EOL> @ spam . global_handler ( '<STR_LIT>' ) <EOL> @ spam . global_handler ( '<STR_LIT>' ) <EOL> def random_bool ( record , field ) : <EOL> return bool ( random . randint ( <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> @ spam . global_handler ( '<STR_LIT>' ) <EOL> def random_email ( record , field ) : <EOL> return "<STR_LIT>" % ( slugify ( _random_words ( <NUM_LIT:1> ) ) , <EOL> slugify ( _random_words ( <NUM_LIT:1> ) ) , <EOL> random . choice ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> ) <EOL> @ spam . global_handler ( '<STR_LIT>' ) <EOL> @ spam . global_handler ( '<STR_LIT>' ) <EOL> def random_int ( record , field ) : <EOL> return random . randint ( - <NUM_LIT> , <NUM_LIT> ) <EOL> @ spam . global_handler ( '<STR_LIT>' ) <EOL> def random_bigint ( record , field ) : <EOL> return random . randint ( - <NUM_LIT:10> ** <NUM_LIT:10> , <NUM_LIT:10> ** <NUM_LIT:10> ) <EOL> @ spam . global_handler ( '<STR_LIT>' ) <EOL> def random_decimal ( record , field ) : <EOL> return decimal . Decimal ( str ( random . random ( ) + random . randint ( <NUM_LIT:1> , <NUM_LIT:20> ) ) ) <EOL> @ spam . global_handler ( '<STR_LIT>' ) <EOL> def random_posint ( record , field ) : <EOL> return random . randint ( <NUM_LIT:0> , <NUM_LIT> ) <EOL> @ spam . global_handler ( '<STR_LIT>' ) <EOL> @ spam . global_handler ( '<STR_LIT>' ) <EOL> @ spam . global_handler ( '<STR_LIT>' ) <EOL> def random_datetime ( record , field ) : <EOL> """<STR_LIT>""" <EOL> random_minutes = random . randint ( - <NUM_LIT> , <NUM_LIT> ) <EOL> return datetime . datetime . now ( ) + datetime . timedelta ( minutes = random_minutes ) <EOL> @ spam . global_handler ( '<STR_LIT>' ) <EOL> def random_file ( record , field ) : <EOL> """<STR_LIT>""" <EOL> def _random_image ( field ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> from identicon import identicon <EOL> name = "<STR_LIT>" % random_slug ( record , field ) <EOL> icon = identicon . render_identicon ( random . randint ( <NUM_LIT:5> ** <NUM_LIT:5> , <NUM_LIT:10> ** <NUM_LIT:10> ) , random . randint ( <NUM_LIT:64> , <NUM_LIT> ) ) <EOL> tmp_file = StringIO ( ) <EOL> icon . save ( tmp_file , '<STR_LIT>' ) <EOL> name = field . generate_filename ( record . obj , name ) <EOL> saved_name = field . storage . save ( name , ContentFile ( tmp_file . getvalue ( ) ) ) <EOL> return saved_name <EOL> def _random_textfile ( field ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> name = "<STR_LIT>" % random_slug ( record , field ) <EOL> name = field . generate_filename ( record . obj , name ) <EOL> saved_name = field . storage . save ( name , ContentFile ( _random_words ( <NUM_LIT:10> ) ) ) <EOL> return saved_name <EOL> try : <EOL> from django . db . models import ImageField <EOL> if isinstance ( field , ImageField ) : <EOL> return _random_image ( field ) <EOL> return _random_textfile ( field ) <EOL> except ImportError , e : <EOL> log . warn ( e ) <EOL> return _random_textfile ( field ) <EOL> @ spam . global_handler ( '<STR_LIT>' ) <EOL> def random_fk ( record , field , limit = None ) : <EOL> Related = field . rel . to <EOL> log . debug ( '<STR_LIT>' % Related ) <EOL> models_to_exclude = getattr ( settings , '<STR_LIT>' , None ) <EOL> if models_to_exclude : <EOL> try : <EOL> excluded_models = [ get_model ( * m . split ( '<STR_LIT:.>' ) ) for m in models_to_exclude ] <EOL> if Related in excluded_models : <EOL> log . info ( '<STR_LIT>' % ( Related , field . name ) ) <EOL> return None <EOL> except ValueError : <EOL> pass <EOL> try : <EOL> query = Related . objects . all ( ) . order_by ( '<STR_LIT:?>' ) <EOL> if field . rel . limit_choices_to : <EOL> log . debug ( '<STR_LIT>' % field ) <EOL> query . filter ( ** field . rel . limit_choices_to ) <EOL> if limit : <EOL> return query [ : limit ] <EOL> return query [ <NUM_LIT:0> ] <EOL> except IndexError , e : <EOL> log . warn ( '<STR_LIT>' % field . name ) <EOL> return None <EOL> except Exception , e : <EOL> log . critical ( str ( e ) ) <EOL> raise <EOL> @ spam . global_handler ( '<STR_LIT>' ) <EOL> def random_manytomany ( record , field ) : <EOL> return random_fk ( record , field , random . randint ( <NUM_LIT:1> , <NUM_LIT:5> ) ) </s>
