X-Git-Url: https://git.llucax.com/software/sercom.git/blobdiff_plain/081f501efd6fd1c2833faafb45ed4f57fa4bb8c8..d47dd9949d44d12944f557b0efd80459b857ce74:/sercom/subcontrollers/docente/__init__.py diff --git a/sercom/subcontrollers/docente/__init__.py b/sercom/subcontrollers/docente/__init__.py index c4dcfc5..df4a2b7 100644 --- a/sercom/subcontrollers/docente/__init__.py +++ b/sercom/subcontrollers/docente/__init__.py @@ -1,20 +1,26 @@ # vim: set et sw=4 sts=4 encoding=utf-8 : +import cherrypy from turbogears import controllers, expose, redirect from turbogears import validate, validators, flash, error_handler -from sercom.model import 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 Docente cls = Docente name = 'docente' namepl = name + 's' +def validate_get(id): return val.validate_get(cls, name, id) +def validate_set(id, data): return val.validate_set(cls, name, id, data) +def validate_new(data): return val.validate_new(cls, name, data) + form = TableForm(fields=[ TextField(name='usuario', label=_(u'Usuario'), - help_text=_(u'Requerido.'), + help_text=_(u'Requerido y único.'), validator=validators.UnicodeString(min=3, max=10, strip=True)), TextField(name='nombre', label=_(u'Nombre'), help_text=_(u'Requerido.'), @@ -37,6 +43,7 @@ form = TableForm(fields=[ #help_text=_(u'Si no está activo no puede ingresar al sistema.'), validator=validators.Bool(if_empty=1)), ]) +form.javascript.append(JSSource("MochiKit.DOM.focusOnLoad('form_usuario');")) class DocenteController(controllers.Controller, identity.SecureResource): """Basic model admin interface""" @@ -51,50 +58,41 @@ class DocenteController(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__) @paginate('records') - def list(self, **kw): + def list(self): """List records in model""" - f = kw.get('tg_flash', None) r = cls.select() - - return dict(records=r, name=name, namepl=namepl, tg_flash=f) + return dict(records=r, name=name, namepl=namepl) @expose() def activate(self, id, activo): """Save or create record to model""" - cls.get(int(id)).activo = int(activo) - - raise redirect('../list') + r = validate_get(id) + try: + r.activo = bool(int(activo)) + except ValueError: + raise cherrypy.NotFound + raise redirect('../../list') - @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) - return dict(name=name, namepl=namepl, form=form, tg_flash=f, values=kw) + return dict(name=name, namepl=namepl, form=form, values=kw) @validate(form=form) @error_handler(new) @expose() def create(self, **kw): """Save or create record to model""" - try: - Docente(**kw) - except Exception, e: - raise redirect('new', tg_flash=_(u'No se pudo crear el nuevo %s, ' \ - 'probablemente ya existe uno con el mismo usuario (error: %s).' - % (name, e)), **kw) - - raise redirect('list', tg_flash=_(u'Se creó un nuevo %s.') % name) + validate_new(kw) + flash(_(u'Se creó un nuevo %s.') % name) + raise redirect('list') - @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""" - try: - r = cls.get(int(id)) - except LookupError: - flash = _('No existe el docente con identificador %d.') % id - + r = validate_get(id) return dict(name=name, namepl=namepl, record=r, form=form) @validate(form=form) @@ -102,25 +100,25 @@ class DocenteController(controllers.Controller, identity.SecureResource): @expose() def update(self, id, **kw): """Save or create record to model""" - record = cls.get(int(id)) - record.set(**kw) - - raise redirect('../list', - tg_flash=_(u'El %s fue actualizado.') % name) + r = validate_set(id, kw) + flash(_(u'El %s fue actualizado.') % name) + raise redirect('../list') - @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 = cls.get(int(id)) - r.obs = publish_parts(r.observaciones, writer_name='html')['html_body'] - + r = validate_get(id) + if r.observaciones is None: + r.obs = '' + else: + r.obs = publish_parts(r.observaciones, writer_name='html')['html_body'] return dict(name=name, namepl=namepl, record=r) @expose() def delete(self, id): """Destroy record in model""" - cls.delete(int(id)) - - raise redirect('../list', - tg_flash=_(u'El %s fue eliminado permanentemente.') % name) + r = validate_get(id) + r.destroySelf() + flash(_(u'El %s fue eliminado permanentemente.') % name) + raise redirect('../list')