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
24 fknamepl = fkname + 's'
28 fk1namepl = fk1name + 's'
32 def validate_fk(data):
33 fk = data.get(fkname + 'ID', None)
39 flash(_(u'No se pudo crear el nuevo %s porque el %s con '
40 'identificador %d no existe.' % (name, fkname, fk)))
41 raise redirect('new', **data)
42 data.pop(fkname + 'ID', None)
46 def validate_fk1(data):
47 fk = data.get(fk1name + 'ID', None)
53 flash(_(u'No se pudo crear el nuevo %s porque el %s con '
54 'identificador %d no existe.' % (name, fk1name, fk)))
55 raise redirect('new', **data)
56 data.pop(fk1name + 'ID', None)
61 return val.validate_get(cls, name, id)
63 def validate_set(id, data):
66 return val.validate_set(cls, name, id, data)
68 def validate_new(data):
71 return val.validate_new(cls, name, data)
76 return [(0, _(u'--'))] + [(fk.id, fk.shortrepr()) for fk in fkcls.select()]
79 return [(0, _(u'--'))] + [(fk1.id, fk1.shortrepr()) for fk1 in fk1cls.select()]
81 class EjercicioForm(W.TableForm):
82 class Fields(W.WidgetsList):
83 fk = W.SingleSelectField(name=fkname+'ID', label=_(fkname.capitalize()),
84 options=get_options, validator=V.Int(not_empty=True))
85 numero = W.TextField(name="numero",label=_(u'Nro'),
86 help_text=_(u'Requerido.'),
87 validator=V.Int(not_empty=True))
88 fk1 = W.SingleSelectField(name=fk1name+'ID', label=_(fk1name.capitalize()),
89 options=get_options1, validator=V.Int(not_empty=True))
90 grupal = W.CheckBox(name='grupal', label=_(u"Grupal?"))
92 javascript = [W.JSSource("MochiKit.DOM.focusOnLoad('form_nombre');")]
94 form = EjercicioForm()
98 class EjercicioController(controllers.Controller, identity.SecureResource):
99 """Basic model admin interface"""
100 require = identity.has_permission('admin')
103 def default(self, tg_errors=None):
104 """handle non exist urls"""
105 raise redirect('list')
109 raise redirect('list')
111 @expose(template='kid:%s.templates.list' % __name__)
112 @validate(validators=dict(autor=V.Int))
114 def list(self, autor=None):
115 """List records in model"""
117 return dict(records=r, name=name, namepl=namepl, parcial=autor)
119 @expose(template='kid:%s.templates.new' % __name__)
121 """Create new records in model"""
122 return dict(name=name, namepl=namepl, form=form, values=kw)
127 def create(self, **kw):
128 """Save or create record to model"""
130 flash(_(u'Se creó un nuevo %s.') % name)
131 raise redirect('list')
133 @expose(template='kid:%s.templates.edit' % __name__)
134 def edit(self, id, **kw):
135 """Edit record in model"""
137 return dict(name=name, namepl=namepl, record=r, form=form)
142 def update(self, id, **kw):
143 """Save or create record to model"""
144 r = validate_set(id, kw)
145 flash(_(u'El %s fue actualizado.') % name)
146 raise redirect('../list')
148 @expose(template='kid:%s.templates.show' % __name__)
149 def show(self,id, **kw):
150 """Show record in model"""
152 return dict(name=name, namepl=namepl, record=r)
155 def delete(self, id):
156 """Destroy record in model"""
159 flash(_(u'El %s fue eliminado permanentemente.') % name)
160 raise redirect('../list')
165 response.headers["Content-Type"] = r.archivo_type
166 response.headers["Content-disposition"] = "attachment;filename=%s" % (r.archivo_name)
167 flash(_(u'El %s fue eliminado permanentemente.') % name)