-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
169 lines (128 loc) · 5.18 KB
/
model.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
from datetime import datetime
from collections import OrderedDict
import data_source
class Student(object):
# Input:
# recursos/demo/dados_alunos_demo.txt
# 000000001,1,1,Ada Lovelace,ada.lovelace
# new format:
# 2,2,113210097,FABRICIO DE LIMA SILVA,fabricio.silva,[email protected],
def __init__(self, line):
#self.matricula, self.tt, self.tp, self.name, self.email = line.split(',')
self.tp, self.tt, self.matricula, _, self.name, self.email, _ = line.split(',')
self.submissions = []
self.questions = {} # {id: Question}
def _add_submission(self, questions, submission):
self.submissions.append(submission)
question = self.questions.get(submission.question, Question(submission.question))
question.submissions += 1
question.success = question.success or submission.success
self.questions[submission.question] = question
if submission.question not in questions:
questions.append(submission.question)
def __eq__(self, other):
return self.matricula == other.matricula
def __hash__(self):
return hash(self.matricula)
class Turma(): # FIXME -- translate
def __init__(self, name, alunos):
self.alunos = list(alunos)
self.nome = name
class Question(object):
def __init__(self, name):
self.name = name
self.submissions = 0
self.success = False
def __eq__(self, other):
return self.name == other.name
def __hash__(self):
return hash(self.name)
class _TestResult(object):
def __init__(self, result):
self.result = result.strip()
self.success = len(self.result.replace('.', '')) == 0
class Submission(object):
# FIXME max submission was removed... use it again?
# Input:
# recursos/resultados_teste.txt
# 16,112210650,1,2,29/11/2012 09:07:31,FFFF
# new format:
# 2013,12,12,12,09,00,314720|[email protected]|media_aluno|1|.....
def __init__(self, line):
#_, self.matricula, self.question, self.submission, date, tests = line.split(',')
#self.date = datetime(int(date[6:10]), int(date[3:5]), int(date[0:2]), int(date[11:13]), int(date[14:16]), int(date[17:19]))
date, self.email, self.question, self.submission, tests = line.split('|')
self.date = datetime(*map(int, date.split(',')))
self._test = _TestResult(tests)
self.success = self._test.success
self.result = self._test.result
def create_students_questions_and_classes(start_date=None, end_date=None):
# TODO Cache this return -- specially submissions
# FIXME Translate
alunos = {}
email_alunos = {}
dicionario_turmas_p = {}
dicionario_turmas_t = {}
questions = []
for linha_info in data_source.get_students_data():
aluno = Student(linha_info.strip())
email_alunos[aluno.email] = aluno
pratica = dicionario_turmas_p.get(aluno.tp, set())
pratica.add(aluno)
dicionario_turmas_p[aluno.tp] = pratica
teorica = dicionario_turmas_t.get(aluno.tt, set())
teorica.add(aluno)
dicionario_turmas_t[aluno.tt] = teorica
alunos[aluno.matricula] = aluno
# Reading submissions...
for linha_dados in reversed(data_source.get_test_data()):
submissao = Submission(linha_dados)
if submissao.email not in email_alunos:
continue
submissao.matricula = email_alunos[submissao.email].matricula
# FIXME leave this to controller..
if submissao.matricula in alunos and ((start_date is None and end_date is None) or (start_date <= submissao.date <= end_date)):
alunos.get(submissao.matricula)._add_submission(questions, submissao)
# creating classes...
turmas = {}
for turma in dicionario_turmas_p:
nome = "TP" + turma
turmas[nome] = Turma("TP" + turma, dicionario_turmas_p[turma])
for turma in dicionario_turmas_t:
nome = "TT" + turma
turmas[nome] = Turma("TT" + turma, dicionario_turmas_t[turma])
return OrderedDict(sorted(alunos.items(), key=lambda a: a[1].name)), questions, \
OrderedDict(sorted(turmas.items(), key=lambda a: a[0]))
def search_name(students, classes, parameters):
result = []
if parameters.strip() == '':
return students
match = False
for parameter in map(unicode.strip, parameters.split(',')):
if parameter in classes:
result.extend(classes[parameter].alunos)
match = True
if match:
return result
for parameter in map(unicode.strip, parameters.split(',')):
for matricula, student in students.items():
if parameter.lower() in student.name.lower() or parameter.lower() in matricula:
result.append(student)
return result
def _is_admin(user):
adms = open("recursos/admins.txt").readlines()
admins = []
for adm in adms:
adm = adm.strip()
admins.append(adm)
if user.email() in admins:
return True
return False
def info(user):
if user:
is_adm = _is_admin(user)
email = user.email()
else:
is_adm = False
email = ''
return user, is_adm, email