Esempio n. 1
0
def _validate_post_data(dbm, request):
    form = ReporterRegistrationForm(request.POST)
    message = None
    success = False
    form_errors = []
    form_errors.extend(form.non_field_errors())
    if form.is_valid():
        form_errors = []
        form_data = {k: v for (k, v) in form.cleaned_data.items() if not is_empty(v)}
        try:
            entered_telephone_number = form_data.get("telephone_number")
            tel_number = _get_telephone_number(entered_telephone_number)
            if not helper.unique(dbm, tel_number):
                raise MultipleReportersForANumberException(entered_telephone_number)

            web_player = WebPlayer(dbm, SubmissionHandler(dbm))
            response = web_player.accept(
                Request(message=_get_data(form_data),
                        transportInfo=TransportInfo(transport='web', source='web', destination='mangrove')))
            message = get_success_msg_for_registration_using(response, "web")
            success = True
        except MangroveException as exception:
            form_errors.append(exception.message)
            success = False
    return form, form_errors, message, success
Esempio n. 2
0
def _validate_post_data(dbm, request):
    form = ReporterRegistrationForm(request.POST)
    message = None
    success = False
    form_errors = []
    form_errors.extend(form.non_field_errors())
    if form.is_valid():
        form_errors = []
        form_data = {
            k: v
            for (k, v) in form.cleaned_data.items() if not is_empty(v)
        }
        try:
            entered_telephone_number = form_data.get("telephone_number")
            tel_number = _get_telephone_number(entered_telephone_number)
            if not helper.unique(dbm, tel_number):
                raise MultipleReportersForANumberException(
                    entered_telephone_number)

            web_player = WebPlayer(dbm, SubmissionHandler(dbm))
            response = web_player.accept(
                Request(message=_get_data(form_data),
                        transportInfo=TransportInfo(transport='web',
                                                    source='web',
                                                    destination='mangrove')))
            message = get_success_msg_for_registration_using(response, "web")
            success = True
        except MangroveException as exception:
            form_errors.append(exception.message)
            success = False
    return form, form_errors, message, success
Esempio n. 3
0
    def get(self, request, *args, **kwargs):
        if request.GET.get('project_id'):
            form = ReporterRegistrationForm(initial={'project_id': request.GET.get('project_id')})
        else:
            form = ReporterRegistrationForm()
        save_button_text = self._get_save_button_text(request.GET.get('project_id'))

        return self.render_to_response(RequestContext(request, {
            'form': form,
            'current_language': translation.get_language(),
            'registration_link': '/entity/datasender/register/',
            'button_text': save_button_text
        }))
Esempio n. 4
0
 def get(self, request, *args, **kwargs):
     if request.GET.get('project_id'):
         form = ReporterRegistrationForm(
             initial={'project_id': request.GET.get('project_id')})
     else:
         form = ReporterRegistrationForm()
     return self.render_to_response({
         'form':
         form,
         'current_language':
         translation.get_language(),
         'registration_link':
         '/entity/datasender/register/',
     })
Esempio n. 5
0
    def post(self, request, *args, **kwargs):
        entity_links = {
            'registered_datasenders_link': reverse("all_datasenders")
        }
        dbm = get_database_manager(request.user)
        org_id = request.user.get_profile().org_id
        form = ReporterRegistrationForm(org_id=org_id, data=request.POST)
        try:
            reporter_id, message = process_create_data_sender_form(
                dbm, form, org_id)
        except DataObjectAlreadyExists as e:
            message = _(
                "Data Sender with Unique Identification Number (ID) = %s already exists."
            ) % e.data[1]
        if len(form.errors) == 0 and form.requires_web_access(
        ) and reporter_id:
            email_id = form.cleaned_data['email']
            create_single_web_user(org_id=org_id,
                                   email_address=email_id,
                                   reporter_id=reporter_id,
                                   language_code=request.LANGUAGE_CODE)

        if message is not None and reporter_id:
            if form.cleaned_data['project_id'] != "":
                questionnaire = Project.get(dbm,
                                            form.cleaned_data['project_id'])
                questionnaire.associate_data_sender_to_project(
                    dbm, reporter_id)
                questionnaire = questionnaire.name
            else:
                questionnaire = ""
            if not len(form.errors):
                UserActivityLog().log(request,
                                      action=REGISTERED_DATA_SENDER,
                                      detail=json.dumps(
                                          dict({"Unique ID": reporter_id})),
                                      project=questionnaire)
            form = ReporterRegistrationForm(
                initial={'project_id': form.cleaned_data['project_id']})
        return render_to_response(
            'datasender_form.html', {
                'form': form,
                'message': message,
                'success': reporter_id is not None,
                'project_inks': entity_links,
                'current_language': translation.get_language(),
                'registration_link': '/entity/datasender/register/',
            },
            context_instance=RequestContext(request))
