1 # vim: set et sw=4 sts=4 encoding=utf-8 :
4 from turbogears import controllers, expose, redirect
5 from turbogears import validate, validators, flash, error_handler
6 from turbogears.widgets import *
7 from turbogears import identity
8 from turbogears import paginate
9 from docutils.core import publish_parts
10 from sercom.subcontrollers import validate as val
11 from sercom.model import Enunciado, Docente
21 fknamepl = fkname + 'es'
25 def validate_fk(data):
26 fk = data.get(fkname + 'ID', None)
32 flash(_(u'No se pudo crear el nuevo %s porque el %s con '
33 'identificador %d no existe.' % (name, fkname, fk)))
34 raise redirect('new', **data)
35 data.pop(fkname + 'ID', None)
40 return val.validate_get(cls, name, id)
42 def validate_set(id, data):
44 return val.validate_set(cls, name, id, data)
46 def validate_new(data):
48 return val.validate_new(cls, name, data)
53 return [(0, _(u'--'))] + [(fk.id, fk.shortrepr()) for fk in fkcls.select()]
55 form = TableForm(fields=[
56 TextField(name='nombre', label=_(u'Nombre'),
57 help_text=_(u'Requerido y único.'),
58 validator=validators.UnicodeString(min=5, max=60, strip=True)),
59 SingleSelectField(name=fkname+'ID', label=_(fkname.capitalize()),
60 options=get_options, validator=validators.Int(not_empty=False)),
61 TextField(name='descripcion', label=_(u'Descripción'),
62 validator=validators.UnicodeString(not_empty=False, max=255, strip=True)),
64 form.javascript.append(JSSource("MochiKit.DOM.focusOnLoad('form_nombre');"))
68 class EnunciadoController(controllers.Controller, identity.SecureResource):
69 """Basic model admin interface"""
70 require = identity.has_permission('admin')
73 def default(self, tg_errors=None):
74 """handle non exist urls"""
75 raise redirect('list')
79 raise redirect('list')
81 @expose(template='kid:%s.templates.list' % __name__)
82 @validate(validators=dict(autor=validators.Int))
84 def list(self, autor=None):
85 """List records in model"""
89 r = cls.selectBy(autorID=autor)
90 return dict(records=r, name=name, namepl=namepl, parcial=autor)
92 @expose(template='kid:%s.templates.new' % __name__)
94 """Create new records in model"""
95 return dict(name=name, namepl=namepl, form=form, values=kw)
100 def create(self, **kw):
101 """Save or create record to model"""
103 flash(_(u'Se creó un nuevo %s.') % name)
104 raise redirect('list')
106 @expose(template='kid:%s.templates.edit' % __name__)
107 def edit(self, id, **kw):
108 """Edit record in model"""
110 return dict(name=name, namepl=namepl, record=r, form=form)
115 def update(self, id, **kw):
116 """Save or create record to model"""
117 r = validate_set(id, kw)
118 flash(_(u'El %s fue actualizado.') % name)
119 raise redirect('../list')
121 @expose(template='kid:%s.templates.show' % __name__)
122 def show(self,id, **kw):
123 """Show record in model"""
125 if r.descripcion is None:
128 r.desc = publish_parts(r.descripcion, writer_name='html')['html_body']
129 return dict(name=name, namepl=namepl, record=r)
132 def delete(self, id):
133 """Destroy record in model"""
136 flash(_(u'El %s fue eliminado permanentemente.') % name)
137 raise redirect('../list')