-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick-django.py
320 lines (237 loc) · 7.17 KB
/
quick-django.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
import os
import time
import sys
import platform
# This project developed by Momin Iqbal
# Support : mefiz.com
# ================================================================================================================
# Create django project quickly single command with all necessary file like djnago app, urls.py, templates folder,
# static folder and add the default code in view.py,models.py,admin.py and create index.html
# ----------------------------------------------------------------------------------------------------------------
# How to Use quick-django
# Step 1:
# 1) copy quick-django.py
# 2) past this location C:\Users\mypcname\AppData\Local\Programs\Python\Python37\Lib
# Step 2
# open cmd in your porject folder and run this command
# python -m quick-django [project name] [app name]
# python -m quick-django myproject myproject_app
# Check Our Site : https://mefiz.com
class Create:
def html():
return """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>mefiz.com</title>
</head>
<body>
<h1>visit : <a href="https://mefiz.com">mefiz.com</a></h1>
</body>
</html>
"""
def admin_py(app_name):
return f"""
from django.contrib import admin
from {app_name}.models import MyTableName
from django.contrib.auth.models import User,Group
# Remove Group and User Table in Admin Panel
# admin.site.unregister(User)
# admin.site.unregister(Group)
# View DB Table in Admin Panel
admin.site.register(MyTableName)
# ----------------------------------
# @admin.register(MyTableName)
# class MyTableName(admin.ModelAdmin):
# list_display = ['id','name']
"""
def models_py():
return """
from django.db import models
class MyTableName(models.Model):
name = models.CharField(max_length=255)
class Meta:
db_table = "MyTableName"
def __str__(self):
return self.name
"""
def setting_py():
return """
# Generated by quick-django for Reference
# STATIC_URL = '/static/'
# STATIC_ROOT = 'static'
# STATICFILES_DIRS = [
# os.path.join(BASE_DIR, "static")
# ]
# MEDIA_ROOT = os.path.join(BASE_DIR, "media")
# MEDIA_URL = '/media/'
"""
def urls_py(django_app):
return f"""
from django.urls import path
from {django_app} import views
urlpatterns = [
path('',views.Home.as_view(),name=''),
]
"""
def views_py():
return f"""
from django.shortcuts import render,HttpResponse
from django.views import View
class Home(View):
def get(self,request):
return HttpResponse('<h1>visit : <a href="https://mefiz.com">mefiz.com</a></h1>')
# return render(request,'index.html')
"""
def project_urls_py(django_app):
return f"""
from django.contrib import admin
from django.urls import path,include
from django.conf.urls.static import static
from django.conf import settings
admin.site.site_header = "mysite header"
admin.site.site_title = "mysite title"
admin.site.index_title = "mysite index title"
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('{django_app}.urls')),
]
urlpatterns = urlpatterns+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
"""
# For API
class CreateApi:
def serializers_py(app_name):
return f"""
from rest_framework import serializers
# from {app_name}.models import MyTableName
# class MyTableNameSerializers(serializers.ModelSerializer):
# class Meta:
# model = MyTableName
# fields = '__all__'
# class MyTableNameSerializers(serializers.ModelSerializer):
# class Meta:
# model = MyTableName
# fields = ('id','name')
"""
def views_py_api(app_name):
return f"""
from django.shortcuts import render,HttpResponse
from rest_framework.decorators import api_view
from rest_framework.response import Response
# from {app_name}.models import MyTableName
# from .serializers import MyTableNameSerializers
@api_view(['GET'])
def home(request):
return Response("hello world")
# items = MyTableName.objects.all()
# serializer = MyTableNameSerializers(items,many=True)
# return Response(serializer.data)
"""
def urls_py_api(django_app):
return f"""
from django.urls import path
from {django_app} import views
urlpatterns = [
path('',views.home,name=''),
]
"""
def prCyan(skk): print("\033[96m {}\033[00m" .format(skk))
def prRed(skk): print("\033[91m {}\033[00m" .format(skk))
projectname = sys.argv[1]
app_name = sys.argv[2]
try:
restapi = sys.argv[3]
restapi = restapi[2:]
except:
restapi = None
if os.path.exists(projectname):
prRed(f"[0]: {projectname} project name already available")
exit()
else:
pass
# Create Django Project
os.system(f'django-admin startproject {projectname}')
time.sleep(1)
if os.path.exists(projectname):
pass
else:
exit()
# Change Folder
os.chdir(projectname)
os.mkdir('static')
os.mkdir('media')
prCyan(f"[1]: {projectname} Project Create Successfully ......... OK ")
# Create App
if platform.system() == 'Linux':
os.system(f'python3 manage.py startapp {app_name}')
else:
os.system(f'python manage.py startapp {app_name}')
time.sleep(1)
prCyan(f"[2]: {app_name} App Create Successfully ......... OK ")
# Change Folder
os.chdir(projectname)
# Edit Project urls.py
prCyan(f"[3]: Modify Project urls.py ......... OK ")
urlscode = Create.project_urls_py(app_name)
file = open("urls.py","w")
file.write(urlscode)
file.close()
prCyan(f"[4]: Modify Project urls.py successful ......... OK ")
# Edit Setting.py File
prCyan(f"[5]: Modify Project setting.py ......... OK ")
urlscode = Create.setting_py()
file = open("settings.py","a")
file.write(urlscode)
file.close()
prCyan(f"[6]: Modify Project setting.py successful ......... OK ")
os.chdir("..")
# Edit Your Django App
urlscode = Create.urls_py(app_name)
views = Create.views_py()
admin = Create.admin_py(app_name)
sample_html = Create.html()
models = Create.models_py()
# API
serializerspy = CreateApi.serializers_py(app_name)
viewspyapi = CreateApi.views_py_api(app_name)
urlspyapi = CreateApi.urls_py_api(app_name)
os.chdir(app_name)
prCyan(f"[7]: Modification in {app_name} ......... OK ")
file = open("urls.py","w")
file.write(urlscode)
file.close()
file = open("views.py","w")
file.write(views)
file.close()
file = open("admin.py","w")
file.write(admin)
file.close()
file = open("models.py","w")
file.write(models)
file.close()
# API
if restapi == 'restapi':
file = open("serializers.py","w")
file.write(serializerspy)
file.close()
file = open("views.py","w")
file.write(viewspyapi)
file.close()
file = open("urls.py","w")
file.write(urlspyapi)
file.close()
os.mkdir('templates')
os.chdir("templates")
file = open("index.html","w")
file.write(sample_html)
file.close()
os.chdir("..")
os.chdir("..")
if platform.system() == 'Linux':
os.system(f'python3 manage.py migrate')
else:
os.system(f'python manage.py migrate')
prCyan(f"[8]: All operation successful ......... OK ")