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 sercom.model import Enunciado, Docente
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
16 def validate_autor(data):
17 autor = data.get('autorID', None)
18 if autor == 0: autor = None
21 autor = Docente.get(autor)
23 raise redirect('new', tg_flash=_(u'No se pudo crear el nuevo ' \
24 '%s porque el autor con identificador %d no existe.'
25 % (name, autor)), **data)
26 data.pop('autorID', None)
30 return val.validate_get(cls, name, id)
32 def validate_set(id, data):
34 return val.validate_set(cls, name, id, data)
36 def validate_new(data):
38 return val.validate_new(cls, name, data)
41 return [(0, _(u'--'))] + [(a.id, a.shortrepr()) for a in Docente.select()]
43 form = TableForm(fields=[
44 TextField(name='nombre', label=_(u'Nombre'),
45 help_text=_(u'Requerido y único.'),
46 validator=validators.UnicodeString(min=5, max=60, strip=True)),
47 SingleSelectField(name='autorID', label=_(u'Autor'),
48 options=get_options, validator=validators.Int(not_empty=False)),
49 TextField(name='descripcion', label=_(u'Descripción'),
50 validator=validators.UnicodeString(not_empty=False, max=255, strip=True)),
53 class EnunciadoController(controllers.Controller, identity.SecureResource):
54 """Basic model admin interface"""
55 require = identity.has_permission('admin')
58 def default(self, tg_errors=None):
59 """handle non exist urls"""
60 raise redirect('list')
64 raise redirect('list')
66 @expose(template='kid:%s.templates.list' % __name__)
69 """List records in model"""
70 f = kw.get('tg_flash', None)
72 return dict(records=r, name=name, namepl=namepl, tg_flash=f)
74 @expose(template='kid:%s.templates.new' % __name__)
76 """Create new records in model"""
77 f = kw.get('tg_flash', None)
78 return dict(name=name, namepl=namepl, form=form, tg_flash=f, values=kw)
83 def create(self, **kw):
84 """Save or create record to model"""
86 raise redirect('list', tg_flash=_(u'Se creó un nuevo %s.') % name)
88 @expose(template='kid:%s.templates.edit' % __name__)
89 def edit(self, id, **kw):
90 """Edit record in model"""
92 return dict(name=name, namepl=namepl, record=r, form=form,
93 tg_flash=kw.get('tg_flash', None))
98 def update(self, id, **kw):
99 """Save or create record to model"""
100 r = validate_set(id, kw)
101 raise redirect('../list',
102 tg_flash=_(u'El %s fue actualizado.') % name)
104 @expose(template='kid:%s.templates.show' % __name__)
105 def show(self,id, **kw):
106 """Show record in model"""
108 r.desc = publish_parts(r.descripcion, writer_name='html')['html_body']
109 return dict(name=name, namepl=namepl, record=r)
112 def delete(self, id):
113 """Destroy record in model"""
116 raise redirect('../list',
117 tg_flash=_(u'El %s fue eliminado permanentemente.') % name)