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
26 fknamepl = fkname + 's'
30 def validate_fk(data):
31 fk = data.get(fkname + 'ID', None)
37 flash(_(u'No se pudo crear el nuevo %s porque el %s con '
38 'identificador %d no existe.' % (name, fkname, fk)))
39 raise redirect('new', **data)
40 data.pop(fkname + 'ID', None)
45 return val.validate_get(cls, name, id)
47 def validate_set(id, data):
49 return val.validate_set(cls, name, id, data)
51 def validate_new(data):
53 return val.validate_new(cls, name, data)
56 return val.validate_del(cls, name, id)
60 class EjercicioForm(W.TableForm):
61 class Fields(W.WidgetsList):
62 cursoID = W.HiddenField(validator=V.Int)
63 numero = W.TextField(name="numero",label=_(u'Nro'),
64 help_text=_(u'Requerido.'),
65 validator=V.Int(not_empty=True))
66 fk = W.SingleSelectField(name=fkname+'ID', label=_(fkname.capitalize()),
67 validator=V.Int(not_empty=True))
68 grupal = W.CheckBox(name='grupal', label=_(u"Grupal?"), validator=V.Bool(if_empty=0), default=0)
70 javascript = [W.JSSource("MochiKit.DOM.focusOnLoad('form_numero');")]
72 form = EjercicioForm()
76 class EjercicioController(controllers.Controller, identity.SecureResource):
77 """Basic model admin interface"""
78 require = identity.has_permission('entregar')
80 entrega = EntregaController()
82 @expose(template='kid:%s.templates.list' % __name__)
83 @validate(validators=dict(curso=V.Int))
85 def list(self, curso):
86 """List records in model"""
87 r = cls.selectBy(cursoID=curso).orderBy(cls.q.numero)
88 return dict(records=r, name=name, namepl=namepl, curso=curso)
90 @expose(template='kid:%s.templates.new' % __name__)
91 @validate(validators=dict(curso=V.Int))
92 @identity.require(identity.has_permission('admin'))
93 def new(self, curso, **kw):
94 """Create new records in model"""
96 curso = Curso.get(curso)
97 options = { fkname+'ID': [(fk.id, fk.shortrepr()) for fk in
98 Enunciado.selectBy(anio=curso.anio,
99 cuatrimestre=curso.cuatrimestre)] }
100 return dict(name=name, namepl=namepl, form=form, values=kw, options=options)
104 @identity.require(identity.has_permission('admin'))
106 def create(self, **kw):
107 """Save or create record to model"""
109 flash(_(u'Se creó un nuevo %s.') % name)
110 raise redirect('list/%s' % kw['cursoID'])
112 @expose(template='kid:%s.templates.edit' % __name__)
113 @identity.require(identity.has_permission('admin'))
114 def edit(self, id, **kw):
115 """Edit record in model"""
117 curso = Curso.get(r.cursoID)
118 options = { fkname+'ID': [(fk.id, fk.shortrepr()) for fk in
119 Enunciado.selectBy(anio=curso.anio,
120 cuatrimestre=curso.cuatrimestre)] }
121 return dict(name=name, namepl=namepl, record=r, form=form, options=options)
126 @identity.require(identity.has_permission('admin'))
127 def update(self, id, **kw):
128 """Save or create record to model"""
129 r = validate_set(id, kw)
130 flash(_(u'El %s fue actualizado.') % name)
131 raise redirect('../list/%s' % r.cursoID)
133 @expose(template='kid:%s.templates.show' % __name__)
134 def show(self,id, **kw):
135 """Show record in model"""
137 return dict(name=name, namepl=namepl, record=r)
140 @identity.require(identity.has_permission('admin'))
141 def delete(self, id):
142 """Destroy record in model"""
144 flash(_(u'El %s fue eliminado permanentemente.') % name)
145 raise redirect('../list')