mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 01:41:05 -05:00
Initial code added for models and services not functional yet. Bootstrap and migrations added for db.
This commit is contained in:
@@ -2,11 +2,15 @@ import os
|
||||
|
||||
from flask._compat import string_types
|
||||
from flask import Flask, _request_ctx_stack
|
||||
from flask.ext.sqlalchemy import SQLAlchemy
|
||||
from werkzeug.local import LocalProxy
|
||||
from config import configs
|
||||
from utils import logging
|
||||
|
||||
|
||||
db = SQLAlchemy()
|
||||
|
||||
|
||||
api_user = LocalProxy(lambda: _request_ctx_stack.top.api_user)
|
||||
|
||||
|
||||
@@ -16,6 +20,7 @@ def create_app(config_name):
|
||||
application.config['NOTIFY_API_ENVIRONMENT'] = config_name
|
||||
application.config.from_object(configs[config_name])
|
||||
|
||||
db.init_app(application)
|
||||
init_app(application)
|
||||
|
||||
logging.init_app(application)
|
||||
@@ -26,6 +31,8 @@ def create_app(config_name):
|
||||
from .status import status as status_blueprint
|
||||
application.register_blueprint(status_blueprint)
|
||||
|
||||
from app import models
|
||||
|
||||
return application
|
||||
|
||||
|
||||
|
||||
0
app/main/dao/__init__.py
Normal file
0
app/main/dao/__init__.py
Normal file
28
app/main/dao/services_dao.py
Normal file
28
app/main/dao/services_dao.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy.orm import load_only
|
||||
|
||||
from app import db
|
||||
from app.models import Service
|
||||
|
||||
|
||||
def create_new_service(service_name,
|
||||
user,
|
||||
limit=1000,
|
||||
active=False,
|
||||
restricted=True):
|
||||
service = Service(name=service_name,
|
||||
created_at=datetime.now(),
|
||||
limit=limit,
|
||||
active=active,
|
||||
restricted=restricted)
|
||||
db.session.add(service)
|
||||
service.users.append(user)
|
||||
db.session.commit()
|
||||
return service.id
|
||||
|
||||
|
||||
def get_services(user, service_id=None):
|
||||
if service_id:
|
||||
return Service.query.filter_by(user=user, service_id=service_id).one()
|
||||
return Service.query.filter_by(user=user).all()
|
||||
20
app/main/dao/users_dao.py
Normal file
20
app/main/dao/users_dao.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy.orm import load_only
|
||||
|
||||
from app import db
|
||||
from app.models import User
|
||||
|
||||
|
||||
def create_new_user(email_address):
|
||||
user = User(email_address=email_address,
|
||||
created_at=datetime.now())
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
return user.id
|
||||
|
||||
|
||||
def get_users(user_id=None):
|
||||
if user_id:
|
||||
return User.query.filter_by(user_id=user_id).one()
|
||||
return User.query.filter_by().all()
|
||||
@@ -2,11 +2,16 @@ from flask import jsonify
|
||||
from .. import main
|
||||
|
||||
|
||||
# TODO need for health check url
|
||||
|
||||
|
||||
# TODO remove
|
||||
@main.route('/', methods=['GET'])
|
||||
def get_index():
|
||||
return jsonify(result="hello world"), 200
|
||||
|
||||
|
||||
# TODO remove
|
||||
@main.route('/', methods=['POST'])
|
||||
def post_index():
|
||||
return jsonify(result="hello world"), 200
|
||||
|
||||
38
app/main/views/services.py
Normal file
38
app/main/views/services.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from flask import jsonify
|
||||
from app.main.dao.services_dao import (create_new_service, get_services)
|
||||
from app.main.dao.users_dao import (get_users)
|
||||
from .. import main
|
||||
|
||||
|
||||
# TODO auth to be added.
|
||||
@main.route('/service', methods=['POST'])
|
||||
def create_service():
|
||||
return jsonify(result="created"), 201
|
||||
|
||||
|
||||
# TODO auth to be added
|
||||
@main.route('/service/<int:service_id>', method=['PUT'])
|
||||
def update_service(service_id):
|
||||
return jsonify(result="updated")
|
||||
|
||||
|
||||
# TODO auth to be added.
|
||||
# Should be restricted by user, user id
|
||||
# is pulled from the token
|
||||
@main.route('/service/<int:service_id>', method=['GET'])
|
||||
@main.route('/service', methods=['GET'])
|
||||
def get_service(service_id=None):
|
||||
services = get_services
|
||||
return jsonify(
|
||||
data=services
|
||||
)
|
||||
|
||||
|
||||
# TODO auth to be added
|
||||
# auth should be allow for admin tokens only
|
||||
@main.route('/user/<int:user_id>/service', method=['GET'])
|
||||
@main.route('/user/<int:user_id>/service/<int:service_id>', method=['GET'])
|
||||
def get_service_by_user_id(user_id, service_id=None):
|
||||
user = get_users(user_id=user_id)
|
||||
services = get_services(user, service_id=service_id)
|
||||
return jsonify(data=services)
|
||||
43
app/models.py
Normal file
43
app/models.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from . import db
|
||||
|
||||
|
||||
class User(db.Model):
|
||||
__tablename__ = 'users'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
email_address = db.Column(db.String(255), nullable=False, index=True, unique=True)
|
||||
created_at = db.Column(db.DateTime, index=False, unique=False, nullable=False)
|
||||
updated_at = db.Column(db.DateTime, index=False, unique=False, nullable=True)
|
||||
|
||||
|
||||
user_to_service = db.Table(
|
||||
'user_to_service',
|
||||
db.Model.metadata,
|
||||
db.Column('user_id', db.Integer, db.ForeignKey('users.id')),
|
||||
db.Column('service_id', db.Integer, db.ForeignKey('services.id'))
|
||||
)
|
||||
|
||||
|
||||
class Service(db.Model):
|
||||
__tablename__ = 'services'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(255), nullable=False)
|
||||
created_at = db.Column(db.DateTime, index=False, unique=False, nullable=False)
|
||||
active = db.Column(db.Boolean, index=False, unique=False, nullable=False)
|
||||
limit = db.Column(db.BigInteger, index=False, unique=False, nullable=False)
|
||||
users = db.relationship('User', secondary=user_to_service, backref=db.backref('user_to_service', lazy='dynamic'))
|
||||
restricted = db.Column(db.Boolean, index=False, unique=False, nullable=False)
|
||||
|
||||
def serialize(self):
|
||||
serialized = {
|
||||
'id': self.id,
|
||||
'name': self.name,
|
||||
'createdAt': self.created_at.strftime(DATETIME_FORMAT),
|
||||
'active': self.active,
|
||||
'restricted': self.restricted,
|
||||
'limit': self.limit,
|
||||
'user': self.users.serialize()
|
||||
}
|
||||
|
||||
return filter_null_value_fields(serialized)
|
||||
Reference in New Issue
Block a user