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 raise redirect('new', tg_flash=_(u'No se pudo crear el nuevo ' \
28 '%s porque el %s con identificador %d no existe.'
29 % (name, fkname, fk)), **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(autorID=validators.Int))
75 """List records in model"""
76 f = kw.get('tg_flash', None)
78 return dict(records=r, name=name, namepl=namepl, tg_flash=f)
80 @expose(template='kid:%s.templates.new' % __name__)
82 """Create new records in model"""
83 f = kw.get('tg_flash', None)
84 return dict(name=name, namepl=namepl, form=form, tg_flash=f, values=kw)
89 def create(self, **kw):
90 """Save or create record to model"""
92 raise redirect('list', tg_flash=_(u'Se creó un nuevo %s.') % name)
94 @expose(template='kid:%s.templates.edit' % __name__)
95 def edit(self, id, **kw):
96 """Edit record in model"""
98 return dict(name=name, namepl=namepl, record=r, form=form,
99 tg_flash=kw.get('tg_flash', None))
104 def update(self, id, **kw):
105 """Save or create record to model"""
106 r = validate_set(id, kw)
107 raise redirect('../list',
108 tg_flash=_(u'El %s fue actualizado.') % name)
110 @expose(template='kid:%s.templates.show' % __name__)
111 def show(self,id, **kw):
112 """Show record in model"""
114 if r.descripcion is None:
117 r.desc = publish_parts(r.descripcion, writer_name='html')['html_body']
118 return dict(name=name, namepl=namepl, record=r)
121 def delete(self, id):
122 """Destroy record in model"""
125 raise redirect('../list',
126 tg_flash=_(u'El %s fue eliminado permanentemente.') % name)