1 # vim: set et sw=4 sts=4 encoding=utf-8 :
3 from turbogears import controllers, expose, view, url
4 from turbogears import widgets as W, validators as V
5 from turbogears import identity, redirect
6 from cherrypy import request, response
7 from model import InstanciaDeEntrega, Correccion
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):
30 raise redirect('/dashboard')
32 @expose(template='.templates.welcome')
33 @identity.require(identity.has_permission('entregar'))
37 if 'admin' in identity.current.permissions:
38 from sqlobject import DateTimeCol
39 # TODO : Fijar el curso !!
40 record['entregas_para_corregir'] = Correccion.selectBy(corrector=identity.current.user, nota=None).count()
42 record['proxima_entrega'] = InstanciaDeEntrega.select(InstanciaDeEntrega.q.inicio >= DateTimeCol.now() and InstanciaDeEntrega.q.fin > DateTimeCol.now()).getOne()
43 record['proxima_entrega'] = record['proxima_entrega'][0]
45 record['proxima_entrega'] = None
46 log.debug('Happy TurboGears Controller Responding For Duty')
47 return dict(now=time.ctime(), record=record)
49 @expose(template='.templates.login')
50 def login(self, forward_url=None, previous_url=None, tg_errors=None, *args,
54 flash(_(u'Hubo un error en el formulario!'))
56 if not identity.current.anonymous \
57 and identity.was_login_attempted() \
58 and not identity.get_identity_errors():
59 raise redirect(forward_url)
62 previous_url = request.path
64 if identity.was_login_attempted():
65 msg = _(u'Las credenciales proporcionadas no son correctas o no '
66 'le dan acceso al recurso solicitado.')
67 elif identity.get_identity_errors():
68 msg = _(u'Debe proveer sus credenciales antes de acceder a este '
71 msg = _(u'Por favor ingrese.')
72 forward_url = request.headers.get('Referer', '/')
74 fields = list(LoginForm.fields)
76 fields.append(W.HiddenField(name='forward_url'))
77 fields.extend([W.HiddenField(name=name) for name in request.params
78 if name not in ('login_user', 'login_password', 'login_submit',
80 login_form = LoginForm(fields=fields, action=previous_url)
82 values = dict(forward_url=forward_url)
83 values.update(request.params)
86 return dict(login_form=login_form, form_data=values, message=msg,
91 identity.current.logout()
94 docente = DocenteController()
96 grupo = GrupoController()
98 alumno = AlumnoController()
100 enunciado = EnunciadoController()
102 ejercicio = EjercicioController()
104 caso_de_prueba = CasoDePruebaController()
106 curso = CursoController()
108 docente_inscripto = DocenteInscriptoController()
110 correccion = CorreccionController()
113 #{{{ Agrega summarize a namespace tg de KID
114 def summarize(text, size, concat=True, continuation='...'):
115 """Summarize a string if it's length is greater than a specified size. This
116 is useful for table listings where you don't want the table to grow because
119 >>> from sercome.controllers
120 >>> text = '''Why is it that nobody remembers the name of Johann
121 ... Gambolputty de von Ausfern-schplenden-schlitter-crasscrenbon-fried-
122 ... digger-dingle-dangle-dongle-dungle-burstein-von-knacker-thrasher-apple-
123 ... banger-horowitz-ticolensic-grander-knotty-spelltinkle-grandlich-
124 ... grumblemeyer-spelterwasser-kurstlich-himbleeisen-bahnwagen-gutenabend-
125 ... bitte-ein-nurnburger-bratwustle-gernspurten-mitz-weimache-luber-
126 ... hundsfut-gumberaber-shonedanker-kalbsfleisch-mittler-aucher von
127 ... Hautkopft of Ulm?'''
128 >>> summarize(text, 30)
129 'Why is it that nobody remem...'
130 >>> summarize(text, 68, False, ' [...]')
131 'Why is it that nobody remembers the name of Johann\nGambolputty [...]'
132 >>> summarize(text, 68, continuation=' >>')
133 'Why is it that nobody remembers the name of Johann Gambolputty de >>'
137 text = text.replace('\n', ' ')
139 text = text[:size-len(continuation)] + continuation
142 def add_custom_stdvars(vars):
143 return vars.update(dict(summarize=summarize))
145 view.variable_providers.append(add_custom_stdvars)