def save(): ''' Save a new or an existing organisation :return: User's organisations home page ''' form = OrganisationForm() if form.validate_on_submit(): # check if user is trying to edit id = request.form['id'] new = id == "-1" user_can_edit = not new and user_has_rights(id) if not new and not user_has_rights(id): error = "Not allowed to edit this organisation" return to_403(error) # Create/get organisation if new: organisation = Organisation( name=request.form['name'], manager_name=request.form['manager_name']) organisation.users.append(g.user) db.session.add(organisation) db.session.commit() else: organisation = Organisation.query.get_or_404(id) organisation.name = request.form['name'] organisation.manager_name = request.form['manager_name'] if new: address = Address(address=request.form['address'], city=request.form['city'], province=request.form['province'], country=request.form['country'], postal_code=request.form['postal_code'], organisation_id=organisation.id) db.session.add(address) else: address = organisation.addresses.first() address.address = request.form['address'] address.city = request.form['city'] address.province = request.form['province'] address.country = request.form['country'] address.postal_code = request.form['postal_code'] address.organisation_id = organisation.id db.session.commit() flash(organisation.name + " has been saved") return redirect(url_for('organisations.index'))