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
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 if 'admin' in identity.current.permissions:
38 # TODO : Fijar el curso !!
39 correcciones = Correccion.selectBy(corrector=identity.current.user,
40 corregido=None).count()
41 now = DateTimeCol.now()
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 @expose(template='.templates.login')
50 def login(self, forward_url=None, previous_url=None, tg_errors=None, *args,
54 flash(_(u'Hubo un error en el formulario!'))
56 if not identity.current.anonymous \
57 and identity.was_login_attempted() \
58 and not identity.get_identity_errors():
59 raise redirect(forward_url)
62 previous_url = request.path
64 if identity.was_login_attempted():
65 msg = _(u'Las credenciales proporcionadas no son correctas o no '
66 'le dan acceso al recurso solicitado.')
67 elif identity.get_identity_errors():
68 msg = _(u'Debe proveer sus credenciales antes de acceder a este '
71 msg = _(u'Por favor ingrese.')
72 forward_url = request.headers.get('Referer', '/')
74 fields = list(LoginForm.fields)
76 fields.append(W.HiddenField(name='forward_url'))
77 fields.extend([W.HiddenField(name=name) for name in request.params
78 if name not in ('login_user', 'login_password', 'login_submit',
80 login_form = LoginForm(fields=fields, action=previous_url)
82 values = dict(forward_url=forward_url)
83 values.update(request.params)
86 return dict(login_form=login_form, form_data=values, message=msg,
91 identity.current.logout()
92 raise redirect(url('/'))
94 docente = DocenteController()
96 grupo = GrupoController()
98 alumno = AlumnoController()
100 enunciado = EnunciadoController()
102 ejercicio = EjercicioController()
104 caso_de_prueba = CasoDePruebaController()
106 curso = CursoController()
108 docente_inscripto = DocenteInscriptoController()
110 alumno_inscripto = AlumnoInscriptoController()
112 correccion = CorreccionController()
114 admin = identity.SecureObject(CatWalk(model), identity.has_permission('admin'))
116 #{{{ Agrega summarize a namespace tg de KID
117 def summarize(text, size, concat=True, continuation='...'):
118 """Summarize a string if it's length is greater than a specified size. This
119 is useful for table listings where you don't want the table to grow because
122 >>> from sercome.controllers
123 >>> text = '''Why is it that nobody remembers the name of Johann
124 ... Gambolputty de von Ausfern-schplenden-schlitter-crasscrenbon-fried-
125 ... digger-dingle-dangle-dongle-dungle-burstein-von-knacker-thrasher-apple-
126 ... banger-horowitz-ticolensic-grander-knotty-spelltinkle-grandlich-
127 ... grumblemeyer-spelterwasser-kurstlich-himbleeisen-bahnwagen-gutenabend-
128 ... bitte-ein-nurnburger-bratwustle-gernspurten-mitz-weimache-luber-
129 ... hundsfut-gumberaber-shonedanker-kalbsfleisch-mittler-aucher von
130 ... Hautkopft of Ulm?'''
131 >>> summarize(text, 30)
132 'Why is it that nobody remem...'
133 >>> summarize(text, 68, False, ' [...]')
134 'Why is it that nobody remembers the name of Johann\nGambolputty [...]'
135 >>> summarize(text, 68, continuation=' >>')
136 'Why is it that nobody remembers the name of Johann Gambolputty de >>'
140 text = text.replace('\n', ' ')
142 text = text[:size-len(continuation)] + continuation
145 def add_custom_stdvars(vars):
146 return vars.update(dict(summarize=summarize))
148 view.variable_providers.append(add_custom_stdvars)