-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCRUD_comand.py
58 lines (47 loc) · 2.09 KB
/
CRUD_comand.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
import model
from model import *
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
dbname = 'test.sqlite'
def create_object(object, dbname=dbname, **kwargs):
engine = create_engine(f'sqlite:///{dbname}')
with Session(bind=engine) as ses:
ses.add(object(**kwargs))
return ses.commit()
def read_object(object, object_id=0, dbname=dbname):
engine = create_engine(f'sqlite:///{dbname}')
with Session(bind=engine) as ses:
if object_id == 0:
return ses.query(object).all()
else:
return ses.query(object).where(object.id == object_id).one()
def update_object(object, object_id: int, dbname=dbname, **kwargs):
engine = create_engine(f'sqlite:///{dbname}')
with Session(bind=engine) as ses:
if 'build_lists' in kwargs:
ses.query(object).where(object.id == object_id).one().build_lists = []
ses.query(object).where(object.id == object_id).one().build_lists = kwargs['build_lists']
del kwargs['build_lists']
if len(kwargs) == 0:
return ses.commit()
ses.query(object).where(object.id == object_id).update(kwargs)
return ses.commit()
def delete_object(object, object_id: int, dbname=dbname):
engine = create_engine(f'sqlite:///{dbname}')
with Session(bind=engine) as ses:
ses.query(object).where(object.id == object_id).delete()
return ses.commit()
def get_builds(profile:model.ProfileList):
engine = create_engine(f'sqlite:///{dbname}')
with Session(bind=engine) as ses:
if profile not in ses:
profile = ses.query(ProfileList).get(profile.id)
print(profile.build_lists)
return profile.build_lists
if __name__ == '__main__':
print(read_object(ProfileList, object_id=1))
update_object(ProfileList, 2, build_lists=[read_object(BuildList, object_id=3)])
engine = create_engine(f'sqlite:///{dbname}')
#with Session(bind=engine) as ses:
#ses.query(ProfileList).where(ProfileList.id == 2).one().build_lists = [read_object(BuildList, object_id=2)]
#ses.commit()