Esempio n. 6
0
 def get(self, request, reporter_id, *args, **kwargs):
     create_data_sender = False
     manager = get_database_manager(request.user)
     reporter_entity = ReporterEntity(
         get_by_short_code(manager, reporter_id, [REPORTER]))
     entity_links = {
         'registered_datasenders_link': reverse("all_datasenders")
     }
     datasender = {'short_code': reporter_id}
     get_datasender_user_detail(datasender, request.user)
     email = datasender.get(
         'email') if datasender.get('email') != '--' else False
     name = reporter_entity.name
     phone_number = reporter_entity.mobile_number
     location = reporter_entity.location
     geo_code = reporter_entity.geo_code
     form = ReporterRegistrationForm(
         initial={
             'name': name,
             'telephone_number': phone_number,
             'location': location,
             'geo_code': geo_code
         })
     return self.render_to_response({
         'reporter_id': reporter_id,
         'form': form,
         'project_links': entity_links,
         'email': email,
         'create_data_sender': create_data_sender
     })
Esempio n. 7
0
    def post(self, request, *args, **kwargs):
        entity_links = {'registered_datasenders_link': reverse("all_datasenders")}
        dbm = get_database_manager(request.user)
        org_id = request.user.get_profile().org_id
        project_id = request.POST.get('project_id')
        form = ReporterRegistrationForm(org_id=org_id, data=request.POST)
        reporter_id = None
        try:
            reporter_id, message = process_create_data_sender_form(dbm, form, org_id)
            update_datasender_on_open_submissions.delay(dbm.database_name, reporter_id)

        except DataObjectAlreadyExists as e:
            if project_id:
                message = _("Data Sender with ID Number '%s' already exists.") % e.data[1]
            else:
                message = _("Contact with ID Number '%s' already exists.") % e.data[1]

        self._give_web_access_to_successful_datasender_registration(form, org_id, reporter_id, request)

        if message is not None and reporter_id:
            questionnaire_id = form.cleaned_data['project_id']

            if questionnaire_id != "":
                questionnaire_name = self._associate_datasender_to_questionnaire(dbm, reporter_id, questionnaire_id)
            else:
                questionnaire_name = ""

            self._update_activity_log(form, questionnaire_name, reporter_id, request)
            form = ReporterRegistrationForm(initial={'project_id': questionnaire_id})

        save_button_text = self._get_save_button_text(project_id)
        message_text = self._get_message_text(message, project_id, reporter_id)

        return render_to_response('datasender_form.html',
                                  {
                                      'form': form,
                                      'message': message_text,
                                      'success': (reporter_id is not None),
                                      'project_inks': entity_links,
                                      'current_language': translation.get_language(),
                                      'registration_link': '/entity/datasender/register/',
                                      'button_text': save_button_text
                                  },
                                  context_instance=RequestContext(request))
Esempio n. 8
0
def create_datasender(request):
    if request.method == 'GET':
        form = ReporterRegistrationForm()
        return render_to_response('entity/create_datasender.html',
                                  {'form': form},
                                  context_instance=RequestContext(request))
    if request.method == 'POST':
        dbm = get_database_manager(request)
        form, form_errors, message, success = _validate_post_data(dbm, request)
        if success:
            form = ReporterRegistrationForm()
        response = render_to_response('datasender_form.html', {
            'form': form,
            'message': message,
            'form_errors': form_errors,
            'success': success
        },
                                      context_instance=RequestContext(request))
        return response
