<s> from __future__ import print_function <EOL> import os <EOL> from django . db . models . fields import SlugField <EOL> import re <EOL> from django . template . loader import get_template <EOL> from django . template import Context <EOL> from django . utils . six import iteritems <EOL> class Baker ( object ) : <EOL> """<STR_LIT>""" <EOL> def bake ( self , apps_and_models ) : <EOL> """<STR_LIT>""" <EOL> for app_label , models in iteritems ( apps_and_models ) : <EOL> model_names = { model . __name__ : self . get_field_names_for_model ( model ) for model in models } <EOL> self . create_directories ( app_label ) <EOL> self . create_init_files ( app_label , model_names . keys ( ) , models ) <EOL> self . remove_empty_startapp_files ( app_label ) <EOL> for file_name in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> file_path = "<STR_LIT>" % ( app_label , file_name ) <EOL> template_path = "<STR_LIT>" % ( file_name ) <EOL> self . create_file_from_template ( file_path , template_path , { "<STR_LIT>" : model_names } ) <EOL> for model in models : <EOL> model_attributes = self . model_attributes ( app_label , model ) <EOL> self . create_files_from_templates ( model_attributes ) <EOL> def get_field_names_for_model ( self , model ) : <EOL> """<STR_LIT>""" <EOL> return [ field . name for field in model . _meta . fields if field . name != "<STR_LIT:id>" and not <EOL> ( field . get_internal_type ( ) == "<STR_LIT>" and <EOL> ( field . auto_now is True or field . auto_now_add is True ) ) ] <EOL> def create_directories ( self , app_label ) : <EOL> """<STR_LIT>""" <EOL> for folder_name in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" % app_label ] : <EOL> directory_path = "<STR_LIT>" % ( app_label , folder_name ) <EOL> if not os . path . exists ( directory_path ) : <EOL> os . makedirs ( directory_path ) <EOL> def create_init_files ( self , app_label , model_names , models ) : <EOL> """<STR_LIT>""" <EOL> model_name_slugs = [ "<STR_LIT>" % ( self . camel_to_slug ( model_name ) ) for model_name in model_names ] <EOL> model_names_dict = { self . camel_to_slug ( model . __name__ ) : self . camel_to_slug ( self . model_name_plural ( model ) ) for <EOL> model in models } <EOL> for folder_name in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> file_path = "<STR_LIT>" % ( app_label , folder_name ) <EOL> template_path = "<STR_LIT>" % folder_name <EOL> self . create_file_from_template ( file_path , template_path , { "<STR_LIT>" : app_label , <EOL> "<STR_LIT>" : model_name_slugs , <EOL> "<STR_LIT>" : model_names_dict <EOL> } ) <EOL> def model_attributes ( self , app_label , model ) : <EOL> """<STR_LIT>""" <EOL> model_name = model . __name__ <EOL> model_name_plural = self . model_name_plural ( model ) <EOL> slug_field = self . get_unique_slug_field_name ( model ) <EOL> slug_field_name = slug_field . name if slug_field else "<STR_LIT>" <EOL> lookup_field = slug_field_name if slug_field else "<STR_LIT>" <EOL> return { <EOL> '<STR_LIT>' : app_label , <EOL> '<STR_LIT>' : model , <EOL> '<STR_LIT>' : model_name , <EOL> '<STR_LIT>' : self . camel_to_slug ( model_name ) , <EOL> '<STR_LIT>' : model_name_plural , <EOL> '<STR_LIT>' : self . camel_to_slug ( model_name_plural ) , <EOL> '<STR_LIT>' : self . get_field_names_for_model ( model ) , <EOL> '<STR_LIT>' : slug_field , <EOL> '<STR_LIT>' : slug_field_name , <EOL> '<STR_LIT>' : lookup_field <EOL> } <EOL> def create_files_from_templates ( self , model_attributes ) : <EOL> """<STR_LIT>""" <EOL> for folder_name in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> file_path = "<STR_LIT>" % ( model_attributes [ '<STR_LIT>' ] , folder_name , <EOL> model_attributes [ '<STR_LIT>' ] , folder_name ) <EOL> template_path = "<STR_LIT>" % ( folder_name ) <EOL> self . create_file_from_template ( file_path , template_path , model_attributes ) <EOL> for file_name in [ "<STR_LIT>" , "<STR_LIT:list>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> file_path = "<STR_LIT>" % ( model_attributes [ '<STR_LIT>' ] , model_attributes [ '<STR_LIT>' ] , <EOL> model_attributes [ '<STR_LIT>' ] , file_name ) <EOL> template_path = "<STR_LIT>" % ( file_name ) <EOL> self . create_file_from_template ( file_path , template_path , model_attributes ) <EOL> def create_file_from_template ( self , file_path , template_path , context_variables ) : <EOL> """<STR_LIT>""" <EOL> if os . path . exists ( file_path ) : <EOL> print ( "<STR_LIT>" + file_path + "<STR_LIT>" + "<STR_LIT>" ) <EOL> return <EOL> with open ( file_path , '<STR_LIT:w>' ) as new_file : <EOL> new_file . write ( get_template ( template_path ) . render ( Context ( context_variables ) ) ) <EOL> print ( "<STR_LIT>" + "<STR_LIT>" + file_path + "<STR_LIT>" ) <EOL> def remove_empty_startapp_files ( self , app_label ) : <EOL> """<STR_LIT>""" <EOL> for file_name in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> file_path = "<STR_LIT>" % ( app_label , file_name ) <EOL> if os . path . exists ( file_path ) : <EOL> num_lines = sum ( <NUM_LIT:1> for line in open ( file_path ) ) <EOL> if num_lines <= <NUM_LIT:4> : <EOL> os . remove ( file_path ) <EOL> def camel_to_slug ( self , name ) : <EOL> """<STR_LIT>""" <EOL> name = re . sub ( r'<STR_LIT>' , r'<STR_LIT>' , name ) . title ( ) . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) . replace ( "<STR_LIT:_>" , "<STR_LIT>" ) <EOL> s1 = re . sub ( '<STR_LIT>' , r'<STR_LIT>' , name ) <EOL> slug = re . sub ( '<STR_LIT>' , r'<STR_LIT>' , s1 ) . lower ( ) <EOL> return slug <EOL> def model_name_plural ( self , model ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( model . _meta . verbose_name_plural , str ) : <EOL> return model . _meta . verbose_name_plural <EOL> return "<STR_LIT>" % model . __name__ <EOL> def get_unique_slug_field_name ( self , model ) : <EOL> """<STR_LIT>""" <EOL> slug_fields = [ ] <EOL> for field in model . _meta . fields : <EOL> if field . unique and isinstance ( field , SlugField ) : <EOL> slug_fields . append ( field ) <EOL> if len ( slug_fields ) == <NUM_LIT:1> : <EOL> return slug_fields [ <NUM_LIT:0> ] <EOL> return None </s>
