1 # vim: set et sw=4 sts=4 encoding=utf-8 :
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 model import InstanciaDeEntrega, Correccion, AND, DateTimeCol
8 # from sercom import json
10 from subcontrollers import *
13 log = logging.getLogger("sercom.controllers")
15 class LoginForm(W.TableForm):
16 class Fields(W.WidgetsList):
17 login_user = W.TextField(label=_(u'Usuario'),
18 validator=V.NotEmpty())
19 login_password = W.PasswordField(label=_(u'Contraseña'),
20 validator=V.NotEmpty())
22 javascript = [W.JSSource("MochiKit.DOM.focusOnLoad('form_login_user');")]
23 submit = W.SubmitButton(name='login_submit')
24 submit_text = _(u'Ingresar')
26 class Root(controllers.RootController):
30 raise redirect(url('/dashboard'))
32 @expose(template='.templates.welcome')
33 @identity.require(identity.not_anonymous())
35 if 'admin' in identity.current.permissions:
36 # TODO : Fijar el curso !!
37 correcciones = Correccion.selectBy(corrector=identity.current.user,
38 corregido=None).count()
39 now = DateTimeCol.now()
40 instancias = list(InstanciaDeEntrega.select(
41 AND(InstanciaDeEntrega.q.inicio <= now,
42 InstanciaDeEntrega.q.fin > now))
43 .orderBy(InstanciaDeEntrega.q.fin))
44 return dict(a_corregir=correcciones,
45 instancias_activas=instancias, now=now)
47 @expose(template='.templates.login')
48 def login(self, forward_url=None, previous_url=None, tg_errors=None, *args,
52 flash(_(u'Hubo un error en el formulario!'))
54 if not identity.current.anonymous \
55 and identity.was_login_attempted() \
56 and not identity.get_identity_errors():
57 raise redirect(forward_url)
60 previous_url = request.path
62 if identity.was_login_attempted():
63 msg = _(u'Las credenciales proporcionadas no son correctas o no '
64 'le dan acceso al recurso solicitado.')
65 elif identity.get_identity_errors():
66 msg = _(u'Debe proveer sus credenciales antes de acceder a este '
69 msg = _(u'Por favor ingrese.')
70 forward_url = request.headers.get('Referer', '/')
72 fields = list(LoginForm.fields)
74 fields.append(W.HiddenField(name='forward_url'))
75 fields.extend([W.HiddenField(name=name) for name in request.params
76 if name not in ('login_user', 'login_password', 'login_submit',
78 login_form = LoginForm(fields=fields, action=previous_url)
80 values = dict(forward_url=forward_url)
81 values.update(request.params)
84 return dict(login_form=login_form, form_data=values, message=msg,
89 identity.current.logout()
90 raise redirect(url('/'))
92 docente = DocenteController()
94 grupo = GrupoController()
96 alumno = AlumnoController()
98 enunciado = EnunciadoController()
100 ejercicio = EjercicioController()
102 caso_de_prueba = CasoDePruebaController()
104 curso = CursoController()
106 docente_inscripto = DocenteInscriptoController()
108 alumno_inscripto = AlumnoInscriptoController()
110 correccion = CorreccionController()
113 #{{{ Agrega summarize a namespace tg de KID
114 def summarize(text, size, concat=True, continuation='...'):
115 """Summarize a string if it's length is greater than a specified size. This
116 is useful for table listings where you don't want the table to grow because
119 >>> from sercome.controllers
120 >>> text = '''Why is it that nobody remembers the name of Johann
121 ... Gambolputty de von Ausfern-schplenden-schlitter-crasscrenbon-fried-
122 ... digger-dingle-dangle-dongle-dungle-burstein-von-knacker-thrasher-apple-
123 ... banger-horowitz-ticolensic-grander-knotty-spelltinkle-grandlich-
124 ... grumblemeyer-spelterwasser-kurstlich-himbleeisen-bahnwagen-gutenabend-
125 ... bitte-ein-nurnburger-bratwustle-gernspurten-mitz-weimache-luber-
126 ... hundsfut-gumberaber-shonedanker-kalbsfleisch-mittler-aucher von
127 ... Hautkopft of Ulm?'''
128 >>> summarize(text, 30)
129 'Why is it that nobody remem...'
130 >>> summarize(text, 68, False, ' [...]')
131 'Why is it that nobody remembers the name of Johann\nGambolputty [...]'
132 >>> summarize(text, 68, continuation=' >>')
133 'Why is it that nobody remembers the name of Johann Gambolputty de >>'
137 text = text.replace('\n', ' ')
139 text = text[:size-len(continuation)] + continuation
142 def add_custom_stdvars(vars):
143 return vars.update(dict(summarize=summarize))
145 view.variable_providers.append(add_custom_stdvars)