def reminders(request, project_id):
    if request.method == 'GET':
        dbm = get_database_manager(request.user)
        project = Project.load(dbm.database, project_id)
        questionnaire = FormModel.get(dbm, project.qid)
        from datawinners.project.views import make_project_links

        project_links = make_project_links(project, questionnaire.form_code)
        reminders = Reminder.objects.filter(voided=False, project_id=project_id).order_by('id')
        organization = get_organization(request)
        from datawinners.project.views import _format_reminders, create_reminder

        return render_to_response('project/reminders.html',
                                  {'project': project,
                                   'reminders': _format_reminders(reminders, project_id),
                                   'in_trial_mode': organization.in_trial_mode,
                                   'create_reminder_link': reverse(create_reminder, args=[project_id]),
                                   'project_links': project_links,
                                   'is_quota_reached': is_quota_reached(request, organization=organization)},
                                  context_instance=RequestContext(request))
Exemplo n.º 2
0
def questionnaire(request, project_id=None):
    manager = get_database_manager(request.user)
    if request.method == 'GET':
        project = Project.load(manager.database, project_id)
        form_model = FormModel.get(manager, project.qid)
        fields = form_model.fields
        if form_model.is_entity_type_reporter():
            fields = helper.hide_entity_question(form_model.fields)
        existing_questions = json.dumps(fields, default=field_to_json)
        project_links = make_project_links(project, form_model.form_code)
        in_trial_mode = _in_trial_mode(request)
        return render_to_response('project/questionnaire.html',
                                  {"existing_questions": repr(existing_questions),
                                   'questionnaire_code': form_model.form_code,
                                   'project': project,
                                   'project_links': project_links,
                                   'is_quota_reached': is_quota_reached(request),
                                   'in_trial_mode': in_trial_mode,
                                   'preview_links': get_preview_and_instruction_links_for_questionnaire()},
                                  context_instance=RequestContext(request))
def reminder_settings(request, project_id):
    dbm = get_database_manager(request.user)
    project = Project.load(dbm.database, project_id)
    questionnaire = FormModel.get(dbm, project.qid)
    from datawinners.project.views.views import make_project_links

    project_links = make_project_links(project, questionnaire.form_code)
    org_id = (NGOUserProfile.objects.get(user=request.user)).org_id
    organization = Organization.objects.get(org_id=org_id)
    html = 'project/reminders_trial.html' if organization.in_trial_mode else 'project/reminder_settings.html'
    if request.method == 'GET':
        form = ReminderForm(data=(_reminder_info_about_project(project)))
        return render_to_response(html,
                                  {'project_links': project_links,
                                   'is_quota_reached': is_quota_reached(request, organization=organization),
                                   'project': project, 'form': form}, context_instance=RequestContext(request))

    if request.method == 'POST':
        form = ReminderForm(data=request.POST.copy())
        if form.is_valid():
            org_id = NGOUserProfile.objects.get(user=request.user).org_id
            organization = Organization.objects.get(org_id=org_id)
            reminder_list = Reminder.objects.filter(project_id=project.id)
            action = _get_activity_log_action(reminder_list, form.cleaned_data)
            project, set_deadline = _add_reminder_info_to_project(form.cleaned_data, project, organization,
                                                                  reminder_list=reminder_list)
            project.save(dbm)
            if action is not None:
                UserActivityLog().log(request, action=action, project=project.name)
            if set_deadline:
                UserActivityLog().log(request, action=SET_DEADLINE, project=project.name)
            messages.success(request, _("Reminder settings saved successfully."))
            return HttpResponseRedirect('')
        else:
            return render_to_response(html,
                                      {'project_links': project_links,
                                       'is_quota_reached': is_quota_reached(request, organization=organization),
                                       'project': project, 'form': form}, context_instance=RequestContext(request))
Exemplo n.º 4
0
def get_submissions_about_project(request, project_id):
    dbm = get_database_manager(request.user)
    project = Project.load(dbm.database, project_id)
    form_model = FormModel.get(dbm, project.qid)
    rows = dbm.load_all_rows_in_view('undeleted_submission_log',
                                     reduce=False,
                                     descending=True,
                                     startkey=[form_model.form_code, {}],
                                     endkey=[form_model.form_code],
                                     limit=7)
    submission_list = []
    for row in rows:
        reporter = _find_reporter_name(dbm, row)
        message = _make_message(row)
        submission = dict(
            message=message,
            created=row.value["submitted_on"].strftime("%B %d %y %H:%M"),
            reporter=reporter,
            status=row.value["status"])
        submission_list.append(submission)

    submission_response = json.dumps(submission_list)
    return HttpResponse(submission_response)
