-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.py
234 lines (180 loc) · 8.93 KB
/
console.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
# -*- coding: utf-8 -*-
# import logging
from google.appengine.api import mail
from google.appengine.api import users
import webapp2
from datetime import datetime, timedelta
from logica_console import Console
import model
import os
import jinja2
import util
DEBUG = False
jinja_environment = jinja2.Environment(extensions=['jinja2.ext.i18n', 'jinja2.ext.loopcontrols'], loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class Fields(dict):
def __new_setitem__(self, value):
raise Exception("frozen dict")
def __init__(self, request):
self['datainicio'] = request.get("di", default_value="")
self['datafim'] = request.get("df", default_value="")
self['horainicio'] = request.get("hi", default_value="")
self['horafim'] = request.get("hf", default_value="")
self['tabela_grafico'] = request.get("tg", default_value="")
self['turma'] = request.get("trm", default_value="")
self['questoes'] = request.get("qts", default_value="")
self.__setitem__ = self.__new_setitem__
def trata_parametros(datainicio, horainicio, datafim, horafim, questoes):
hoje = datetime.now() - timedelta(0, 3 * 3600)
if not horainicio:
if 8 <= hoje.hour <= 17:
horainicial = timedelta(0, (hoje.hour - (hoje.hour % 2)) * 3600)
else:
horainicial = timedelta(0, 0)
else:
hora = horainicio[0:2]
minutos = horainicio[2:4]
horainicial = timedelta(0, (int(hora) * 3600) + (int(minutos) * 60))
""" Tratamento Parametro Hora Fim """
if not horafim:
horafinal = timedelta(0, (hoje.hour * 3600) + (hoje.minute * 60) + hoje.second)
else:
hora = horafim[0:2]
minutos = horafim[2:4]
horafinal = timedelta(0, (int(hora) * 3600) + (int(minutos) * 60))
""" Tratamento Parametro Data Fim """
if not datafim:
datafinal = datetime(hoje.year, hoje.month, hoje.day) + horafinal
else:
datafinal = datetime.strptime(datafim, "%d%m%Y") + horafinal
""" Tratamento Parametro Data Inicio """
if not datainicio:
datainicial = datetime(hoje.year, hoje.month, hoje.day) + horainicial
else:
if horafim and not datafim:
datainicial = datetime.strptime(datainicio, "%d%m%Y") + horainicial
datafinal = datetime.strptime(datainicio, "%d%m%Y") + horafinal
else:
datainicial = datetime.strptime(datainicio, "%d%m%Y") + horainicial
if questoes:
questoes = questoes.replace(" ", "").split(",")
else:
questoes = []
return datainicial, datafinal, questoes
class WebApp(util.AppRequestHandler):
def grafico(self, console, datainicial, datafinal, questao):
alunos_ok, alunos_error, alunos_missing = console.sumario_grafico(questao)
grafico_values = {
"certas": len(alunos_ok),
"erradas": len(alunos_error),
"num_nao_fizeram": len(alunos_missing),
"questao_sumarizada": questao,
"fizeram_certo": alunos_ok,
"fizeram_errado": alunos_error,
"nao_fizeram": alunos_missing,
"data_report": datetime.strftime(datainicial, "%d%m%Y"),
"dataf_report": datetime.strftime(datafinal, "%d%m%Y")
}
grafico_env = jinja_environment.get_template(
"templates/grafico.html")
self.response.out.write(grafico_env.render(grafico_values))
def get(self):
self.response.headers["Content-Type"] = "text/html; charset=utf-8"
user, is_adm, email = model.info(users.get_current_user())
fields = Fields(self.request)
self.login(user is not None, fields['datainicio'], fields['datafim'], fields['horainicio'],
fields['horafim'], fields['tabela_grafico'], fields['turma'], fields['questoes'])
datainicial, datafinal, lista_questoes = trata_parametros(fields['datainicio'], fields['horainicio'],
fields['datafim'], fields['horafim'],
fields['questoes'])
console = Console(datainicial, datafinal, fields['turma'], lista_questoes)
# cabecalho
self.render("templates/cabecalho.html",
{"print_intervalo": u"Submissões no intervalo (Das %s de %s às %s):" % (datetime.strftime(datainicial, "%H:%M"),
datetime.strftime(datainicial, "%d/%m/%Y"),
datetime.strftime(datafinal, "%H:%M de %d/%m/%Y"))})
# main
if fields['tabela_grafico'] == "graph":
self.grafico(console, datainicial, datafinal, lista_questoes[0])
else:
# This will create a RefreshData.get call:
self.render("templates/ajax.html", fields)
# forms
formularios_values = dict(fields)
formularios_values["checkg"] = fields['tabela_grafico'] and "true" or "false"
formularios_values["checkt"] = fields['tabela_grafico'] and "false" or "true"
self.render("templates/formularios.html", formularios_values)
def login(self, loged, datainicio, datafim, horainicio, horafim, tabela_grafico, turma, questoes):
url = str(("/?trm=%s&di=%s&hi=%s&df=%s&hf=%s&tg=%s&qts=%s") %
(turma, datainicio, horainicio, datafim, horafim, tabela_grafico, questoes))
if loged:
log = '\"%s\"' % users.create_logout_url(url)
loger = "Logout"
else:
log = '\"%s\"' % users.create_login_url(url)
loger = "Login"
login_values = {
"log": log,
"loger": loger
}
login = jinja_environment.get_template("templates/login.html")
self.response.out.write(login.render(login_values))
navigation = jinja_environment.get_template('static/templates/navigation.html')
self.response.out.write(navigation.render())
class RefreshData(WebApp, util.AppRequestHandler):
def get(self):
user, is_adm, email = model.info(users.get_current_user())
self.response.headers["Content-Type"] = "text/html; charset=utf-8"
fields = Fields(self.request)
datainicial, datafinal, lista_questoes = trata_parametros(fields['datainicio'], fields['horainicio'],
fields['datafim'], fields['horafim'],
fields['questoes'])
console = Console(datainicial, datafinal, fields['turma'], lista_questoes)
if console.lista_questoes:
if fields['tabela_grafico'] == "table":
values = dict(fields)
values.update({
"lista_questoes": console.lista_questoes,
"alunos": console.turma_analisada,
"alunos_sessao": console.alunos_set,
"tabela_grafico": "graph",
"email": email,
"admin": is_adm
})
self.render("templates/tabela.html", values)
else:
values = {
"alunos": console.turma_analisada,
"alunos_sessao": console.alunos_set,
"data_report": datetime.strftime(datainicial, "%d%m%Y"),
"dataf_report": datetime.strftime(datafinal, "%d%m%Y"),
"email": email,
"admin": is_adm
}
self.render("templates/lista_submissoes.html", values)
class Codigo(webapp2.RequestHandler):
def get(self):
matricula_emails = model.create_students_questions_and_classes()[0]
user, is_adm, email = model.info(users.get_current_user())
#questao = self.request.get("questao", default_value="")
#submissao = self.request.get("submissao", default_value="")
matricula = self.request.get("matricula", default_value="")
# Service is down and we are missing k field password (it is a secret)
if is_adm or email == matricula_emails[matricula]:
pass
#self.response.out.write(cgi.escape((urllib2.urlopen(
# ("http://download-webshell.appspot.com/download?view=true&questao=%s&submissao=%s&matricula=%s&k=") % (questao, submissao, matricula))).read()))
class Email(webapp2.RequestHandler):
def post(self):
user = users.get_current_user()
send = user.email()
receiver = self.request.get("receiver")
subj = self.request.get("subject")
body = self.request.get("body")
message = mail.EmailMessage(sender=send,
subject=subj)
message.to = receiver
message.body = body
message.send()
self.redirect("/")
app = webapp2.WSGIApplication(
[("/", WebApp), ("/refreshdata", RefreshData), ("/codigos", Codigo), ("/email", Email)], debug=True)