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)
def edit_view(self): return_url = get_redirect_target() or self.get_url('.index_view') if not self.can_edit: return redirect(return_url) id = get_mdict_item_or_list(request.args, 'id') if id is None: return redirect(return_url) model = self.get_one(id) if model is None: return redirect(return_url) form = self.edit_form(obj=model) if self.validate_form(form): if self.update_model(form, model): if '_continue_editing' in request.form: flash(gettext('Model was successfully saved.')) return redirect(request.url) else: return redirect(return_url) form_widget_args = self.form_widget_args if hasattr(self, 'edit_form_widget_args'): form_widget_args = self.edit_form_widget_args form_opts = FormOpts(widget_args=form_widget_args, form_rules=self._form_edit_rules) template = self.edit_ajax_template if self.edit_ajax == True else self.edit_template return self.render(template, model=model, form=form, form_opts=form_opts, return_url=return_url)
def delete_view(self): """ Delete model view. Only POST method is allowed. """ return_url = get_redirect_target() or self.get_url(".index_view") if not self.can_delete: return redirect(return_url) form = self.delete_form() if self.validate_form(form): # id is Required() id = form.id.data model = self.get_one(id) if model is None: return redirect(return_url) # message is flashed from within delete_model if it fails if self.delete_model(model): flash(gettext("Record was successfully deleted.")) return redirect(return_url) else: flash_errors(form, message="Failed to delete record. %(error)s") return redirect(return_url)
def edit_view(self): """ Edit model view """ return_url = get_redirect_target() or url_for(".index_view") if not self.can_edit: return redirect(return_url) id = get_mdict_item_or_list(request.args, "id") if id is None: return redirect(return_url) model = self.get_one(id) if model is None: return redirect(return_url) form = self.edit_form(obj=model) if validate_form_on_submit(form): if self.update_model(form, model): if "_continue_editing" in request.form: flash(gettext("Model was successfully saved.")) return redirect(request.url) else: return redirect(return_url) if request.method == "GET": self.on_form_prefill(form, id) form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_edit_rules) return self.render(self.edit_template, model=model, form=form, form_opts=form_opts, return_url=return_url)
def create_view(self): """ Create model view """ return_url = get_redirect_target() or url_for('.index_view') if not self.can_create: return redirect(return_url) form = self.create_form() if validate_form_on_submit(form): if self.create_model(form): if '_add_another' in request.form: flash(gettext('Model was successfully created.')) return redirect(url_for('.create_view', url=return_url)) else: return redirect(return_url) form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_create_rules) return self.render(self.create_template, form=form, form_opts=form_opts, return_url=return_url)
def index(self): """ A view for performing various acts of data maintenance. """ return_url = get_redirect_target() or \ self.get_url('maintenanceview.index') if request.method == 'GET': # Get all categories and group them # using the remedyblueprint method grouped_categories = group_active_categories( Category.query.all()) # Get all populations and group them # using the remedyblueprint method grouped_populations = group_active_populations( Population.query.all()) return self.render( 'admin/maintenance.html', grouped_populations=grouped_populations, grouped_categories=grouped_categories, return_url=return_url) else: query = Resource.query # See if there's categories/populations to filter on categories = request.form.getlist('categories') populations = request.form.getlist('populations') if len(categories) > 0: query = query.filter(Resource.categories.any( Category.id.in_(categories))) if len(populations) > 0: query = query.filter(Resource.populations.any( Population.id.in_(populations))) # Now apply our filtering. target_resources = query.all() if len(target_resources) > 0: # Touch the last-updated date. for resource in target_resources: resource.last_updated = datetime.utcnow() # Save our changes. self.session.commit() # Indicate how many we changed. flash( 'Updated ' + str(len(target_resources)) + ' resource(s).', 'success') else: flash('No resources matched the provided query.', 'warning') return redirect(return_url)
def index(self): """ A view for performing various acts of data maintenance. """ return_url = get_redirect_target() or \ self.get_url('maintenanceview.index') if request.method == 'GET': # Get all categories and group them # using the remedyblueprint method grouped_categories = group_active_categories(Category.query.all()) # Get all populations and group them # using the remedyblueprint method grouped_populations = group_active_populations( Population.query.all()) return self.render('admin/maintenance.html', grouped_populations=grouped_populations, grouped_categories=grouped_categories, return_url=return_url) else: query = Resource.query # See if there's categories/populations to filter on categories = request.form.getlist('categories') populations = request.form.getlist('populations') if len(categories) > 0: query = query.filter( Resource.categories.any(Category.id.in_(categories))) if len(populations) > 0: query = query.filter( Resource.populations.any(Population.id.in_(populations))) # Now apply our filtering. target_resources = query.all() if len(target_resources) > 0: # Touch the last-updated date. for resource in target_resources: resource.last_updated = datetime.utcnow() # Save our changes. self.session.commit() # Indicate how many we changed. flash( 'Updated ' + str(len(target_resources)) + ' resource(s).', 'success') else: flash('No resources matched the provided query.', 'warning') return redirect(return_url)
def action_assignpopulations(self, ids): """ Sets up a redirection action for mass-assigning populations to the specified resources. Args: ids: The list of resource IDs that should be updated. """ return_url = get_redirect_target() or self.get_url('.index_view') return redirect(self.get_url('resourcepopulationassignview.index', url=return_url, ids=ids))
def action_assignpopulations(self, ids): """ Sets up a redirection action for mass-assigning populations to the specified resources. Args: ids: The list of resource IDs that should be updated. """ return_url = get_redirect_target() or self.get_url('.index_view') return redirect( self.get_url('resourcepopulationassignview.index', url=return_url, ids=ids))
def edit_view(self): """ Edit model view """ return_url = get_redirect_target() or self.get_url('.index_view') if not self.can_edit: return redirect(return_url) id = get_mdict_item_or_list(request.args, 'id') if id is None: return redirect(return_url) model = self.get_one(id) if model is None: return redirect(return_url) form = self.edit_form(obj=model) if not hasattr(form, '_validated_ruleset') or not form._validated_ruleset: self._validate_form_instance(ruleset=self._form_create_rules, form=form) if self.validate_form(form): if self.update_model(form, model): flash(gettext('Record was successfully saved.')) if '_save_upload' in request.form: if self.upload_model(model) is True: flash(gettext('Record was successfully uploaded.')) else: flash(gettext('Record could not be uploaded.')) return redirect(request.url) if '_continue_editing' in request.form: return redirect(request.url) else: return redirect(return_url) if request.method == 'GET': self.on_form_prefill(form, id) form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_edit_rules) return self.render(self.edit_template, model=model, form=form, form_opts=form_opts, return_url=return_url)
def delete_view(self): """ Delete model view. Only POST method is allowed. """ return_url = get_redirect_target() or url_for('.index_view') # TODO: Use post if not self.can_delete: return redirect(return_url) id = get_mdict_item_or_list(request.args, 'id') if id is None: return redirect(return_url) model = self.get_one(id) if model: self.delete_model(model) return redirect(return_url)
def edit_view(self): """ Edit model view """ return_url = get_redirect_target() or self.get_url('.index_view') if not self.can_edit: return redirect(return_url) id = get_mdict_item_or_list(request.args, 'id') if id is None: return redirect(return_url) model = self.get_one(id) if model is None: return redirect(return_url) form = self.edit_form(obj=model) if self.validate_form(form): if self.update_model(form, model): if '_continue_editing' in request.form: flash(gettext('Model was successfully saved.')) return redirect(request.url) else: return redirect(return_url) form_widget_args = self.form_widget_args if hasattr(self, 'edit_form_widget_args'): form_widget_args = self.edit_form_widget_args form_opts = FormOpts(widget_args=form_widget_args, form_rules=self._form_edit_rules) return self.render(self.edit_template, model=model, form=form, form_opts=form_opts, return_url=return_url)
def create_view(self): """ Create model view """ return_url = get_redirect_target() or self.get_url(".index_view") if not self.can_create: return redirect(return_url) form = self.create_form() if self.validate_form(form): if self.create_model(form): if "_add_another" in request.form: flash(gettext("Record was successfully created.")) return redirect(request.url) else: return redirect(return_url) form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_create_rules) return self.render(self.create_template, form=form, form_opts=form_opts, return_url=return_url)
def upload_view(self): """ upload the page and return to the list view """ return_url = get_redirect_target() or self.get_url('.index_view') if not self.can_upload: flash('Page upload not allowed', 'error') _id = request.args.get('id', None) if _id is None: flash('Page id not given', 'error') model = self.get_one(_id) if model is None: flash('Page not found', 'error') if not self.upload_model(model): flash('Failed to upload Page', 'error') else: flash('Uploaded Page successfully', 'message') return redirect(return_url)
def create_view(self): return_url = get_redirect_target() or self.get_url('.index_view') if not self.can_create: return redirect(return_url) self.template = request.args.get('template', None) form = self.create_form() if self.validate_form(form): try: ret_bool, obj = self.create_model(form) except NotUniqueError: flash('Record creation failed.', 'error') return redirect(return_url) if ret_bool: if '_add_another' in request.form: flash('Record was successfully created.') return redirect(return_url) elif obj is not None: return_url = self.get_url( '.edit_view', id=obj.pk, url=self.get_url('.index_view') ) flash('Record was successfully created.') return redirect(return_url) else: flash('Record creation failed.') return redirect(return_url) form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_create_rules) return self.render(self.create_template, form=form, form_opts=form_opts, return_url=return_url)
def create_view(self): return_url = get_redirect_target() or self.get_url('.index_view') if not self.can_create: return redirect(return_url) form = self.create_form() if self.validate_form(form): if self.create_model(form): if '_add_another' in request.form: flash(gettext('Model was successfully created.')) return redirect(request.url) else: return redirect(return_url) form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_create_rules) template = self.create_ajax_template if self.create_ajax == True else self.create_template return self.render(template, form=form, form_opts=form_opts, return_url=return_url)
def edit_view(self): """ Edit model view """ return_url = get_redirect_target() or url_for('.index_view') if not self.can_edit: return redirect(return_url) id = get_mdict_item_or_list(request.args, 'id') if id is None: return redirect(return_url) model = self.get_one(id) if model is None: return redirect(return_url) form = self.edit_form(obj=model) if validate_form_on_submit(form): if self.update_model(form, model): if '_continue_editing' in request.form: flash(gettext('Model was successfully saved.')) return redirect(request.url) else: return redirect(return_url) form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_create_rules) return self.render(self.edit_template, model=model, form=form, form_opts=form_opts, return_url=return_url)
def index(self): """ A view for mass-assigning resources to populations. """ return_url = get_redirect_target() or \ self.get_url('population-resourceview.index_view') # Load all resources by the set of IDs target_resources = Resource.query. \ filter(Resource.id.in_(request.args.getlist('ids'))) target_resources = target_resources. \ order_by(Resource.name.asc()).all() # Make sure we have some, and go back to the resources # view (for assigning populations) if we don't. if len(target_resources) == 0: flash('At least one resource must be selected.', 'error') return redirect(url_for(return_url)) if request.method == 'GET': # Get all populations available_populations = Population.query. \ order_by(Population.name.asc()).all() # Group them using the remedyblueprint method grouped_populations = group_active_populations( available_populations) # Return the view for assigning populations return self.render( 'admin/resource_assign_populations.html', ids=request.args.getlist('ids'), resources=target_resources, grouped_populations=grouped_populations, return_url=return_url) else: # Get the selected populations - use request.form, # not request.args target_populations = Population.query.filter( Population.id.in_(request.form.getlist('populations'))).all() if len(target_populations) > 0: # Build a list of all the results results = [] for resource in target_resources: # Build a helpful message string to use for resources. resource_str = 'resource #' + str(resource.id) + \ ' (' + resource.name + ')' try: # Assign all populations for population in target_populations: # Make sure we're not double-adding if population not in resource.populations: resource.populations.append(population) resource.last_updated = datetime.utcnow() except Exception as ex: results.append( 'Error updating ' + resource_str + ': ' + str(ex)) else: results.append( 'Updated ' + resource_str + '.') # Save our changes. self.session.commit() # Flash the results of everything flash("\n".join(msg for msg in results)) else: flash('At least one population must be selected.', 'error') return redirect(return_url)
def index(self): """ A view for mass-assigning resources to populations. """ return_url = get_redirect_target() or \ self.get_url('population-resourceview.index_view') # Load all resources by the set of IDs target_resources = Resource.query. \ filter(Resource.id.in_(request.args.getlist('ids'))) target_resources = target_resources. \ order_by(Resource.name.asc()).all() # Make sure we have some, and go back to the resources # view (for assigning populations) if we don't. if len(target_resources) == 0: flash('At least one resource must be selected.', 'error') return redirect(url_for(return_url)) if request.method == 'GET': # Get all populations available_populations = Population.query. \ order_by(Population.name.asc()).all() # Group them using the remedyblueprint method grouped_populations = group_active_populations( available_populations) # Return the view for assigning populations return self.render('admin/resource_assign_populations.html', ids=request.args.getlist('ids'), resources=target_resources, grouped_populations=grouped_populations, return_url=return_url) else: # Get the selected populations - use request.form, # not request.args target_populations = Population.query.filter( Population.id.in_(request.form.getlist('populations'))).all() if len(target_populations) > 0: # Build a list of all the results results = [] for resource in target_resources: # Build a helpful message string to use for resources. resource_str = 'resource #' + str(resource.id) + \ ' (' + resource.name + ')' try: # Assign all populations for population in target_populations: # Make sure we're not double-adding if population not in resource.populations: resource.populations.append(population) resource.last_updated = datetime.utcnow() except Exception as ex: results.append('Error updating ' + resource_str + ': ' + str(ex)) else: results.append('Updated ' + resource_str + '.') # Save our changes. self.session.commit() # Flash the results of everything flash("\n".join(msg for msg in results)) else: flash('At least one population must be selected.', 'error') return redirect(return_url)