summaryrefslogtreecommitdiffstats
path: root/src/frest/manage/utils.py
blob: 4f35937d2f709a7ae713b343531d9a776e653479 (plain)
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
import os
from .bcolors import COLORS


ENDC = len(COLORS) - 1


def logging(text, _type=ENDC, end=""):
    print(f"{COLORS[_type]}{text}{COLORS[ENDC]}", end=end)


def logging_arg(text, *args):
    args = [f"{COLORS[0]}{i}{COLORS[ENDC]}" for i in args]
    print(text.format(*args), end="")


def logo():
    print(
        """  
   __               _    
  / _|             | |   
 | |_ _ __ ___  ___| |_ 
 |  _| '__/ _ \/ __| __| 
 | | | | |  __/\__ \ |_  
 |_| |_|  \___||___/\__|
    \n\n"""
    )


def inputsr():
    return input().strip().replace(" ", "_").lower()


def create_field(fields):
    field = {"name": "", "type": "", "nullable": True}
    logging("Choose field name: ")
    field_name = inputsr()
    while (len(field_name) < 1 or field_name[0].isdigit()) or (
        field_name in fields or field_name in ["id", "created_at", "updated_at"]
    ):
        if len(field_name) < 1 or field_name[0].isdigit():
            logging("Field name must not be empty or starts with a number", 2, "\n")
        else:
            logging("Field name already exists", 2, "\n")

        logging("Choose field name: ")
        field_name = inputsr()

    field["name"] = field_name

    logging("Choose field type: ")
    logging("int, str, text, datetime, float, bool", 0, " ")
    field_type = inputsr()
    if field_type not in ["int", "str", "text", "datetime", "float", "bool"]:
        logging("Field type must be one of the supported type", 2, "\n")
        logging("Choose field type: ")
        logging("int, str, text, datetime, float, bool", 0, " ")
        field_type = inputsr()

    if field_type == "str":
        logging("Choose string size: ")
        stringsize = inputsr()
        while not stringsize.isdigit():
            logging("String size must be an integer number", 2, "\n")
            logging("Choose string size: ")
            stringsize = inputsr()

        stringsize = int(stringsize)
        if stringsize < 1:
            logging("You inserted 0, so it will be 1 by default", 4, "\n")
            stringsize = 1

        field["size"] = stringsize

    field["type"] = field_type

    logging("Field is nullable? (Y/n): ")
    field_nullable = inputsr()

    if len(field_nullable) > 0:
        while True:
            if field_nullable[0] not in ["y", "n"]:
                logging("Field is nullable? (Y/n): ")
                field_nullable = inputsr()
            else:
                break

        if field_nullable[0] == "n":
            field["nullable"] = False

    return field


def create_model_cli(name):
    logging(
        "Fields id, created_at, updated_at are default on every new model, you can delete it from model file",
        4,
        "\n\n",
    )
    fields = []
    logging("Create field: (Y/n)")
    answer = inputsr()

    while answer in ["y", "", "yes"]:
        field = create_field(fields)
        fields.append(field)

        logging("Create new field: (Y/n)")
        answer = inputsr()

    return fields


def create_app(name):
    name = name.lower()
    if len(name) < 2:
        logging("Name of app must be minimun 2 characters long", 2)
        return

    if os.path.exists(f"scheme/{name}"):
        logging("App already exists", 2)
        return

    logging_arg("Create {}... ", f"scheme/{name}")
    os.mkdir(f"scheme/{name}")
    logging("OK", 3, "\n")

    logging_arg("Create {}... ", f"scheme/{name}/__init__.py")
    open(f"scheme/{name}/__init__.py", "w").close()
    logging("OK", 3, "\n")

    logging_arg("Create model for {}...\n", name)
    fields = create_model_cli(name)
    print(fields)
    logging("OK", 3, "\n")