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 CasoDePrueba, Enunciado
13 name = 'caso de prueba'
14 namepl = 'casos de prueba'
18 fknamepl = fkname + 's'
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'<<General>>'))] + [(fk.id, fk.shortrepr())
47 for fk in fkcls.select()]
49 form = TableForm(fields=[
50 TextField(name='nombre', label=_(u'Nombre'),
51 help_text=_(u'Requerido y único.'),
52 validator=validators.UnicodeString(min=5, max=60, strip=True)),
53 SingleSelectField(name=fkname+'ID', label=_(fkname.capitalize()),
54 options=get_options, validator=validators.Int(not_empty=False)),
55 TextField(name='descripcion', label=_(u'Descripción'),
56 validator=validators.UnicodeString(not_empty=False, max=255, strip=True)),
57 TextField(name='retorno', label=_(u'Código de retorno'),
58 validator=validators.Int(not_empty=False, strip=True)),
59 TextField(name='tiempo_cpu', label=_(u'Tiempo de CPU'),
60 validator=validators.Number(not_empty=False, strip=True)),
63 class CasoDePruebaController(controllers.Controller, identity.SecureResource):
64 """Basic model admin interface"""
65 require = identity.has_permission('admin')
68 def default(self, tg_errors=None):
69 """handle non exist urls"""
70 raise redirect('list')
74 raise redirect('list')
76 @expose(template='kid:%s.templates.list' % __name__)
77 @validate(validators=dict(enunciadoID=validators.Int))
79 def list(self, enunciadoID=None, tg_flash=None):
80 """List records in model"""
81 if enunciadoID is None:
84 r = cls.selectBy(enunciadoID=enunciadoID)
85 return dict(records=r, name=name, namepl=namepl, tg_flash=tg_flash)
87 @expose(template='kid:%s.templates.new' % __name__)
89 """Create new records in model"""
90 f = kw.get('tg_flash', None)
91 return dict(name=name, namepl=namepl, form=form, tg_flash=f, values=kw)
96 def create(self, **kw):
97 """Save or create record to model"""
99 raise redirect('list', tg_flash=_(u'Se creó un nuevo %s.') % name)
101 @expose(template='kid:%s.templates.edit' % __name__)
102 def edit(self, id, **kw):
103 """Edit record in model"""
105 return dict(name=name, namepl=namepl, record=r, form=form,
106 tg_flash=kw.get('tg_flash', None))
111 def update(self, id, **kw):
112 """Save or create record to model"""
113 r = validate_set(id, kw)
114 raise redirect('../list',
115 tg_flash=_(u'El %s fue actualizado.') % name)
117 @expose(template='kid:%s.templates.show' % __name__)
118 def show(self, id, **kw):
119 """Show record in model"""
121 if r.descripcion is None:
124 r.desc = publish_parts(r.descripcion, writer_name='html')['html_body']
125 return dict(name=name, namepl=namepl, record=r)
128 def delete(self, id):
129 """Destroy record in model"""
132 raise redirect('../list',
133 tg_flash=_(u'El %s fue eliminado permanentemente.') % name)