-
Notifications
You must be signed in to change notification settings - Fork 0
/
script_for_making_model.py
78 lines (61 loc) · 2.6 KB
/
script_for_making_model.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
import os
model_base_template = """
class {}(models.Model):
"""
import sys
def add_model(name, model_dict):
"""
Right now only CharField and IntegerField accepted with timestamp as a datetimefield
Expecting a dict like this:
{
"name": {"type": "CharField", "max_length": 100, "primary_key":True},
"age": {"type": "IntegerField", "default":0},
"desc": {"type": "CharField", "max_length": 100, blank=True}
"timestamp":True
}
"""
model_base = model_base_template.format(name)
model_stuct = ""
for i in model_dict:
default = None
primary_key = False
additional_params = ""
new_column_template = "\t{}=models.{}({})\n"
if model_dict[i]["type"] == "CharField":
if "max_length" not in model_dict[i]:
print("Need a max_length for CharField to proceed!!!")
sys.exit(0)
else:
additional_params+="max_length={}".format(model_dict[i]['max_length'])
if "default" in model_dict[i]:
default=model_dict[i]["default"]
additional_params+=",default={}".format(default)
if "primary_key" in model_dict[i]:
primary_key=model_dict[i]["primary_key"]
additional_params+=",primary_key=True"
model_stuct += new_column_template.format(name, model_dict[i]["type"], additional_params)
def add_model_temp_fix(project_name, app_name, model_name, field_names):
base_template = "\t{} = models.CharField(max_length=500)\n"
final_model = model_base_template.format(model_name)
for i in field_names:
final_model += base_template.format(i)
#print(final_model)
with open(os.path.join(os.getcwd(), project_name, app_name, 'models.py'), 'r') as e:
model = e.read()
model = model + final_model
with open(os.path.join(os.getcwd(), project_name, app_name, 'models.py'), 'w') as e:
e.write(model)
print("Successfully added models!!")
serializer = """
class {}Serializer(serializers.ModelSerializer):
class Meta:
model = {}
fields = "__all__"
"""
with open(os.path.join(os.getcwd(), project_name, app_name, 'serializers.py'), 'r') as e:
serializers = e.read()
serializers = serializers + serializer.format(model_name, model_name)
with open(os.path.join(os.getcwd(), project_name, app_name, 'serializers.py'), 'w') as e:
e.write(serializers)
print("Successfully added serializer!!")
#add_model_temp_fix('new','api','User', ["name", "description"])