Ejemplo n.º 1
0
def create_job():
    job_from_request = get_json_from_request('job')

    validation_result, validation_errors = valid_job_submission(job_from_request)
    if not validation_result:
        return jsonify(
            error="Invalid JSON",
            error_details=validation_errors
        ), 400

    job = Job(
        name=job_from_request['name'],
        service_id=job_from_request['serviceId'],
        created_at=datetime.utcnow()
    )

    if "filename" in job_from_request:
        job.filename = job_from_request['filename']

    try:
        db.session.add(job)
        db.session.commit()
        return jsonify(
            job=job.serialize()
        ), 201
    except IntegrityError as e:
        db.session.rollback()
        abort(400, e.orig)
Ejemplo n.º 2
0
def auth_user():
    user_authentication_request = get_json_from_request('userAuthentication')

    validation_result, validation_errors = valid_user_authentication_submission(user_authentication_request)
    if not validation_result:
        return jsonify(
            error="Invalid JSON",
            error_details=validation_errors
        ), 400

    user = User.query.filter(
        User.email_address == user_authentication_request['emailAddress'].lower()
    ).first()

    if user is None:
        return jsonify(authorization=False), 404
    elif valid_user_auth(user_authentication_request['password'], user):
        user.logged_in_at = datetime.utcnow()
        user.failed_login_count = 0
        db.session.add(user)
        db.session.commit()

        return jsonify(users=user.serialize()), 200
    else:
        user.failed_login_count += 1
        db.session.add(user)
        db.session.commit()

        return jsonify(authorization=False), 403
Ejemplo n.º 3
0
def create_user():
    user_creation_request = get_json_from_request('user')
    validation_result, validation_errors = valid_create_user_submission(user_creation_request)
    if not validation_result:
        return jsonify(
            error="Invalid JSON",
            error_details=validation_errors
        ), 400

    user = User(
        email_address=user_creation_request['emailAddress'].lower(),
        mobile_number=user_creation_request['mobileNumber'],
        password=hashpw(user_creation_request['password']),
        active=False,
        created_at=datetime.utcnow(),
        updated_at=datetime.utcnow(),
        logged_in_at=datetime.utcnow(),
        password_changed_at=datetime.utcnow(),
        failed_login_count=0,
        role='admin'
    )

    try:
        db.session.add(user)
        db.session.commit()
        return jsonify(
            users=user.serialize()
        ), 201
    except IntegrityError as e:
        print(e.orig)
        db.session.rollback()
        abort(400, "failed to create user")
Ejemplo n.º 4
0
def create_service():
    service_from_request = get_json_from_request('service')

    validation_result, validation_errors = valid_service_submission(service_from_request)
    if not validation_result:
        return jsonify(
            error="Invalid JSON",
            error_details=validation_errors
        ), 400

    user = User.query.get(service_from_request['userId'])

    if not user:
        return jsonify(
            error="failed to create service - invalid user"
        ), 400

    try:
        token = Token(token=uuid4(), type='client')
        db.session.add(token)
        db.session.flush()

        service = Service(
            name=service_from_request['name'],
            created_at=datetime.utcnow(),
            token_id=token.id,
            active=True,
            restricted=True,
            limit=current_app.config['MAX_SERVICE_LIMIT']
        )
        service.users.append(user)
        db.session.add(service)
        db.session.commit()
        return jsonify(
            service=service.serialize()
        ), 201
    except IntegrityError as e:
        print(e.orig)
        db.session.rollback()
        abort(400, "failed to create service")
Ejemplo n.º 5
0
def create_job():
    job_from_request = get_json_from_request('job')

    validation_result, validation_errors = valid_job_submission(
        job_from_request)
    if not validation_result:
        return jsonify(error="Invalid JSON",
                       error_details=validation_errors), 400

    job = Job(name=job_from_request['name'],
              service_id=job_from_request['serviceId'],
              created_at=datetime.utcnow())

    if "filename" in job_from_request:
        job.filename = job_from_request['filename']

    try:
        db.session.add(job)
        db.session.commit()
        return jsonify(job=job.serialize()), 201
    except IntegrityError as e:
        db.session.rollback()
        abort(400, e.orig)
Ejemplo n.º 6
0
def add_user_to_service(service_id):
    json_request = get_json_from_request('user')

    validation_result, validation_errors = valid_email_address(json_request)
    if not validation_result:
        return jsonify(
            error="Invalid JSON",
            error_details=validation_errors
        ), 400

    user, service = check_user_and_service(service_id, json_request['emailAddress'])

    service.users.append(user)
    try:
        db.session.add(service)
        db.session.commit()
        return jsonify(
            users=service.serialize()
        ), 200
    except IntegrityError as e:
        print(e.orig)
        db.session.rollback()
        abort(400, "failed to add user to service")
Ejemplo n.º 7
0
def create_sms_notification():
    if not current_app.config['SMS_ENABLED']:
        return jsonify(error="SMS is unavailable"), 503

    notification_request = get_json_from_request('notification')

    validation_result, validation_errors = valid_sms_notification(notification_request)

    if not validation_result:
        return jsonify(
            error="Invalid JSON",
            error_details=validation_errors
        ), 400

    incoming_token = get_token_from_headers(request.headers)

    if not incoming_token:
        return jsonify(error="No credentials supplied"), 400

    service = Service.query.join(Token).filter(Token.token == incoming_token).first()

    if not service:
        return jsonify(error="No service associated with these credentials"), 400

    if not service.active:
        return jsonify(error="Service is inactive"), 400

    if "jobId" in notification_request:
        job = Job.query.filter(Job.id == notification_request["jobId"]).first()
        if not job:
            return jsonify(error="No job associated with this job id"), 400
        elif job.service_id != service.id:
            abort(400, "Invalid job id for these credentials")
    else:
        job = Job(name="Autogenerated", created_at=datetime.utcnow(), service=service)

    if service.restricted:
        if not notification_request['to'] in [user.mobile_number for user in service.users]:
            abort(400, "Restricted service: cannot send notification to this number")

    usage = Usage.query.filter(Usage.day == datetime.utcnow().date(), Usage.service_id == service.id).first()
    if usage:
        usage.count += 1
    else:
        usage = Usage(
            day=datetime.utcnow().date(),
            count=1,
            service_id=service.id
        )

    if usage.count > service.limit:
        abort(429, "Exceeded sending limits for today")

    notification = Notification(
        to=notification_request['to'],
        message=notification_request['message'],
        status='created',
        method='sms',
        created_at=datetime.utcnow(),
        job=job
    )

    if 'description' in notification_request:
        job.name = notification_request['description']

    try:
        db.session.add(usage)
        db.session.add(notification)
        db.session.commit()

        send_messages_to_queue('sms', [notification])

    except IntegrityError:
        db.session.rollback()
        abort(400, "Failed to create notification: DB error")

    return jsonify(notification=Notification.query.filter(Notification.id == notification.id).first().serialize()), 201