<commit_before>from flask import Flask, jsonify, render_template, url_for, request
from flask_login import UserMixin, LoginManager, login_required, login_user, logout_user, current_user
from flask_mongoengine import MongoEngine, DoesNotExist
from settings import db,app
from models import User
import bcrypt

# now get all the models from models.py ( this can be inside app.py)
# am just making it in models.py for readbility

login_manager = LoginManager()
login_manager.init_app(app)


@login_manager.user_loader
def load_user(user):
	return User.objects.get(id=user)

# ------------------------------------------------------------------------------------
# 										Flask Login ends
# ------------------------------------------------------------------------------------
@app.route("/")
def index():
	try:
		user = User.objects.get(id=current_user.id)
		return render_template('index.html', username = current_user.username)
	except (AttributeError, DoesNotExist):
		return render_template('index.html', username = False)

@app.route("/login", methods=['POST'])
def login():
	params = request.get_json()
	print params
	try:
		user = User.objects.get(username=params['username'])
		if user.validate_login(user.password, params['password']):
			user_obj = User.objects.get(id=user.id)
			login_user(user_obj)
			return jsonify({'status': True})
	except DoesNotExist:
		return jsonify({'status': False})
	return jsonify({'status': False})

@app.route("/signup", methods=['POST'])
def signup():
	params = request.get_json()
	print params
	try:
		print 'try'
		data = User.objects.get(username=params['username'])
		return jsonify({'status': False})
	except DoesNotExist:
		print 'except'
		hashed_pass = bcrypt.hashpw(str(params['password']), bcrypt.gensalt())
		user_obj = User(username=params['username'])
		user_obj.set_password(params['password'])
		user_obj.save()
	return jsonify({'status':True})

@app.route("/logout", methods=['POST'])
def logout():
	logout_user()
	return jsonify({'status':True})

# ------------------------------------------------------------------------------------------------
# 										TASK CRUDs
# ------------------------------------------------------------------------------------------------
# CREATE
@app.route("/new", methods=['POST'])
@login_required
def new_task():
	data = request.get_json()
	# Add new task for the user
	return jsonify({'status':True})

# UPDATE<commit_msg>Add basic new task function<commit_after>from flask import Flask, jsonify, render_template, url_for, request
from flask_login import UserMixin, LoginManager, login_required, login_user, logout_user, current_user
from flask_mongoengine import MongoEngine, DoesNotExist
from settings import db,app
from models import User, Task
import bcrypt
from uuid import uuid4

# now get all the models from models.py ( this can be inside app.py)
# am just making it in models.py for readbility

login_manager = LoginManager()
login_manager.init_app(app)


@login_manager.user_loader
def load_user(user):
	return User.objects.get(id=user)

# ------------------------------------------------------------------------------------
# 										Flask Login ends
# ------------------------------------------------------------------------------------
@app.route("/")
def index():
	try:
		user = User.objects.get(id=current_user.id)
		return render_template('index.html', username = current_user.username)
	except (AttributeError, DoesNotExist):
		return render_template('index.html', username = False)

@app.route("/login", methods=['POST'])
def login():
	params = request.get_json()
	print params
	try:
		user = User.objects.get(username=params['username'])
		if user.validate_login(user.password, params['password']):
			user_obj = User.objects.get(id=user.id)
			login_user(user_obj)
			return jsonify({'status': True})
	except DoesNotExist:
		return jsonify({'status': False})
	return jsonify({'status': False})

@app.route("/signup", methods=['POST'])
def signup():
	params = request.get_json()
	print params
	try:
		print 'try'
		data = User.objects.get(username=params['username'])
		return jsonify({'status': False})
	except DoesNotExist:
		print 'except'
		hashed_pass = bcrypt.hashpw(str(params['password']), bcrypt.gensalt())
		user_obj = User(username=params['username'])
		user_obj.set_password(params['password'])
		user_obj.save()
	return jsonify({'status':True})

@app.route("/logout", methods=['POST'])
def logout():
	logout_user()
	return jsonify({'status':True})

# ------------------------------------------------------------------------------------------------
# 										TASK CRUDs
# ------------------------------------------------------------------------------------------------
# CREATE
@app.route("/new", methods=['POST'])
@login_required
def new_task():
	data = request.get_json()
	# Add new task for the user
	print data
	try:
		task_obj = Task()
		task_obj.user = User.objects.get(id=current_user.id)
		task_obj.task_title = data['task_title']
		task_obj.task_content = data['task_content']
		task_obj.task_id = str(uuid4())
		task_obj.save()

		# Now get all task for this user and return it
		all_tasks = Task.objects(user = current_user.id)
		his_tasks = []
		for task in all_tasks:
			his_tasks.append({
				'task_title':task.task_title,
				'task_content':task.task_content,
				'task_id':task.task_id
				})
		return jsonify({'status':True,'tasks':his_tasks})
	except Exception as e:
		print e
		return jsonify({'status':False})

# UPDATE
{'fraction_non_alphanumeric': 0.25169197000182914, 'fraction_numerical': 0.0003658313517468447, 'mean_word_length': 2.707118644067797, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5404914', 'n_tokens_mistral': 1708, 'n_tokens_neox': 1597, 'n_words': 378}