1 # vim: set et sw=4 sts=4 encoding=utf-8 foldmethod=marker :
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 turbogears.toolbox.catwalk import CatWalk
9 from model import InstanciaDeEntrega, Correccion, AND, DateTimeCol, Entrega, Grupo, AlumnoInscripto
10 from sqlobject import *
11 # from sercom import json
13 from subcontrollers import *
16 log = logging.getLogger("sercom.controllers")
18 class LoginForm(W.TableForm):
19 class Fields(W.WidgetsList):
20 login_user = W.TextField(label=_(u'Usuario'),
21 validator=V.NotEmpty())
22 login_password = W.PasswordField(label=_(u'Contraseña'),
23 validator=V.NotEmpty())
25 javascript = [W.JSSource("MochiKit.DOM.focusOnLoad('form_login_user');")]
26 submit = W.SubmitButton(name='login_submit')
27 submit_text = _(u'Ingresar')
29 class Root(controllers.RootController):
33 raise redirect(url('/dashboard'))
35 @expose(template='.templates.welcome')
36 @identity.require(identity.not_anonymous())
38 now = DateTimeCol.now()
39 if 'admin' in identity.current.permissions:
40 # TODO : Fijar el curso !!
41 correcciones = Correccion.selectBy(corrector=identity.current.user,
42 corregido=None).count()
43 instancias = list(InstanciaDeEntrega.select(
44 AND(InstanciaDeEntrega.q.inicio <= now,
45 InstanciaDeEntrega.q.fin > now))
46 .orderBy(InstanciaDeEntrega.q.fin))
47 return dict(a_corregir=correcciones,
48 instancias_activas=instancias, now=now)
50 if 'entregar' in identity.current.permissions:
51 # Proximas instancias de entrega
52 instancias = list(InstanciaDeEntrega.select(
53 AND(InstanciaDeEntrega.q.inicio <= now,
54 InstanciaDeEntrega.q.fin > now)).orderBy(InstanciaDeEntrega.q.fin))
55 # Ultimas N entregas realizadas
56 # Grupos en los que el usuario formo parte
57 m = [i.grupo.id for i in Grupo.selectByAlumno(identity.current.user)]
58 entregador = AlumnoInscripto.selectByAlumno(identity.current.user)
59 m.append(entregador.id)
60 entregas = list(Entrega.select(IN(Entrega.q.entregadorID, m))[:5])
61 return dict(instancias_activas=instancias, now=now, entregas=entregas)
64 @expose(template='.templates.login')
65 def login(self, forward_url=None, previous_url=None, tg_errors=None, *args,
69 flash(_(u'Hubo un error en el formulario!'))
71 if not identity.current.anonymous \
72 and identity.was_login_attempted() \
73 and not identity.get_identity_errors():
74 raise redirect(forward_url)
77 previous_url = request.path
79 if identity.was_login_attempted():
80 msg = _(u'Las credenciales proporcionadas no son correctas o no '
81 'le dan acceso al recurso solicitado.')
82 elif identity.get_identity_errors():
83 msg = _(u'Debe proveer sus credenciales antes de acceder a este '
86 msg = _(u'Por favor ingrese.')
87 forward_url = request.headers.get('Referer', '/')
89 fields = list(LoginForm.fields)
91 fields.append(W.HiddenField(name='forward_url'))
92 fields.extend([W.HiddenField(name=name) for name in request.params
93 if name not in ('login_user', 'login_password', 'login_submit',
95 login_form = LoginForm(fields=fields, action=previous_url)
97 values = dict(forward_url=forward_url)
98 values.update(request.params)
101 return dict(login_form=login_form, form_data=values, message=msg,
106 identity.current.logout()
107 raise redirect(url('/'))
109 docente = DocenteController()
111 grupo = GrupoController()
113 alumno = AlumnoController()
115 enunciado = EnunciadoController()
117 ejercicio = EjercicioController()
119 caso_de_prueba = CasoDePruebaController()
121 curso = CursoController()
123 docente_inscripto = DocenteInscriptoController()
125 alumno_inscripto = AlumnoInscriptoController()
127 correccion = CorreccionController()
129 admin = identity.SecureObject(CatWalk(model), identity.has_permission('admin'))
131 mis_entregas = MisEntregasController()
133 mis_correcciones = MisCorreccionesController()
135 #{{{ Agrega summarize a namespace tg de KID
136 def summarize(text, size, concat=True, continuation='...'):
137 """Summarize a string if it's length is greater than a specified size. This
138 is useful for table listings where you don't want the table to grow because
141 >>> from sercome.controllers
142 >>> text = '''Why is it that nobody remembers the name of Johann
143 ... Gambolputty de von Ausfern-schplenden-schlitter-crasscrenbon-fried-
144 ... digger-dingle-dangle-dongle-dungle-burstein-von-knacker-thrasher-apple-
145 ... banger-horowitz-ticolensic-grander-knotty-spelltinkle-grandlich-
146 ... grumblemeyer-spelterwasser-kurstlich-himbleeisen-bahnwagen-gutenabend-
147 ... bitte-ein-nurnburger-bratwustle-gernspurten-mitz-weimache-luber-
148 ... hundsfut-gumberaber-shonedanker-kalbsfleisch-mittler-aucher von
149 ... Hautkopft of Ulm?'''
150 >>> summarize(text, 30)
151 'Why is it that nobody remem...'
152 >>> summarize(text, 68, False, ' [...]')
153 'Why is it that nobody remembers the name of Johann\nGambolputty [...]'
154 >>> summarize(text, 68, continuation=' >>')
155 'Why is it that nobody remembers the name of Johann Gambolputty de >>'
159 text = text.replace('\n', ' ')
161 text = text[:size-len(continuation)] + continuation
164 def add_custom_stdvars(vars):
165 return vars.update(dict(summarize=summarize))
167 view.variable_providers.append(add_custom_stdvars)