Exemplo n.º 1
0
def create_notification(
        template=None,
        job=None,
        job_row_number=None,
        to_field=None,
        status='created',
        reference=None,
        created_at=None,
        sent_at=None,
        updated_at=None,
        billable_units=1,
        personalisation=None,
        api_key=None,
        key_type=KEY_TYPE_NORMAL,
        sent_by=None,
        client_reference=None,
        rate_multiplier=None,
        international=False,
        phone_prefix=None,
        scheduled_for=None,
        normalised_to=None,
        one_off=False,
        reply_to_text=None,
        created_by_id=None,
        postage=None
):
    assert job or template
    if job:
        template = job.template

    if created_at is None:
        created_at = datetime.utcnow()

    if to_field is None:
        to_field = '+16502532222' if template.template_type == SMS_TYPE else '*****@*****.**'

    if status != 'created':
        sent_at = sent_at or datetime.utcnow()
        updated_at = updated_at or datetime.utcnow()

    if not one_off and (job is None and api_key is None):
        # we didn't specify in test - lets create it
        api_key = ApiKey.query.filter(ApiKey.service == template.service, ApiKey.key_type == key_type).first()
        if not api_key:
            api_key = create_api_key(template.service, key_type=key_type)

    if template.template_type == 'letter' and postage is None:
        postage = 'second'

    data = {
        'id': uuid.uuid4(),
        'to': to_field,
        'job_id': job and job.id,
        'job': job,
        'service_id': template.service.id,
        'service': template.service,
        'template_id': template.id,
        'template_version': template.version,
        'status': status,
        'reference': reference,
        'created_at': created_at,
        'sent_at': sent_at,
        'billable_units': billable_units,
        'personalisation': personalisation,
        'notification_type': template.template_type,
        'api_key': api_key,
        'api_key_id': api_key and api_key.id,
        'key_type': api_key.key_type if api_key else key_type,
        'sent_by': sent_by,
        'updated_at': updated_at,
        'client_reference': client_reference,
        'job_row_number': job_row_number,
        'rate_multiplier': rate_multiplier,
        'international': international,
        'phone_prefix': phone_prefix,
        'normalised_to': normalised_to,
        'reply_to_text': reply_to_text,
        'created_by_id': created_by_id,
        'postage': postage
    }
    notification = Notification(**data)
    dao_create_notification(notification)
    if scheduled_for:
        scheduled_notification = ScheduledNotification(id=uuid.uuid4(),
                                                       notification_id=notification.id,
                                                       scheduled_for=datetime.strptime(scheduled_for,
                                                                                       "%Y-%m-%d %H:%M"))
        if status != 'created':
            scheduled_notification.pending = False
        dao_created_scheduled_notification(scheduled_notification)

    return notification
Exemplo n.º 2
0
def create_notification(
    template=None,
    job=None,
    job_row_number=None,
    to_field=None,
    status="created",
    reference=None,
    created_at=None,
    sent_at=None,
    updated_at=None,
    billable_units=1,
    personalisation=None,
    api_key=None,
    key_type=KEY_TYPE_NORMAL,
    sent_by=None,
    client_reference=None,
    rate_multiplier=None,
    international=False,
    phone_prefix=None,
    scheduled_for=None,
    normalised_to=None,
    one_off=False,
    reply_to_text=None,
    created_by_id=None,
    postage=None,
):
    assert job or template
    if job:
        template = job.template

    if created_at is None:
        created_at = datetime.utcnow()

    if to_field is None:
        to_field = "+16502532222" if template.template_type == SMS_TYPE else "*****@*****.**"

    if status != "created":
        sent_at = sent_at or datetime.utcnow()
        updated_at = updated_at or datetime.utcnow()

    if not one_off and (job is None and api_key is None):
        # we didn't specify in test - lets create it
        api_key = ApiKey.query.filter(ApiKey.service == template.service,
                                      ApiKey.key_type == key_type).first()
        if not api_key:
            api_key = create_api_key(template.service, key_type=key_type)

    if template.template_type == "letter" and postage is None:
        postage = "second"

    data = {
        "id": uuid.uuid4(),
        "to": to_field,
        "job_id": job and job.id,
        "job": job,
        "service_id": template.service.id,
        "service": template.service,
        "template_id": template.id,
        "template_version": template.version,
        "status": status,
        "reference": reference,
        "created_at": created_at,
        "sent_at": sent_at,
        "billable_units": billable_units,
        "personalisation": personalisation,
        "notification_type": template.template_type,
        "api_key": api_key,
        "api_key_id": api_key and api_key.id,
        "key_type": api_key.key_type if api_key else key_type,
        "sent_by": sent_by,
        "updated_at": updated_at,
        "client_reference": client_reference,
        "job_row_number": job_row_number,
        "rate_multiplier": rate_multiplier,
        "international": international,
        "phone_prefix": phone_prefix,
        "normalised_to": normalised_to,
        "reply_to_text": reply_to_text,
        "created_by_id": created_by_id,
        "postage": postage,
    }
    notification = Notification(**data)
    dao_create_notification(notification)
    if scheduled_for:
        scheduled_notification = ScheduledNotification(
            id=uuid.uuid4(),
            notification_id=notification.id,
            scheduled_for=datetime.strptime(scheduled_for, "%Y-%m-%d %H:%M"),
        )
        if status != "created":
            scheduled_notification.pending = False
        dao_created_scheduled_notification(scheduled_notification)

    return notification
