1 # vim: set et sw=4 sts=4 encoding=utf-8 foldmethod=marker :
3 from turbogears import controllers, expose, view, url
4 from turbogears import widgets as W, validators as V
5 from turbogears import identity, redirect
6 from cherrypy import request, response
7 from turbogears.toolbox.catwalk import CatWalk
9 from model import InstanciaDeEntrega, Correccion, AND, DateTimeCol, Entrega
10 # from sercom import json
12 from subcontrollers import *
15 log = logging.getLogger("sercom.controllers")
17 class LoginForm(W.TableForm):
18 class Fields(W.WidgetsList):
19 login_user = W.TextField(label=_(u'Usuario'),
20 validator=V.NotEmpty())
21 login_password = W.PasswordField(label=_(u'Contraseña'),
22 validator=V.NotEmpty())
24 javascript = [W.JSSource("MochiKit.DOM.focusOnLoad('form_login_user');")]
25 submit = W.SubmitButton(name='login_submit')
26 submit_text = _(u'Ingresar')
28 class Root(controllers.RootController):
32 raise redirect(url('/dashboard'))
34 @expose(template='.templates.welcome')
35 @identity.require(identity.not_anonymous())
37 now = DateTimeCol.now()
38 if 'admin' in identity.current.permissions:
39 # TODO : Fijar el curso !!
40 correcciones = Correccion.selectBy(corrector=identity.current.user,
41 corregido=None).count()
42 instancias = list(InstanciaDeEntrega.select(
43 AND(InstanciaDeEntrega.q.inicio <= now,
44 InstanciaDeEntrega.q.fin > now))
45 .orderBy(InstanciaDeEntrega.q.fin))
46 return dict(a_corregir=correcciones,
47 instancias_activas=instancias, now=now)
49 if 'entregar' in identity.current.permissions:
50 # Proximas instancias de entrega
51 instancias = list(InstanciaDeEntrega.select(
52 AND(InstanciaDeEntrega.q.inicio <= now,
53 InstanciaDeEntrega.q.fin > now)).orderBy(InstanciaDeEntrega.q.fin))
54 # Ultimas N entregas realizadas
55 entregas = list(Entrega.select(Entrega.q.entregadorID == identity.current.user.id))
56 return dict(instancias_activas=instancias, now=now, entregas=entregas)
59 @expose(template='.templates.login')
60 def login(self, forward_url=None, previous_url=None, tg_errors=None, *args,
64 flash(_(u'Hubo un error en el formulario!'))
66 if not identity.current.anonymous \
67 and identity.was_login_attempted() \
68 and not identity.get_identity_errors():
69 raise redirect(forward_url)
72 previous_url = request.path
74 if identity.was_login_attempted():
75 msg = _(u'Las credenciales proporcionadas no son correctas o no '
76 'le dan acceso al recurso solicitado.')
77 elif identity.get_identity_errors():
78 msg = _(u'Debe proveer sus credenciales antes de acceder a este '
81 msg = _(u'Por favor ingrese.')
82 forward_url = request.headers.get('Referer', '/')
84 fields = list(LoginForm.fields)
86 fields.append(W.HiddenField(name='forward_url'))
87 fields.extend([W.HiddenField(name=name) for name in request.params
88 if name not in ('login_user', 'login_password', 'login_submit',
90 login_form = LoginForm(fields=fields, action=previous_url)
92 values = dict(forward_url=forward_url)
93 values.update(request.params)
96 return dict(login_form=login_form, form_data=values, message=msg,
101 identity.current.logout()
102 raise redirect(url('/'))
104 docente = DocenteController()
106 grupo = GrupoController()
108 alumno = AlumnoController()
110 enunciado = EnunciadoController()
112 ejercicio = EjercicioController()
114 caso_de_prueba = CasoDePruebaController()
116 curso = CursoController()
118 docente_inscripto = DocenteInscriptoController()
120 alumno_inscripto = AlumnoInscriptoController()
122 correccion = CorreccionController()
124 admin = identity.SecureObject(CatWalk(model), identity.has_permission('admin'))
126 mis_entregas = MisEntregasController()
128 #{{{ Agrega summarize a namespace tg de KID
129 def summarize(text, size, concat=True, continuation='...'):
130 """Summarize a string if it's length is greater than a specified size. This
131 is useful for table listings where you don't want the table to grow because
134 >>> from sercome.controllers
135 >>> text = '''Why is it that nobody remembers the name of Johann
136 ... Gambolputty de von Ausfern-schplenden-schlitter-crasscrenbon-fried-
137 ... digger-dingle-dangle-dongle-dungle-burstein-von-knacker-thrasher-apple-
138 ... banger-horowitz-ticolensic-grander-knotty-spelltinkle-grandlich-
139 ... grumblemeyer-spelterwasser-kurstlich-himbleeisen-bahnwagen-gutenabend-
140 ... bitte-ein-nurnburger-bratwustle-gernspurten-mitz-weimache-luber-
141 ... hundsfut-gumberaber-shonedanker-kalbsfleisch-mittler-aucher von
142 ... Hautkopft of Ulm?'''
143 >>> summarize(text, 30)
144 'Why is it that nobody remem...'
145 >>> summarize(text, 68, False, ' [...]')
146 'Why is it that nobody remembers the name of Johann\nGambolputty [...]'
147 >>> summarize(text, 68, continuation=' >>')
148 'Why is it that nobody remembers the name of Johann Gambolputty de >>'
152 text = text.replace('\n', ' ')
154 text = text[:size-len(continuation)] + continuation
157 def add_custom_stdvars(vars):
158 return vars.update(dict(summarize=summarize))
160 view.variable_providers.append(add_custom_stdvars)