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):
17 W.TextField(name='login_user', label=_(u'Usuario'),
18 validator=V.NotEmpty()),
19 W.PasswordField(name='login_password', 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 enunciado = EnunciadoController()
84 caso_de_prueba = CasoDePruebaController()
86 #{{{ Agrega summarize a namespace tg de KID
87 def summarize(text, size, concat=True, continuation='...'):
88 """Summarize a string if it's length is greater than a specified size. This
89 is useful for table listings where you don't want the table to grow because
92 >>> from sercome.controllers
93 >>> text = '''Why is it that nobody remembers the name of Johann
94 ... Gambolputty de von Ausfern-schplenden-schlitter-crasscrenbon-fried-
95 ... digger-dingle-dangle-dongle-dungle-burstein-von-knacker-thrasher-apple-
96 ... banger-horowitz-ticolensic-grander-knotty-spelltinkle-grandlich-
97 ... grumblemeyer-spelterwasser-kurstlich-himbleeisen-bahnwagen-gutenabend-
98 ... bitte-ein-nurnburger-bratwustle-gernspurten-mitz-weimache-luber-
99 ... hundsfut-gumberaber-shonedanker-kalbsfleisch-mittler-aucher von
100 ... Hautkopft of Ulm?'''
101 >>> summarize(text, 30)
102 'Why is it that nobody remem...'
103 >>> summarize(text, 68, False, ' [...]')
104 'Why is it that nobody remembers the name of Johann\nGambolputty [...]'
105 >>> summarize(text, 68, continuation=' >>')
106 'Why is it that nobody remembers the name of Johann Gambolputty de >>'
110 text = text.replace('\n', ' ')
112 text = text[:size-len(continuation)] + continuation
115 def add_custom_stdvars(vars):
116 return vars.update(dict(summarize=summarize))
118 view.variable_providers.append(add_custom_stdvars)