Exemplo n.º 5
0
def questionnaire_preview(request, project_id=None, sms_preview=False):
    manager = get_database_manager(request.user)
    if request.method == 'GET':
        project = Project.load(manager.database, project_id)
        form_model = FormModel.get(manager, project.qid)
        fields = form_model.fields
        if form_model.is_entity_type_reporter():
            fields = helper.hide_entity_question(form_model.fields)
        project_links = make_project_links(project, form_model.form_code)
        questions = []
        for field in fields:
            question = helper.get_preview_for_field(field)
            questions.append(question)
        example_sms = "%s" % (
            form_model.form_code)
        example_sms += get_example_sms(fields)

    template = 'project/questionnaire_preview.html' if sms_preview else 'project/questionnaire_preview_list.html'
    return render_to_response(template,
                              {"questions": questions, 'questionnaire_code': form_model.form_code,
                               'project': project, 'project_links': project_links,
                               'is_quota_reached': is_quota_reached(request),
                               'example_sms': example_sms, 'org_number': get_organization_telephone_number(request)},
                              context_instance=RequestContext(request))
Exemplo n.º 6
0
def project_overview(request, project_id=None):
    manager = get_database_manager(request.user)
    project = Project.load(manager.database, project_id)
    questionnaire = FormModel.get(manager, project.qid)
    number_of_questions = len(questionnaire.fields)
    questionnaire_code = questionnaire.form_code
    project_links = make_project_links(project, questionnaire_code)
    map_api_key = get_map_key(request.META['HTTP_HOST'])
    number_data_sender = len(project.data_senders)
    number_records = survey_response_count(manager, questionnaire_code, None, None)
    number_reminders = Reminder.objects.filter(project_id=project.id).count()
    links = {'registered_data_senders': reverse("registered_datasenders", args=[project_id]),
             'web_questionnaire_list': reverse('web_questionnaire', args=[project_id])}
    add_data_senders_to_see_on_map_msg = _(
        "Register Data Senders to see them on this map") if number_data_sender == 0 else ""
    add_subjects_to_see_on_map_msg = _(
        "Register %s to see them on this map") % project.entity_type if get_entity_count_for_type(manager,
                                                                                                  project.entity_type) == 0 else ""
    in_trial_mode = _in_trial_mode(request)
    return render_to_response('project/overview.html', RequestContext(request, {
        'project': project,
        'entity_type': project['entity_type'],
        'project_links': project_links,
        'is_quota_reached': is_quota_reached(request),
        'number_of_questions': number_of_questions,
        'map_api_key': map_api_key,
        'number_data_sender': number_data_sender,
        'number_records': number_records,
        'number_reminders': number_reminders,
        'links': links,
        'add_data_senders_to_see_on_map_msg': add_data_senders_to_see_on_map_msg,
        'add_subjects_to_see_on_map_msg': add_subjects_to_see_on_map_msg,
        'in_trial_mode': in_trial_mode,
        'questionnaire_code': questionnaire_code,
        'org_number':get_organization_telephone_number(request)
    }))
Exemplo n.º 7
0
def delete_datasender_from_project(manager, all_ids):
    for entity_id in all_ids:
        associated_projects = get_all_projects(manager, data_sender_id=entity_id)
        for associated_project in associated_projects:
            project = Project.load(manager.database, associated_project['value']['_id'])
            project.delete_datasender(manager, entity_id)
Exemplo n.º 8
0
 def test_get_one_project(self):
     self.assertEquals(Project.load(self.manager.database, self.project1_id)['_id'], self.project1_id)
