1 # vim: set et sw=4 sts=4 encoding=utf-8 foldmethod=marker :
4 from turbogears import controllers, expose, redirect
5 from turbogears import validate, flash, error_handler
6 from turbogears import validators as V
7 from turbogears import widgets as W
8 from turbogears import identity
9 from turbogears import paginate
10 from docutils.core import publish_parts
11 from sercom.subcontrollers import validate as val
12 from sercom.model import Ejercicio, Curso, Enunciado
13 from cherrypy import request, response
14 from instancia import InstanciaController
24 fknamepl = fkname + 's'
28 def validate_fk(data):
29 fk = data.get(fkname + 'ID', None)
35 flash(_(u'No se pudo crear el nuevo %s porque el %s con '
36 'identificador %d no existe.' % (name, fkname, fk)))
37 raise redirect('new', **data)
38 data.pop(fkname + 'ID', None)
43 return val.validate_get(cls, name, id)
45 def validate_set(id, data):
47 return val.validate_set(cls, name, id, data)
49 def validate_new(data):
51 return val.validate_new(cls, name, data)
54 return val.validate_del(cls, name, id)
58 class EjercicioForm(W.TableForm):
59 class Fields(W.WidgetsList):
60 cursoID = W.HiddenField(validator=V.Int)
61 numero = W.TextField(name="numero",label=_(u'Nro'),
62 help_text=_(u'Requerido.'),
63 validator=V.Int(not_empty=True))
64 fk = W.SingleSelectField(name=fkname+'ID', label=_(fkname.capitalize()),
65 validator=V.Int(not_empty=True))
66 grupal = W.CheckBox(name='grupal', label=_(u"Grupal?"), validator=V.Bool(if_empty=0), default=0)
68 javascript = [W.JSSource("MochiKit.DOM.focusOnLoad('form_numero');")]
70 form = EjercicioForm()
74 class EjercicioController(controllers.Controller, identity.SecureResource):
75 """Basic model admin interface"""
76 require = identity.has_permission('entregar')
78 instancia = InstanciaController()
80 @expose(template='kid:%s.templates.list' % __name__)
81 @validate(validators=dict(curso=V.Int))
83 def list(self, curso):
84 """List records in model"""
85 r = cls.selectBy(cursoID=curso).orderBy(cls.q.numero)
86 return dict(records=r, name=name, namepl=namepl, curso=Curso.get(curso))
88 @expose(template='kid:%s.templates.new' % __name__)
89 @validate(validators=dict(curso=V.Int))
90 @identity.require(identity.has_permission('admin'))
91 def new(self, curso, **kw):
92 """Create new records in model"""
94 curso = Curso.get(curso)
95 options = { fkname+'ID': [(fk.id, fk.shortrepr()) for fk in
96 Enunciado.selectBy(anio=curso.anio,
97 cuatrimestre=curso.cuatrimestre)] }
98 return dict(name=name, namepl=namepl, form=form, values=kw, options=options)
102 @identity.require(identity.has_permission('admin'))
104 def create(self, **kw):
105 """Save or create record to model"""
107 flash(_(u'Se creó un nuevo %s.') % name)
108 raise redirect('list/%s' % kw['cursoID'])
110 @expose(template='kid:%s.templates.edit' % __name__)
111 @identity.require(identity.has_permission('admin'))
112 def edit(self, id, **kw):
113 """Edit record in model"""
115 curso = Curso.get(r.cursoID)
116 options = { fkname+'ID': [(fk.id, fk.shortrepr()) for fk in
117 Enunciado.selectBy(anio=curso.anio,
118 cuatrimestre=curso.cuatrimestre)] }
119 return dict(name=name, namepl=namepl, record=r, form=form, options=options)
124 @identity.require(identity.has_permission('admin'))
125 def update(self, id, **kw):
126 """Save or create record to model"""
127 r = validate_set(id, kw)
128 flash(_(u'El %s fue actualizado.') % name)
129 raise redirect('../list/%s' % r.cursoID)
131 @expose(template='kid:%s.templates.show' % __name__)
132 def show(self,id, **kw):
133 """Show record in model"""
135 return dict(name=name, namepl=namepl, record=r)
138 @identity.require(identity.has_permission('admin'))
139 def delete(self, id):
140 """Destroy record in model"""
142 flash(_(u'El %s fue eliminado permanentemente.') % name)
143 raise redirect('../list')