diff --git a/.gitignore b/.gitignore index 7324359..3d791fa 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ dist/ build/ *.egg-info/ .DS_Store -.vscode/settings.json \ No newline at end of file +.vscode/settings.json +.idea/ diff --git a/.run/devserver.run.xml b/.run/devserver.run.xml new file mode 100644 index 0000000..e5974e0 --- /dev/null +++ b/.run/devserver.run.xml @@ -0,0 +1,13 @@ + + + + \ No newline at end of file diff --git a/.run/initdb.run.xml b/.run/initdb.run.xml new file mode 100644 index 0000000..6c532e9 --- /dev/null +++ b/.run/initdb.run.xml @@ -0,0 +1,13 @@ + + + + \ No newline at end of file diff --git a/jcrm_lite/api/__init__.py b/jcrm_lite/api/__init__.py index e2bef47..03476b2 100644 --- a/jcrm_lite/api/__init__.py +++ b/jcrm_lite/api/__init__.py @@ -1 +1 @@ -from .api import register_api +from .api import bp diff --git a/jcrm_lite/api/api.py b/jcrm_lite/api/api.py index b7b292d..4a0c689 100644 --- a/jcrm_lite/api/api.py +++ b/jcrm_lite/api/api.py @@ -3,41 +3,41 @@ from ..db.models import Contact from ..db.model_utils import jsonify_one, jsonify_list from ..auth import login_required -from flask import jsonify, request, session +from flask import Blueprint, jsonify, request, session from datetime import datetime -API_PREFIX = '/api/v1' +bp = Blueprint('api', __name__, url_prefix='/api/v1') -def register_api(app): +@bp.route('/contacts', methods=['GET']) +@login_required +def get_contacts(): + contacts = Contact.query.all() + return jsonify_list(contacts) - @app.route(API_PREFIX + '/contacts', methods=['GET']) - @login_required - def get_contacts(): - contacts = Contact.query.all() - return jsonify_list(contacts) - @app.route(API_PREFIX + '/contact/', methods=['GET']) - @login_required - def get_contact(contact_id): - contact = Contact.query.filter_by(id=contact_id).one() - return jsonify_one(contact) +@bp.route('/contact/', methods=['GET']) +@login_required +def get_contact(contact_id): + contact = Contact.query.filter_by(id=contact_id).one() + return jsonify_one(contact) - @app.route(API_PREFIX + '/contact/', methods=['PUT']) - @login_required - def update_contact(contact_id): - data = request.get_json() +@bp.route('/contact/', methods=['PUT']) +@login_required +def update_contact(contact_id): - contact = Contact.query.filter_by(id=contact_id).one() + data = request.get_json() - contact.company_name = data['company_name'] - contact.first_name = data['first_name'] - contact.last_name = data['last_name'] + contact = Contact.query.filter_by(id=contact_id).one() - contact.updated_date = datetime.utcnow() - contact.updated_user_id = session.get("user_id") + contact.company_name = data['company_name'] + contact.first_name = data['first_name'] + contact.last_name = data['last_name'] - db.session.commit() + contact.updated_date = datetime.utcnow() + contact.updated_user_id = session.get('user_id') - return jsonify({'success': True}) + db.session.commit() + + return jsonify({'success': True}) diff --git a/jcrm_lite/app.py b/jcrm_lite/app.py index 4d6fba8..4e848ca 100644 --- a/jcrm_lite/app.py +++ b/jcrm_lite/app.py @@ -32,9 +32,6 @@ def create_app(test_config=None): db.init_app(app) initdb.register_command(app) - # init auth - app.register_blueprint(auth.bp) - # routes routes.register_routes(app) diff --git a/jcrm_lite/auth/__init__.py b/jcrm_lite/auth/__init__.py index cecc13e..c082b2b 100644 --- a/jcrm_lite/auth/__init__.py +++ b/jcrm_lite/auth/__init__.py @@ -1,3 +1 @@ -from .auth import ( - login_required, bp -) +from .auth import login_required, bp diff --git a/jcrm_lite/auth/auth.py b/jcrm_lite/auth/auth.py index 9e5a0ea..3eeeac6 100644 --- a/jcrm_lite/auth/auth.py +++ b/jcrm_lite/auth/auth.py @@ -8,7 +8,7 @@ from ..db import db from ..db.models import User -bp = Blueprint("auth", __name__, url_prefix="/auth") +bp = Blueprint('auth', __name__, url_prefix='/auth') def login_required(view): @@ -17,7 +17,7 @@ def login_required(view): @functools.wraps(view) def wrapped_view(**kwargs): if g.user is None: - return redirect(url_for("auth.login")) + return redirect(url_for('auth.login')) return view(**kwargs) @@ -28,7 +28,7 @@ def wrapped_view(**kwargs): def load_logged_in_user(): """If a user id is stored in the session, load the user object from the database into ``g.user``.""" - user_id = session.get("user_id") + user_id = session.get('user_id') if user_id is None: g.user = None @@ -36,27 +36,27 @@ def load_logged_in_user(): g.user = User.query.filter_by(id=user_id).first() -@bp.route("/register", methods=("GET", "POST")) +@bp.route('/register', methods=('GET', 'POST')) def register(): """Register a new user. Validates that the username is not already taken. Hashes the password for security. """ - if request.method == "POST": - username = request.form["username"] - password = request.form["password"] + if request.method == 'POST': + username = request.form['username'] + password = request.form['password'] error = None if not username: - error = "Username is required." + error = 'Username is required.' elif not password: - error = "Password is required." + error = 'Password is required.' elif ( User.query.filter_by(username=username).first() is not None ): - error = "User {0} is already registered.".format(username) + error = 'User {0} is already registered.'.format(username) if error is None: # the name is available, store it in the database and go to @@ -66,40 +66,40 @@ def register(): password=generate_password_hash(password) )) db.session.commit() - return redirect(url_for("auth.login")) + return redirect(url_for('auth.login')) flash(error) - return render_template("auth/register.html") + return render_template('auth/register.html') -@bp.route("/login", methods=("GET", "POST")) +@bp.route('/login', methods=('GET', 'POST')) def login(): """Log in a registered user by adding the user id to the session.""" - if request.method == "POST": - username = request.form["username"] - password = request.form["password"] + if request.method == 'POST': + username = request.form['username'] + password = request.form['password'] error = None user = User.query.filter_by(username=username).first() if user is None: - error = "Incorrect username." + error = 'Incorrect username.' elif not check_password_hash(user.password, password): - error = "Incorrect password." + error = 'Incorrect password.' if error is None: # store the user id in a new session and return to the index session.clear() - session["user_id"] = user.id - return redirect(url_for("index")) + session['user_id'] = user.id + return redirect(url_for('index')) flash(error) - return render_template("auth/login.html") + return render_template('auth/login.html') -@bp.route("/logout") +@bp.route('/logout') def logout(): """Clear the current session, including the stored user id.""" session.clear() - return redirect(url_for("index")) + return redirect(url_for('index')) diff --git a/jcrm_lite/routes.py b/jcrm_lite/routes.py index 3f8e2d9..01ccbc2 100644 --- a/jcrm_lite/routes.py +++ b/jcrm_lite/routes.py @@ -1,7 +1,8 @@ from flask import render_template from .auth import login_required -from .api import register_api +from .api import bp as api_bp +from .auth import bp as auth_bp def register_routes(app): @@ -12,4 +13,5 @@ def register_routes(app): def index(path='/'): return render_template('app.html') - register_api(app) + app.register_blueprint(auth_bp) + app.register_blueprint(api_bp) diff --git a/jcrm_lite/templates/base.html b/jcrm_lite/templates/base.html index a7a1d3a..a629e75 100644 --- a/jcrm_lite/templates/base.html +++ b/jcrm_lite/templates/base.html @@ -30,12 +30,12 @@