1 # vim: set et sw=4 sts=4 encoding=utf-8 foldmethod=marker :
4 from turbogears import controllers, expose, redirect
5 from turbogears import validate, flash, error_handler
6 from turbogears import validators as V
7 from turbogears import widgets as W
8 from turbogears import identity
9 from turbogears import paginate
10 from docutils.core import publish_parts
11 from sercom.subcontrollers import validate as val
12 from sercom.model import CasoDePrueba, Enunciado
13 from sercom.validators import ParamsValidator
18 name = 'caso de prueba'
19 namepl = 'casos de prueba'
23 fknamepl = fkname + 's'
27 def validate_fk(data):
28 fk = data.get(fkname + 'ID', None)
34 flash(_(u'No se pudo crear el nuevo %s porque el %s con '
35 'identificador %d no existe.' % (name, fkname, fk)))
36 raise redirect('new', **data)
37 data.pop(fkname + 'ID', None)
42 return val.validate_get(cls, name, id)
44 def validate_set(id, data):
46 return val.validate_set(cls, name, id, data)
48 def validate_new(data):
50 return val.validate_new(cls, name, data)
53 return val.validate_del(cls, name, id)
58 return [(0, _(u'<<General>>'))] + [(fk.id, fk.shortrepr())
59 for fk in fkcls.select()]
61 class CasoDePruebaForm(W.TableForm):
62 class Fields(W.WidgetsList):
63 nombre = W.TextField(label=_(u'Nombre'),
64 help_text=_(u'Requerido y único.'),
65 validator=V.UnicodeString(min=5, max=60, strip=True))
66 fk = W.SingleSelectField(name=fkname+'ID', label=_(fkname.capitalize()),
67 options=get_options, validator=V.Int(not_empty=False))
68 descripcion = W.TextField(label=_(u'Descripción'),
69 validator=V.UnicodeString(not_empty=False, max=255,
71 comando = W.TextField(label=_(u'Comando'),
72 validator=ParamsValidator(not_empty=False, strip=True))
73 retorno = W.TextField(label=_(u'Código de retorno'),
74 validator=V.Int(not_empty=False, strip=True))
75 tiempo_cpu = W.TextField(label=_(u'Tiempo de CPU'),
76 validator=V.Number(not_empty=False, strip=True))
78 javascript = [W.JSSource("MochiKit.DOM.focusOnLoad('form_nombre');")]
80 form = CasoDePruebaForm()
85 def params2str(params):
86 return ' '.join([repr(p)[1:] for p in params])
88 class CasoDePruebaController(controllers.Controller, identity.SecureResource):
89 """Basic model admin interface"""
90 require = identity.has_permission('admin')
93 def default(self, tg_errors=None):
94 """handle non exist urls"""
95 raise redirect('list')
99 raise redirect('list')
101 @expose(template='kid:%s.templates.list' % __name__)
102 @validate(validators=dict(enunciado=V.Int))
104 def list(self, enunciado=None):
105 """List records in model"""
106 if enunciado is None:
109 r = cls.selectBy(enunciadoID=enunciado)
110 return dict(records=r, name=name, namepl=namepl, parcial=enunciado,
111 params2str=params2str)
113 @expose(template='kid:%s.templates.new' % __name__)
115 """Create new records in model"""
116 return dict(name=name, namepl=namepl, form=form, values=kw)
121 def create(self, **kw):
122 """Save or create record to model"""
124 flash(_(u'Se creó un nuevo %s.') % name)
125 raise redirect('list')
127 @expose(template='kid:%s.templates.edit' % __name__)
128 def edit(self, id, **kw):
129 """Edit record in model"""
131 return dict(name=name, namepl=namepl, record=r, form=form,
132 params2str=params2str)
137 def update(self, id, **kw):
138 """Save or create record to model"""
139 r = validate_set(id, kw)
140 flash(_(u'El %s fue actualizado.') % name)
141 raise redirect('../list')
143 @expose(template='kid:%s.templates.show' % __name__)
144 def show(self, id, **kw):
145 """Show record in model"""
147 if r.descripcion is None:
150 r.desc = publish_parts(r.descripcion, writer_name='html')['html_body']
151 return dict(name=name, namepl=namepl, record=r, params2str=params2str)
154 def delete(self, id):
155 """Destroy record in model"""
157 flash(_(u'El %s fue eliminado permanentemente.') % name)
158 raise redirect('../list')