-
Notifications
You must be signed in to change notification settings - Fork 12
/
models.py
180 lines (144 loc) · 5.72 KB
/
models.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
'''
Copyright (C) 2014 Muhd Amirul Ashraf bin Mohd Fauzi <asdacap@gmail.com>
This file is part of Automatic IIUM Schedule Formatter.
Automatic IIUM Schedule Formatter is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Automatic IIUM Schedule Formatter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Automatic IIUM Schedule Formatter. If not, see <http://www.gnu.org/licenses/>.
'''
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column,Text,DateTime,Integer,String,Float,ForeignKey
from sqlalchemy.orm import relationship, backref
from sqlalchemy.schema import UniqueConstraint
import sqlalchemy.orm.exc
import json
import re
import tempfile
import subprocess
import os
import os.path
import logging
from bootstrap import db,app
DBBase=db.Model
# To make things easy
class CMethods(object):
@classmethod
def get_by_key_name(cls,key):
return db.session.query(cls).get(key)
def put(self):
db.session.add(self)
db.session.commit()
@classmethod
def all(cls):
return db.session.query(cls)
# Where the schedule store
class SavedSchedule(DBBase,CMethods):
__tablename__='savedschedules'
token=Column(String(150),primary_key=True)
data=Column(Text)
createddate=Column(DateTime)
def post_process(self):
#add section data
self.data=self.data.replace(unichr(160)," ")
obj=json.loads(self.data)
for s in obj['coursearray']:
s['lecturer']=''
if(obj['scheduletype']!='MAINCAMPUS'):
#we have no data other than main campus data but we still need to format it
self.data=json.dumps(obj)
return
for s in obj['coursearray']:
sectiondata=SectionData.get_section_data(s['code'],obj['session'],obj['semester'],s['section'])
if(sectiondata==None):
app.logger.warning('Warning, no section data for %s session %s semester %s section %s'%(s['code'],obj['session'],obj['semester'],s['section']))
else:
s['lecturer']=sectiondata.lecturer
self.data=json.dumps(obj)
# The error they report goes here.
class ErrorLog(DBBase,CMethods):
__tablename__='errorlogs'
id = Column(Integer, primary_key=True)
submitter=Column(String(200))
html=Column(Text)
error=Column(Text)
additionalinfo=Column(Text)
created_at=Column(DateTime)
# The subject data
class SubjectData(DBBase,CMethods):
__tablename__='subjectdatas'
__table_args__= (
UniqueConstraint('code','session','semester'),
)
id = Column(Integer, primary_key=True)
code=Column(String(20))
title=Column(String(200))
credit=Column(Float)
coursetype=Column(String(200))
kuliyyah=Column(String(200))
session=Column(String(200))
semester=Column(Integer)
@classmethod
def get_subject_data(cls,code,session,semester):
try:
return cls.query.filter(cls.code==code).filter(cls.semester==semester).filter(cls.session==session).one()
except sqlalchemy.orm.exc.NoResultFound:
return None
# The subject data for the subject
class SectionData(DBBase,CMethods):
__tablename__='sectiondatas'
__table_args__= (
UniqueConstraint('subject_id','sectionno'),
)
id = Column(Integer, primary_key=True)
subject_id=Column(Integer,ForeignKey('subjectdatas.id'))
sectionno = Column(Integer)
lecturer=Column(String(200))
subject=relationship(SubjectData,backref=backref('sections',cascade="all, delete-orphan"))
@classmethod
def get_section_data(cls,code,session,semester,section):
subject=SubjectData.get_subject_data(code,session,semester)
if(subject==None):
return None
try:
return cls.query.filter(cls.subject==subject).filter(cls.sectionno==section).one()
except sqlalchemy.orm.exc.NoResultFound:
return None
# The schedule data for the subject
class SectionScheduleData(DBBase,CMethods):
__tablename__='sectionscheduledatas'
id = Column(Integer, primary_key=True)
section_id=Column(Integer,ForeignKey('sectiondatas.id'))
venue=Column(String(200))
day=Column(String(200))
time=Column(String(200))
lecturer=Column(String(200))
section=relationship(SectionData,backref=backref('schedules',cascade="all, delete-orphan"))
# Themes data goes here
class Theme(DBBase,CMethods):
__tablename__='themes'
name=Column(String(250),primary_key=True)
submitter=Column(String(250))
email=Column(String(250))
data=Column(Text)
counter=Column(Integer)
rendered=Column(Text)
def simple_to_hash(self):
return { "name":self.name, "submitter":self.submitter }
def generate_photo(self):
with tempfile.NamedTemporaryFile(suffix=".html") as tfile:
tfile.write(self.rendered.encode('utf8'))
tfile.flush()
try:
subprocess.check_output(["python","/usr/local/bin/webkit2png","-x","800","600",os.path.abspath(tfile.name),"--output="+os.path.join(os.path.dirname(__file__), "static/themeimage/%s.png"%(self.name)),"--scale=200","150"], cwd=os.path.dirname(__file__) )
except subprocess.CalledProcessError as e:
logging.error("Exception on Generate Photo")
logging.error(e.cmd)
logging.error(e.returncode)
logging.error(e.output)
raise e