This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassModel.py
275 lines (235 loc) · 8.58 KB
/
ClassModel.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
"""
ClassModel.py will act as an ORM for the class table in the sqlite database
to the objects that we will use within this application. This will attempt
to make querying the table much simpler.
Authors:
(RegTools)
Joseph Goh
Mason Sayyadi
Owen McEvoy
Ryan Gurnick
Samuel Lundquist
Priority credit to:
Ryan Gurnick - 2/27/20 Creation
"""
import sqlite3
from datetime import date, datetime
import Datastore
class ClassModel:
def __init__(self, db_file):
"""
Initializer for the class model. This will want to know which file to look at for the db.
Then it will provide a connection to that database and allow for some ORM opterations, such
as insert and select.
db_file :str
Example Usage:
cm = ClassModel.ClassModel('testing.db')
"""
# init db connection using the datastore
self.conn = Datastore.DB(db_file).ret().conn
def insert(self, term, name, subject, number, credits, total_count, lecture_count, lab_count, discussion_count,
other_count, sections):
"""
Insert method for the class model that allows the caller to provide the specified
information and create a record within the database, this will return the id of
the row created.
:param
name :str
subject :str
number :str
credits :str
total_count :int
lecture_count :int
lab_count :int
discussion_count :int
other_count :int
sections :str
Example Usage:
cm = ClassModel.ClassModel('testing.db')
cm.insert(321321, 201901, '123', '{}', 1, 2, 0, 0, '{}')
"""
# "lecture_count", "lab_count", "discussion_count", "other_count"
sql = '''INSERT INTO "main"."classes"("term","name","subject","number","credits","total_count", "lecture_count", "lab_count", "discussion_count", "other_count","sections","created_at","updated_at") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?);'''
cur = self.conn.cursor()
try:
cur.execute(sql, (
term, name, subject, number, credits, total_count, lecture_count, lab_count, discussion_count,
other_count,
sections, date.today(), date.today()))
self.conn.commit()
return cur.lastrowid
except sqlite3.IntegrityError:
return None
def find_by(self, haystack, needle):
"""
Search for records within the class database and return them as lists of lists.
:param
haystack :str
needle :str
return :list
Example Usage:
cm = ClassModel.ClassModel('testing.db')
cm.find_by('term', 201901)
cm.find_by('crn', 321321)
"""
sql = "SELECT * FROM \"main\".\"classes\" WHERE \"{}\" = \"{}\"".format(haystack, needle)
cur = self.conn.cursor()
try:
cur.execute(sql)
except ValueError:
return cur.fetchall()
return cur.fetchall()
def find_by_term(self, subject, year, term):
"""
Search for records within the class database and return them as lists of lists.
:param
subject :str
year :str
term :str
return :list
Example Usage:
cm = ClassModel.ClassModel('testing.db')
cm.find_by_term('CIS', 2019, 1)
cm.find_by_term('CIS', 2015, 2)
"""
s = str(year) + "0" + str(term)
sql = "SELECT * FROM \"main\".\"classes\" WHERE \"subject\" = \"{}\" AND \"term\" = \"{}\";".format(subject, s)
cur = self.conn.cursor()
try:
cur.execute(sql)
except ValueError:
return cur.fetchall()
return cur.fetchall()
def find_class_id(self, subject, courseNumber, term):
"""
Finds records in the classes table given a subject, courseNumber, and term
:param subject:
:param courseNumber:
:param term:
:return:
"""
sql = "SELECT \"number\", \"id\" FROM \"main\".\"classes\" WHERE \"subject\" = \"{}\" and \"number\" = \"{}\" and \"term\" = \"{}\";".format(
subject, courseNumber, term)
cur = self.conn.cursor()
try:
cur.execute(sql)
except ValueError:
return cur.fetchall()
return cur.fetchall()
def predict_future_class_id(self, subject, courseNumber):
"""
The query used to determine the most recent occurance of a specific subject and course number combination.
:param subject:
:param courseNumber:
:return:
"""
sql = "SELECT \"number\", \"id\" FROM \"main\".\"classes\" WHERE \"subject\" = \"{}\" AND \"number\" = {} ORDER BY \"term\" desc LIMIT 1;".format(
subject, courseNumber)
cur = self.conn.cursor()
try:
cur.execute(sql)
except:
return cur.fetchall()
return cur.fetchall()
def crt_class_search(self, typeNum, priorYear):
"""
This function takes the type code for the arts and letters or social sciences and then
returns a list of courses that have at least one record.
:param typeNum:
:param priorYear:
:return:
"""
sql = """SELECT \"subject\", \"term\", \"number\", \"name\", \"aprnce\"
FROM (SELECT \"subject\", \"term\", \"number\", \"name\",count(*) as \"aprnce\"
FROM \"main\".\"classes\" WHERE \"term\" like \"{}%\"
GROUP BY \"subject\", \"number\") miniQuery
WHERE \"aprnce\" = 1
AND name like \"%>{}\"
ORDER BY \"subject\", \"number\" asc;""".format(priorYear, typeNum)
cur = self.conn.cursor()
try:
cur.execute(sql)
except ValueError:
return cur.fetchall()
return cur.fetchall()
def delete_sub_term(self, subject, term):
"""
This function will delete a set of records from the database given a subject and term that match.
:param subject:
:param term:
:return:
"""
sql = "DELETE FROM \"main\".\"classes\" WHERE \"subject\" = \"{}\" AND \"term\" = \"{}\";".format(subject, term)
cur = self.conn.cursor()
try:
cur.execute(sql)
self.conn.commit()
except ValueError:
return cur.fetchall()
def count(self, subject, number):
"""
This function will provide count values for the critical counts and fill in the data
for previous section distribution.
:param subject:
:param number:
:return:
"""
sql = "SELECT \"term\", \"subject\", \"number\", \"total_count\", \"lecture_count\", \"lab_count\", \"discussion_count\", \"other_count\" FROM \"main\".\"classes\" WHERE \"subject\"=\"{}\" AND \"number\"=\"{}\";".format(
subject, number)
cur = self.conn.cursor()
try:
cur.execute(sql)
except ValueError:
return cur.fetchall()
return cur.fetchall()
def find_course(self, name, subject, number, term, year):
"""
Search for records within the class database and return them as lists of lists.
:param
haystack :str
needle :str
return :list
Example Usage:
cm = ClassModel.ClassModel('testing.db')
cm.find_by('term', 201901)
cm.find_by('crn', 321321)
"""
s = str(year) + "0" + str(term)
t = None
y = int(year)
if term == "Fall":
t = 1
elif term == "Winter":
y -= 1
t = 2
elif term == "Spring":
y -= 1
t = 3
elif term == "Summer":
y -= 1
t = 4
sql = "SELECT * FROM \"main\".\"classes\" WHERE \"name\" LIKE '%{}%' AND \"subject\" LIKE '%{}%' AND \"number\" LIKE '%{}%' and \"term\" = '{}' ".format(
name, subject, number, str(y) + "0" + str(t))
cur = self.conn.cursor()
try:
cur.execute(sql)
except ValueError:
return cur.fetchall()
return cur.fetchall()
def distinct(self, needle):
"""
Get the distinct values for the column of needle.
:param
needle :str
return :list
Example Usage:
cm = ClassModel.ClassModel('testing.db')
cm.distinct('subject')
"""
sql = "SELECT DISTINCT {} FROM classes".format(needle)
cur = self.conn.cursor()
try:
cur.execute(sql)
except ValueError:
return cur.fetchall()
return cur.fetchall()