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 fk1namepl = fk1name + 's'
34 def validate_fk(data):
35 fk = data.get(fkname + 'ID', None)
41 flash(_(u'No se pudo crear el nuevo %s porque el %s con '
42 'identificador %d no existe.' % (name, fkname, fk)))
43 raise redirect('new', **data)
44 data.pop(fkname + 'ID', None)
48 def validate_fk1(data):
49 fk = data.get(fk1name + 'ID', None)
55 flash(_(u'No se pudo crear el nuevo %s porque el %s con '
56 'identificador %d no existe.' % (name, fk1name, fk)))
57 raise redirect('new', **data)
58 data.pop(fk1name + 'ID', None)
63 return val.validate_get(cls, name, id)
65 def validate_set(id, data):
68 return val.validate_set(cls, name, id, data)
70 def validate_new(data):
73 return val.validate_new(cls, name, data)
78 return [(0, _(u'--'))] + [(fk.id, fk.shortrepr()) for fk in fkcls.select()]
81 return [(0, _(u'--'))] + [(fk1.id, fk1.shortrepr()) for fk1 in fk1cls.select()]
83 class EjercicioForm(W.TableForm):
84 class Fields(W.WidgetsList):
85 fk = W.SingleSelectField(name=fkname+'ID', label=_(fkname.capitalize()),
86 options=get_options, validator=V.Int(not_empty=True))
87 numero = W.TextField(name="numero",label=_(u'Nro'),
88 help_text=_(u'Requerido.'),
89 validator=V.Int(not_empty=True))
90 fk1 = W.SingleSelectField(name=fk1name+'ID', label=_(fk1name.capitalize()),
91 options=get_options1, validator=V.Int(not_empty=True))
92 grupal = W.CheckBox(name='grupal', label=_(u"Grupal?"))
94 javascript = [W.JSSource("MochiKit.DOM.focusOnLoad('form_nombre');")]
96 form = EjercicioForm()
100 class EjercicioController(controllers.Controller, identity.SecureResource):
101 """Basic model admin interface"""
102 require = identity.has_permission('admin')
104 entrega = EntregaController()
107 def default(self, tg_errors=None):
108 """handle non exist urls"""
109 raise redirect('list')
113 raise redirect('list')
115 @expose(template='kid:%s.templates.list' % __name__)
116 @validate(validators=dict(autor=V.Int))
118 def list(self, autor=None):
119 """List records in model"""
121 return dict(records=r, name=name, namepl=namepl, parcial=autor)
123 @expose(template='kid:%s.templates.new' % __name__)
125 """Create new records in model"""
126 return dict(name=name, namepl=namepl, form=form, values=kw)
131 def create(self, **kw):
132 """Save or create record to model"""
134 flash(_(u'Se creó un nuevo %s.') % name)
135 raise redirect('list')
137 @expose(template='kid:%s.templates.edit' % __name__)
138 def edit(self, id, **kw):
139 """Edit record in model"""
141 return dict(name=name, namepl=namepl, record=r, form=form)
146 def update(self, id, **kw):
147 """Save or create record to model"""
148 r = validate_set(id, kw)
149 flash(_(u'El %s fue actualizado.') % name)
150 raise redirect('../list')
152 @expose(template='kid:%s.templates.show' % __name__)
153 def show(self,id, **kw):
154 """Show record in model"""
156 return dict(name=name, namepl=namepl, record=r)
159 def delete(self, id):
160 """Destroy record in model"""
163 flash(_(u'El %s fue eliminado permanentemente.') % name)
164 raise redirect('../list')
169 response.headers["Content-Type"] = r.archivo_type
170 response.headers["Content-disposition"] = "attachment;filename=%s" % (r.archivo_name)
171 flash(_(u'El %s fue eliminado permanentemente.') % name)