X-Git-Url: https://git.llucax.com/z.facultad/75.52/sercom.git/blobdiff_plain/d9cd422b175f8e5ebb8c98d974c139586da8217d..355619b49d3e30fe5154254606f3d6c1a7e30c6e:/sercom/subcontrollers/enunciado/__init__.py diff --git a/sercom/subcontrollers/enunciado/__init__.py b/sercom/subcontrollers/enunciado/__init__.py index 569b2ac..d2897a5 100644 --- a/sercom/subcontrollers/enunciado/__init__.py +++ b/sercom/subcontrollers/enunciado/__init__.py @@ -2,49 +2,54 @@ from turbogears import controllers, expose, redirect from turbogears import validate, validators, flash, error_handler -from sercom.model import Enunciado, Docente from turbogears.widgets import * from turbogears import identity from turbogears import paginate from docutils.core import publish_parts from sercom.subcontrollers import validate as val +from sercom.model import Enunciado, Docente cls = Enunciado name = 'enunciado' namepl = name + 's' -def validate_autor(data): - autor = data.get('autorID', None) - if autor == 0: autor = None - if autor is not None: +fkcls = Docente +fkname = 'autor' +fknamepl = fkname + 'es' + +def validate_fk(data): + fk = data.get(fkname + 'ID', None) + if fk == 0: fk = None + if fk is not None: try: - autor = Docente.get(autor) + fk = fkcls.get(fk) except LookupError: raise redirect('new', tg_flash=_(u'No se pudo crear el nuevo ' \ - '%s porque el autor con identificador %d no existe.' - % (name, autor)), **data) - data.pop('autorID', None) - data['autor'] = autor + '%s porque el %s con identificador %d no existe.' + % (name, fkname, fk)), **data) + data.pop(fkname + 'ID', None) + data[fkname] = fk + return fk def validate_get(id): return val.validate_get(cls, name, id) def validate_set(id, data): - validate_autor(data) + validate_fk(data) return val.validate_set(cls, name, id, data) def validate_new(data): - validate_autor(data) + validate_fk(data) return val.validate_new(cls, name, data) def get_options(): - return [(0, _(u'--'))] + [(a.id, a.shortrepr()) for a in Docente.select()] + return [(0, _(u'--'))] + [(fk.id, fk.shortrepr()) for fk in fkcls.select()] form = TableForm(fields=[ TextField(name='nombre', label=_(u'Nombre'), help_text=_(u'Requerido y único.'), validator=validators.UnicodeString(min=5, max=60, strip=True)), - SingleSelectField(name='autorID', label=_(u'Autor'), + SingleSelectField(name=fkname+'ID', label=_(fkname.capitalize()), options=get_options, validator=validators.Int(not_empty=False)), TextField(name='descripcion', label=_(u'Descripción'), validator=validators.UnicodeString(not_empty=False, max=255, strip=True)), @@ -63,7 +68,8 @@ class EnunciadoController(controllers.Controller, identity.SecureResource): def index(self): raise redirect('list') - @expose(template='kid:sercom.subcontrollers.%s.templates.list' % name) + @expose(template='kid:%s.templates.list' % __name__) + @validate(validators=dict(autorID=validators.Int)) @paginate('records') def list(self, **kw): """List records in model""" @@ -71,7 +77,7 @@ class EnunciadoController(controllers.Controller, identity.SecureResource): r = cls.select() return dict(records=r, name=name, namepl=namepl, tg_flash=f) - @expose(template='kid:sercom.subcontrollers.%s.templates.new' % name) + @expose(template='kid:%s.templates.new' % __name__) def new(self, **kw): """Create new records in model""" f = kw.get('tg_flash', None) @@ -85,7 +91,7 @@ class EnunciadoController(controllers.Controller, identity.SecureResource): validate_new(kw) raise redirect('list', tg_flash=_(u'Se creó un nuevo %s.') % name) - @expose(template='kid:sercom.subcontrollers.%s.templates.edit' % name) + @expose(template='kid:%s.templates.edit' % __name__) def edit(self, id, **kw): """Edit record in model""" r = validate_get(id) @@ -101,11 +107,14 @@ class EnunciadoController(controllers.Controller, identity.SecureResource): raise redirect('../list', tg_flash=_(u'El %s fue actualizado.') % name) - @expose(template='kid:sercom.subcontrollers.%s.templates.show' % name) + @expose(template='kid:%s.templates.show' % __name__) def show(self,id, **kw): """Show record in model""" r = validate_get(id) - r.desc = publish_parts(r.descripcion, writer_name='html')['html_body'] + if r.descripcion is None: + r.desc = '' + else: + r.desc = publish_parts(r.descripcion, writer_name='html')['html_body'] return dict(name=name, namepl=namepl, record=r) @expose()