Esempio n. 9
0
def create_data_sender(request):
    create_data_sender = True
    entity_links = {'back_link': reverse(all_datasenders)}

    if request.method == 'GET':
        form = ReporterRegistrationForm()
        return render_to_response('entity/create_or_edit_data_sender.html', {
            'form': form,
            'create_data_sender': create_data_sender,
            'entity_links': entity_links
        },
                                  context_instance=RequestContext(request))
    if request.method == 'POST':
        dbm = get_database_manager(request.user)
        org_id = request.user.get_profile().org_id
        form = ReporterRegistrationForm(org_id=org_id, data=request.POST)
        success = False
        try:
            reporter_id, message = process_create_data_sender_form(
                dbm, form, org_id)
            success = True
        except DataObjectAlreadyExists as e:
            message = _(
                "Data Sender with Unique Identification Number (ID) = %s already exists."
            ) % e.data[1]
        if len(form.errors) == 0 and form.requires_web_access() and success:
            email_id = request.POST['email']
            create_single_web_user(org_id=org_id,
                                   email_address=email_id,
                                   reporter_id=reporter_id,
                                   language_code=request.LANGUAGE_CODE)

        if message is not None and success:
            if form.cleaned_data['project_id'] != "":
                project = Project.load(dbm.database,
                                       form.cleaned_data['project_id'])
                project = project.name
            else:
                project = ""
            if not len(form.errors):
                UserActivityLog().log(request,
                                      action=REGISTERED_DATA_SENDER,
                                      detail=json.dumps(
                                          dict({"Unique ID": reporter_id})),
                                      project=project)
            form = ReporterRegistrationForm(
                initial={'project_id': form.cleaned_data['project_id']})
        return render_to_response('datasender_form.html', {
            'form': form,
            'message': message,
            'success': success,
            'entity_links': entity_links
        },
                                  context_instance=RequestContext(request))
Esempio n. 10
0
 def test_should_remove_disabled_attribute_if_short_code_is_given(self):
     form = ReporterRegistrationForm()
     self.assertFalse(form.is_valid())
     self.assertTrue(
         "disabled" in form.fields.get("short_code").widget.attrs)
     form = ReporterRegistrationForm(data={'short_code': "rep004"})
     self.assertFalse(form.is_valid())
     self.assertFalse(
         "disabled" in form.fields.get("short_code").widget.attrs)
 def get(self, request, *args, **kwargs):
     create_data_sender = True
     entity_links = {
         'registered_datasenders_link': reverse("all_datasenders")
     }
     form = ReporterRegistrationForm()
     return self.render_to_response({
         'form':
         form,
         'create_data_sender':
         create_data_sender,
         'project_links':
         entity_links,
         'current_language':
         translation.get_language(),
         'create_datasender_link':
         "/entity/datasender/create",
     })
Esempio n. 12
0
def datasenders_wizard(request, project_id=None):
    if request.method == 'GET':
        manager = get_database_manager(request)
        reg_form = get_form_model_by_code(manager, REGISTRATION_FORM_CODE)
        previous_link = reverse(questionnaire_wizard, args=[project_id])
        project = models.get_project(project_id, manager)
        import_reporter_form = ReporterRegistrationForm()
        _format_field_description_for_data_senders(reg_form.fields)
        cleaned_up_fields = _get_questions_for_datasenders_registration_for_wizard(
            reg_form.fields)
        return render_to_response(
            'project/datasenders_wizard.html', {
                'fields': cleaned_up_fields,
                "previous": previous_link,
                'form': import_reporter_form,
                'post_url': reverse(import_subjects_from_project_wizard),
                'project': project,
                'step': 'datasenders'
            },
            context_instance=RequestContext(request))

    if request.method == 'POST':
        return HttpResponseRedirect(reverse(finish, args=[project_id]))
