def account_form(self): """ Return a user account edition form """ form = None if self.context.user_id is not None: # There is an associated user account schema = get_user_schema(edit=True, permanent=False) schema = schema.bind(request=self.request) action = self.request.route_path( 'user', id=self.context.user_id, _query=dict(action="edit"), ) form = deform.Form( schema, buttons=(submit_btn, ), action=action, counter=self.counter, ) user_obj = self.context.user appstruct = schema.dictify(user_obj) appstruct.pop('pwd') appstruct['companies'] = [c.name for c in user_obj.companies] appstruct['groups'] = user_obj.groups form.set_appstruct(appstruct) return form
def account_form(self): """ Return a user account edition form """ form = None if self.context.user_id is not None: # There is an associated user account schema = get_user_schema(edit=True, permanent=False) schema = schema.bind(request=self.request) action = self.request.route_path( 'user', id=self.context.user_id, _query=dict(action="edit"), ) form = deform.Form( schema, buttons=(submit_btn,), action=action, counter=self.counter, ) user_obj = self.context.user appstruct = schema.dictify(user_obj) appstruct.pop('pwd') appstruct['companies'] = [c.name for c in user_obj.companies] appstruct['groups'] = user_obj.groups form.set_appstruct(appstruct) return form
class PermanentUserEditView(PermanentUserAddView): """ User edition view """ validate_msg = u"Le compte a bien été modifié" schema = get_user_schema(edit=True) @reify def title(self): """ form title """ return u"Modification de {0}".format(format_account(self.context)) def before(self, form): """ Set the context datas in the view attributes before view execution """ appstruct = self.schema.dictify(self.context) appstruct.pop('pwd') appstruct['groups'] = self.context.groups appstruct['companies'] = [c.name for c in self.context.companies] form.set_appstruct(appstruct) populate_actionmenu(self.request, self.context)
class UserEditView(PermanentUserEditView): """ Contractor's account edition view """ schema = get_user_schema(edit=True, permanent=False) def redirect_url(self, user_model): """ Redirect to the userdatas view associated to the given model pass also the form3 tab name """ return self.request.route_path("userdata", id=user_model.userdatas.id, _anchor='tab3')
def get_user_form(request): """ Return the user add form """ schema = get_user_schema().bind(request=request) return deform.Form(schema, buttons=(submit_btn,))
class PermanentUserAddView(BaseFormView): """ User add form, allows to add : * managers * admins """ title = u"Ajout d'un nouveau compte" validate_msg = u"Le compte a bien été ajouté" schema = get_user_schema() def _get_company(self, name, user): """ Return a company object, create a new one if needed """ query = Company.query() company = query.filter(Company.name == name).first() # avoid creating duplicate companies if company is None: company = self._add_company(name, user) return company def _add_company(self, name, user): """ Add a company 'name' in the database ( set its goal by default ) """ logger.info(u"Adding company : %s" % name) company = Company() company.name = name company.goal = u"Entreprise de {0}".format( format_account(user, reverse=False)) company.contribution = self.request.config.get('contribution_cae') company = self.dbsession.merge(company) self.dbsession.flush() return company def before(self, form): """ populate the actionmenu before entering the view """ populate_actionmenu(self.request) def redirect_url(self, user_model): """ Return the url we should redirect to """ return self.request.route_path("user", id=user_model.id) def _handle_companies(self, company_names, user_model): """ Handle companies if needed if company_names is None (contractor case): nothing happens if company_names is [] (permanent case): user will have no companies """ if company_names is not None: companies = [] for company_name in company_names: company = self._get_company(company_name, user_model) companies.append(company) user_model.companies = companies def submit_success(self, appstruct): """ Add a user to the database Add its companies Add a relationship between companies and the new account """ if "companies" in appstruct: company_names = set(appstruct.pop('companies')) else: company_names = None groups = set(appstruct.pop('groups', [])) password = appstruct.pop('pwd', None) if self.context.__name__ == 'user': user_model = self.schema.objectify(appstruct, self.context) logger.info(u"Edit user : {0}".format(format_account(user_model))) else: user_model = self.schema.objectify(appstruct) logger.info(u"Add user : {0}".format(format_account(user_model))) if password is not None: user_model.set_password(password) user_model.groups = groups self._handle_companies(company_names, user_model) user_model = self.dbsession.merge(user_model) # Here we flush to get an id for the redirect self.dbsession.flush() self.session.flash(self.validate_msg) return HTTPFound(self.redirect_url(user_model))
def get_user_form(request): """ Return the user add form """ schema = get_user_schema().bind(request=request) return deform.Form(schema, buttons=(submit_btn, ))