Package ajango :: Package generator :: Module project_manager
[hide private]
[frames] | no frames]

Source Code for Module ajango.generator.project_manager

 1  ########################################################################### 
 2  #                                                                         # 
 3  #  Copyright (C) 2016  Rafal Kobel <rafyco1@gmail.com>                    # 
 4  #                                                                         # 
 5  #  This program is free software: you can redistribute it and/or modify   # 
 6  #  it under the terms of the GNU General Public License as published by   # 
 7  #  the Free Software Foundation, either version 3 of the License, or      # 
 8  #  (at your option) any later version.                                    # 
 9  #                                                                         # 
10  #  This program is distributed in the hope that it will be useful,        # 
11  #  but WITHOUT ANY WARRANTY; without even the implied warranty of         # 
12  #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the           # 
13  #  GNU General Public License for more details.                           # 
14  #                                                                         # 
15  #  You should have received a copy of the GNU General Public License      # 
16  #  along with this program.  If not, see <http://www.gnu.org/licenses/>.  # 
17  #                                                                         # 
18  ########################################################################### 
19  """ Pakiet zarzadzajacy projektem Django. """ 
20   
21  import os.path 
22  from django.core.management.base import CommandError 
23  from ajango.core                 import add_to_python_array 
24  from ajango.core                 import file_to_buffor, buffor_to_file 
25   
26 -class ProjectManager(object):
27 """ Klasa zarzadzajaca projektem Django. """
28 - def __init__(self, project_name):
29 if not os.path.isdir(project_name): 30 raise CommandError("%r: Cannot find project" % project_name) 31 self.project_name = project_name 32 self._verify_file('settings.py') 33 self._verify_file('urls.py') 34 self._verify_file('wsgi.py') 35 self.apps = [] 36 self.urls = [] 37 self.is_global_url = False
38 - def _verify_file(self, file_skeleton):
39 """ Sprawdzenie czy istnieje plik. """ 40 search_file = self.project_name + '/' + file_skeleton 41 if not os.path.isfile(search_file): 42 raise CommandError("Cannot find file: %r" % search_file)
43 - def add_application(self, app):
44 """ Dodanie aplikacji do pliku settingsow projektu. """ 45 self.apps.append(app)
46 - def add_url(self, viewname, appname):
47 """ Add view url. """ 48 self.urls.append("url(r'^%s/%s/', %s.views.%s, name=%r)," % 49 (appname, viewname, appname, viewname, viewname))
50 - def add_main_view_url(self, app):
51 """ Zarejestrowanie widoku jako glowny widok aplikacji. """ 52 appname = app.get_name() 53 viewname = app.get_main_view().get_name() 54 self.urls.append("url(r'^%s$', %s.views.%s, name='%s_main')," % 55 (appname, appname, viewname, appname))
56 - def add_main_url(self, app):
57 """ Zarejestrowanie glownego ekranu dla aplikacji. """ 58 if app == None: 59 raise CommandError("Cannot set 'None' value as application") 60 if self.is_global_url: 61 raise CommandError("Cannot set two or more main application to url") 62 self.is_global_url = True 63 viewname = app.get_main_view().get_name() 64 self.urls.append("url(r'^$', %s.views.%s, name='main')," % 65 (app.get_name(), viewname))
66 - def execute_url(self):
67 """ Dodanie odpowiednich adresow url do pliku. """ 68 address = self.project_name + "/urls.py" 69 buffor = file_to_buffor(address) 70 import_str = "" 71 for app in self.apps: 72 import_str += "import %s.views\n" % app 73 buffor = import_str + buffor 74 url_str = "" 75 for url in self.urls: 76 url_str += "\n %s" % url 77 buffor = add_to_python_array(buffor, "urlpatterns", url_str) 78 buffor_to_file(address, buffor)
79 - def execute_settings(self):
80 """ dodanie aplikacji do pliku z ustawieniami settings.py""" 81 address = self.project_name + "/settings.py" 82 buffor = file_to_buffor(address) 83 app_str = "" 84 for app in self.apps: 85 app_str += "\n '%s'," % app 86 buffor = add_to_python_array(buffor, "INSTALLED_APPS", app_str) 87 buffor_to_file(address, buffor)
88 - def execute(self):
89 """ Wykonanie zadan obiektu. """ 90 self.execute_url() 91 self.execute_settings()
92