Exemple #1
0
def create_subject(request):
    db_manager = get_database_manager(request)
    entity_types = get_all_entity_types(db_manager)
    project_helper.remove_reporter(entity_types)
    return render_to_response("entity/create_subject.html", {
        "post_url": reverse(submit),
        "entity_types": entity_types
    },
                              context_instance=RequestContext(request))
Exemple #2
0
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.entity_defaults_to_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)})
            project.update(form.cleaned_data)
            try:
                old_fields = questionnaire.fields
                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()
                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)
            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}))
def create_project(request):
    manager = get_database_manager(request.user)
    entity_list = get_all_entity_types(manager)
    entity_list = helper.remove_reporter(entity_list)
    all_projects_name = get_all_project_names(get_database_manager(request.user))
    name = generate_project_name(all_projects_name)
    project_summary = dict(name=name)
    ngo_admin = NGOUserProfile.objects.get(user=request.user)

    if request.method == 'GET':
        form = CreateProject(entity_list=entity_list, initial={'name': name})
        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)
        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(activity_report_questions), 'project': project_summary,
                                   'questionnaire_code': helper.generate_questionnaire_code(manager),
                                   'is_edit': 'false',
                                   'post_url': reverse(create_project)}, 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():
            project = Project(name=form.cleaned_data['name'], goals=form.cleaned_data['goals'],
                              project_type='survey', entity_type=form.cleaned_data['entity_type'],
                              activity_report=form.cleaned_data['activity_report'],
                              state=request.POST['project_state'], devices=[u'sms', u'web', u'smartPhone'],
                              language=form.cleaned_data['language'])

            if ngo_admin.reporter_id is not None:
                project.data_senders.append(ngo_admin.reporter_id)

            try:
                questionnaire = create_questionnaire(post=request.POST, manager=manager,
                                                     entity_type=form.cleaned_data['entity_type'],
                                                     name=form.cleaned_data['name'],
                                                     language=form.cleaned_data['language'])
            except (QuestionCodeAlreadyExistsException, QuestionAlreadyExistsException,
                    EntityQuestionAlreadyExistsException) as ex:
                return HttpResponse(
                    json.dumps({'success': False, 'error_message': _(ex.message), 'error_in_project_section': False}))

            try:
                project.qid = questionnaire.save()
            except DataObjectAlreadyExists:
                return HttpResponse(json.dumps(
                    {'success': False, 'error_message': "Questionnaire with this code already exists",
                     'error_in_project_section': False}))

            try:
                project.save(manager)
                UserActivityLog().log(request, action=CREATED_PROJECT, project=project.name, detail=project.name)
            except DataObjectAlreadyExists as ex:
                questionnaire.delete()
                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}))
Exemple #4
0
def create_subject(request):
    db_manager = get_database_manager(request)
    entity_types = get_all_entity_types(db_manager)
    project_helper.remove_reporter(entity_types)
    return render_to_response("entity/create_subject.html", {"post_url": reverse(submit), "entity_types": entity_types},
                              context_instance=RequestContext(request))