1 # vim: set et sw=4 sts=4 encoding=utf-8 :
3 from turbogears import controllers, expose, view
4 from turbogears import widgets as W, validators as V
5 from turbogears import identity, redirect
6 from cherrypy import request, response
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):
28 @expose(template='.templates.welcome')
29 @identity.require(identity.has_permission('entregar'))
32 log.debug('Happy TurboGears Controller Responding For Duty')
33 return dict(now=time.ctime())
35 @expose(template='.templates.login')
36 def login(self, forward_url=None, previous_url=None, tg_errors=None, *args,
40 flash(_(u'Hubo un error en el formulario!'))
42 if not identity.current.anonymous \
43 and identity.was_login_attempted() \
44 and not identity.get_identity_errors():
45 raise redirect(forward_url)
48 previous_url = request.path
50 if identity.was_login_attempted():
51 msg = _(u'Las credenciales proporcionadas no son correctas o no '
52 'le dan acceso al recurso solicitado.')
53 elif identity.get_identity_errors():
54 msg = _(u'Debe proveer sus credenciales antes de acceder a este '
57 msg = _(u'Por favor ingrese.')
58 forward_url = request.headers.get('Referer', '/')
60 fields = list(LoginForm.fields)
62 fields.append(W.HiddenField(name='forward_url'))
63 fields.extend([W.HiddenField(name=name) for name in request.params
64 if name not in ('login_user', 'login_password', 'login_submit',
66 login_form = LoginForm(fields=fields, action=previous_url)
68 values = dict(forward_url=forward_url)
69 values.update(request.params)
72 return dict(login_form=login_form, form_data=values, message=msg,
77 identity.current.logout()
80 docente = DocenteController()
82 alumno = AlumnoController()
84 enunciado = EnunciadoController()
86 ejercicio = EjercicioController()
88 caso_de_prueba = CasoDePruebaController()
90 curso = CursoController()
92 docente_inscripto = DocenteInscriptoController()
94 #{{{ Agrega summarize a namespace tg de KID
95 def summarize(text, size, concat=True, continuation='...'):
96 """Summarize a string if it's length is greater than a specified size. This
97 is useful for table listings where you don't want the table to grow because
100 >>> from sercome.controllers
101 >>> text = '''Why is it that nobody remembers the name of Johann
102 ... Gambolputty de von Ausfern-schplenden-schlitter-crasscrenbon-fried-
103 ... digger-dingle-dangle-dongle-dungle-burstein-von-knacker-thrasher-apple-
104 ... banger-horowitz-ticolensic-grander-knotty-spelltinkle-grandlich-
105 ... grumblemeyer-spelterwasser-kurstlich-himbleeisen-bahnwagen-gutenabend-
106 ... bitte-ein-nurnburger-bratwustle-gernspurten-mitz-weimache-luber-
107 ... hundsfut-gumberaber-shonedanker-kalbsfleisch-mittler-aucher von
108 ... Hautkopft of Ulm?'''
109 >>> summarize(text, 30)
110 'Why is it that nobody remem...'
111 >>> summarize(text, 68, False, ' [...]')
112 'Why is it that nobody remembers the name of Johann\nGambolputty [...]'
113 >>> summarize(text, 68, continuation=' >>')
114 'Why is it that nobody remembers the name of Johann Gambolputty de >>'
118 text = text.replace('\n', ' ')
120 text = text[:size-len(continuation)] + continuation
123 def add_custom_stdvars(vars):
124 return vars.update(dict(summarize=summarize))
126 view.variable_providers.append(add_custom_stdvars)