Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history


# Conflicts:
#	django_excel_to_model/django_app_generator.py
  • Loading branch information
weijia committed Aug 20, 2016
2 parents 7931eb8 + 767d8c8 commit 3c29e8f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
16 changes: 14 additions & 2 deletions django_excel_to_model/django_app_generator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os

from ufs_tools.app_tools import get_executable
from ufs_tools.app_tools import get_executable_folder
from ufs_tools.folder_tool import ensure_dir


Expand All @@ -9,14 +9,23 @@ def __init__(self, app_name):
super(DjangoAppGenerator, self).__init__()
self.app_name = app_name
self.app_module_name = app_name.replace("-", "_")
self.module_path = None
self.admin_file_content = '''import models
from djangoautoconf.auto_conf_admin_tools.admin_register import AdminRegister
factory = AdminRegister()
factory.register_all_model(models)
'''
self.urls_content = '''from djangoautoconf.model_utils.url_for_models import add_all_urls
from django.conf.urls import patterns
import models
urlpatterns = patterns('',)
add_all_urls(urlpatterns, models)
'''

def create_default_structure(self):
repo_root = os.path.abspath(get_executable())
repo_root = os.path.abspath(get_executable_folder())
target_repo_folder = os.path.join(repo_root, "external_app_repos/auto-generated-apps")
target_app_folder = os.path.join(target_repo_folder, self.app_name)
ensure_dir(target_repo_folder)
Expand All @@ -26,10 +35,13 @@ def create_default_structure(self):
os.mkdir(target_app_folder)
self.module_path = os.path.join(target_app_folder, self.app_module_name)
os.mkdir(self.module_path)
os.mkdir(os.path.join(self.module_path, "migrations"))

self.create_module_file("admin.py", self.admin_file_content)
self.create_module_file("urls.py", self.urls_content)
self.create_module_file("default_settings.py")
self.create_module_file("__init__.py")
self.create_module_file("migrations/__init__.py")

def create_module_file(self, target_file, file_content=""):
admin_file_path = os.path.join(self.module_path, target_file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def create_model(self, data_start_row=1):
mapping = None
exec code

res = [u"", u"",
res = [u"", u"", u"# @python_2_unicode_compatible"
u"class %s(models.Model):" % self.class_name]
for row_data_dict in self.worksheet.enumerate(data_start_row):
for key in attr_order:
Expand All @@ -53,6 +53,9 @@ def create_model(self, data_start_row=1):
break
res.append(self.indents + u'data_import_id = models.CharField(max_length=TEXT_LENGTH_128, '
u'help_text=_("Id for this import"), null=True, blank=True)')
res.append(self.indents + u'')
res.append(self.indents + u'# def __str__(self):')
res.append(self.indents + u'# ' + self.indents + u'return "%s" % self.name')
self.model_code_lines = res
return u"\n".join(res)

Expand Down Expand Up @@ -113,6 +116,7 @@ def create_attr_list_code_lines(self):
def get_mapping_and_attr(self):
res = [u"from django.db import models",
u"from django.utils.translation import ugettext as _",
u"from six import python_2_unicode_compatible",
u"from djangoautoconf.model_utils.len_definitions import %s, TEXT_LENGTH_128" % self.field_len_definition,
u"",
u""]
Expand Down

0 comments on commit 3c29e8f

Please sign in to comment.