def signup(request):
    signupobj = MySite_UserForm()
    if (request.method == "POST"):
        enter_password = request.POST['user_password']
        conf_password = request.POST['confirm_password']
        if (enter_password == conf_password):
            password = make_password(conf_password)
        else:
            return render(request, "signup.html", {'wp': True})
        f = signupobj.save(commit=False)
        f.user_name = request.POST['user_name']
        f.user_email = request.POST['user_email']
        f.user_mobile = request.POST['user_mobile']
        f.user_password = password
        f.role_id_id = request.POST['role_id']
        f.user_sign_up = datetime.now()
        rn = random.randint(100000, 10000000)
        token = request.POST['user_email'][0:5] + str(rn) + str(
            request.POST['user_mobile'][5:10])
        f.user_token = token
        verify = "http://127.0.0.1:8000/clicktoverifyyouraccount?email=" + request.POST[
            'user_email'] + "&token=" + token
        os.email(f.user_email, "verification link", verify)
        f.save()
        return render(request, "home.html", {'sign_up': True})
    return render(request, "signup.html")
def change_password(request):
    session_mail = request.session['emailid']
    data = MySite_User.objects.get(user_email=session_mail)
    get_id = data.role_id_id
    try:
        auth = au.authorizeuser(request.session["authenticated"],
                                request.session["roleid"], get_id)
    except:
        return HttpResponse("<h1>You are not login</h1>")
    if auth == True:
        if (request.method == "POST"):
            opassword = request.POST['old_password']
            npassword = request.POST['new_password']
            confpass = request.POST['confirm_password']
            new_otp = request.POST['otp']
            otp_db = data.otp
            if (otp_db == new_otp):
                if (npassword == confpass):
                    dbpass = data.user_password
                    auth_pass = check_password(opassword, dbpass)
                    if (auth_pass == True):
                        updatedata = MySite_User(
                            user_email=session_mail,
                            user_password=make_password(npassword))
                        updatedata.save(update_fields=["user_password"])
                        try:
                            os.email(session_mail, "confirmation mail",
                                     "password changed successfully")
                            return redirect("/")
                        except:
                            return render(request, "change_password.html",
                                          {'email': True})
                    else:
                        return render(request, "change_password.html",
                                      {'op': True})
                else:
                    return render(request, "change_password.html",
                                  {'password': True})
            else:
                return render(request, "change_password.html", {'w_otp': True})
        else:
            otp_gen, otptime = og.otpgenerate()
            updatedata = MySite_User(user_email=session_mail,
                                     otp=otp_gen,
                                     otp_time=otptime)
            updatedata.save(update_fields=["otp", "otp_time"])
            os.otpsend(otp_gen, session_mail, "new Otp", "do not share")
            return render(request, "change_password.html")
    else:
        aut, message = auth
        if (message == "Wrong user Type"):
            return HttpResponse("<h1>You are a wrong user</h1>")
        elif (message == "Not login"):
            return HttpResponse("<h1>You are not login</h1>")
def forgot_password(request):
    if request.method == "POST":
        email = request.POST["user_email"]
        entered_otp = request.POST["enter_otp"]
        try:
            userdata = MySite_User.objects.get(user_email=email)
            if (email != "" and entered_otp == ""):
                otp_gen, otptime = og.otpgenerate()
                updatedata = MySite_User(user_email=email,
                                         otp=otp_gen,
                                         otp_time=otptime)
                updatedata.save(update_fields=["otp", "otp_time"])
                os.otpsend(otp_gen, email, "new Otp", "do not share")
                return render(request, "forgot_password.html", {
                    "otp_gen": True,
                    "os": True,
                    'em': email
                })
            elif (email != "" and entered_otp != ""):
                db_otp = userdata.otp
                new_password = request.POST["new_password"]
                conf_password = request.POST["confirm_password"]
                if (db_otp == entered_otp):
                    if (new_password == conf_password):
                        updatetable = MySite_User(
                            user_email=email,
                            user_password=make_password(conf_password))
                        updatetable.save(update_fields=["user_password"])
                        os.email(email, "password changed",
                                 "your password changed successfully")
                        return render(request, "home.html", {"pu": True})
                    else:
                        return render(request, "forgot_password.html", {
                            "wrong": True,
                            "otp_gen": True,
                            'em': email
                        })
                else:
                    return render(request, "forgot_password.html", {
                        "wrong_otp": True,
                        "otp_gen": True,
                        'em': email
                    })
        except:
            return render(request, "forgot_password.html", {
                "not_sent": True,
                "cp": True
            })
    return render(request, "forgot_password.html", {'cp': True})
def contact(request):
    contcatobj = ContactTableForm()
    if (request.method == "POST"):

        f = contcatobj.save(commit=False)
        f.contact_name = request.POST['name']
        f.contact_email = request.POST['email']
        f.contact_subject = request.POST['subject']
        f.contact_message = request.POST['message']
        f.contact_time = datetime.now()
        f.save()

        os.email("*****@*****.**", "query", f.contact_message)
        return render(request, "contact.html", {'query': True})
    return render(request, "contact.html")
def index(request):

    if (request.method == "POST"):
        useremail = request.POST['user_email']
        userpassword = request.POST['user_password']
        try:
            userdata = MySite_User.objects.get(user_email=useremail)
            dp = userdata.user_password
            uname = userdata.user_name
            mob = str(userdata.user_mobile)
            verified = userdata.is_verified
            authtoken = userdata.user_token
            auth_pass = check_password(userpassword, dp)
            if (auth_pass == True):
                if verified == False:
                    if authtoken == "":
                        rn = random.randint(100000, 10000000)
                        token = useremail[0:5] + str(rn) + mob[5:10]
                        verify = "http://127.0.0.1:8000/clicktoverifyyouraccount?email=" + useremail + "&token=" + token
                        os.email(useremail, "verification link", verify)
                        update = MySite_User(user_email=useremail,
                                             user_token=token)
                        update.save(update_fields=["user_token"])
                        return render(request, "home.html", {'ver': True})
                    return render(request, "home.html", {'login': True})
                elif verified == True:
                    userroleid = userdata.role_id_id
                    request.session['authenticated'] = True
                    request.session['emailid'] = useremail
                    request.session['roleid'] = userroleid
                    request.session['name'] = uname
                    email = request.session['emailid']
                    log_obj = Login_detailsForm(request.POST)
                    if log_obj.is_valid():
                        f = log_obj.save(commit=False)
                        f.user_name = email
                        f.login_time = str(datetime.now())
                        f.save()
                    log_id_data = Login_details.objects.filter(
                        user_name=email).order_by('-login_id')[0:1]
                    lgid = 0
                    for i in log_id_data:
                        lgid = i.login_id
                    request.session['login_id'] = lgid
                    if (userroleid == 1):
                        return redirect("/admin_panel/admin_index/")
                    elif (userroleid == 2):
                        return redirect("/nutritionist/nutri_index/")
                    elif (userroleid == 3):
                        return redirect("/")
                    elif (userroleid == 4):
                        return redirect("/fitness_panel/index_fitness/")
            else:
                return render(request, "home.html", {'wrong_pass': True})
        except:
            return render(request, "home.html", {'wrong_user': True})
    recipe = recipes.objects.all()[0:4]
    exercise = AddExercise.objects.all()[0:3]
    return render(request, "home.html", {
        'recipe': recipe,
        'exercise': exercise
    })