/
__init__.py
221 lines (167 loc) · 6.87 KB
/
__init__.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# Contest Management System - http://cms-dev.github.io/
# Copyright © 2010-2012 Giovanni Mascellani <mascellani@poisson.phc.unipi.it>
# Copyright © 2010-2017 Stefano Maggiolo <s.maggiolo@gmail.com>
# Copyright © 2010-2012 Matteo Boscariol <boscarim@hotmail.com>
# Copyright © 2013 Bernard Blackham <bernard@largestprime.net>
# Copyright © 2013 Luca Wehrstedt <luca.wehrstedt@gmail.com>
# Copyright © 2016 Myungwoo Chun <mc.tamaki@gmail.com>
# Copyright © 2016 Masaki Hara <ackie.h.gmai@gmail.com>
# Copyright © 2016 Amir Keivan Mohtashami <akmohtashami97@gmail.com>
#
# 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 (at your option) 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/>.
"""Utilities functions that interacts with the database.
"""
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
import logging
from sqlalchemy import create_engine
from sqlalchemy.orm import configure_mappers, joinedload, subqueryload
from cms import config
logger = logging.getLogger(__name__)
# Define what this package will provide.
__all__ = [
"version", "engine",
# session
"Session", "ScopedSession", "SessionGen", "custom_psycopg2_connection",
# base
"metadata", "Base",
# types
"RepeatedUnicode",
# contest
"Contest", "Announcement",
# user
"User", "Team", "Participation", "Message", "Question",
# admin
"Admin",
# task
"Task", "Statement", "Attachment", "SubmissionFormatElement", "Dataset",
"Manager", "Testcase",
# submission
"Submission", "File", "Token", "SubmissionResult", "Executable",
"Evaluation",
# usertest
"UserTest", "UserTestFile", "UserTestManager", "UserTestResult",
"UserTestExecutable",
# printjob
"PrintJob",
# fsobject
"FSObject",
# init
"init_db",
# drop
"drop_db",
# util
"test_db_connection", "get_contest_list", "is_contest_id",
"ask_for_contest",
]
# Instantiate or import these objects.
version = 25
engine = create_engine(config.database, echo=config.database_debug,
pool_timeout=60, pool_recycle=120)
from .session import Session, ScopedSession, SessionGen, \
custom_psycopg2_connection
from .types import RepeatedUnicode
from .base import metadata, Base
from .contest import Contest, Announcement
from .user import User, Team, Participation, Message, Question
from .admin import Admin
from .task import Task, Statement, Attachment, SubmissionFormatElement, \
Dataset, Manager, Testcase
from .submission import Submission, File, Token, SubmissionResult, \
Executable, Evaluation
from .usertest import UserTest, UserTestFile, UserTestManager, \
UserTestResult, UserTestExecutable
from .printjob import PrintJob
from .fsobject import FSObject
from .init import init_db
from .drop import drop_db
from .util import test_db_connection, get_contest_list, is_contest_id, \
ask_for_contest
configure_mappers()
# The following are methods of Contest that cannot be put in the right
# file because of circular dependencies.
def get_submissions(self):
"""Return a list of submissions (with the information about the
corresponding task) referring to the contest.
returns (list): list of submissions.
"""
return self.sa_session.query(Submission)\
.join(Task).filter(Task.contest == self)\
.options(joinedload(Submission.token))\
.options(joinedload(Submission.results)).all()
def get_submission_results(self):
"""Return a list of submission results for all submissions in
the current contest, as evaluated against the active dataset
for each task.
returns ([SubmissionResult]): list of submission results.
"""
return self.sa_session.query(SubmissionResult)\
.join(Submission).join(Task).filter(Task.contest == self)\
.filter(Task.active_dataset_id == SubmissionResult.dataset_id)\
.all()
def get_user_tests(self):
"""Return a list of user tests (with the information about the
corresponding user) referring to the contest.
return (list): list of user tests.
"""
return self.sa_session.query(UserTest)\
.join(Task).filter(Task.contest == self)\
.options(joinedload(UserTest.results)).all()
def get_user_test_results(self):
"""Returns a list of user_test results for all user_tests in
the current contest, as evaluated against the active dataset
for each task.
returns (list): list of user test results.
"""
return self.sa_session.query(UserTestResult)\
.join(UserTest).join(Task).filter(Task.contest == self)\
.filter(Task.active_dataset_id == UserTestResult.dataset_id)\
.all()
Contest.get_submissions = get_submissions
Contest.get_submission_results = get_submission_results
Contest.get_user_tests = get_user_tests
Contest.get_user_test_results = get_user_test_results
# The following is a method of Dataset that cannot be put in the right
# file because of circular dependencies.
def get_submission_results_for_dataset(self, dataset):
"""Return a list of all submission results against the specified
dataset.
Also preloads the executable and evaluation objects relative to
the submission results.
returns ([SubmissionResult]): list of submission results.
"""
# We issue this query manually to optimize it: we load all
# executables and evaluations at once instead of having SA
# lazy-load them when we access them for each SubmissionResult,
# one at a time.
return self.sa_session\
.query(SubmissionResult)\
.filter(SubmissionResult.dataset == dataset)\
.options(joinedload(SubmissionResult.submission))\
.options(subqueryload(SubmissionResult.executables))\
.options(subqueryload(SubmissionResult.evaluations))\
.all()
Dataset.get_submission_results = get_submission_results_for_dataset
# The following is a method of Participation that cannot be put in the right
# file because of circular dependencies.
def get_tokens(self):
"""Returns a list of tokens used by a user participation.
returns (list): list of tokens.
"""
return self.sa_session.query(Token)\
.join(Submission).filter(Submission.participation == self).all()
Participation.get_tokens = get_tokens