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