Skip to content
Snippets Groups Projects
Verified Commit 6f621ae0 authored by Sachin Kamath's avatar Sachin Kamath
Browse files

Update instructions in README and minor code refactor

parent 9c8a6b32
Branches
No related tags found
No related merge requests found
......@@ -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!
......
......@@ -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',
......
......@@ -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
......
......@@ -4,6 +4,7 @@ from hodor import db
from sqlalchemy import inspect
from sqlalchemy_utils import PasswordType
class User(db.Model):
__tablename__ = 'users'
......
......@@ -6,3 +6,4 @@ flask_api
termcolor
flask_sqlalchemy
sqlalchemy_utils
SQLAlchemy
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment