From 6f621ae02c618ed29ac2834f50042b85b8ee3b7f Mon Sep 17 00:00:00 2001
From: "Sachin S. Kamath" <sskamath96@gmail.com>
Date: Tue, 24 Oct 2017 22:01:23 +0530
Subject: [PATCH] Update instructions in README and minor code refactor

Signed-off-by: Sachin S. Kamath <sskamath96@gmail.com>
---
 README.md                 |  8 ++++++++
 hodor/__init__.py         |  7 ++++---
 hodor/controllers/user.py | 19 ++++++++++++-------
 hodor/models/user.py      |  3 ++-
 requirements.txt          |  3 ++-
 5 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/README.md b/README.md
index bba44c1..f4b62a4 100644
--- a/README.md
+++ b/README.md
@@ -3,10 +3,18 @@
 The web application for the Hodor Project
 
 ## Quickstart
+* Install postgresql database. The installation will depend on your operating system and distribution.
 * Clone the repo using `git clone https://git.amrita.edu/hodorsec/hodor-webapp`
 * Create a virtualenv (see below for instructions) and activate it.
 * After activating it, `pip install --user -r requirements.txt` to
 install dependencies.
+* To initialize the database, run the following:
+```bash
+python manage.py db init
+python manage.py db migrate
+```
+This will create a folder called `migrations` in the working environment. In most cases, you do not need to care about what's in there. 
+
 * `./runserver.py` to run the app.
 * Navigate to `http://127.0.0.1:8080` to watch it in action!
 
diff --git a/hodor/__init__.py b/hodor/__init__.py
index a4c0d45..a9ec7a8 100644
--- a/hodor/__init__.py
+++ b/hodor/__init__.py
@@ -21,8 +21,8 @@ db.init_app(application)
 application.config['SECRET_KEY'] = 'blehblehbleh'
 
 
-
-# Delay the application launch to get the user attention if running in testing mode
+# Delay the application launch to get the user attention if running in
+# testing mode
 '''
 if config_name != 'production':
     for x in range(0, 5):
@@ -33,7 +33,8 @@ if config_name != 'production':
 '''
 print(colored("\n\nRunning app in {} mode..".format(config_name), 'yellow'))
 
-# This disables the HTML API renderer for flask_api when called through browsers.
+# This disables the HTML API renderer for flask_api when called through
+# browsers.
 if config_name == 'production':
     application.config['DEFAULT_RENDERERS'] = [
         'flask.ext.api.renderers.JSONRenderer',
diff --git a/hodor/controllers/user.py b/hodor/controllers/user.py
index e894e82..769ce8f 100644
--- a/hodor/controllers/user.py
+++ b/hodor/controllers/user.py
@@ -5,7 +5,9 @@ from hodor.models.user import User
 from sqlalchemy.exc import IntegrityError
 
 
-# Get all Users
+#########################################
+# Get all the user from the database    #
+#########################################
 @app.route('/users', methods=['GET', 'POST'])
 def get_all_users():
     # TODO: Authentication for calling this API endpoint. Admin only.
@@ -69,19 +71,19 @@ def check_for_existing_user():
     # If the username field is passed, checl the username field.
     if request.data.get('username'):
         check_username = str(request.data.get('username').strip())
-        user_checkuser = User.query.filter_by(username=check_username).first()
-        if user_checkuser:
+        user_check_user = User.query.filter_by(username=check_username).first()
+        if user_check_user:
             errors.append({
                 'field': 'username',
                 'error': '{} is taken'.format(check_username)
             })
-        del(user_checkuser)
+        del user_check_user
 
     # If the email field is set, check for duplicate email
     if request.data.get('email'):
         check_email = str(request.data.get('email').strip())
-        user_checkmail = User.query.filter_by(email=check_email).first()
-        if user_checkmail:
+        user_check_email = User.query.filter_by(email=check_email).first()
+        if user_check_email:
             errors.append({
                 'field': 'email',
                 'error': '{} exists in the database'.format(check_email)
@@ -96,8 +98,11 @@ def check_for_existing_user():
         )
 
 
+################################################
+# Handle Integrity Exceptions in API           #
+################################################
 @app.errorhandler(IntegrityError)
-def handle_sqlalchemy_assertion_error(err):
+def handle_sql_assertion_error(err):
     try:
         '''
         err.orig.args is from the DBAPIError class of SQLAlchemy. It usually
diff --git a/hodor/models/user.py b/hodor/models/user.py
index 6b07ed7..bf750c9 100644
--- a/hodor/models/user.py
+++ b/hodor/models/user.py
@@ -4,6 +4,7 @@ from hodor import db
 from sqlalchemy import inspect
 from sqlalchemy_utils import PasswordType
 
+
 class User(db.Model):
     __tablename__ = 'users'
 
@@ -13,7 +14,7 @@ class User(db.Model):
     last_name = db.Column(db.String(32), nullable=False)
     email = db.Column(db.String(64), unique=True, nullable=False)
     country = db.Column(db.String(64))
-    '''PasswordType is an awesome function. To check for passwords later, 
+    '''PasswordType is an awesome function. To check for passwords later,
         you can just do user['password'] == 'plaintext' for a boolean response.'''
     password = db.Column(PasswordType(
         schemes=[
diff --git a/requirements.txt b/requirements.txt
index bd50cd1..626ae4e 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -5,4 +5,5 @@ Flask-DebugToolbar
 flask_api
 termcolor
 flask_sqlalchemy
-sqlalchemy_utils
\ No newline at end of file
+sqlalchemy_utils
+SQLAlchemy
-- 
GitLab