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)),
57 form.javascript.append(JSSource("MochiKit.DOM.focusOnLoad('form_nombre');"))
59 class EnunciadoController(controllers.Controller, identity.SecureResource):
60 """Basic model admin interface"""
61 require = identity.has_permission('admin')
64 def default(self, tg_errors=None):
65 """handle non exist urls"""
66 raise redirect('list')
70 raise redirect('list')
72 @expose(template='kid:%s.templates.list' % __name__)
73 @validate(validators=dict(autor=validators.Int))
75 def list(self, autor=None):
76 """List records in model"""
80 r = cls.selectBy(autorID=autor)
81 return dict(records=r, name=name, namepl=namepl, parcial=autor)
83 @expose(template='kid:%s.templates.new' % __name__)
85 """Create new records in model"""
86 return dict(name=name, namepl=namepl, form=form, values=kw)
91 def create(self, **kw):
92 """Save or create record to model"""
94 flash(_(u'Se creó un nuevo %s.') % name)
95 raise redirect('list')
97 @expose(template='kid:%s.templates.edit' % __name__)
98 def edit(self, id, **kw):
99 """Edit record in model"""
101 return dict(name=name, namepl=namepl, record=r, form=form)
106 def update(self, id, **kw):
107 """Save or create record to model"""
108 r = validate_set(id, kw)
109 flash(_(u'El %s fue actualizado.') % name)
110 raise redirect('../list')
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 flash(_(u'El %s fue eliminado permanentemente.') % name)
128 raise redirect('../list')