コード例 #1
0
    def _post_save(self, response, new_submission=False):
        mail_feed_errors(response, self.manager.database_name)
        if response.errors:
            logger.error("Error in submission : \n%s" %
                         get_errors(response.errors))
            messages.error(self.request,
                           ugettext('Submission failed %s' %
                                    get_errors(response.errors)),
                           extra_tags='error')
            return HttpResponseBadRequest()

        self.organization.increment_message_count_for(incoming_web_count=1)
        content = json.dumps({
            'submission_uuid':
            response.survey_response_id,
            'version':
            response.version,
            'created':
            py_datetime_to_js_datestring(response.created)
        })
        success_response = HttpResponse(content,
                                        status=201,
                                        content_type='application/json')
        success_response['submission_id'] = response.survey_response_id

        if new_submission:
            messages.success(self.request,
                             ugettext('Successfully submitted'),
                             extra_tags='success')

        check_quotas_and_update_users(self.organization)
        return success_response
コード例 #2
0
ファイル: views.py プロジェクト: venumurthy/datawinners
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
コード例 #3
0
 def player_response(self, created_request, reporter_id):
     user_profile = NGOUserProfile.objects.get(user=self.request.user)
     additional_feed_dictionary = get_feed_dictionary(self.questionnaire)
     if not reporter_id:
         reporter_id = user_profile.reporter_id
     web_player = WebPlayerV2(self.manager, self.feeds_dbm,
                              user_profile.reporter_id)
     response = web_player.add_survey_response(created_request, reporter_id,
                                               additional_feed_dictionary,
                                               websubmission_logger)
     mail_feed_errors(response, self.manager.database_name)
     if response.success and not created_request.is_update:
         organization = Organization.objects.get(org_id=user_profile.org_id)
         organization.increment_message_count_for(incoming_web_count=1)
         check_quotas_and_update_users(organization)
     return response
コード例 #4
0
 def test_should_send_all_email_type_when_all_limit_are_about_to_reached(
         self):
     organization = self.incoming_request.get('organization')
     with patch.object(Organization, "get_total_submission_count"
                       ) as patch_get_total_submission_count:
         with patch.object(Organization, "get_total_incoming_message_count"
                           ) as patch_get_total_incoming_message_count:
             patch_get_total_submission_count.return_value = NEAR_SUBMISSION_LIMIT_TRIGGER
             patch_get_total_incoming_message_count.return_value = NEAR_SMS_LIMIT_TRIGGER
             check_quotas_and_update_users(organization=organization,
                                           sms_channel=True)
             expected_subjects = [
                 u'DataWinners | 50 SMS Submission Limit Almost Reached: Upgrade to Continue Collecting Data via SMS!',
                 u'Your DataWinners Submission Limit is Approaching!'
             ]
             msgs = [mail.outbox.pop() for i in range(len(mail.outbox))]
             for msg in msgs:
                 self.assertIn(msg.subject, expected_subjects)
コード例 #5
0
 def test_should_send_mail_to_when_sms_limit_is_about_to_reached(self):
     token = Token.objects.get_or_create(user=self.user)[0].key
     organization = self.incoming_request.get('organization')
     with patch.object(Organization, "get_total_incoming_message_count"
                       ) as patch_get_total_incoming_message_count:
         patch_get_total_incoming_message_count.return_value = NEAR_SMS_LIMIT_TRIGGER
         check_quotas_and_update_users(organization=organization,
                                       sms_channel=True)
         site = Site.objects.get_current()
         email = mail.outbox.pop()
         self.assertEqual(['*****@*****.**'], email.to)
         ctx = {
             'username': '******',
             'organization': organization,
             'site': site,
             'token': token
         }
         self.assertEqual(
             render_to_string(
                 'email/basicaccount/about_to_reach_sms_limit_en.html',
                 ctx), email.body)
コード例 #6
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
コード例 #7
0
 def _increment_web_counter(self):
     self.organization.increment_message_count_for(incoming_web_count=1)
     check_quotas_and_update_users(self.organization)