Example #1
0
def check_service_message_limit(key_type, service):
    if key_type != KEY_TYPE_TEST:
        cache_key = redis.daily_limit_cache_key(service.id)
        service_stats = redis_store.get(cache_key)
        if not service_stats:
            service_stats = services_dao.fetch_todays_total_message_count(service.id)
            redis_store.set(cache_key, service_stats, ex=3600)
        if int(service_stats) >= service.message_limit:
            raise TooManyRequestsError(service.message_limit)
def check_service_over_daily_message_limit(key_type, service):
    if key_type != KEY_TYPE_TEST and current_app.config['REDIS_ENABLED']:
        cache_key = daily_limit_cache_key(service.id)
        service_stats = redis_store.get(cache_key)
        if not service_stats:
            service_stats = services_dao.fetch_todays_total_message_count(service.id)
            redis_store.set(cache_key, service_stats, ex=3600)
        if int(service_stats) >= service.message_limit:
            current_app.logger.info(
                "service {} has been rate limited for daily use sent {} limit {}".format(
                    service.id, int(service_stats), service.message_limit)
            )
            raise TooManyRequestsError(service.message_limit)
Example #3
0
def __sending_limits_for_job_exceeded(service, job, job_id):
    total_sent = fetch_todays_total_message_count(service.id)

    if total_sent + job.notification_count > service.message_limit:
        job.job_status = 'sending limits exceeded'
        job.processing_finished = datetime.utcnow()
        dao_update_job(job)
        current_app.logger.info(
            "Job {} size {} error. Sending limits {} exceeded".format(
                job_id, job.notification_count, service.message_limit)
        )
        return True
    return False
Example #4
0
def __sending_limits_for_job_exceeded(service, job, job_id):
    total_sent = fetch_todays_total_message_count(service.id)

    if total_sent + job.notification_count > service.message_limit:
        job.job_status = "sending limits exceeded"
        job.processing_finished = datetime.utcnow()
        dao_update_job(job)
        current_app.logger.info(
            "Job {} size {} error. Sending limits {} exceeded".format(
                job_id, job.notification_count, service.message_limit
            )
        )
        return True
    return False
Example #5
0
def post_bulk():
    try:
        request_json = request.get_json()
    except werkzeug.exceptions.BadRequest as e:
        raise BadRequestError(message=f"Error decoding arguments: {e.description}", status_code=400)

    max_rows = current_app.config["CSV_MAX_ROWS"]
    form = validate(request_json, post_bulk_request(max_rows))

    if len([source for source in [form.get("rows"), form.get("csv")] if source]) != 1:
        raise BadRequestError(message="You should specify either rows or csv", status_code=400)
    template = validate_template_exists(form["template_id"], authenticated_service)
    check_service_has_permission(template.template_type, authenticated_service.permissions)

    remaining_messages = authenticated_service.message_limit - fetch_todays_total_message_count(authenticated_service.id)

    form["validated_sender_id"] = validate_sender_id(template, form.get("reply_to_id"))

    try:
        if form.get("rows"):
            output = StringIO()
            writer = csv.writer(output)
            writer.writerows(form["rows"])
            file_data = output.getvalue()
        else:
            file_data = form["csv"]

        recipient_csv = RecipientCSV(
            file_data,
            template_type=template.template_type,
            placeholders=template._as_utils_template().placeholders,
            max_rows=max_rows,
            safelist=safelisted_members(authenticated_service, api_user.key_type),
            remaining_messages=remaining_messages,
        )
    except csv.Error as e:
        raise BadRequestError(message=f"Error converting to CSV: {str(e)}", status_code=400)

    check_for_csv_errors(recipient_csv, max_rows, remaining_messages)
    job = create_bulk_job(authenticated_service, api_user, template, form, recipient_csv)

    return jsonify(data=job_schema.dump(job).data), 201
Example #6
0
def test_dao_fetch_todays_total_message_count_returns_0_when_no_messages_for_today(notify_db,
                                                                                   notify_db_session):
    assert fetch_todays_total_message_count(uuid.uuid4()) == 0
Example #7
0
def test_dao_fetch_todays_total_message_count_returns_count_for_today(notify_db_session):
    notification = create_notification(template=create_template(service=create_service()))
    assert fetch_todays_total_message_count(notification.service.id) == 1
def test_dao_fetch_todays_total_message_count_returns_0_when_no_messages_for_today(notify_db, notify_db_session):
    assert fetch_todays_total_message_count(uuid.uuid4()) == 0
def test_dao_fetch_todays_total_message_count_returns_count_for_today(
    notify_db, notify_db_session, sample_notification
):
    assert fetch_todays_total_message_count(sample_notification.service.id) == 1