def edit_project(request, project_id=None):
    manager = get_database_manager(request.user)
    entity_list = get_all_entity_types(manager)
    entity_list = helper.remove_reporter(entity_list)
    project = Project.load(manager.database, project_id)
    questionnaire = FormModel.get(manager, project.qid)
    if request.method == 'GET':
        form = CreateProject(data=project, entity_list=entity_list)
        activity_report_questions = json.dumps(helper.get_activity_report_questions(manager), default=field_to_json)
        subject_report_questions = json.dumps(helper.get_subject_report_questions(manager), default=field_to_json)
        fields = questionnaire.fields
        if questionnaire.is_entity_type_reporter():
            fields = helper.hide_entity_question(questionnaire.fields)
        existing_questions = json.dumps(fields, default=field_to_json)

        return render_to_response('project/create_project.html',
                                  {'form': form, "activity_report_questions": repr(activity_report_questions),
                                   'subject_report_questions': repr(subject_report_questions),
                                   'preview_links': get_preview_and_instruction_links(),
                                   'existing_questions': repr(existing_questions),
                                   'questionnaire_code': questionnaire.form_code,
                                   'project': project, 'is_edit': 'true',
                                   'post_url': reverse(edit_project, args=[project_id])},
                                  context_instance=RequestContext(request))

    if request.method == 'POST':
        project_info = json.loads(request.POST['profile_form'])
        form = CreateProject(entity_list, data=project_info)
        if form.is_valid():
            detail = dict()
            for key, changed in enumerate(form.changed_data):
                if getattr(project, changed) != form.cleaned_data.get(changed):
                    detail.update({changed.capitalize(): form.cleaned_data.get(changed)})
            is_project_name_changed = project_info['name'] != questionnaire.name
            project.update(form.cleaned_data)
            try:
                old_fields = questionnaire.fields
                old_form_code = questionnaire.form_code
                old_field_codes = questionnaire.field_codes()
                questionnaire = update_questionnaire(questionnaire, request.POST, form.cleaned_data['entity_type'],
                                                     form.cleaned_data['name'], manager, form.cleaned_data['language'])
                changed_questions = get_changed_questions(old_fields, questionnaire.fields, subject=False)
                detail.update(changed_questions)
                project.state = request.POST['project_state']
                project.qid = questionnaire.save()

                deleted_question_codes = _get_deleted_question_codes(old_codes=old_field_codes,
                                                                     new_codes=questionnaire.field_codes())

                update_associated_submissions.delay(manager.database_name, old_form_code,
                                                    questionnaire.form_code,
                                                    deleted_question_codes)
                UserActivityLog().log(request, project=project.name, action=EDITED_PROJECT, detail=json.dumps(detail))
            except (QuestionCodeAlreadyExistsException, QuestionAlreadyExistsException,
                    EntityQuestionAlreadyExistsException) as ex:
                return HttpResponse(
                    json.dumps({'success': False, 'error_in_project_section': False, 'error_message': _(ex.message)}))
            except DataObjectAlreadyExists:
                return HttpResponse(json.dumps({'success': False, 'error_in_project_section': False,
                                                'error_message': 'Questionnaire with this code already exists'}))

            try:

                project.save(manager, process_post_update=is_project_name_changed)
            except DataObjectAlreadyExists as ex:
                message = _("%s with %s = %s already exists.") % (_(ex.data[2]), _(ex.data[0]), "'%s'" % project.name)
                return HttpResponse(
                    json.dumps({'success': False, 'error_message': message, 'error_in_project_section': True}))

            return HttpResponse(json.dumps({'success': True, 'project_id': project.id}))
Exemplo n.º 10
0
def _get_project_and_project_link(manager, project_id, reporter_id=None):
    project = Project.load(manager.database, project_id)
    questionnaire = FormModel.get(manager, project.qid)
    project_links = make_project_links(project, questionnaire.form_code, reporter_id)
    return project, project_links
Exemplo n.º 11
0
def undelete_project(request, project_id):
    manager = get_database_manager(request.user)
    project = Project.load(manager.database, project_id)
    helper.delete_project(manager, project, void=False)
    return HttpResponseRedirect(reverse(index))
