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 Enunciado, Docente
22 fknamepl = fkname + 'es'
26 def validate_fk(data):
27 fk = data.get(fkname + 'ID', None)
33 flash(_(u'No se pudo crear el nuevo %s porque el %s con '
34 'identificador %d no existe.' % (name, fkname, fk)))
35 raise redirect('new', **data)
36 data.pop(fkname + 'ID', None)
41 return val.validate_get(cls, name, id)
43 def validate_set(id, data):
45 return val.validate_set(cls, name, id, data)
47 def validate_new(data):
49 return val.validate_new(cls, name, data)
54 return [(0, _(u'--'))] + [(fk.id, fk.shortrepr()) for fk in fkcls.select()]
56 class EnunciadoForm(W.TableForm):
57 class Fields(W.WidgetsList):
58 nombre = W.TextField(label=_(u'Nombre'),
59 help_text=_(u'Requerido y único.'),
60 validator=V.UnicodeString(min=5, max=60, strip=True))
61 fk = W.SingleSelectField(name=fkname+'ID', label=_(fkname.capitalize()),
62 options=get_options, validator=V.Int(not_empty=False))
63 descripcion = W.TextField(label=_(u'Descripción'),
64 validator=V.UnicodeString(not_empty=False, max=255, strip=True))
66 javascript = [W.JSSource("MochiKit.DOM.focusOnLoad('form_nombre');")]
68 form = EnunciadoForm()
72 class EnunciadoController(controllers.Controller, identity.SecureResource):
73 """Basic model admin interface"""
74 require = identity.has_permission('admin')
77 def default(self, tg_errors=None):
78 """handle non exist urls"""
79 raise redirect('list')
83 raise redirect('list')
85 @expose(template='kid:%s.templates.list' % __name__)
86 @validate(validators=dict(autor=V.Int))
88 def list(self, autor=None):
89 """List records in model"""
93 r = cls.selectBy(autorID=autor)
94 return dict(records=r, name=name, namepl=namepl, parcial=autor)
96 @expose(template='kid:%s.templates.new' % __name__)
98 """Create new records in model"""
99 return dict(name=name, namepl=namepl, form=form, values=kw)
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')
110 @expose(template='kid:%s.templates.edit' % __name__)
111 def edit(self, id, **kw):
112 """Edit record in model"""
114 return dict(name=name, namepl=namepl, record=r, form=form)
119 def update(self, id, **kw):
120 """Save or create record to model"""
121 r = validate_set(id, kw)
122 flash(_(u'El %s fue actualizado.') % name)
123 raise redirect('../list')
125 @expose(template='kid:%s.templates.show' % __name__)
126 def show(self,id, **kw):
127 """Show record in model"""
129 if r.descripcion is None:
132 r.desc = publish_parts(r.descripcion, writer_name='html')['html_body']
133 return dict(name=name, namepl=namepl, record=r)
136 def delete(self, id):
137 """Destroy record in model"""
140 flash(_(u'El %s fue eliminado permanentemente.') % name)
141 raise redirect('../list')