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
23 fknamepl = fkname + 's'
27 fk1namepl = fk1name + 's'
31 def validate_fk(data):
32 fk = data.get(fkname + 'ID', None)
38 flash(_(u'No se pudo crear el nuevo %s porque el %s con '
39 'identificador %d no existe.' % (name, fkname, fk)))
40 raise redirect('new', **data)
41 data.pop(fkname + 'ID', None)
45 def validate_fk1(data):
46 fk = data.get(fk1name + 'ID', None)
52 flash(_(u'No se pudo crear el nuevo %s porque el %s con '
53 'identificador %d no existe.' % (name, fk1name, fk)))
54 raise redirect('new', **data)
55 data.pop(fk1name + 'ID', None)
60 return val.validate_get(cls, name, id)
62 def validate_set(id, data):
65 return val.validate_set(cls, name, id, data)
67 def validate_new(data):
70 return val.validate_new(cls, name, data)
75 return [(0, _(u'--'))] + [(fk.id, fk.shortrepr()) for fk in fkcls.select()]
78 return [(0, _(u'--'))] + [(fk1.id, fk1.shortrepr()) for fk1 in fk1cls.select()]
80 class EjercicioForm(W.TableForm):
81 class Fields(W.WidgetsList):
82 fk = W.SingleSelectField(name=fkname+'ID', label=_(fkname.capitalize()),
83 options=get_options, validator=V.Int(not_empty=True))
84 numero = W.TextField(name="numero",label=_(u'Nro'),
85 help_text=_(u'Requerido.'),
86 validator=V.Int(not_empty=True))
87 fk1 = W.SingleSelectField(name=fk1name+'ID', label=_(fk1name.capitalize()),
88 options=get_options1, validator=V.Int(not_empty=True))
89 grupal = W.CheckBox(name='grupal', label=_(u"Grupal?"))
91 javascript = [W.JSSource("MochiKit.DOM.focusOnLoad('form_nombre');")]
93 form = EjercicioForm()
97 class EjercicioController(controllers.Controller, identity.SecureResource):
98 """Basic model admin interface"""
99 require = identity.has_permission('admin')
102 def default(self, tg_errors=None):
103 """handle non exist urls"""
104 raise redirect('list')
108 raise redirect('list')
110 @expose(template='kid:%s.templates.list' % __name__)
111 @validate(validators=dict(autor=V.Int))
113 def list(self, autor=None):
114 """List records in model"""
116 return dict(records=r, name=name, namepl=namepl, parcial=autor)
118 @expose(template='kid:%s.templates.new' % __name__)
120 """Create new records in model"""
121 return dict(name=name, namepl=namepl, form=form, values=kw)
126 def create(self, **kw):
127 """Save or create record to model"""
129 flash(_(u'Se creó un nuevo %s.') % name)
130 raise redirect('list')
132 @expose(template='kid:%s.templates.edit' % __name__)
133 def edit(self, id, **kw):
134 """Edit record in model"""
136 return dict(name=name, namepl=namepl, record=r, form=form)
141 def update(self, id, **kw):
142 """Save or create record to model"""
143 r = validate_set(id, kw)
144 flash(_(u'El %s fue actualizado.') % name)
145 raise redirect('../list')
147 @expose(template='kid:%s.templates.show' % __name__)
148 def show(self,id, **kw):
149 """Show record in model"""
151 return dict(name=name, namepl=namepl, record=r)
154 def delete(self, id):
155 """Destroy record in model"""
158 flash(_(u'El %s fue eliminado permanentemente.') % name)
159 raise redirect('../list')
164 response.headers["Content-Type"] = r.archivo_type
165 response.headers["Content-disposition"] = "attachment;filename=%s" % (r.archivo_name)
166 flash(_(u'El %s fue eliminado permanentemente.') % name)