Exemplo n.º 3
0
def sample_notification(
    notify_db,
    notify_db_session,
    service=None,
    template=None,
    job=None,
    job_row_number=None,
    to_field=None,
    status='created',
    reference=None,
    created_at=None,
    sent_at=None,
    billable_units=1,
    personalisation=None,
    api_key=None,
    key_type=KEY_TYPE_NORMAL,
    sent_by=None,
    international=False,
    client_reference=None,
    rate_multiplier=1.0,
    scheduled_for=None,
    normalised_to=None,
    postage=None,
):
    if created_at is None:
        created_at = datetime.utcnow()
    if service is None:
        service = create_service(check_if_service_exists=True)
    if template is None:
        template = create_template(service=service)

    if job is None and api_key is None:
        # we didn't specify in test - lets create it
        api_key = ApiKey.query.filter(ApiKey.service == template.service,
                                      ApiKey.key_type == key_type).first()
        if not api_key:
            api_key = create_api_key(template.service, key_type=key_type)

    notification_id = uuid.uuid4()

    if to_field:
        to = to_field
    else:
        to = '+16502532222'

    data = {
        'id':
        notification_id,
        'to':
        to,
        'job_id':
        job.id if job else None,
        'job':
        job,
        'service_id':
        service.id,
        'service':
        service,
        'template_id':
        template.id,
        'template_version':
        template.version,
        'status':
        status,
        'reference':
        reference,
        'created_at':
        created_at,
        'sent_at':
        sent_at,
        'billable_units':
        billable_units,
        'personalisation':
        personalisation,
        'notification_type':
        template.template_type,
        'api_key':
        api_key,
        'api_key_id':
        api_key and api_key.id,
        'key_type':
        api_key.key_type if api_key else key_type,
        'sent_by':
        sent_by,
        'updated_at':
        created_at if status in NOTIFICATION_STATUS_TYPES_COMPLETED else None,
        'client_reference':
        client_reference,
        'rate_multiplier':
        rate_multiplier,
        'normalised_to':
        normalised_to,
        'postage':
        postage,
    }
    if job_row_number is not None:
        data['job_row_number'] = job_row_number
    notification = Notification(**data)
    dao_create_notification(notification)
    if scheduled_for:
        scheduled_notification = ScheduledNotification(
            id=uuid.uuid4(),
            notification_id=notification.id,
            scheduled_for=datetime.strptime(scheduled_for, "%Y-%m-%d %H:%M"))
        if status != 'created':
            scheduled_notification.pending = False
        db.session.add(scheduled_notification)
        db.session.commit()

    return notification
Exemplo n.º 4
0
def sample_notification(
    notify_db,
    notify_db_session,
    service=None,
    template=None,
    job=None,
    job_row_number=None,
    to_field=None,
    status="created",
    provider_response=None,
    reference=None,
    created_at=None,
    sent_at=None,
    billable_units=1,
    personalisation=None,
    api_key=None,
    key_type=KEY_TYPE_NORMAL,
    sent_by=None,
    international=False,
    client_reference=None,
    rate_multiplier=1.0,
    scheduled_for=None,
    normalised_to=None,
    postage=None,
):
    if created_at is None:
        created_at = datetime.utcnow()
    if service is None:
        service = create_service(check_if_service_exists=True)
    if template is None:
        template = create_template(service=service)

    if job is None and api_key is None:
        # we didn't specify in test - lets create it
        api_key = ApiKey.query.filter(ApiKey.service == template.service, ApiKey.key_type == key_type).first()
        if not api_key:
            api_key = create_api_key(template.service, key_type=key_type)

    notification_id = uuid.uuid4()

    if to_field:
        to = to_field
    else:
        to = "+16502532222"

    data = {
        "id": notification_id,
        "to": to,
        "job_id": job.id if job else None,
        "job": job,
        "service_id": service.id,
        "service": service,
        "template_id": template.id,
        "template_version": template.version,
        "status": status,
        "provider_response": provider_response,
        "reference": reference,
        "created_at": created_at,
        "sent_at": sent_at,
        "billable_units": billable_units,
        "personalisation": personalisation,
        "notification_type": template.template_type,
        "api_key": api_key,
        "api_key_id": api_key and api_key.id,
        "key_type": api_key.key_type if api_key else key_type,
        "sent_by": sent_by,
        "updated_at": created_at if status in NOTIFICATION_STATUS_TYPES_COMPLETED else None,
        "client_reference": client_reference,
        "rate_multiplier": rate_multiplier,
        "normalised_to": normalised_to,
        "postage": postage,
    }
    if job_row_number is not None:
        data["job_row_number"] = job_row_number
    notification = Notification(**data)
    dao_create_notification(notification)
    if scheduled_for:
        scheduled_notification = ScheduledNotification(
            id=uuid.uuid4(),
            notification_id=notification.id,
            scheduled_for=datetime.strptime(scheduled_for, "%Y-%m-%d %H:%M"),
        )
        if status != "created":
            scheduled_notification.pending = False
        db.session.add(scheduled_notification)
        db.session.commit()

    return notification