This repository has been archived by the owner on Jun 18, 2020. It is now read-only.
/
event.py
465 lines (377 loc) · 14.5 KB
/
event.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# -*- coding: utf-8 -*-
'''Event model module
@author: moschlar
'''
#
## SAUCE - System for AUtomated Code Evaluation
## Copyright (C) 2013 Moritz Schlarb
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU Affero General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## any later version.
##
## This program 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 Affero General Public License for more details.
##
## You should have received a copy of the GNU Affero General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from datetime import datetime, timedelta
from warnings import warn
from sqlalchemy import Column, ForeignKey, Index, Table, UniqueConstraint, union
from sqlalchemy.inspection import inspect
from sqlalchemy.orm import backref, relationship
from sqlalchemy.types import Boolean, DateTime, Enum, Integer, String, Unicode
from sauce.lib.helpers import link
from sauce.model import DeclarativeBase, metadata
from sauce.model.user import Team, User, event_members, lesson_members, team_members
__all__ = ('Event', 'Course', 'Contest', 'Lesson')
# secondary table for many-to-many relation
event_teachers = Table('event_teachers', metadata,
Column('user_id', Integer, ForeignKey('users.id'), primary_key=True),
Column('event_id', Integer, ForeignKey('events.id'), primary_key=True),
)
class Event(DeclarativeBase):
'''An Event'''
__tablename__ = 'events'
id = Column(Integer, primary_key=True, nullable=False)
type = Column(Enum('course', 'contest', name='event_type'), nullable=False)
_url = Column('url', String(255), nullable=False, index=True, unique=True)
name = Column(Unicode(255), nullable=False)
description = Column(Unicode(64 * 1024), nullable=True)
start_time = Column(DateTime, nullable=False, default=datetime.now)
end_time = Column(DateTime, nullable=False, default=lambda: datetime.now() + timedelta(days=31))
password = Column(Unicode(255), nullable=True,
doc='The password students have to enter in order to enroll to an event')
enroll = Column(Enum('event', 'lesson', 'lesson_team', 'team', 'team_new', name='event_enroll'),
nullable=True, default=None)
public = Column(Boolean, nullable=False, default=True,
doc='Whether this Event is accessible for non-logged in users and non-enrolled students')
enabled = Column(Boolean, nullable=False, default=False,
doc='Whether this Event is administratively enabled or disabled')
_members = relationship('User',
secondary=event_members,
order_by='User.user_name',
backref=backref('_events',
order_by=id,
)
)
teachers = relationship('User',
secondary=event_teachers,
order_by='User.user_name',
backref=backref('teached_events'),
)
_teacher_id = Column('teacher_id', Integer, ForeignKey('users.id'))
_teacher = relationship('User',
#backref=backref('events',
# cascade='all, delete-orphan'),
doc='(Deprecated) The main teacher, displayed as contact on event details')
@property
def teacher(self): # pragma: no cover
warn('Event.teacher', DeprecationWarning, stacklevel=2)
if self._teacher:
return self._teacher
elif self.teachers:
if len(self.teachers) > 1:
warn('len(Event.teachers) > 1', stacklevel=2)
return self.teachers[0]
else:
return None
@teacher.setter
def teacher(self, teacher): # pragma: no cover
# The setter is okay to use because it makes injection in CRC easier
#warn('Event.teacher', DeprecationWarning, stacklevel=2)
self._teacher = teacher
try:
self.teachers.remove(teacher)
except ValueError:
pass
finally:
self.teachers.insert(0, teacher)
__mapper_args__ = {
'polymorphic_on': 'type',
'order_by': [end_time, start_time, _url],
}
def __repr__(self):
return (u'<%s: id=%r, name=%r, url=%r>'
% (self.__class__.__name__,
self.id, self.name, self._url)
).encode('utf-8')
def __unicode__(self):
return u'Event "%s"' % self.name
def __contains__(self, item):
if isinstance(item, User):
return item in self.tutors or item in self.members
def clone(self, i=0, recursive=True,
teachers=False, members=False, lessons=False, teams=False):
e = Event(**dict((attr.key, getattr(self, attr.key)) for attr in inspect(self).mapper.column_attrs
if attr.key != 'id'))
e._url = '%s%d' % (self._url, i + 1) # TODO: Not uniqueness-safe
if teachers:
e.teachers = [t for t in self.teachers]
if members:
e._members = [m for m in self._members]
if lessons:
e.lessons = [l.clone(i=i, teams=teams) for l in self.lessons]
if recursive:
e.sheets = [s.clone(i=j, recursive=recursive) for j, s in enumerate(self.sheets)]
return e
#----------------------------------------------------------------------------
# Properties
@property
def current_sheets(self):
return [s for s in self.sheets if s.start_time < datetime.now() < s.end_time]
@property
def previous_sheets(self):
return [s for s in self.sheets if s.end_time < datetime.now()]
@property
def future_sheets(self):
return [s for s in self.sheets if s.start_time > datetime.now()]
@property
def public_sheets(self):
return [s for s in self.sheets if s.public]
@property
def url(self):
return '/events/%s' % self._url
@property
def link(self):
'''Link for this event'''
return link(self.name, self.url)
@property
def breadcrumbs(self):
'''Array of links for breadcrumb navigation'''
return [self.link]
parent = None
@property
def teams(self):
t = set()
for l in self.lessons:
t |= set(l.teams)
return t
@property
def children(self):
return self.sheets
@property
def is_active(self):
'''If the event is active at the moment'''
return self.start_time < datetime.now() < self.end_time
@property
def remaining_time(self):
'''Remaining time for event'''
return max(self.end_time - datetime.now(), timedelta(0))
@property
def tutorsandteachers(self):
return set(self.teachers) | set(self.tutors)
@property
def tutors(self):
tuts = set()
for l in self.lessons:
tuts |= set(l.tutors)
return tuts
# return [l.tutor for l in self.lessons]
@property
def members(self):
studs = set(self._members)
for l in self.lessons:
studs |= set(l.members)
return studs
@property
def students(self): # pragma: no cover
warn('Event.students', DeprecationWarning, stacklevel=2)
return self.members
def members_query(self, qry=None):
if not qry:
qry = User.query
qry = qry.select_entity_from(union(
qry.join(event_members).join(Event)
.filter_by(id=self.id).order_by(None),
qry.join(lesson_members).join(Lesson)
.filter_by(event_id=self.id).order_by(None),
qry.join(team_members).join(Team).join(Lesson)
.filter_by(event_id=self.id).order_by(None),
)).order_by(User.user_name)
return qry
def tutors_query(self, qry=None):
if not qry:
qry = User.query
qry = qry.join(lesson_tutors).join(Lesson).filter_by(event_id=self.id)
return qry
#----------------------------------------------------------------------------
# Classmethods
@classmethod
def by_url(cls, url):
'''Return the event specified by url'''
return cls.query.filter_by(enabled=True).filter(cls._url == url).one()
# @classmethod
# def all_events(cls, only_public=True):
# '''Return a 3-tuple (current, previous, future) containing all events'''
# return (cls.current_events(only_public).all(),
# cls.previous_events(only_public).all(),
# cls.future_events(only_public).all())
@classmethod
def current_events(cls, only_public=False):
'''Return a query for currently active events'''
q = cls.query
q = q.filter_by(enabled=True)
if only_public:
q = q.filter_by(public=True)
q = q.filter(cls.start_time < datetime.now()).filter(cls.end_time > datetime.now())
return q
@classmethod
def previous_events(cls, only_public=False):
'''Return a query for previously active events'''
q = cls.query
q = q.filter_by(enabled=True)
if only_public:
q = q.filter_by(public=True)
q = q.filter(cls.end_time < datetime.now())
return q
@classmethod
def future_events(cls, only_public=False):
'''Return a query for future active events'''
q = cls.query
q = q.filter_by(enabled=True)
if only_public:
q = q.filter_by(public=True)
q = q.filter(cls.start_time > datetime.now())
return q
class Course(Event):
'''An Event with type course'''
__mapper_args__ = {'polymorphic_identity': 'course'}
def __unicode__(self):
return u'Course "%s"' % self.name
class Contest(Event):
'''An Event with type contest'''
__mapper_args__ = {'polymorphic_identity': 'contest'}
def __unicode__(self):
return u'Contest "%s"' % self.name
# secondary table for many-to-many relation
lesson_tutors = Table('lesson_tutors', metadata,
Column('user_id', Integer, ForeignKey('users.id'), primary_key=True),
Column('lesson_id', Integer, ForeignKey('lessons.id'), primary_key=True),
)
class Lesson(DeclarativeBase):
'''A Lesson'''
__tablename__ = 'lessons'
id = Column(Integer, primary_key=True, nullable=False)
lesson_id = Column(Integer, index=True, nullable=False,
doc='The lesson_id specific to the parent event')
# _url = Column('url', String(255), nullable=True)
# '''Not used right now!'''
name = Column(Unicode(255), nullable=False)
event_id = Column(Integer, ForeignKey('events.id'), nullable=False)
event = relationship('Event',
backref=backref('lessons',
order_by=lesson_id,
cascade='all, delete-orphan',
)
)
_tutor_id = Column('tutor_id', Integer, ForeignKey('users.id'), nullable=True)
_tutor = relationship('User',
# backref=backref('tutored_lessons',
# order_by=lesson_id,
# cascade='all, delete-orphan')
)
tutors = relationship('User',
secondary=lesson_tutors,
order_by='User.user_name',
backref=backref('tutored_lessons',
order_by=lesson_id),
)
_members = relationship('User',
secondary=lesson_members,
order_by='User.user_name',
backref=backref('_lessons',
order_by=lesson_id,
)
)
__mapper_args__ = {'order_by': [event_id, name]}
__table_args__ = (
UniqueConstraint('event_id', 'lesson_id'),
Index('idx_event_lesson', event_id, lesson_id, unique=True),
)
def __repr__(self):
return (u'<Lesson: id=%r, event_id=%r, lesson_id=%r, name=%r>'
% (self.id, self.event_id, self.lesson_id, self.name)
).encode('utf-8')
def __unicode__(self):
return u'Lesson "%s"' % (self.name)
def __contains__(self, item):
return item in self.members
def clone(self, i=0,
tutors=False, members=False, teams=False):
l = Lesson(**dict((attr.key, getattr(self, attr.key)) for attr in inspect(self).mapper.column_attrs
if attr.key != 'id'))
l.lesson_id = i + 1 # TODO: Not uniqueness-safe
if tutors:
l.tutors = [t for t in self.tutors]
if members:
l._members = [m for m in self._members]
if teams:
l.teams = [t.clone(i=i, members=members) for t in self.teams]
return l
@property
def parent(self):
'''Parent entity for generic hierarchy traversal'''
return self.event
@property
def url(self):
return self.event.url + '/lessons/%s' % self.lesson_id
@property
def link(self):
'''Link for this lesson'''
return link(self.name, self.url)
@property
def breadcrumbs(self):
'''Array of links for breadcrumb navigation'''
return self.event.breadcrumbs + [self.link]
@property
def tutor(self): # pragma: no cover
warn('Lesson.tutor', DeprecationWarning, stacklevel=2)
if self._tutor:
return self._tutor
elif self.tutors:
return self.tutors[0]
else:
return None
@tutor.setter
def tutor(self, tutor): # pragma: no cover
# The setter is okay to use because it makes injection in CRC easier
#warn('Lesson.tutor', DeprecationWarning, stacklevel=2)
self._tutor = tutor
try:
self.tutors.remove(tutor)
except ValueError:
pass
finally:
self.tutors.insert(0, tutor)
@property
def members(self):
s = set(self._members)
for t in self.teams:
s |= set(t.members)
return s
def members_query(self, qry=None):
if not qry:
qry = User.query
qry = qry.select_entity_from(union(
qry.join(lesson_members).filter_by(lesson_id=self.id).order_by(None),
qry.join(team_members).join(Team).filter_by(lesson_id=self.id).order_by(None),
)).order_by(User.id)
return qry
@property
def students(self): # pragma: no cover
warn('Lesson.students', DeprecationWarning, stacklevel=2)
return self.members
@property
def teacher(self): # pragma: no cover
warn('Lesson.teachers', DeprecationWarning, stacklevel=2)
return self.tutor
#----------------------------------------------------------------------------
# Classmethods
@classmethod
def by_lesson_id(cls, lesson_id, event):
return cls.query.filter(cls.event_id == event.id).filter(cls.lesson_id == lesson_id).one()