-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
86 lines (63 loc) · 1.97 KB
/
manage.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
"""
This is not from a Django project. This file is used
to run management commands.
"""
import os
import click
from api.database import db
from api.database.models.images import Image
models = [
Image
]
@click.group()
def cmds():
pass
@click.group()
def database():
pass
@click.command(help="Create tables")
def maketables():
click.echo("Creating tables...")
db.connect()
db.create_tables(models)
click.echo("Tables created")
@click.command(help="Delete database and recreate tables")
@click.option('--confirm', prompt=True, default="No")
def reset(confirm):
if confirm.lower() == 'yes':
click.echo("Reseting database...")
with open('db.sqlite3', 'w') as f:
f.write('')
f.close()
db.connect()
db.create_tables(models)
click.echo("Database recreated successfully.")
else:
click.echo("Cancelled.")
@click.group()
def images():
pass
@click.command(help="Update images database")
def update():
for path, subdirs, files in os.walk("media/images/"):
for name in files:
try:
Image.create(file_path=os.path.join(path, name), category=Image.Category(path.split('/', 2)[2]).value)
click.echo(f"Addded image {os.path.join(path, name)}")
except Exception as e:
click.echo(f"Skipping existent image {os.path.join(path, name)}")
@click.command(help="Rebuild images table")
def rebuild():
Image.delete().execute()
for path, subdirs, files in os.walk("media/images/"):
for name in files:
Image.create(file_path=os.path.join(path, name), category=Image.Category(path.split('/', 2)[2]).value)
click.echo(f"Addded image {os.path.join(path, name)}")
database.add_command(maketables)
database.add_command(reset)
images.add_command(update)
images.add_command(rebuild)
cmds.add_command(database)
cmds.add_command(images)
if __name__ == '__main__':
cmds()