1 # vim: set et sw=4 sts=4 encoding=utf-8 foldmethod=marker :
5 from turbogears import controllers, expose, redirect
6 from turbogears import validate, flash, error_handler
7 from turbogears import validators as V
8 from turbogears import widgets as W
9 from turbogears import identity
10 from turbogears import paginate
11 from docutils.core import publish_parts
12 from sercom.subcontrollers import validate as val
13 from sercom.model import Curso, Ejercicio, Alumno, Docente, Grupo, DocenteInscripto, AlumnoInscripto
18 name = 'Alumno del curso'
19 namepl = 'Alumnos del curso'
24 return val.validate_get(cls, name, id)
26 def validate_set(id, data):
27 return val.validate_set(cls, name, id, data)
29 def validate_new(data):
30 return val.validate_new(cls, name, data)
34 return [(fk1.id, fk1.shortrepr()) for fk1 in Ejercicio.select()]
37 return [(fk1.id, fk1.shortrepr()) for fk1 in Docente.select()]
39 def get_alumnos_inscriptos(curso):
40 return [(fk1.id, fk1.shortrepr()) for fk1 in AlumnoInscripto.selectBy(curso)]
43 return [(fk1.id, fk1.shortrepr()) for fk1 in Alumno.select()]
46 return [(fk1.id, fk1.shortrepr()) for fk1 in Grupo.select()]
49 function makeOption(option) {
50 return OPTION({"value": option.value}, option.text);
53 function moveOption( fromSelect, toSelect) {
54 // add 'selected' nodes toSelect
55 appendChildNodes(toSelect,
56 map( makeOption,ifilter(itemgetter('selected'), $(fromSelect).options)));
57 // remove the 'selected' fromSelect
58 replaceChildNodes(fromSelect,
59 list(ifilterfalse(itemgetter('selected'), $(fromSelect).options))
65 class CursoAlumnoForm(W.TableForm):
66 class Fields(W.WidgetsList):
67 alumnos = W.MultipleSelectField(label=_(u'Alumnos'),
68 attrs=dict( style='width:250px'),
70 validator = V.Int(not_empty=False))
71 inscribir = W.Button(default='Inscribir', label='',
72 attrs=dict( onclick='moveOption("form_alumnos","form_inscriptos")'))
73 desinscribir = W.Button(default='Desinscribir', label='',
74 attrs=dict( onclick='moveOption("form_inscriptos","form_alumnos")'))
75 inscriptos = W.MultipleSelectField(label=_(u'Alumnos Inscriptos'),
76 attrs=dict( style='width:250px'),
77 validator = V.Int(not_empty=False))
79 javascript = [W.JSSource("MochiKit.DOM.focusOnLoad('alumnos');"),
81 form = CursoAlumnoForm()
85 class CursoAlumnoController(controllers.Controller, identity.SecureResource):
86 """Basic model admin interface"""
87 require = identity.has_permission('admin')
90 def default(self, curso_id):
91 """handle non exist urls"""
92 return dict(records=r, name=name, namepl=namepl, alumnos=alumnos)
96 raise redirect('/curso/list')
98 @expose(template='kid:%s.templates.list' % __name__)
101 """List records in model"""
103 return dict(records=r, name=name, namepl=namepl)
106 def activate(self, id, activo):
107 """Save or create record to model"""
110 r.activo = bool(int(activo))
112 raise cherrypy.NotFound
113 raise redirect('../../list')
115 @expose(template='kid:%s.templates.new' % __name__)
116 def new(self,curso_id, **kw):
117 """Create new records in model"""
118 curso = Curso.get(curso_id)
119 alumnos_inscriptos = AlumnoInscripto.selectBy(curso=curso)
120 # kw['alumnos'] = alumnos_inscriptos
121 # form.fields.alumnos.options = alumnos_inscriptos
122 return dict(name=name, namepl=namepl, form=form, values=kw)
127 def create(self, **kw):
128 """Save or create record to model"""
130 docentes = kw.get('docentes', [])
131 alumnos = kw.get('alumnos', [])
132 """ Elimino todos los docentes asignados al curso y los agrego nuevamente"""
133 for d in DocenteInscripto.selectBy(curso=r):
135 """ Agrego la nueva seleccion """
137 r.add_docente(Docente(d))
138 """ Elimino a los alumnos y los vuelvo a agregar """
139 for a in AlumnoInscripto.selectBy(curso=r):
142 r.add_alumno(Alumno(a))
143 flash(_(u'Se creó un nuevo %s.') % name)
144 raise redirect('list')
146 @expose(template='kid:%s.templates.edit' % __name__)
147 def edit(self, id, **kw):
148 """Edit record in model"""
150 return dict(name=name, namepl=namepl, record=r, form=form)
155 def update(self, id, **kw):
156 """Save or create record to model"""
157 r = validate_set(id, kw)
158 flash(_(u'El %s fue actualizado.') % name)
159 raise redirect('../list')
161 @expose(template='kid:%s.templates.show' % __name__)
162 def show(self,id, **kw):
163 """Show record in model"""
165 return dict(name=name, namepl=namepl, record=r)
168 def delete(self, id):
169 """Destroy record in model"""
172 flash(_(u'El %s fue eliminado permanentemente.') % name)
173 raise redirect('../list')