class WorkshopAddView(BaseFormView): """ View for adding workshop """ title = u"Créer un nouvel atelier" schema = WorkshopSchema() def before(self, form): auto_need(form) timepicker_fr.need() default_timeslots = get_default_timeslots() form.set_appstruct({'timeslots': default_timeslots}) def submit_success(self, appstruct): """ Create a new workshop """ come_from = appstruct.pop('come_from') timeslots_datas = appstruct.pop('timeslots') for i in timeslots_datas: i.pop('id', None) timeslots_datas.sort(key=lambda val: val['start_time']) appstruct['datetime'] = timeslots_datas[0]['start_time'] appstruct['timeslots'] = [ models.Timeslot(**data) for data in timeslots_datas ] participants_ids = set(appstruct.pop('participants', [])) appstruct['participants'] = [ user.User.get(id_) for id_ in participants_ids ] for timeslot in appstruct['timeslots']: timeslot.participants = appstruct['participants'] workshop_obj = models.Workshop(**appstruct) self.dbsession.add(workshop_obj) self.dbsession.flush() workshop_url = self.request.route_path("workshop", id=workshop_obj.id, _query=dict(action="edit")) if not come_from: redirect = workshop_url else: msg = WORKSHOP_SUCCESS_MSG.format(workshop_url) self.session.flash(msg) redirect = come_from return HTTPFound(redirect)
class WorkshopEditView(BaseFormView): """ Workshop edition view Provide edition functionnality and display a form for attendance recording """ schema = WorkshopSchema() add_template_vars = ( 'title', 'available_status', ) @property def title(self): return self.context.title @property def available_status(self): return ATTENDANCE_STATUS def before(self, form): """ Populate the form before rendering form The deform form object used in this form view (see parent class in pyramid_deform) """ populate_actionmenu(self.request) auto_need(form) timepicker_fr.need() appstruct = self.context.appstruct() participants = self.context.participants appstruct['participants'] = [p.id for p in participants] timeslots = self.context.timeslots appstruct['timeslots'] = [t.appstruct() for t in timeslots] form.set_appstruct(appstruct) return form def _retrieve_workshop_timeslot(self, id_): """ Retrieve an existing workshop model from the current context """ for timeslot in self.context.timeslots: if timeslot.id == id_: return timeslot log.warn(u"Possible break in attempt : On essaye d'éditer un timeslot \ qui n'appartient pas au contexte courant !!!!") raise HTTPForbidden() def _get_timeslots(self, appstruct): datas = appstruct.pop('timeslots') objects = [] datas.sort(key=lambda val: val['start_time']) for data in datas: id_ = data.pop('id', None) if id_ is None: # New timeslots objects.append(models.Timeslot(**data)) else: # existing timeslots obj = self._retrieve_workshop_timeslot(id_) merge_session_with_post(obj, data) objects.append(obj) return objects def submit_success(self, appstruct): """ Handle successfull submission of our edition form """ come_from = appstruct.pop('come_from') appstruct['timeslots'] = self._get_timeslots(appstruct) appstruct['datetime'] = appstruct['timeslots'][0].start_time participants_ids = set(appstruct.pop('participants', [])) appstruct['participants'] = [ user.User.get(id_) for id_ in participants_ids ] for timeslot in appstruct['timeslots']: timeslot.participants = appstruct['participants'] merge_session_with_post(self.context, appstruct) self.dbsession.merge(self.context) workshop_url = self.request.route_path("workshop", id=self.context.id, _query=dict(action="edit")) if not come_from: redirect = workshop_url else: msg = WORKSHOP_SUCCESS_MSG.format(workshop_url) self.session.flash(msg) redirect = come_from return HTTPFound(redirect)
class WorkshopAddView(BaseFormView): """ View for adding workshop """ title = u"Créer un nouvel atelier" schema = WorkshopSchema() def before(self, form): auto_need(form) timepicker_fr.need() default_timeslots = get_default_timeslots() form.set_appstruct({ 'timeslots': default_timeslots, 'owner': self.request.user.id, }) if not self.request.has_permission('edit_owner.event'): form['owner'].widget.readonly = True # Default to current user if not form['owner'].cstruct: form['owner'].cstruct = self.request.user.id def submit_success(self, appstruct): """ Create a new workshop """ come_from = appstruct.pop('come_from') timeslots_datas = appstruct.pop('timeslots') for i in timeslots_datas: i.pop('id', None) timeslots_datas.sort(key=lambda val: val['start_time']) appstruct['datetime'] = timeslots_datas[0]['start_time'] appstruct['timeslots'] = [ models.Timeslot(**data) for data in timeslots_datas ] participants_ids = set(appstruct.pop('participants', [])) appstruct['participants'] = [ user.User.get(id_) for id_ in participants_ids ] for timeslot in appstruct['timeslots']: timeslot.participants = appstruct['participants'] trainers_ids = set(appstruct.pop('trainers', [])) appstruct['trainers'] = [ user.User.get(id_) for id_ in trainers_ids ] # Current user by default if ( self.request.has_permission('edit_owner.event') and appstruct.get('owner') ): appstruct['owner'] = user.User.get(appstruct['owner']) else: appstruct['owner'] = user.User.get(self.request.user.id) workshop_obj = models.Workshop(**appstruct) workshop_obj = merge_session_with_post( workshop_obj, appstruct, remove_empty_values=False, ) self.dbsession.add(workshop_obj) self.dbsession.flush() workshop_url = self.request.route_path( "workshop", id=workshop_obj.id, _query=dict(action="edit") ) if not come_from: redirect = workshop_url else: msg = WORKSHOP_SUCCESS_MSG.format(workshop_url) self.session.flash(msg) redirect = come_from return HTTPFound(redirect)