def edit(request, project_id, survey_response_id, tab=0):
    manager = get_database_manager(request.user)
    project = Project.load(manager.database, project_id)
    questionnaire_form_model = FormModel.get(manager, project.qid)

    disable_link_class, hide_link_class = get_visibility_settings_for(
        request.user)
    survey_response = get_survey_response_by_id(manager, survey_response_id)
    back_link = reverse(index,
                        kwargs={
                            "project_id": project_id,
                            "questionnaire_code":
                            questionnaire_form_model.form_code,
                            "tab": tab
                        })
    form_ui_model = build_static_info_context(manager, survey_response)
    form_ui_model.update({"back_link": back_link})
    if request.method == 'GET':
        form_initial_values = construct_request_dict(survey_response,
                                                     questionnaire_form_model)
        survey_response_form = EditSubmissionForm(manager, project,
                                                  questionnaire_form_model,
                                                  form_initial_values)

        form_ui_model.update(
            get_form_context(questionnaire_form_model.form_code, project,
                             survey_response_form, manager, hide_link_class,
                             disable_link_class))
        form_ui_model.update({"redirect_url": ""})

        if not survey_response_form.is_valid():
            error_message = _("Please check your answers below for errors.")
            form_ui_model.update({'error_message': error_message})
        return render_to_response("project/web_questionnaire.html",
                                  form_ui_model,
                                  context_instance=RequestContext(request))

    if request.method == 'POST':
        original_survey_response = survey_response.copy()
        is_errored_before_edit = True if survey_response.errors != '' else False
        form_ui_model.update(
            {"redirect_url": request.POST.get("redirect_url")})
        form_ui_model.update(
            {"click_after_reload": request.POST.get("click_after_reload")})
        if request.POST.get("discard"):
            survey_response_form = EditSubmissionForm(
                manager, project, questionnaire_form_model,
                survey_response.values)

            form_ui_model.update(
                get_form_context(questionnaire_form_model.form_code, project,
                                 survey_response_form, manager,
                                 hide_link_class, disable_link_class))
            return render_to_response("project/web_questionnaire.html",
                                      form_ui_model,
                                      context_instance=RequestContext(request))
        else:
            survey_response_form = EditSubmissionForm(
                manager, project, questionnaire_form_model, request.POST)

        form_ui_model.update(
            get_form_context(questionnaire_form_model.form_code, project,
                             survey_response_form, manager, hide_link_class,
                             disable_link_class))
        if not survey_response_form.is_valid():
            error_message = _("Please check your answers below for errors.")
            form_ui_model.update({'error_message': error_message})
            return render_to_response("project/web_questionnaire.html",
                                      form_ui_model,
                                      context_instance=RequestContext(request))

        success_message = _("Your changes have been saved.")
        form_ui_model.update({'success_message': success_message})
        if len(survey_response_form.changed_data) or is_errored_before_edit:
            created_request = helper.create_request(survey_response_form,
                                                    request.user.username)

            additional_feed_dictionary = get_project_details_dict_for_feed(
                project)
            user_profile = NGOUserProfile.objects.get(user=request.user)
            feeds_dbm = get_feeds_database(request.user)
            if questionnaire_form_model.entity_type == ["reporter"]:
                owner_id = request.POST["eid"]
            else:
                owner_id = None
            response = WebPlayerV2(manager, feeds_dbm, user_profile.reporter_id) \
                .edit_survey_response(created_request, survey_response, owner_id,
                                      additional_feed_dictionary, websubmission_logger)
            mail_feed_errors(response, manager.database_name)
            if response.success:
                build_static_info_context(manager, survey_response,
                                          form_ui_model)
                ReportRouter().route(
                    get_organization(request).org_id, response)
                _update_static_info_block_status(form_ui_model,
                                                 is_errored_before_edit)
                log_edit_action(original_survey_response, survey_response,
                                request, project.name,
                                questionnaire_form_model)
                if request.POST.get("redirect_url"):
                    return HttpResponseRedirect(
                        request.POST.get("redirect_url"))
            else:
                del form_ui_model["success_message"]
                survey_response_form._errors = helper.errors_to_list(
                    response.errors, questionnaire_form_model.fields)
        return render_to_response("project/web_questionnaire.html",
                                  form_ui_model,
                                  context_instance=RequestContext(request))
Exemplo n.º 13
0
def _associate_data_senders_to_project(imported_entities, manager, project_id):
    project = Project.load(manager.database, project_id)
    project.data_senders.extend(
        [k for k, v in imported_entities.items() if v == REPORTER])
    project.save(manager)