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
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 Root(controllers.RootController):
17 @expose(template='.templates.welcome')
18 @identity.require(identity.has_permission('entregar'))
21 log.debug('Happy TurboGears Controller Responding For Duty')
22 return dict(now=time.ctime())
24 @expose(template='.templates.login')
25 def login(self, forward_url=None, previous_url=None, tg_errors=None, *args,
29 flash(_(u'Hubo un error en el formulario!'))
31 if not identity.current.anonymous \
32 and identity.was_login_attempted() \
33 and not identity.get_identity_errors():
34 raise redirect(forward_url)
37 previous_url = request.path
39 if identity.was_login_attempted():
40 msg = _(u'Las credenciales proporcionadas no son correctas o no '
41 'le dan acceso al recurso solicitado.')
42 elif identity.get_identity_errors():
43 msg = _(u'Debe proveer sus credenciales antes de acceder a este '
46 msg = _(u'Por favor ingrese.')
47 forward_url = request.headers.get('Referer', '/')
50 w.TextField(name='login_user', label=_(u'Usuario'),
51 validator=validators.NotEmpty()),
52 w.PasswordField(name='login_password', label=_(u'Contraseña'),
53 validator=validators.NotEmpty())
56 fields.append(w.HiddenField(name='forward_url'))
57 fields.extend([w.HiddenField(name=name) for name in request.params
58 if name not in ('login_user', 'login_password', 'login_submit',
61 submit = w.SubmitButton(name='login_submit')
63 login_form = w.TableForm(fields=fields, action=previous_url,
64 submit_text=_(u'Ingresar'), submit=submit)
66 values = dict(forward_url=forward_url)
67 values.update(request.params)
70 return dict(login_form=login_form, form_data=values, message=msg,
75 identity.current.logout()
78 docente = DocenteController()
79 enunciado = EnunciadoController()
81 #{{{ Agrega summarize a namespace tg de KID
82 def summarize(text, size, concat=True, continuation='...'):
83 """Summarize a string if it's length is greater than a specified size. This
84 is useful for table listings where you don't want the table to grow because
87 >>> from sercome.controllers
88 >>> text = '''Why is it that nobody remembers the name of Johann
89 ... Gambolputty de von Ausfern-schplenden-schlitter-crasscrenbon-fried-
90 ... digger-dingle-dangle-dongle-dungle-burstein-von-knacker-thrasher-apple-
91 ... banger-horowitz-ticolensic-grander-knotty-spelltinkle-grandlich-
92 ... grumblemeyer-spelterwasser-kurstlich-himbleeisen-bahnwagen-gutenabend-
93 ... bitte-ein-nurnburger-bratwustle-gernspurten-mitz-weimache-luber-
94 ... hundsfut-gumberaber-shonedanker-kalbsfleisch-mittler-aucher von
95 ... Hautkopft of Ulm?'''
96 >>> summarize(text, 30)
97 'Why is it that nobody remem...'
98 >>> summarize(text, 68, False, ' [...]')
99 'Why is it that nobody remembers the name of Johann\nGambolputty [...]'
100 >>> summarize(text, 68, continuation=' >>')
101 'Why is it that nobody remembers the name of Johann Gambolputty de >>'
105 text = text.replace('\n', ' ')
107 text = text[:size-len(continuation)] + continuation
110 def add_custom_stdvars(vars):
111 return vars.update(dict(summarize=summarize))
113 view.variable_providers.append(add_custom_stdvars)