1 # vim: set et sw=4 sts=4 encoding=utf-8 :
3 from turbogears import controllers, expose, redirect
4 from turbogears import validate, validators, flash, error_handler
5 from turbogears.widgets import *
6 from turbogears import identity
7 from turbogears import paginate
8 from docutils.core import publish_parts
9 from sercom.subcontrollers import validate as val
10 from sercom.model import Enunciado, Docente
18 fknamepl = fkname + 'es'
20 def validate_fk(data):
21 fk = data.get(fkname + 'ID', None)
27 flash(_(u'No se pudo crear el nuevo %s porque el %s con '
28 'identificador %d no existe.' % (name, fkname, fk)))
29 raise redirect('new', **data)
30 data.pop(fkname + 'ID', None)
35 return val.validate_get(cls, name, id)
37 def validate_set(id, data):
39 return val.validate_set(cls, name, id, data)
41 def validate_new(data):
43 return val.validate_new(cls, name, data)
46 return [(0, _(u'--'))] + [(fk.id, fk.shortrepr()) for fk in fkcls.select()]
48 form = TableForm(fields=[
49 TextField(name='nombre', label=_(u'Nombre'),
50 help_text=_(u'Requerido y único.'),
51 validator=validators.UnicodeString(min=5, max=60, strip=True)),
52 SingleSelectField(name=fkname+'ID', label=_(fkname.capitalize()),
53 options=get_options, validator=validators.Int(not_empty=False)),
54 TextField(name='descripcion', label=_(u'Descripción'),
55 validator=validators.UnicodeString(not_empty=False, max=255, strip=True)),
58 class EnunciadoController(controllers.Controller, identity.SecureResource):
59 """Basic model admin interface"""
60 require = identity.has_permission('admin')
63 def default(self, tg_errors=None):
64 """handle non exist urls"""
65 raise redirect('list')
69 raise redirect('list')
71 @expose(template='kid:%s.templates.list' % __name__)
72 @validate(validators=dict(autor=validators.Int))
74 def list(self, autor=None):
75 """List records in model"""
79 r = cls.selectBy(autorID=autor)
80 return dict(records=r, name=name, namepl=namepl, parcial=autor)
82 @expose(template='kid:%s.templates.new' % __name__)
84 """Create new records in model"""
85 return dict(name=name, namepl=namepl, form=form, values=kw)
90 def create(self, **kw):
91 """Save or create record to model"""
93 flash(_(u'Se creó un nuevo %s.') % name)
94 raise redirect('list')
96 @expose(template='kid:%s.templates.edit' % __name__)
97 def edit(self, id, **kw):
98 """Edit record in model"""
100 return dict(name=name, namepl=namepl, record=r, form=form)
105 def update(self, id, **kw):
106 """Save or create record to model"""
107 r = validate_set(id, kw)
108 flash(_(u'El %s fue actualizado.') % name)
109 raise redirect('../list')
111 @expose(template='kid:%s.templates.show' % __name__)
112 def show(self,id, **kw):
113 """Show record in model"""
115 if r.descripcion is None:
118 r.desc = publish_parts(r.descripcion, writer_name='html')['html_body']
119 return dict(name=name, namepl=namepl, record=r)
122 def delete(self, id):
123 """Destroy record in model"""
126 flash(_(u'El %s fue eliminado permanentemente.') % name)
127 raise redirect('../list')