Add dao to delete inbound sms after seven days

This commit is contained in:
Imdad Ahad
2017-06-02 12:21:12 +01:00
parent 449c47c227
commit 0631b6c988
4 changed files with 70 additions and 15 deletions

View File

@@ -1,6 +1,13 @@
from datetime import (
timedelta,
datetime
)
from app import db from app import db
from app.dao.dao_utils import transactional from app.dao.dao_utils import transactional
from app.models import InboundSms from app.models import InboundSms
from app.statsd_decorators import statsd
@transactional @transactional
@@ -28,3 +35,15 @@ def dao_count_inbound_sms_for_service(service_id):
return InboundSms.query.filter( return InboundSms.query.filter(
InboundSms.service_id == service_id InboundSms.service_id == service_id
).count() ).count()
@statsd(namespace="dao")
@transactional
def delete_inbound_sms_created_more_than_a_week_ago():
seven_days_ago = datetime.utcnow() - timedelta(days=7)
deleted = db.session.query(InboundSms).filter(
InboundSms.created_at < seven_days_ago
).delete(synchronize_session='fetch')
return deleted

View File

@@ -2,7 +2,8 @@ import functools
from datetime import ( from datetime import (
datetime, datetime,
timedelta, timedelta,
date) date
)
from flask import current_app from flask import current_app
@@ -26,6 +27,7 @@ from app.models import (
NotificationHistory, NotificationHistory,
NotificationStatistics, NotificationStatistics,
Template, Template,
ScheduledNotification,
NOTIFICATION_CREATED, NOTIFICATION_CREATED,
NOTIFICATION_DELIVERED, NOTIFICATION_DELIVERED,
NOTIFICATION_SENDING, NOTIFICATION_SENDING,
@@ -35,7 +37,8 @@ from app.models import (
NOTIFICATION_PERMANENT_FAILURE, NOTIFICATION_PERMANENT_FAILURE,
KEY_TYPE_NORMAL, KEY_TYPE_TEST, KEY_TYPE_NORMAL, KEY_TYPE_TEST,
LETTER_TYPE, LETTER_TYPE,
NOTIFICATION_SENT, ScheduledNotification) NOTIFICATION_SENT,
)
from app.dao.dao_utils import transactional from app.dao.dao_utils import transactional
from app.statsd_decorators import statsd from app.statsd_decorators import statsd

View File

@@ -1,11 +1,16 @@
from datetime import datetime from datetime import datetime, timedelta
from freezegun import freeze_time from freezegun import freeze_time
from app.dao.inbound_sms_dao import dao_get_inbound_sms_for_service, dao_count_inbound_sms_for_service from app.dao.inbound_sms_dao import (
dao_get_inbound_sms_for_service,
dao_count_inbound_sms_for_service,
delete_inbound_sms_created_more_than_a_week_ago
)
from tests.app.db import create_inbound_sms, create_service from tests.app.db import create_inbound_sms, create_service
from app.models import InboundSms
def test_get_all_inbound_sms(sample_service): def test_get_all_inbound_sms(sample_service):
inbound = create_inbound_sms(sample_service) inbound = create_inbound_sms(sample_service)
@@ -57,3 +62,27 @@ def test_count_inbound_sms_for_service(notify_db_session):
create_inbound_sms(service_two) create_inbound_sms(service_two)
assert dao_count_inbound_sms_for_service(service_one.id) == 2 assert dao_count_inbound_sms_for_service(service_one.id) == 2
@freeze_time("2017-01-01 12:00:00")
def test_should_delete_inbound_sms_older_than_seven_days(sample_service):
older_than_seven_days = datetime.utcnow() - timedelta(days=7, seconds=1)
create_inbound_sms(sample_service, created_at=older_than_seven_days)
delete_inbound_sms_created_more_than_a_week_ago()
assert len(InboundSms.query.all()) == 0
@freeze_time("2017-01-01 12:00:00")
def test_should_not_delete_inbound_sms_before_seven_days(sample_service):
yesterday = datetime.utcnow() - timedelta(days=1)
just_before_seven_days = datetime.utcnow() - timedelta(days=6, hours=23, minutes=59, seconds=59)
older_than_seven_days = datetime.utcnow() - timedelta(days=7, seconds=1)
create_inbound_sms(sample_service, created_at=yesterday)
create_inbound_sms(sample_service, created_at=just_before_seven_days)
create_inbound_sms(sample_service, created_at=older_than_seven_days)
delete_inbound_sms_created_more_than_a_week_ago()
assert len(InboundSms.query.all()) == 2

View File

@@ -2,6 +2,7 @@ from datetime import datetime
import uuid import uuid
from app.dao.inbound_sms_dao import dao_create_inbound_sms
from app.dao.jobs_dao import dao_create_job from app.dao.jobs_dao import dao_create_job
from app.models import ( from app.models import (
InboundSms, InboundSms,
@@ -12,6 +13,7 @@ from app.models import (
ScheduledNotification, ScheduledNotification,
ServicePermission, ServicePermission,
Job, Job,
InboundSms,
EMAIL_TYPE, EMAIL_TYPE,
SMS_TYPE, SMS_TYPE,
KEY_TYPE_NORMAL, KEY_TYPE_NORMAL,
@@ -151,14 +153,15 @@ def create_notification(
return notification return notification
def create_job(template, def create_job(
notification_count=1, template,
created_at=None, notification_count=1,
job_status='pending', created_at=None,
scheduled_for=None, job_status='pending',
processing_started=None, scheduled_for=None,
original_file_name='some.csv'): processing_started=None,
original_file_name='some.csv'
):
data = { data = {
'id': uuid.uuid4(), 'id': uuid.uuid4(),
'service_id': template.service_id, 'service_id': template.service_id,
@@ -193,11 +196,12 @@ def create_inbound_sms(
user_number='447700900111', user_number='447700900111',
provider_date=None, provider_date=None,
provider_reference=None, provider_reference=None,
content='Hello' content='Hello',
created_at=None
): ):
inbound = InboundSms( inbound = InboundSms(
service=service, service=service,
created_at=datetime.utcnow(), created_at=created_at or datetime.utcnow(),
notify_number=notify_number or service.sms_sender, notify_number=notify_number or service.sms_sender,
user_number=user_number, user_number=user_number,
provider_date=provider_date or datetime.utcnow(), provider_date=provider_date or datetime.utcnow(),