Beispiel #1
0
 def setUp(self):
     super(TestXformPlayerIT, self).setUp()
     self.org_id = 'SLX364903'
     self.player = XFormPlayerV2(self.manager)
     message = self.xform_message
     self.transport = TransportInfo(transport="xform",
                                    source="*****@*****.**",
                                    destination="5678")
     self.request = Request(message=message, transportInfo=self.transport)
     TestData(self.manager)
     register_datasender(self.manager)
Beispiel #2
0
def submission(request):
    if request.method != 'POST':
        response = HttpResponse(status=204)
        response['Location'] = request.build_absolute_uri()
        return response

    send_to_carbon(create_path('submissions.smartphone'), 1)
    request_user = request.user
    submission_file = request.FILES.get("xml_submission_file").read()
    manager = get_database_manager(request_user)

    if not __authorized_to_make_submission_on_requested_form(request_user, submission_file, manager) \
            or is_quota_reached(request):
        response = HttpResponse(status=403)
        return response

    player = XFormPlayerV2(manager, get_feeds_database(request_user))
    try:
        user_profile = NGOUserProfile.objects.get(user=request_user)
        mangrove_request = Request(
            message=submission_file,
            transportInfo=TransportInfo(transport=SMART_PHONE,
                                        source=request_user.email,
                                        destination=''),
            media=request.FILES if len(request.FILES) > 1 else [])

        response = player.add_survey_response(mangrove_request,
                                              user_profile.reporter_id,
                                              logger=sp_submission_logger)
        mail_feed_errors(response, manager.database_name)
        if response.errors:
            logger.error("Error in submission : \n%s" %
                         get_errors(response.errors))
            return HttpResponseBadRequest()

    except MediaAttachmentNotFoundException as me:
        _send_media_error_mail(request, request_user, user_profile, me.message)
        return HttpResponseBadRequest()

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

    organization = Organization.objects.get(org_id=user_profile.org_id)
    organization.increment_message_count_for(incoming_sp_count=1)

    check_quotas_and_update_users(organization)
    response = HttpResponse(status=201)
    response['Location'] = request.build_absolute_uri(request.path)
    return response
Beispiel #3
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)
Beispiel #4
0
def submission(request):
    if request.method != 'POST':
        response = HttpResponse(status=204)
        response['Location'] = request.build_absolute_uri()
        return response

    send_to_carbon(create_path('submissions.smartphone'), 1)
    request_user = request.user
    submission_file = request.FILES.get("xml_submission_file").read()
    manager = get_database_manager(request_user)

    if not __authorized_to_make_submission_on_requested_form(request_user, submission_file, manager) \
            or is_quota_reached(request):
        response = HttpResponse(status=403)
        return response

    player = XFormPlayerV2(manager, get_feeds_database(request_user))
    try:
        user_profile = NGOUserProfile.objects.get(user=request_user)
        mangrove_request = Request(
            message=submission_file,
            transportInfo=TransportInfo(transport=SMART_PHONE,
                                        source=request_user.email,
                                        destination=''),
            media=request.FILES if len(request.FILES) > 1 else [])
        form_code, values = XFormParser(manager).parse(
            mangrove_request.message)
        form_model = get_form_model_by_code(manager, form_code)

        if isinstance(
                form_model,
                EntityFormModel) and form_model.is_entity_registration_form:
            location_tree = LocationBridge(
                get_location_tree(), get_loc_hierarchy=get_location_hierarchy)
            response = player.add_subject(form_model, values, location_tree)
        else:
            response = player.add_survey_response(mangrove_request,
                                                  user_profile.reporter_id,
                                                  logger=sp_submission_logger)

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

    except MediaAttachmentNotFoundException as me:
        _send_media_error_mail(request, request_user, user_profile, me.message)
        return HttpResponseBadRequest()

    except DataObjectAlreadyExists as doe:
        send_email_for_duplicate_unique_id_registration(
            request, doe.data[2], doe.data[1])
        return HttpResponseBadRequest()

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

    organization = Organization.objects.get(org_id=user_profile.org_id)
    organization.increment_message_count_for(incoming_sp_count=1)

    check_quotas_and_update_users(organization)
    response = HttpResponse(status=201)
    response['Location'] = request.build_absolute_uri(request.path)
    return response
Beispiel #5
0
def attachment_post(request, survey_response_id):
    player = XFormPlayerV2(get_database_manager(request.user))
    player.add_new_attachments(request.FILES, survey_response_id)
    return HttpResponse(status=201)