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