Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
Latest commit
108 lines (94 loc) · 3.15 KB
/
Copy pathcli.py
File metadata and controls
108 lines (94 loc) · 3.15 KB
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
fromcontextlibimportcontextmanager
fromdatetimeimportdatetime, timedelta
importtyper
fromsqlalchemyimporttext
fromsqlmodelimportSQLModel
fromtyperimportArgument
fromapp.dependencies.mainimportget_session
fromapp.factoriesimport (
ConfigurationFactory,
ExternalModuleOnOfferFactory,
InternalModuleOnOfferFactory,
OfferingGroupFactory,
all_factories,
)
fromapp.schemas.configurationsimportModuleSelectionStatus
fromapp.schemas.constraintsimportOfferingGroupLabel
cli=typer.Typer()
@contextmanager
defdynamic_session():
session=next(get_session())
forfinall_factories:
f._meta.sqlalchemy_session=session
yield
@cli.command(name="erase_data")
deferase_data():
"""
WARNING: This command will erase all data from the tables without dropping the tables.
"""
confirm=typer.confirm(
"Are you sure you want to erase all data from the tables? This action cannot be undone."
)
ifconfirm:
session=next(get_session())
# Iterate over all tables and delete data
fortableinreversed(SQLModel.metadata.sorted_tables):
session.execute(text(f"DELETE FROM {table.name}")) # nosec
session.commit()
typer.echo("All data erased successfully.")
else:
typer.echo("Operation cancelled.")
@cli.command(name="populate_db")
defpopulate_db(
year: str=Argument(help="Academic year in short form e.g. 2324 for 2023-2024"),
):
"""
Populates the database with dummy data.
"""
withdynamic_session():
ConfigurationFactory(
year=year,
with_periods=[dict(end=datetime.utcnow() +timedelta(weeks=2))],
status=ModuleSelectionStatus.USE_PERIODS,
)
optional_group=OfferingGroupFactory(
year=year, label=OfferingGroupLabel.OPTIONAL
)
selective_group=OfferingGroupFactory(
year=year, label=OfferingGroupLabel.SELECTIVE
)
ExternalModuleOnOfferFactory.create_batch(size=3, year=year)
InternalModuleOnOfferFactory.create_batch(
size=5,
year=year,
with_regulations=[
dict(degree="c3", offering_group=optional_group),
dict(degree="v5", offering_group=selective_group),
],
)
InternalModuleOnOfferFactory.create_batch(
size=3,
year=year,
with_regulations=[
dict(degree="c3", offering_group=selective_group),
],
)
# Add choices for Happy Potter
ExternalModuleOnOfferFactory.create_batch(
size=3, year=year, with_applications=[dict(username="hpotter")]
)
InternalModuleOnOfferFactory.create_batch(
size=2,
year=year,
with_regulations=[dict(degree="mcai1")],
)
InternalModuleOnOfferFactory.create_batch(
size=3,
year=year,
with_regulations=[
dict(degree="mcai1", with_enrollments=[dict(username="hpotter")])
],
)
print("Database populated successfully.")
if__name__=="__main__":
cli()