Exemplo n.º 1
0
def create_request(questionnaire_form, username, is_update=None):
    return Request(message=questionnaire_form.cleaned_data,
        transportInfo=
        TransportInfo(transport="web",
            source=username,
            destination=""
        ), is_update=is_update)
Exemplo n.º 2
0
def process_create_data_sender_form(dbm, form, org_id):
    message = None
    data_sender_id = None

    if form.is_valid():
        try:
            organization = Organization.objects.get(org_id=org_id)
            if organization.in_trial_mode:
                add_data_sender_to_trial_organization(form.cleaned_data["telephone_number"], org_id)
            web_player = WebPlayer(dbm, LocationBridge(location_tree=get_location_tree(), get_loc_hierarchy=get_location_hierarchy))
            reporter_id = form.cleaned_data["short_code"] if form.cleaned_data != "" else None
            request = Request(message=_get_data(form.cleaned_data, organization.country_name(), reporter_id),
                transportInfo=TransportInfo(transport='web', source='web', destination='mangrove'))

            response = web_player.accept(request, logger=websubmission_logger)
            if response.success:
                data_sender_id = response.short_code
                message = get_success_msg_for_registration_using(response, "web")
            else:
                form.update_errors(response.errors)

        except MangroveException as exception:
            message = exception.message

    return data_sender_id,message
Exemplo n.º 3
0
def delete_entity_instance(manager, all_ids, entity_type, transport_info):
    web_player = WebPlayer(manager)
    for entity_id in all_ids:
        message = {ENTITY_TYPE_FIELD_CODE: entity_type,
                   SHORT_CODE: entity_id,
                   'form_code': ENTITY_DELETION_FORM_CODE}
        mangrove_request = Request(message, transport_info)
        web_player.accept(mangrove_request)
def send_sms(source, destination, text):
    print text
    transport_info = TransportInfo(transport="sms",
                                   source=source,
                                   destination=destination)
    response = sms_player.accept(
        Request(message=text, transportInfo=transport_info))
    print response
    return response
Exemplo n.º 5
0
 def _edit_contact(self, form, manager, organization, reporter_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))
     return response
Exemplo n.º 6
0
def process_create_data_sender_form(dbm, form, org_id):
    message = None
    data_sender_id = None

    if form.is_valid():
        try:
            organization = Organization.objects.get(org_id=org_id)
            if organization.in_trial_mode:
                _add_data_sender_to_trial_organization(
                    form.cleaned_data["telephone_number"], org_id)
            web_player = WebPlayer(
                dbm,
                LocationBridge(location_tree=get_location_tree(),
                               get_loc_hierarchy=get_location_hierarchy))
            reporter_id = form.cleaned_data["short_code"].lower(
            ) if form.cleaned_data != "" else None
            request = Request(message=_get_data(form.cleaned_data,
                                                organization.country_name(),
                                                reporter_id),
                              transportInfo=TransportInfo(
                                  transport='web',
                                  source='web',
                                  destination='mangrove'))

            response = web_player.accept(request, logger=websubmission_logger)
            if response.success:
                data_sender_id = response.short_code
                message = get_success_msg_for_ds_registration_using(
                    response, "web")
            else:
                form.update_errors(response.errors)
        except IntegrityError as e:
            form.update_errors({
                MOBILE_NUMBER_FIELD_CODE:
                _(u'Sorry, the telephone number %s has already been registered.'
                  ) % form.cleaned_data["telephone_number"]
            })
        except DataObjectAlreadyExists as e:
            message = _("%s with %s = %s already exists.") % (
                e.data[2],
                e.data[0],
                e.data[1],
            )
        except MangroveException as exception:
            message = exception.message

    return data_sender_id, message
Exemplo n.º 7
0
 def __init__(self, request):
     self.request = request
     self.request_user = request.user
     self.manager = get_database_manager(self.request_user)
     self.player = XFormPlayerV2(self.manager, get_feeds_database(self.request_user))
     self.xml_submission_file = request.POST['form_data']
     self.retain_files = request.POST['retain_files'].split(',') if request.POST.get('retain_files') else None
     self.add_preview_files()
     self.user_profile = NGOUserProfile.objects.get(user=self.request_user)
     self.mangrove_request = Request(message=self.xml_submission_file, media=request.FILES,
         retain_files=self.retain_files,
         transportInfo=
         TransportInfo(transport=WEB,
             source=self.request_user.email,
             destination=''
         ))
     self.organization = Organization.objects.get(org_id=self.user_profile.org_id)
Exemplo n.º 8
0
def submit(request):
    dbm = get_database_manager(request.user)
    post = json.loads(request.POST['data'])
    success = True
    try:
        web_player = WebPlayer(
            dbm,
            LocationBridge(location_tree=get_location_tree(),
                           get_loc_hierarchy=get_location_hierarchy))
        message = post['message']
        message[LOCATION_TYPE_FIELD_CODE] = get_country_appended_location(
            message.get(LOCATION_TYPE_FIELD_CODE),
            get_organization_country(request))
        request = Request(message=message,
                          transportInfo=TransportInfo(
                              transport=post.get('transport'),
                              source=post.get('source'),
                              destination=post.get('destination')))
        response = web_player.accept(request)
        if response.success:
            message = get_success_msg_for_registration_using(response, "web")
        else:
            message = get_submission_error_message_for(response.errors)
        entity_id = response.datarecord_id
    except DataObjectAlreadyExists as exception:
        message = _(
            "Entity with Unique Identification Number (ID) = %s already exists."
        ) % exception.data[1]
        success, entity_id = False, None
    except MangroveException as exception:
        message = get_exception_message_for(exception=exception, channel="web")
        message = _("Please add subject type and then add a subject"
                    ) if message == "t should be present" else message
        success = False
        entity_id = None
    return HttpResponse(
        json.dumps({
            'success': success,
            'message': message,
            'entity_id': entity_id
        }))
Exemplo n.º 9
0
def submission(request):
    if request.method != 'POST':
        response = HttpResponse(status=204)
        response['Location'] = request.build_absolute_uri()
        return response

    request_user = request.user
    submission_file = request.FILES.get("xml_submission_file").read()

    if not __authorized_to_make_submission_on_requested_form(
            request_user, submission_file):
        response = HttpResponse(status=403)
        return response

    manager = get_database_manager(request_user)
    player = XFormPlayer(manager)
    try:
        mangrove_request = Request(message=submission_file,
                                   transportInfo=TransportInfo(
                                       transport=SMART_PHONE,
                                       source=request_user.email,
                                       destination=''))

        response = player.accept(mangrove_request, logger=sp_submission_logger)

        if response.errors:
            logger.error("Error in submission : \n%s" %
                         get_errors(response.errors))
            return HttpResponseBadRequest()

    except Exception as e:
        logger.exception("Exception in submission : \n%s" % e)
        return HttpResponseBadRequest()

    response = HttpResponse(status=201)
    response['Location'] = request.build_absolute_uri(request.path)
    return response
Exemplo n.º 10
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))
Exemplo n.º 12
0
 def _register_entity(self, text):
     self.player.accept(Request(text, self.transport))
Exemplo n.º 13
0
 def submit_data(self, post_data):
     WebPlayer(self.manager).accept(
         Request(message=post_data, transportInfo=self.transport))