# Path: User/models.py
# def start_user_session(request, user_id, user_class):
# 	if request.session is not None:
# 		request.session["user_mail_id"] = user_id
# 		request.session["user_class"] = user_class
# 	return request
# 
# def check_if_auth_user(request):
# 	if (request.session is not None) and request.session.has_key("user_mail_id"):
# 		return request.session["user_mail_id"]
# 	else:
# 		return None
# 
# def stop_user_session(request):
# 	if (request.session is not None) and request.session.has_key("user_mail_id"):
# 		del request.session["user_mail_id"]
# 		del request.session["user_class"]
# 		return True
# 	return False


from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponse
from django.contrib import messages
from django.db import connection, transaction
from passlib.hash import bcrypt
from .models import start_user_session, check_if_auth_user, stop_user_session
from collections import namedtuple
import datetime
import re
	current_user = result[0]

	context_data = {
		'user': current_user,
	}

	return render(request, "profile.html", context_data)



def check_login(request):
	if check_if_auth_user(request):
		messages.error(request, "Log out to perform Log in")
		return redirect("home:welcome")

	temp_id = request.POST.get("email")
	temp_pwd = request.POST.get("passwd")
	
	context_data = {}
	
	if temp_pwd and temp_id:
		cursor = connection.cursor()
		query = "SELECT user_id, user_pwd, auto_id FROM User_expert"
		cursor.execute(query)
		result = namedtuplefetchall(cursor)

		for person in result:
			if person.user_id == temp_id:
				if bcrypt.verify(temp_pwd, person.user_pwd):
					messages.success(request, "Successful Login")

# Path: User/models.py
# def start_user_session(request, user_id, user_class):
# 	if request.session is not None:
# 		request.session["user_mail_id"] = user_id
# 		request.session["user_class"] = user_class
# 	return request
# 
# def check_if_auth_user(request):
# 	if (request.session is not None) and request.session.has_key("user_mail_id"):
# 		return request.session["user_mail_id"]
# 	else:
# 		return None
# 
# def stop_user_session(request):
# 	if (request.session is not None) and request.session.has_key("user_mail_id"):
# 		del request.session["user_mail_id"]
# 		del request.session["user_class"]
# 		return True
# 	return False


# Path: User/views.py
from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponse
from django.contrib import messages
from django.db import connection, transaction
from passlib.hash import bcrypt
from .models import start_user_session, check_if_auth_user, stop_user_session
from collections import namedtuple
import datetime
import re

	current_user = result[0]

	context_data = {
		'user': current_user,
	}

	return render(request, "profile.html", context_data)



def check_login(request):
	if check_if_auth_user(request):
		messages.error(request, "Log out to perform Log in")
		return redirect("home:welcome")

	temp_id = request.POST.get("email")
	temp_pwd = request.POST.get("passwd")
	
	context_data = {}
	
	if temp_pwd and temp_id:
		cursor = connection.cursor()
		query = "SELECT user_id, user_pwd, auto_id FROM User_expert"
		cursor.execute(query)
		result = namedtuplefetchall(cursor)

		for person in result:
			if person.user_id == temp_id:
				if bcrypt.verify(temp_pwd, person.user_pwd):
					messages.success(request, "Successful Login")

					request = start_user_session(request, temp_id, 'E') # 'E' = Expert