This repository has been archived by the owner on Mar 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
script.py
361 lines (313 loc) · 13.6 KB
/
script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import os, sys, platform
try:
setup_file = __import__(sys.argv[1])
except:
print("Try with an argument")
exit()
from project import Field
from project import Model
from project import ViewSet
from project import Route
from project import App
from template import Template
def find_name_in_list(list, name):
i = 0
for item in list:
if item.name == name:
return i
i += 1
return None
class ProjectCommand: # class about project initializing, like commands
def __init__(self, prj_name):
self.prj_name = prj_name
def setup_venv(self):
command = ""
if platform.system() == 'Windows':
command = "python "
else:
command = "python3 "
command += "-m venv myvenv"
os.system(command)
def install_requirements(
self): # pip install django djangorestframework django-cors-headers
os.system("pip install --upgrade pip")
os.system("pip install django djangorestframework django-cors-headers")
def start_project(self):
os.system("django-admin startproject " + str(self.prj_name))
def create_app(self, app_name):
origin_directory = os.getcwd()
os.chdir(f"{os.getcwd()}/{self.prj_name}/")
os.system(f"python manage.py startapp {app_name}")
open(f"{os.getcwd()}/{app_name}/urls.py", 'w').close()
os.chdir(origin_directory)
def makemigrations(self):
origin_directory = os.getcwd()
os.chdir(f"{os.getcwd()}/{self.prj_name}/")
os.system(f"python manage.py makemigrations")
os.chdir(origin_directory)
def migrate(self):
origin_directory = os.getcwd()
os.chdir(f"{os.getcwd()}/{self.prj_name}/")
os.system(f"python manage.py migrate")
os.chdir(origin_directory)
def run_server(self):
origin_directory = os.getcwd()
os.chdir(f"{os.getcwd()}/{self.prj_name}/")
os.system(f"python manage.py runserver")
os.chdir(origin_directory)
class ProjectConfigurations:
def __init__(self, project_name):
self.settings_file_path = f"{os.getcwd()}/{project_name}/{project_name}/settings.py"
self.urls_file_path = f"{os.getcwd()}/{project_name}/{project_name}/urls.py"
self.permissions_file_path = f"{os.getcwd()}/{project_name}/{project_name}/permissions.py"
def load_settings(self):
settings_file = open(self.settings_file_path, 'r')
self.settings = settings_file.read()
settings_file.close()
def set_language_code(self, language):
self.settings = self.settings.replace("LANGUAGE_CODE = en-us",
f"LANGUAGE_CODE = '{language}'")
def set_timezone(self, timezone):
self.settings = self.settings.replace("TIME_ZONE = 'UTC'",
f"TIMEZONE = '{timezone}'")
def load_urls(self):
urls_file = open(self.urls_file_path, 'r')
self.urls = urls_file.read()
urls_file.close()
def add_module(self, module_name):
installed_apps_index = self.settings.find("INSTALLED_APPS")
last_module_index = self.settings.find(
"]", installed_apps_index) - 1 # except itself and the '\n
self.settings = self.settings[:
last_module_index] + "\n\t# added by fastdj\n\t'" + module_name + "'," + self.settings[
last_module_index:]
def add_installed_modules(self): # djangorestframework django-cors-headers
modules = ['rest_framework', 'corsheaders']
for module in modules:
self.add_module(module)
def add_token_login_model(self): # token login model
self.settings += "\n# added by fastdj\nREST_FRAMEWORK = {\n\t'DEFAULT_AUTHENTICATION_CLASSES': (\n\t\t'rest_framework.authentication.BasicAuthentication',\n\t\t'rest_framework.authentication.TokenAuthentication',\n\t),\n}\n"
self.add_module('rest_framework.authtoken')
def set_cross_origin_all(self): # cors origin allow all
self.settings += "\n# added by fastdj\nCORS_ORIGIN_ALLOW_ALL = True\nCORS_ALLOW_CREDENTIALS = True\n"
def set_allowed_hosts_all(self): # set allowed hosts to all
self.settings = self.settings.replace("ALLOWED_HOSTS = []",
"ALLOWED_HOSTS = ['*']")
def add_url_include_module(self):
self.urls = self.urls.replace("from django.urls import path",
"from django.urls import path, include")
def add_url_path(self, app_name):
urlpatterns_index = self.urls.find("urlpatterns")
last_path_index = self.urls.find(
"]", urlpatterns_index) - 1 # except itself and the '\n
self.urls = f"{self.urls[:last_path_index]}\n\t# added by fastdj\n\tpath('{app_name}/', include('{app_name}.urls'), name='{app_name}'),{self.urls[last_path_index:]}\n"
def save_permissions(self):
file = open(self.permissions_file_path, 'w')
file.write("""from rest_framework import permissions
class IsOwnerOrReadOnly(permissions.BasePermission):
def has_object_permission(self, request, view, obj):
if request.method in permissions.SAFE_METHODS:
return True
return obj.owner == request.user""")
file.close()
def save_settings(self):
file = open(self.settings_file_path, 'w')
file.write(self.settings)
file.close()
def save_urls(self):
file = open(self.urls_file_path, 'w')
file.write(self.urls)
file.close()
class Project:
project_name = setup_file.project_name
user_model = setup_file.user_model
def __init__(self):
self.apps = list()
for app in setup_file.apps.keys():
self.apps.append(App(app, self.project_name))
self.apps.append(App('custom_user', self.project_name))
self.cmd = ProjectCommand(self.project_name)
self.confs = ProjectConfigurations(self.project_name)
self.timezone = None
self.language = None
self.use_token_auth = self.user_model.get('use_token_auth', True)
try:
self.timezone = setup_file.timezone
except:
pass
try:
self.language = setup_file.language
except:
pass
def menu(self):
print("0. Create a new venv")
print("1. Create your project")
option_choice = int(input("Type here: "))
if (option_choice == 0):
self.create_venv()
elif (option_choice == 1):
self.create_project()
self.create_apps()
self.register_apps()
# disabled till writing url feature is done
#self.makemigrations_and_migrate()
def create_venv(self):
self.cmd.setup_venv()
if platform.system() == "windows":
print(
"Type 'call myvenv/scripts/activate', re-execute the script and type 1!"
)
return
print(
"Type 'source myvenv/bin/activate', re-execute the script and type 1!"
)
def create_project(self):
self.cmd.install_requirements()
# setting up project
self.cmd.start_project()
self.confs.load_settings()
self.confs.load_urls()
self.confs.add_installed_modules()
self.confs.set_cross_origin_all()
self.confs.set_allowed_hosts_all()
if self.use_token_auth == True:
self.confs.add_token_login_model()
if not self.timezone == None:
self.confs.set_timezone(self.timezone)
if not self.language == None:
self.confs.set_language_code(self.language)
def create_apps(self):
for app in self.apps:
self.cmd.create_app(app.name)
def get_serialized_field(
self, app_name, field_name, field_specs
): # get informations from object field_specs and return it as serialized object Field
return Field(
field_name,
field_specs.get('field'),
app_name=app_name,
serializers=field_specs.get('serializers', {}),
options=field_specs.get('options', list()),
template=field_specs.get('template'),
choices=field_specs.get('choices'),
)
def register_apps(self):
self.confs.add_url_include_module()
for app in self.apps:
# add apps to confs and urls
self.confs.add_module(app.name)
self.confs.add_url_path(app.name)
# register model spces to object
if app.name == 'custom_user':
model = Model("Profile")
fields_name = field_specs = self.user_model.get(
'fields').keys()
for field_name in fields_name:
field_specs = self.user_model.get('fields').get(
field_name) # test required, not done yet
model.add_field(
self.get_serialized_field(app.name, field_name,
field_specs))
model.add_field(
Field(
"user",
"OneToOneField",
options=[
"settings.AUTH_USER_MODEL",
"on_delete=models.CASCADE"
],
not_to_serialize=True,
))
app.add_model(model)
else:
models_name = setup_file.apps[app.name]['models'].keys()
for model_name in models_name:
model = Model(model_name[0].upper() + model_name[1:])
fields_name = setup_file.apps[
app.name]['models'][model_name].keys()
for field_name in fields_name:
field_specs = setup_file.apps[
app.name]['models'][model_name][field_name]
model.add_field(
self.get_serialized_field(app.name, field_name,
field_specs))
app.add_model(model)
# register, token view specs to object
if app.name == 'custom_user':
app.add_view(
ViewSet(
self.project_name,
app.name,
app.models[find_name_in_list(app.models, "Profile")],
Template.user_profile_view,
))
app.add_route(
Route("",
viewset_name_to_route="ProfileAPIView",
template=Template.user_profile_view,
arg_type=None))
token_route = Route("")
token_route.code = " path('auth/', drf_views.obtain_auth_token),\n"
token_route.is_raw = True
app.add_route(token_route)
if self.user_model.get('set_visibility_public', True):
app.add_view(
ViewSet(
self.project_name,
app.name,
app.models[find_name_in_list(
app.models, "Profile")],
Template.user_profile_detail_view,
))
app.add_route(
Route("",
viewset_name_to_route="ProfileDetail",
template=Template.user_profile_detail_view,
arg_type=str))
if self.user_model.get('allow_register', True):
app.add_view(
ViewSet(
self.project_name,
app.name,
app.models[find_name_in_list(
app.models, "Profile")],
Template.user_register_view,
))
app.add_route(Route("register", arg_type=None))
else:
for view_name in setup_file.apps[app.name]['views'].keys():
view = setup_file.apps[app.name]['views'].get(view_name)
target_model_index = find_name_in_list(
app.models, view.get('model'))
app.add_view(
ViewSet(
self.project_name,
app.name,
app.models[target_model_index],
view.get('template'),
name=view_name,
options=view.get('options', list()),
permissions=view.get('permissions', ""),
url_getters=view.get('url_getters', ""),
))
app.add_route(
Route(view_name, template=view.get('template')))
app.save_models()
app.save_serializers()
app.save_views()
app.save_forms()
app.save_admin_file()
app.save_routings()
self.confs.save_settings()
self.confs.save_permissions()
self.confs.save_urls()
self.makemigrations_and_migrate()
self.cmd.run_server()
# save changes
def makemigrations_and_migrate(self):
self.cmd.makemigrations()
self.cmd.migrate()
def main():
project = Project()
project.menu()
main()