-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmydb.py
More file actions
158 lines (134 loc) · 3.65 KB
/
mydb.py
File metadata and controls
158 lines (134 loc) · 3.65 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import datetime
import os
from sqlalchemy import Column, DateTime, Integer, Text, create_engine, inspect
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
DB_URL = os.getenv("DATABASE_URL").replace("postgres://", "postgresql://", 1)
ENGINE = create_engine(DB_URL)
Base = declarative_base()
class Class_Ten(Base):
__tablename__ = "class_ten"
class_name = Column(Text, primary_key=True)
status = Column(Integer)
comment = Column(Text)
status_updated = Column(DateTime)
comment_updated = Column(DateTime)
class Info(Base):
__tablename__ = "info"
id = Column(Integer, primary_key=True)
title = Column(Text)
value = Column(Text)
updated_at = Column(DateTime)
Session = sessionmaker(bind=ENGINE)
session = Session()
def fix_class_ten():
insert_list = list()
default_class_names = [
"1A",
"1B",
"2A",
"2B",
"3A",
"3B",
"11",
"12",
"13",
"14",
"15",
"16",
"17",
"21",
"22",
"23",
"24",
"25",
"26",
"27",
"31",
"32",
"33",
"34",
"35",
"36",
"37",
]
exist_class_names = list()
for i in session.query(Class_Ten.class_name).all():
exist_class_names.append(i[0])
class_names = list(set(default_class_names) - set(exist_class_names))
if class_names:
timestamp = datetime.datetime.now()
for class_name in class_names:
row = dict()
row["class_name"] = class_name
row["status"] = 3
row["comment"] = ""
row["status_updated"] = timestamp
insert_list.append(row)
session.execute(Class_Ten.__table__.insert(), insert_list)
session.commit()
session.close()
def get_class_ten():
resdict = {}
result = session.query(Class_Ten).all()
for row in result:
resdict[row.class_name] = {
"status": row.status,
"comment": row.comment,
# "status_updated": row.status_updated,
# "comment_updated": row.comment_updated,
"updated_at": row.comment_updated
}
return resdict
def update_class_ten(class_name, status, comment, delete, timestamp):
x = session.query(Class_Ten).get(class_name)
x.status = status
if delete:
x.comment = ""
x.comment_updated = timestamp
elif comment:
x.comment = comment
x.comment_updated = timestamp
x.status_updated = timestamp
session.commit()
session.close()
return 0
def get_info():
resdict = {}
result = session.query(Info).all()
count = session.query(Info).count() - 1
for row in result:
resdict[count - row.id] = {
"title": row.title,
"value": row.value,
"updated_at": row.updated_at,
}
return resdict
def add_info(title, value, updated_at):
id = session.query(Info).count()
ncolmn = Info(id=id, title=title, value=value, updated_at=updated_at)
session.add(ncolmn)
session.commit()
session.close()
return 0
def reset_class_ten():
try:
Base.metadata.tables["class_ten"].create(bind=ENGINE)
except Exception:
pass
session.query(Class_Ten).delete()
session.commit()
session.close()
fix_class_ten()
return 0
def reset_info():
try:
Base.metadata.tables["info"].create(bind=ENGINE)
except Exception:
pass
session.query(Info).delete()
session.commit()
session.close()
return 0
if __name__ == "__main__":
print(get_class_ten(), get_info())