Esempio n. 13
0
def create_data_sender_and_web_user(request, project_id=None):
    manager = get_database_manager(request.user)
    project, project_links = _get_project_and_project_link(manager, project_id)
    in_trial_mode = _in_trial_mode(request)

    if request.method == 'GET':
        form = ReporterRegistrationForm(initial={'project_id': project_id})
        return render_to_response('project/register_datasender.html', {
            'project': project,
            'project_links': project_links,
            'is_quota_reached': is_quota_reached(request),
            'form': form,
            'in_trial_mode': in_trial_mode,
            'current_language': translation.get_language()
        }, context_instance=RequestContext(request))

    if request.method == 'POST':
        org_id = request.user.get_profile().org_id
        form = ReporterRegistrationForm(org_id=org_id, data=request.POST)
        try:
            reporter_id, message = process_create_data_sender_form(manager, form, org_id)
        except DataObjectAlreadyExists as e:
            message = _("Data Sender with Unique Identification Number (ID) = %s already exists.") % e.data[1]

        if not len(form.errors) and reporter_id:
            project.associate_data_sender_to_project(manager, reporter_id)
            if form.requires_web_access():
                email_id = request.POST['email']
                create_single_web_user(org_id=org_id, email_address=email_id, reporter_id=reporter_id,
                                       language_code=request.LANGUAGE_CODE)
            UserActivityLog().log(request, action=REGISTERED_DATA_SENDER,
                                  detail=json.dumps(dict({"Unique ID": reporter_id})), project=project.name)
        if message is not None and reporter_id:
            form = ReporterRegistrationForm(initial={'project_id': form.cleaned_data['project_id']})
        context = {'form': form, 'message': message, 'in_trial_mode': in_trial_mode, 'success': reporter_id is not None}
        return render_to_response('datasender_form.html',
                                  context,
                                  context_instance=RequestContext(request))
Esempio n. 14
0
    def post(self, request, reporter_id, *args, **kwargs):
        reporter_id = reporter_id.lower()
        manager = get_database_manager(request.user)
        reporter_entity = ReporterEntity(
            get_by_short_code(manager, reporter_id, [REPORTER]))
        entity_links = {
            'registered_datasenders_link': reverse("all_datasenders")
        }
        datasender = {'short_code': reporter_id}
        get_datasender_user_detail(datasender, request.user)
        email = datasender.get(
            'email') if datasender.get('email') != '--' else False
        org_id = request.user.get_profile().org_id
        form = ReporterRegistrationForm(org_id=org_id, data=request.POST)
        message = None
        if form.is_valid():
            try:
                org_id = request.user.get_profile().org_id
                current_telephone_number = reporter_entity.mobile_number
                current_name = reporter_entity.name
                organization = Organization.objects.get(org_id=org_id)
                web_player = WebPlayer(
                    manager,
                    LocationBridge(location_tree=get_location_tree(),
                                   get_loc_hierarchy=get_location_hierarchy))
                response = web_player.accept(
                    Request(message=_get_data(form.cleaned_data,
                                              organization.country_name(),
                                              reporter_id),
                            transportInfo=TransportInfo(
                                transport='web',
                                source='web',
                                destination='mangrove'),
                            is_update=True))
                if response.success:
                    if organization.in_trial_mode:
                        update_data_sender_from_trial_organization(
                            current_telephone_number,
                            form.cleaned_data["telephone_number"], org_id)
                    if email and current_name != form.cleaned_data["name"]:
                        update_user_name_if_exists(email,
                                                   form.cleaned_data["name"])
                    message = _("Your changes have been saved.")

                    detail_dict = {"Unique ID": reporter_id}
                    current_lang = get_language()
                    activate("en")
                    field_mapping = dict(mobile_number="telephone_number")
                    for field in [
                            "geo_code", "location", "mobile_number", "name"
                    ]:
                        if getattr(reporter_entity,
                                   field) != form.cleaned_data.get(
                                       field_mapping.get(field, field)):
                            label = u"%s" % form.fields[field_mapping.get(
                                field, field)].label
                            detail_dict.update({
                                label:
                                form.cleaned_data.get(
                                    field_mapping.get(field, field))
                            })
                    activate(current_lang)
                    if len(detail_dict) > 1:
                        detail_as_string = json.dumps(detail_dict)
                        UserActivityLog().log(request,
                                              action=EDITED_DATA_SENDER,
                                              detail=detail_as_string)
                else:
                    form.update_errors(response.errors)

            except MangroveException as exception:
                message = exception.message

        return render_to_response('edit_datasender_form.html', {
            'form': form,
            'message': message,
            'reporter_id': reporter_id,
            'email': email,
            'project_links': entity_links
        },
                                  context_instance=RequestContext(request))
