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