This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route('/show_tasks', methods=['GET']) | |
@auth.login_required | |
def list_all_tasks(): | |
user = User.query.filter_by(username=auth.username()).first() | |
if user is None: | |
return jsonify({ | |
'status': 'failed' | |
}) | |
task_list = {} | |
for task in user.tasks: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route('/mark_task_as_done', methods=['POST']) | |
@auth.login_required | |
def mark_task_as_done(): | |
task_id = request.form['task_id'] | |
task = Task.query.get(task_id) | |
if task is None: | |
return jsonify({ | |
'status': 'Failed' | |
}) | |
task.done = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route('/remove_task', methods=['POST']) | |
@auth.login_required | |
def remove_task(): | |
task_id = request.form['task_id'] | |
task = Task.query.get(task_id) | |
if task is None: | |
return jsonify({ | |
'status': 'Failed' | |
}) | |
deleted_task = jsonify({ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route('/add_task', methods=['POST']) | |
@auth.login_required | |
def add_task(): | |
content = request.form['content'] | |
task = Task(content=content, user=User.query.filter_by(username=auth.username()).first()) | |
db.session.add(task) | |
db.session.commit() | |
return jsonify({ | |
'username': auth.username(), | |
'task-id': task.id, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
curl -d "username=viveksb007&password=pass&email=viveksbhadauria007@gmail.com" -X POST http://127.0.0.1:5000/signup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route('/signup', methods=['POST']) | |
def signup(): | |
username = request.form['username'] | |
password = request.form['password'] | |
email = request.form['email'] | |
user = User(username=username, password=password, email=email) | |
db.session.add(user) | |
db.session.commit() | |
return jsonify({ | |
'response': 'User ' + username + ' created successfully' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@auth.get_password | |
def get_pw(username): | |
user = User.query.filter_by(username=username).first() | |
if user is not None: | |
return user.password | |
else: | |
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Task(db.Model): | |
id = db.Column(db.Integer, primary_key=True) | |
content = db.Column(db.Text) | |
add_date = db.Column(db.DateTime, default=datetime.datetime.now()) | |
end_date = db.Column(db.DateTime) | |
done = db.Column(db.Boolean, default=False) | |
user = db.relationship('User', backref='tasks', lazy=True) | |
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) | |
def __init__(self, content, user): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User(db.Model): | |
id = db.Column(db.Integer, primary_key=True) | |
username = db.Column(db.String(80), unique=True, nullable=False) | |
password = db.Column(db.String(20), nullable=False) | |
email = db.Column(db.String(100), unique=True, nullable=False) | |
def __init__(self, username, password, email): | |
self.username = username | |
self.password = password | |
self.email = email |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, jsonify | |
from flask_httpauth import HTTPBasicAuth | |
from flask_sqlalchemy import SQLAlchemy | |
import datetime | |
app = Flask(__name__) | |
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db' | |
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True | |
db = SQLAlchemy(app) | |
auth = HTTPBasicAuth() |