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(autor=validators.Int))
74 def list(self, autor=None, tg_flash=None):
75 """List records in model"""
79 r = cls.selectBy(autorID=autor)
80 return dict(records=r, name=name, namepl=namepl, tg_flash=tg_flash)
82 @expose(template='kid:%s.templates.new' % __name__)
84 """Create new records in model"""
85 f = kw.get('tg_flash', None)
86 return dict(name=name, namepl=namepl, form=form, tg_flash=f, values=kw)
91 def create(self, **kw):
92 """Save or create record to model"""
94 raise redirect('list', tg_flash=_(u'Se creó un nuevo %s.') % name)
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,
101 tg_flash=kw.get('tg_flash', None))
106 def update(self, id, **kw):
107 """Save or create record to model"""
108 r = validate_set(id, kw)
109 raise redirect('../list',
110 tg_flash=_(u'El %s fue actualizado.') % name)
112 @expose(template='kid:%s.templates.show' % __name__)
113 def show(self,id, **kw):
114 """Show record in model"""
116 if r.descripcion is None:
119 r.desc = publish_parts(r.descripcion, writer_name='html')['html_body']
120 return dict(name=name, namepl=namepl, record=r)
123 def delete(self, id):
124 """Destroy record in model"""
127 raise redirect('../list',
128 tg_flash=_(u'El %s fue eliminado permanentemente.') % name)