From 5bcc1ed9df4e7a6b8086cac3f25a466aee3bebcd Mon Sep 17 00:00:00 2001 From: neodarz Date: Sat, 16 Jun 2018 17:52:31 +0200 Subject: Add first structure of the application --- src/lb_app/__init__.py | 35 +++++++++++++++++++++++++++++++++++ src/lb_app/api.py | 43 +++++++++++++++++++++++++++++++++++++++++++ src/lb_app/app_db/__init__.py | 0 src/lb_app/app_db/db.py | 31 +++++++++++++++++++++++++++++++ src/lb_app/app_db/user.py | 27 +++++++++++++++++++++++++++ 5 files changed, 136 insertions(+) create mode 100644 src/lb_app/__init__.py create mode 100644 src/lb_app/api.py create mode 100644 src/lb_app/app_db/__init__.py create mode 100644 src/lb_app/app_db/db.py create mode 100644 src/lb_app/app_db/user.py (limited to 'src/lb_app') diff --git a/src/lb_app/__init__.py b/src/lb_app/__init__.py new file mode 100644 index 0000000..cf7fa7e --- /dev/null +++ b/src/lb_app/__init__.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +""" + liberationCenter + ~~~~~~~~~~~~~~~~ + + Small AppCenter for apps. + + :copyright: (c) 2018 by neodarz. + :licence: GPLv3, see LICENSE for more details. +""" +import os + +from flask import (Flask, render_template) + +def create_app(test_config=None): + app = Flask(__name__, instance_relative_config=True) + app.config.from_mapping( + SECRET_KEY='dev', + MONGO_URI= "mongodb://localhost:27017/liberationCenter", + ) + + if test_config is None: + app.config.from_pyfile('config.py', silent=True) + else: + app.config.from_mapping(test_config) + + try: + os.makedirs(app.instance_path) + except OSError: + pass + + from . import api + app.register_blueprint(api.bp, url_prefix='/api') + + return app diff --git a/src/lb_app/api.py b/src/lb_app/api.py new file mode 100644 index 0000000..6dfe2e8 --- /dev/null +++ b/src/lb_app/api.py @@ -0,0 +1,43 @@ +from flask import (Flask, Blueprint, g, redirect, request, session, jsonify, url_for) +from flask_restplus import Resource, Api, fields + +from bson.json_util import dumps +from json import loads + +from .app_db.user import User + +app = Flask(__name__, instance_relative_config=True) + +bp = Blueprint('api', __name__, url_prefix='/api') + +env = app.config['DEBUG'] + +if env is not False: + env = '/doc' + +api = Api(bp, doc=env) + +user = User(app) + +user_model = api.model('User', { + 'username': fields.String(required=True, description='Username of account', example='Jean') +}) + +@api.route('/users', endpoint='users') +class Users(Resource): + def get(self): + """ Get user list """ + return loads(dumps(user.all())) + + +@api.route('/user/register') +class Register(Resource): + @api.expect(user_model) + def post(self): + """ Register new user """ + args = request.get_json(force=True) + status = user.register(args.get('username')) + + if status is None: + return loads('{"msg": "done"}'), 201 + return loads('{"msg": "'+status+'"}'), 200 diff --git a/src/lb_app/app_db/__init__.py b/src/lb_app/app_db/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/lb_app/app_db/db.py b/src/lb_app/app_db/db.py new file mode 100644 index 0000000..005599b --- /dev/null +++ b/src/lb_app/app_db/db.py @@ -0,0 +1,31 @@ +from flask import current_app, _app_ctx_stack +from flask_pymongo import PyMongo + +class MongoDB(object): + def __init__(self, app=None): + self.app = app + if app is not None: + self.init_app(app) + + def init_app(self, app): + app.teardown_appcontext(self.teardown) + + def connect(self): + return PyMongo(self.app, current_app.config['MONGO_URI']) + + + def teardown(self, exception): + ctx = _app_ctx_stack.top + if hasattr(ctx, 'mongo_db'): + ctx.mongo_db.cx.close() + + def connection(self): + ctx = _app_ctx_stack.top + if ctx is not None: + if not hasattr(ctx, 'mongo_db'): + ctx.mongo_db = self.connect() + return ctx.mongo_db + + def set_up(self): + mongo = PyMongo(self.app) + mongo.cx.drop_database("test_liberationCenter") diff --git a/src/lb_app/app_db/user.py b/src/lb_app/app_db/user.py new file mode 100644 index 0000000..1af957c --- /dev/null +++ b/src/lb_app/app_db/user.py @@ -0,0 +1,27 @@ +from flask import current_app + +from .db import MongoDB + +class User(object): + def __init__(self, app=None): + self.app = app + + def all(self): + db = MongoDB(self.app) + mongo = db.connection() + return list(mongo.db.users.find({})) + + def register(self, username): + db = MongoDB(self.app) + mongo = db.connection() + error = None + + if not username: + error = 'Username is required.' + elif len(list(mongo.db.users.find({"username": username}))) > 0: + error = 'User {} is already registered.'.format(username) + + if error is None: + mongo.db.users.insert({"username": username}) + return None + return error -- cgit v1.2.1