def organisation_edit(organisation_code=None): packages = dqpackages.packages() packagegroups = dqpackages.packageGroups() organisation = dqorganisations.organisations(organisation_code) if request.method == 'POST': if 'addpackages' in request.form: add_packages(organisation) elif 'addpackagegroup' in request.form: add_packagegroup(organisation) elif 'updateorganisation' in request.form: update_organisation(organisation_code) organisationpackages = dqorganisations.organisationPackages( organisation.organisation_code) return render_template( "organisation_edit.html", organisation=organisation, packages=packages, packagegroups=packagegroups, donorresponses=donorresponse.RESPONSE_TYPES, organisationpackages=organisationpackages, admin=usermanagement.check_perms('admin'), loggedinuser=current_user)
def indicators_comparison(indicatorgroup, indicator): indicator = dqindicators.getIndicatorByName(indicator) organisations = dqorganisations.organisations() return render_template("indicator_comparison.html", loggedinuser=current_user, indicator=indicator, organisations=organisations)
def organisation_survey_edit(organisation_code=None, workflow_name=None): workflow = dqsurveys.workflows(workflow_name) if not workflow: flash('That workflow does not exist.', 'error') return abort(404) organisation = dqorganisations.organisations(organisation_code) organisationsurvey = dqsurveys.getOrCreateSurvey({ 'organisation_id': organisation.id }) def allowed(method): permission_name = "survey_" + workflow_name permission_value = {'organisation_code': organisation_code} return usermanagement.check_perms(permission_name, method, permission_value) allowed_to_edit = allowed("edit") allowed_to_view = allowed("view") if not allowed_to_view: flash("Sorry, you do not have permission to view that survey", 'error') if request.referrer is not None: redir_to = request.referrer else: redir_to = url_for('home') return redirect(redir_to) if request.method=='POST': if not allowed_to_edit: flash("Sorry, you do not have permission to update that survey", 'error') if request.referrer is not None: redir_to = request.referrer else: redir_to = url_for('home') if (workflow.WorkflowType.name=='collect'): _survey_process_collect(organisation, workflow, request, organisationsurvey) elif workflow.WorkflowType.name=='send': if workflow.Workflow.id == organisationsurvey.currentworkflow_id: _survey_process_send(organisation_code, workflow, request, organisationsurvey) else: flash("Not possible to send survey to donor because it's not at the current stage in the workflow. Maybe you didn't submit the data, or maybe you already sent it to the donor?", 'error') elif workflow.WorkflowType.name=='review': _survey_process_review(organisation, workflow, request, organisationsurvey) elif workflow.WorkflowType.name=='comment': _survey_process_comment(organisation, workflow, request, organisationsurvey) elif workflow.WorkflowType.name=='finalreview': _survey_process_finalreview(organisation, workflow, request, organisationsurvey) elif workflow.WorkflowType.name=='finalised': return "finalised" return redirect(url_for("organisations", organisation_code=organisation_code)) else: return organisation_survey_view(organisation_code, workflow, workflow_name, organisationsurvey, allowed_to_edit)
def organisations_index(organisation_code=None): aggregation_type=integerise(request.args.get('aggregation_type', 2)) template_args = {} org_packages = dqorganisations.organisationPackages(organisation_code) organisation = dqorganisations.organisations(organisation_code) packagegroups = dqorganisations.organisationPackageGroups(organisation_code) irs = [ir for ir in get_info_results(org_packages, organisation)] coverage = get_coverage(organisation, irs) organisation_survey = dqsurveys.getSurvey(organisation_code) surveydata = dqsurveys.getSurveyDataAllWorkflows(organisation_code) surveydata, _ = dqsurveys.get_survey_data_and_workflow( organisation_survey, surveydata) summary_data = get_summary_data(organisation, aggregation_type) allowed_to_view_survey = usermanagement.check_perms( "survey", "view") allowed_to_edit_survey_researcher = usermanagement.check_perms( "survey_researcher", "edit", {"organisation_code": organisation_code}) show_researcher_button = ( allowed_to_edit_survey_researcher and ( (organisation_survey and organisation_survey.Workflow.name == 'researcher') or (not organisation_survey) ) ) template_args = dict(organisation=organisation, summary_data=summary_data, packagegroups=packagegroups, coverage=coverage, surveydata=surveydata, admin=usermanagement.check_perms('admin'), loggedinuser=current_user, allowed_to_view_survey=allowed_to_view_survey, show_researcher_button=show_researcher_button) return render_template("organisation_index.html", **template_args)
def organisations_coverage(): organisations = dqorganisations.organisations() coverage_data = {} for organisation in organisations: org_packages = dqorganisations.organisationPackages(organisation.organisation_code) irs = [ir for ir in get_info_results(org_packages, organisation)] coverage_data[organisation.id] = (get_coverage(organisation, irs)) return render_template("organisations_coverage.html", organisations=organisations, coverage_data=coverage_data, admin=usermanagement.check_perms('admin'), loggedinuser=current_user )
def organisations(organisation_code=None): check_perms = usermanagement.check_perms('organisation', 'view', {'organisation_code':organisation_code}) if organisation_code is not None: if check_perms: return redirect(url_for('organisations_index', organisation_code=organisation_code)) else: return redirect(url_for('organisation_publication', organisation_code=organisation_code)) else: organisations = dqorganisations.organisations() template_args = dict(organisations=organisations, admin=usermanagement.check_perms('admin'), loggedinuser=current_user) return render_template("organisations.html", **template_args)
def organisation_survey(organisation_code=None): organisation = dqorganisations.organisations(organisation_code) # make sure survey exists dqsurveys.getOrCreateSurvey({'organisation_id':organisation.id}) survey = dqsurveys.getSurvey(organisation_code) surveydata = dqsurveys.getSurveyDataAllWorkflows(organisation_code) workflows = dqsurveys.workflowsAll() pct_complete = completion_percentage(survey) users = dqusers.surveyPermissions(organisation_code) admin = usermanagement.check_perms('admin') loggedinuser = current_user checksurveyOK = dqsurveys.checkSurveyData(organisation_code) return render_template("surveys/survey.html", **locals())
def organisation_survey(organisation_code=None): organisation = dqorganisations.organisations(organisation_code) make_sure_survey_exists = dqsurveys.getOrCreateSurvey({'organisation_id':organisation.id}) survey = dqsurveys.getSurvey(organisation_code) surveydata = dqsurveys.getSurveyDataAllWorkflows(organisation_code) workflows = dqsurveys.workflows() pct_complete = completion_percentage(survey) users = dqusers.surveyPermissions(organisation_code) return render_template("surveys/survey.html", organisation=organisation, survey=survey, workflows=workflows, pct_complete=pct_complete, surveydata=surveydata, users=users, admin=usermanagement.check_perms('admin'), loggedinuser=current_user)
def users_edit(username=None): user = {} permissions = {} if username: user = dqusers.user_by_username(username) permissions = dqusers.userPermissions(user.id) if request.method == 'POST': if user: data = { 'username': username, 'password': request.form.get('password'), 'name': request.form['name'], 'email_address': request.form['email_address'], 'organisation': request.form['organisation'] } user = dqusers.updateUser(data) flash('Successfully updated user.', 'success') else: user = {} flash('Could not update user.', 'error') else: if request.method == 'POST': user = dqusers.addUser({ 'username': request.form['username'], 'password': request.form['password'], 'name': request.form['name'], 'email_address': request.form['email_address'], 'organisation': request.form['organisation'] }) if user: flash('Successfully added new user', 'success') return redirect(url_for('users_edit', username=user.username)) else: flash('Could not add user', 'error') return render_template("users_edit.html", user=user, permissions=permissions, admin=usermanagement.check_perms('admin'), loggedinuser=current_user, organisations=dqorganisations.organisations())
def checkSurveyData(organisation_code): # for each currently active stage of the workflow # check if there is an indicator at each stage of the workflow # if not, then create one allindicators = dqindicators.indicators(app.config["INDICATOR_GROUP"]) allindicators = map(lambda x: x.id, allindicators) organisation = dqorganisations.organisations(organisation_code) org_indicators = dqorganisations._organisation_indicators_split(organisation, 2)["zero"].keys() survey = getSurvey(organisation_code).OrganisationSurvey survey_data = getSurveyDataAllWorkflows(organisation_code) for workflow_name, v in survey_data.items(): workflow = workflowByName(workflow_name) survey_indicators = v.keys() for indicator in org_indicators: if indicator not in survey_indicators: return False return True
def repairSurveyData(organisation_code): # for each currently active stage of the workflow # check if there is an indicator at each stage of the workflow # if not, then create one changes = False changed_indicators = [] allindicators = dqindicators.indicators(app.config["INDICATOR_GROUP"]) allindicators = map(lambda x: x.id, allindicators) organisation = dqorganisations.organisations(organisation_code) org_indicators = dqorganisations._organisation_indicators_split(organisation, 2)["zero"] survey = getSurvey(organisation_code).OrganisationSurvey survey_data = getSurveyDataAllWorkflows(organisation_code) for workflow_name, v in survey_data.items(): workflow = workflowByName(workflow_name) survey_indicators = v.keys() for indicator, indicatordata in org_indicators.items(): if indicator not in survey_indicators: print "NOT FOUND:", indicator data = { 'organisationsurvey_id' : survey.id, 'workflow_id' : workflow.Workflow.id, 'indicator_id' : indicator, 'published_status' : None, 'published_source' : None, 'published_comment' : None, 'published_accepted' : None, 'published_format' : None, 'ordinal_value' : None } addSurveyData(data) changes = True changed_indicators.append(indicatordata["indicator_name"]) else: print "FOUND:", indicator return {'changes': changes, 'changed_indicators': changed_indicators}
def organisations_feedback(organisation_code=None): if (organisation_code is not None): organisation = dqorganisations.organisations(organisation_code) if (request.method=="POST"): for condition in request.form.getlist('feedback'): data = { 'organisation_id': organisation.id, 'uses': request.form['uses'+condition], 'element': request.form['element'+condition], 'where': request.form['where'+condition] } if dqorganisations.addFeedback(data): flash('Successfully added condition.', 'success') else: flash("Couldn't add condition.", 'error') return render_template("organisation_feedback.html", organisation=organisation, admin=usermanagement.check_perms('admin'), loggedinuser=current_user) else: flash('No organisation supplied', 'error') return redirect(url_for('organisations'))
def organisations_index(organisation_code=None): info_results = { } aggregation_type=integerise(request.args.get('aggregation_type', 2)) template_args = {} org_packages = dqorganisations.organisationPackages(organisation_code) def get_info_results(): for _, p in org_packages: package_id = p.package_id from sqlalchemy import func runtime = db.session.query( func.max(InfoResult.runtime_id)).filter( InfoResult.package_id == package_id ).first() import iatidq.inforesult runtime, = runtime results = iatidq.inforesult.info_results(package_id, runtime, organisation.id) if "coverage" in results: try: yield int(results["coverage_current"]) except TypeError: yield 0 organisation = dqorganisations.organisations(organisation_code) packagegroups = dqorganisations.organisationPackageGroups(organisation_code) info_results["coverage_current"] = \ reduce(operator.add, [ir for ir in get_info_results()], 0) # coverage_total = organisation.organisation_total_spend # FIXME: use organisation_total_spend # when data is imported to db coverage_total = (organisation.organisation_total_spend)*1000000 coverage_found = info_results["coverage_current"] if (coverage_total and coverage_found): coverage_pct = int((float(coverage_found)/float(coverage_total))*100) coverage = { 'total': coverage_total, 'found': coverage_found, 'pct': coverage_pct } else: coverage = { 'total': None, 'found': None, 'pct': None } organisation_survey = dqsurveys.getSurvey(organisation_code) surveydata = dqsurveys.getSurveyDataAllWorkflows(organisation_code) if organisation_survey: if organisation_survey.Workflow.name in ['donorreview', 'pwyfreview']: surveydata = surveydata["researcher"] surveydata_workflow = 'donorreview' elif organisation_survey.Workflow.name in ['donorcomments', 'pwyffinal']: surveydata = surveydata["pwyfreview"] surveydata_workflow = 'donorcomments' elif organisation_survey.Workflow.name == 'finalised': surveydata = surveydata["pwyffinal"] surveydata_workflow = 'finalised' else: surveydata = None else: surveydata = None try: summary_data = _organisation_indicators_summary(organisation, aggregation_type) except Exception, e: summary_data = None
def organisation_edit(organisation_code=None): packages = dqpackages.packages() packagegroups = dqpackages.packageGroups() organisation = dqorganisations.organisations(organisation_code) if request.method == 'POST': if 'addpackages' in request.form: condition = request.form['condition'] def add_org_pkg(package): data = { 'organisation_id': organisation.id, 'package_id': package, 'condition': condition } if dqorganisations.addOrganisationPackage(data): flash('Successfully added package to your organisation.', 'success') else: flash("Couldn't add package to your organisation.", 'error') packages = request.form.getlist('package') [ add_org_pkg(package) for package in packages ] elif 'addpackagegroup' in request.form: condition = request.form['condition'] data = { 'organisation_id': organisation.id, 'packagegroup_id': request.form['packagegroup'], 'condition': condition } add_packagegroups = dqorganisations.addOrganisationPackageFromPackageGroup(data) if 'applyfuture' in request.form: if dqorganisations.addOrganisationPackageGroup(data): flash('All future packages in this package group will be added to this organisation', 'success') else: flash('Could not ensure that all packages in this package group will be added to this organisation', 'error') if add_packagegroups: flash('Successfully added ' + str(add_packagegroups) + ' packages to your organisation.', 'success') else: flash("No packages were added to your organisation. This could be because you've already added all existing ones.", 'error') elif 'updateorganisation' in request.form: data = { 'organisation_code': request.form['organisation_code'], 'organisation_name': request.form['organisation_name'] } organisation = dqorganisations.updateOrganisation( organisation_code, data) organisationpackages = dqorganisations.organisationPackages( organisation.organisation_code) return render_template( "organisation_edit.html", organisation=organisation, packages=packages, packagegroups=packagegroups, organisationpackages=organisationpackages, admin=usermanagement.check_perms('admin'), loggedinuser=current_user)
def organisation_survey_edit(organisation_code=None, workflow_name=None): workflow = dqsurveys.workflowByName(workflow_name) if not workflow: flash('That workflow does not exist.', 'error') return abort(404) organisation = dqorganisations.organisations(organisation_code) organisationsurvey = dqsurveys.getOrCreateSurvey({ 'organisation_id': organisation.id }) def allowed(method): permission_name = "survey_" + workflow_name permission_value = {'organisation_code': organisation_code} return usermanagement.check_perms(permission_name, method, permission_value) allowed_to_edit = allowed("edit") allowed_to_view = allowed("view") def no_permission(): # If not logged in, redirect to login page if not current_user.is_authenticated(): flash('You must log in to access that page.', 'error') return redirect(url_for('login', next=request.path)) # Otherwise, redirect to previous page and warn user # they don't have permissions to access the survey. flash("Sorry, you do not have permission to view that survey", 'error') if request.referrer is not None: redir_to = request.referrer else: redir_to = url_for('home') return redirect(redir_to) if not allowed_to_view: return no_permission() if request.method != 'POST': return organisation_survey_view( organisation_code, workflow, workflow_name, organisationsurvey, allowed_to_edit) if not allowed_to_edit: return no_permission() handlers = { "collect": _survey_process_collect, "send": _survey_process_send, "review": _survey_process_review, "comment": _survey_process_comment, "finalreview": _survey_process_finalreview } workflow_name = workflow.WorkflowType.name if workflow_name == "send": if workflow.Workflow.id == organisationsurvey.currentworkflow_id: _survey_process_send( organisation_code, workflow, request, organisationsurvey) else: flash("Not possible to send survey to donor because it's " "not at the current stage in the workflow. " "Maybe you didn't submit the data, or maybe you " "already sent it to the donor?", 'error') elif workflow_name in handlers: handlers[workflow_name]( organisation, workflow, request, organisationsurvey) elif workflow_name == 'finalised': return "finalised" return redirect(url_for("organisations", organisation_code=organisation_code))