def profile(request): if request.session['authenticate'] == True: email = request.session['email'] userdata = MySiteUser.objects.get(user_email=email) if request.method == "POST": user_image = userdata.user_image if request.FILES: myfile = request.FILES['user_image'] fs = FileSystemStorage() filename = fs.save(myfile.name, myfile) fs.url(filename) user_image = myfile.name fname = (request.POST["user_fname"]).capitalize() lname = (request.POST["user_lname"]).capitalize() dob = request.POST["user_dob"] mob = request.POST["user_mobile"] image = user_image gender = request.POST["user_gender"] update = MySiteUser(user_email=email, user_fname=fname, user_lname=lname, user_dob=dob, user_mobile=mob, user_image=image, user_gender=gender) update.save(update_fields=[ "user_fname", "user_gender", "user_mobile", "user_dob", "user_lname", "user_image" ]) request.session['image'] = image request.session['name'] = fname + " " + lname return redirect("/profileupdate") return render(request, "profileupdate.html", {"su": userdata}) else: return redirect("/login")
def login(request): if (request.method == "POST"): try: useremail = request.POST['user_email'] userpassword = request.POST['user_password'] userdata = MySiteUser.objects.get(user_email=useremail) verified = userdata.user_isverified dp = userdata.user_password mob = str(userdata.user_mobile) authtoken = userdata.user_token get_id = userdata.site_role_id_id if check_password(userpassword, dp): if verified == False and authtoken == "": rn = random.randint(100000, 10000000) token = useremail[0:5] + str(rn) + mob[5:10] verify = "http://127.0.0.1:8000/dskjgheriugiurefhkusdjdowieuqhiurehf?email=" + useremail + "&token=" + token email_send(useremail, userpassword, verify) update = MySiteUser(user_email=useremail, user_token=token) update.save(update_fields=["user_token"]) return render(request, "login.html", {'login': True, "vl": True}) elif verified == True: request.session['authenticate'] = True request.session['email'] = useremail request.session['role_id'] = get_id request.session['name']=userdata.user_fname+" "+userdata.user_lname request.session['rolename']=userdata.site_role_id.role_name request.session["image"]=userdata.user_image form = LoginDetailsForm(request.POST) if form.is_valid(): f1 = form.save(commit=False) f1.user_email = request.session['email'] f1.login_time = dt.datetime.now().strftime("%H:%M:%S") f1.save() if get_id == 1: return redirect("/") if get_id == 4: return redirect("/") if get_id == 3: return redirect("/") if get_id == 2: return redirect("/superuser") else: return render(request, "login.html", {'login': True, "vf": True}) else: return render(request, "login.html", {"pass2": True, 'login': True}, ) except: return render(request, "login.html", {"pass1": True, 'login': True}) return render(request, "login.html", {'login': True})
def verify(request): try: email = request.GET['email'] token = request.GET['token'] userdata = MySiteUser.objects.get(user_email=email) verified = userdata.user_isverified if verified == True: return render(request, "verify.html", {"valid": True}) else: dbtoken = userdata.user_token if (dbtoken == token): verified = True update = MySiteUser(user_email=email, user_isverified=verified, user_token="") update.save(update_fields=["user_isverified", "user_token"]) return render(request, "verify.html", {"good": True}) else: return render(request, "404.html", {"nv": True}) except: return render(request, "verify.html", {"nv": True})
def forgototp(request): try: if request.session['authenticate'] == False: pass else: return redirect("/error") except: return redirect("/error") if request.method == "POST": try: email = request.POST['user_email'] userdata = MySiteUser.objects.get(user_email=email) timedb = userdata.otp_time_generation otp_gen = request.POST["otp_gen"] if email == userdata.user_email and otp_gen != "": otp = userdata.otp get_time = dt.datetime.strptime(timedb, '%Y-%m-%d %H:%M:%S.%f') curr_time = dt.datetime.now() interval = curr_time - get_time interval = float(interval.total_seconds()) limit = float(14400) if otp == otp_gen and interval < limit: pwd = make_password(request.POST["new_password"]) update = MySiteUser(user_email=email, user_password=pwd, otp="", otp_time_generation="") update.save(update_fields=[ "user_password", "otp", "otp_time_generation" ]) otp_send(email, request.POST["new_password"], "Password Changed", "Updated Password", "Password") return redirect("/confirmation/?id=forgotpwd") else: return render( request, "login.html", { 'ud': userdata, "otp_gen": True, 'em': email, "valid": True }) elif email == userdata.user_email and otp_gen == "": # timedb = userdata.otp_time_generation if timedb == "": otp, time = otp_generation.otpgenerate() otp_send(email, otp, "Forget Password", "Recover Password", "OTP") update = MySiteUser(user_email=email, otp_time_generation=time, otp=otp) update.save(update_fields=["otp", "otp_time_generation"]) else: get_time = dt.datetime.strptime(timedb, '%Y-%m-%d %H:%M:%S.%f') curr_time = dt.datetime.now() interval = curr_time - get_time interval = float(interval.total_seconds()) limit = float(14400) if interval > limit: otp, time = otp_generation.otpgenerate() update = MySiteUser(user_email=email, otp_time_generation=time, otp=otp) update.save( update_fields=["otp", "otp_time_generation"]) otp_send(email, otp, "Forget Password", "Recover Password", "OTP") return render(request, "login.html", { 'ud': userdata, "otp_gen": True, 'em': email, "sent": True }) else: return render(request, "login.html", { 'ud': userdata, "otp_gen": True, 'em': email, "not_sent": True }) except: return render(request, "login.html", { "fp": True, "not_sent": True }) return render(request, "login.html", {'fp': True})
def updatepassword(request): if request.session['authenticate'] == True: email = request.session['email'] userdata = MySiteUser.objects.get(user_email=email) pw = userdata.user_password timedb = userdata.otp_time_generation if request.method == "POST": get_time = dt.datetime.strptime(timedb, '%Y-%m-%d %H:%M:%S.%f') curr_time = dt.datetime.now() interval = curr_time - get_time interval = float(interval.total_seconds()) limit = float(14400) get_otp = userdata.otp pw = userdata.user_password current_password = request.POST["password"] curr_otp = request.POST['curr_otp'] if check_password(current_password, pw) and get_otp == curr_otp and interval < limit: new_password = make_password(request.POST['new_password']) otp = "" otpgrn = "" update = MySiteUser(user_email=email, user_password=new_password, otp=otp, otp_time_generation=otpgrn) update.save(update_fields=[ "user_password", "otp", "otp_time_generation" ]) return redirect("/confirmation/?id=changepwd") elif get_otp != curr_otp: return render(request, "passwordupdate.html", { 'invalidotp': True, }) elif pw != current_password: return render(request, "passwordupdate.html", { 'invalidpwd': True, }) elif interval > limit: return render(request, "passwordupdate.html", { 'invalidtime': True, }) else: if timedb == "": otp, time = otp_generation.otpgenerate() otp_send(email, otp, "Change Password", "Change Password", "OTP") update = MySiteUser(user_email=email, otp_time_generation=time, otp=otp) update.save(update_fields=["otp", "otp_time_generation"]) else: get_time = dt.datetime.strptime(timedb, '%Y-%m-%d %H:%M:%S.%f') curr_time = dt.datetime.now() interval = curr_time - get_time interval = float(interval.total_seconds()) limit = float(14400) if interval > limit: otp, time = otp_generation.otpgenerate() otp_send(email, otp, "Change Password", "Change Password", "OTP") update = MySiteUser(user_email=email, otp_time_generation=time, otp=otp) update.save(update_fields=["otp", "otp_time_generation"]) else: return redirect("/login") return render(request, "passwordupdate.html")