def edit_data_sender(request, project_id, reporter_id):
    manager = get_database_manager(request.user)
    reporter_entity = ReporterEntity(
        get_by_short_code(manager, reporter_id, [REPORTER]))
    project, links = _get_project_and_project_link(manager, project_id,
                                                   reporter_id)
    user_profile = get_user_profile_by_reporter_id(reporter_id, request.user)
    email = user_profile.user.email if user_profile else None

    if request.method == 'GET':
        location = reporter_entity.location
        geo_code = reporter_entity.geo_code
        form = ReporterRegistrationForm(
            initial={
                'project_id': project_id,
                'name': reporter_entity.name,
                'telephone_number': reporter_entity.mobile_number,
                'location': location,
                'geo_code': geo_code
            })
        return render_to_response('project/edit_datasender.html', {
            'project': project,
            'reporter_id': reporter_id,
            'form': form,
            'project_links': links,
            'in_trial_mode': _in_trial_mode(request),
            'email': email
        },
                                  context_instance=RequestContext(request))

    if request.method == 'POST':
        org_id = request.user.get_profile().org_id
        form = ReporterRegistrationForm(org_id=org_id, data=request.POST)

        message = None
        if form.is_valid():
            try:
                organization = Organization.objects.get(org_id=org_id)
                current_telephone_number = reporter_entity.mobile_number
                web_player = WebPlayer(
                    manager,
                    LocationBridge(location_tree=get_location_tree(),
                                   get_loc_hierarchy=get_location_hierarchy))
                response = web_player.accept(
                    Request(message=_get_data(form.cleaned_data,
                                              organization.country_name(),
                                              reporter_id),
                            transportInfo=TransportInfo(
                                transport='web',
                                source='web',
                                destination='mangrove'),
                            is_update=True))
                if response.success:
                    if organization.in_trial_mode:
                        update_data_sender_from_trial_organization(
                            current_telephone_number,
                            form.cleaned_data["telephone_number"], org_id)
                    message = _("Your changes have been saved.")

                    detail_dict = {"Unique ID": reporter_id}
                    current_lang = get_language()
                    activate("en")
                    field_mapping = dict(mobile_number="telephone_number")
                    for field in [
                            "geo_code", "location", "mobile_number", "name"
                    ]:
                        if getattr(reporter_entity,
                                   field) != form.cleaned_data.get(
                                       field_mapping.get(field, field)):
                            label = u"%s" % form.fields[field_mapping.get(
                                field, field)].label
                            detail_dict.update({
                                label:
                                form.cleaned_data.get(
                                    field_mapping.get(field, field))
                            })
                    activate(current_lang)
                    if len(detail_dict) > 1:
                        detail_as_string = json.dumps(detail_dict)
                        UserActivityLog().log(request,
                                              action=EDITED_DATA_SENDER,
                                              detail=detail_as_string,
                                              project=project.name)
                else:
                    form.update_errors(response.errors)
            except MangroveException as exception:
                message = exception.message

        return render_to_response('edit_datasender_form.html', {
            'project': project,
            'form': form,
            'reporter_id': reporter_id,
            'message': message,
            'project_links': links,
            'in_trial_mode': _in_trial_mode(request),
            'email': email
        },
                                  context_instance=RequestContext(request))