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'),
78 validator = V.Int(not_empty=False))
80 javascript = [W.JSSource("MochiKit.DOM.focusOnLoad('alumnos');"),
82 form = CursoAlumnoForm()
86 class CursoAlumnoController(controllers.Controller, identity.SecureResource):
87 """Basic model admin interface"""
88 require = identity.has_permission('admin')
91 def default(self, curso_id):
92 """handle non exist urls"""
93 return dict(records=r, name=name, namepl=namepl, alumnos=alumnos)
97 raise redirect('/curso/list')
99 @expose(template='kid:%s.templates.list' % __name__)
102 """List records in model"""
104 return dict(records=r, name=name, namepl=namepl)
107 def activate(self, id, activo):
108 """Save or create record to model"""
111 r.activo = bool(int(activo))
113 raise cherrypy.NotFound
114 raise redirect('../../list')
116 @expose(template='kid:%s.templates.new' % __name__)
117 def new(self,curso_id, **kw):
118 """Create new records in model"""
119 return dict(name=name, namepl=namepl, form=form, values=kw)
124 def create(self, **kw):
125 """Save or create record to model"""
127 docentes = kw.get('docentes', [])
128 alumnos = kw.get('alumnos', [])
129 """ Elimino todos los docentes asignados al curso y los agrego nuevamente"""
130 for d in DocenteInscripto.selectBy(curso=r):
132 """ Agrego la nueva seleccion """
134 r.add_docente(Docente(d))
135 """ Elimino a los alumnos y los vuelvo a agregar """
136 for a in AlumnoInscripto.selectBy(curso=r):
139 r.add_alumno(Alumno(a))
140 flash(_(u'Se creó un nuevo %s.') % name)
141 raise redirect('list')
143 @expose(template='kid:%s.templates.edit' % __name__)
144 def edit(self, id, **kw):
145 """Edit record in model"""
147 return dict(name=name, namepl=namepl, record=r, form=form)
152 def update(self, id, **kw):
153 """Save or create record to model"""
154 r = validate_set(id, kw)
155 flash(_(u'El %s fue actualizado.') % name)
156 raise redirect('../list')
158 @expose(template='kid:%s.templates.show' % __name__)
159 def show(self,id, **kw):
160 """Show record in model"""
162 return dict(name=name, namepl=namepl, record=r)
165 def delete(self, id):
166 """Destroy record in model"""
169 flash(_(u'El %s fue eliminado permanentemente.') % name)
170 raise redirect('../list')