Beispiel #1
0
 def edit_view(self):
     if (not request.args) or (not request.args.get('id')):
         return abort(404)
     id = request.args['id']
     procurement = models.Procurement.query.get(id)
     form = forms.ProcurementForm(request.form, procurement)
     # check that the user has permission
     if not g.user.is_admin:
         if procurement.country_id != g.user.country_id:
             return abort(401)
     # set dynamic select choices
     form.supplier.choices = forms.get_supplier_choices()
     form.product.choices = forms.get_product_choices()
     form.source.choices = forms.get_source_choices()
     if request.form:
         # update procurement details
         if request.method == 'POST' and form.validate():
             procurement = self.populate_procurement_from_form(procurement, form)
             db.session.add(procurement)
             db.session.commit()
             flash("The details were updated successfully.", "success")
             if request.args.get('host_url'):
                 target = get_redirect_target(param_name="host_url")
                 return redirect(HOST + target)
             return redirect(url_for('.index_view'))
     else:
         # set field values that weren't picked up automatically
         form.product.process_data(procurement.product_id)
         form.country.process_data(procurement.country_id)
         form.currency.process_data(procurement.currency_id)
         form.incoterm.process_data(procurement.incoterm_id)
         form.supplier.process_data(procurement.supplier_id)
         form.source.process_data(procurement.source_id)
     return self.render('admin/procurement.html', procurement=procurement, form=form, title="Edit procurement record", API_HOST=API_HOST)
Beispiel #2
0
 def add_view(self):
     form = forms.ProcurementForm(request.form)
     # set dynamic select choices
     form.supplier.choices = forms.get_supplier_choices()
     form.product.choices = forms.get_product_choices()
     form.source.choices = forms.get_source_choices()
     if request.method == 'POST' and form.validate():
         procurement = models.Procurement()
         procurement.added_by = g.user
         procurement = self.populate_procurement_from_form(procurement, form)
         db.session.add(procurement)
         db.session.commit()
         flash("The procurement was added successfully.", "success")
         return redirect(url_for('.index_view'))
     if g.user.country:
         form.country.process_data(g.user.country.country_id)
     return self.render('admin/procurement.html', form=form, title="Add procurement record", API_HOST=API_HOST)