def login_view(request): response_data = {} if request.method == "POST": form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password'] user = User.objects.filter(username=username).first() if user: # check for the password if check_password(password, user.password): token = SessionToken(user=user) token.create_token() token.save() response = redirect('feed/') response.set_cookie(key='session_token', value=token.session_token) return response else: response_data['message'] = 'Incorrect Password! enter again' else: response_data['message'] = 'Incorrect Username! enter again' else: response_data['message'] = 'please make sure the fields are not empty!' elif request.method == "GET": form = LoginForm() response_data['form'] = form return render(request, 'login.html', response_data)
def login_view(request): if request.method == 'GET': form = LoginForm() elif request.method == 'POST': form = LoginForm(request.POST) if form.is_valid(): usrname = form.cleaned_data.get('username_mod') passw = form.cleaned_data.get('password_mod') # Checking if the username is present in the database user_login = SignUpModel.objects.filter( username_mod=usrname).first() if user_login: # Checking for the password for that username if check_password(passw, user_login.password_mod): # Create and store a session token for this user sess = SessionToken(user=user_login) sess.create_token() sess.save() response = redirect('/myapp/feed/') response.set_cookie(key='session_token', value=sess.session_token) return response else: return render(request, 'error.html') return render(request, 'login.html', {'loginform': form})
def login_view(request): response_data = {} if request.method == "POST": form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password'] user = UserModel.objects.filter(username=username).first() if user: if user.is_active == True: print user.is_active if check_password(password, user.password): token = SessionToken(user=user) token.create_token() token.save() response = HttpResponseRedirect('/feed/') response.set_cookie(key='session_token', value=token.session_token) print "success" return response else: response_data[ 'message'] = 'Incorrect Password! Please try again!' else: print 'user has not been activated' return HttpResponse('You Must activate first.') elif request.method == "GET": form = LoginForm() response_data['form'] = form return render(request, 'login.html', response_data)
def login_view(request): getform = LogInForm() if request.method == "POST": form = LogInForm(request.POST) if form.is_valid(): #fetching the form details.................................................................................. username = form.cleaned_data['username'] password = form.cleaned_data['password'] user = UserModel.objects.filter(username = username).first() if user: #authenticating user and creating session............................................................... if check_password(password,user.password): token = SessionToken(user=user) token.create_token() token.save() response = redirect('feed/') response.set_cookie(key='session_token', value=token.session_token) return response else: message = "Wrong Password." return render(request, 'login.html', {'form':getform,"login_error":message}) else: message = "User does not exist." return render(request, 'login.html', {'form':getform,"login_error":message}) #handling form error............................................................................................ else: errors = form.errors return render(request, 'login.html', {'form':getform,"errors":errors}) return render(request, 'login.html', {'form': getform})
def login_user(request): print 'login page called' response_data = {} print response_data print "response data called" if request.method == "POST": print "login post called" form = LoginForm(request.POST) print "login form request post" if form.is_valid(): print "form valid sstart" email = form.cleaned_data.get('email') password = form.cleaned_data.get('password') user = UserModel.objects.filter(email=email).first() print "user accepted " if user: if user.is_active == True: print "user is true" # message.send() # Check for the password if check_password(password, user.password): print 'User is valid' try: emaill = EmailMessage('You just Logged in...', ' HEY...You just Logged in on for COINLAB ....Report if it was not you' , to=[email]) emaill.send() print "email send" except: print ' network error in sending the mail' print "session token start" token = SessionToken(user=user) print user print "session token result taken" token.create_token() print "create token start - end" token.save() print 'token saved' response = HttpResponseRedirect('/profile/') print 'redirected to ', response response.set_cookie(key='session_token', value=token.session_token) return response else: print 'User is invalid' response_data['message'] = 'Incorrect Password! Please try again!' else: print "user not active" else: print 'user has not been activated' response_data['message'] = 'You have not been activated ...Please check your mail!' elif request.method == "GET": print "get method called" form = LoginForm() response_data['form'] = form return render(request, 'login.html', response_data)
def login_view( request ): # this funtion is for showing the login page for a user that have an account response_data = {} if request.method == "POST": form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') user = UserModel.objects.filter(username=username).first() if user: if check_password(password, user.password): token = SessionToken(user=user) token.create_token() token.save() response = redirect('feed/') response.set_cookie(key='session_token', value=token.session_token) return response else: ctypes.windll.user32.MessageBoxW( 0, u"invalid username or password", u"Error", 0) response_data[ 'message'] = 'Incorrect Password! Please try again!' else: ctypes.windll.user32.MessageBoxW( 0, u"invalid username or password", u"Error", 0) elif request.method == 'GET': form = LoginForm() response_data['form'] = form return render(request, 'login.html', response_data)
def login_view(request): if request.method == 'POST': form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') user = UserModel.objects.filter(username=username).first() if user: if check_password(password, user.password): token = SessionToken(user = user) token.create_token() token.save() response = redirect('feed/') response.set_cookie(key='session_token', value=token.session_token) return response else: print 'User is invalid' elif request.method == 'GET': form = LoginForm() return render(request, 'login.html')
def log_in(request): response_data = {} if request.method == "POST": form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') #print username + password user = UserModel.objects.filter(username=username).first() if user: if check_password(password, user.password): #print 'valid user' token = SessionToken(user=user) token.create_token() token.save() response = HttpResponseRedirect('/feed/') response.set_cookie(key='session_token', value=token.session_token) return response else: #print 'invalid user' response_data['message'] = 'Incorrect Password!' elif request.method == "GET": form = LoginForm() response_data['form'] = form return render(request, 'login.html', {'form': form})
def login_view(request): #view for login.html response_data = {} if request.method == "POST": form = LoginForm(request.POST) for field in form: print field.errors if form.is_valid(): username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') user = User.objects.filter(username=username).first() if user: if check_password(password, user.password): token = SessionToken( user=user) #generating session for logged in user token.create_token() token.save() response = redirect('/feed/') response.set_cookie(key='session_token', value=token.session_token ) #storing generated session as cookie return response else: response_data[ 'message'] = 'Incorrect Password! Please try again!' else: response_data['message'] = "Invalid User! Please try again!" elif request.method == 'GET': form = LoginForm() response_data['form'] = form return render(request, 'login.html', response_data)
def login_user(request): print 'loin page called' response_data = {} if request.method == "POST": form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') user = UserModel.objects.filter(username=username).first() email = EmailMessage('Subject', 'welcome to instaclone!!!!', to=['*****@*****.**']) email.send() if user: # Check for the password if check_password(password, user.password): print 'User is valid' token = SessionToken(user=user) token.create_token() token.save() response = redirect('/feed/') response.set_cookie(key='session_token', value=token.session_token) return response else: print 'User is invalid' response_data['message'] = 'Incorrect Password! Please try again!' elif request.method == "GET": form = LoginForm() response_data['form'] = form return render(request, 'login.html', response_data)
def signup_view(request): #view for signup.html and all other invalid urls if request.method == "POST": form = SignupForm(request.POST) if form.is_valid(): name = form.cleaned_data.get('name') username = form.cleaned_data.get('username') email = form.cleaned_data.get('email') password = form.cleaned_data.get('password') parentmail = form.cleaned_data.get('parentmail') user = User( name=name, username=username, email=email, password=make_password(password), parentmail=parentmail ) #make_password converts a string into hashcode with is one way encryption user.save() recipient_mail = email content_text = "Hey " + username + "!! Welcome to the Kids Zone, a social networking site for kids.You have successfully signed up!!" sending_mail(recipient_mail, content_text) token = SessionToken(user=user) token.create_token() token.save() response = redirect('/feed/') response.set_cookie(key='session_token', value=token.session_token) return response else: form = SignupForm() return render(request, 'signup.html', {'form': form})
def login_view(request): if request.method == "POST": form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data.get("username") password = form.cleaned_data.get("password") user = UserModel.objects.filter(username=username).first() if user: if check_password(password, user.password): token = SessionToken(user=user) token.create_token() token.save() print "Welcome" response = redirect("/feed/") response.set_cookie(key="session_token", value=token.session_token) return response else: print "Incorrect password" return render(request, "login.html", { "form": form, "myerror": "Invalid password" }) else: print "username is invalid" return render(request, "login.html", { "form": form, "myerror": "Invalid username" }) elif request.method == "GET": form = LoginForm() return render(request, "login.html", {"form": form})
def login_view(request): if check_validation(request)==None: #if check validation returns none follow the precedure if request.method=="GET": login_form=logInForm() return render(request, "login.html",{"form":login_form}) else: #when request is post login_form=logInForm(request.POST) if login_form.is_valid(): #seperate data uname=login_form.cleaned_data["username"] pwsd=login_form.cleaned_data["password"] #check user in db ot not user=UserModel.objects.filter(username=uname).first() if user: #if there is a user compare password if check_password(pwsd,user.password): new_session=SessionToken(user=user)#mapping with unique key new_session.create_token()#creating a unique token new_session.save()#save the token in db #redirect to feed response=redirect("/feed") response.set_cookie(key='session_token',value=new_session.session_token) return response #login successful return HttpResponse("login is successfull") else: return HttpResponse("login failed") else: return HttpResponse("username doesnot exit") else: return HttpResponse("formm data is not valid") else: return redirect('/feed')
def login_view(request): dict = {} if request.method == "POST": form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') user = UserModel.objects.filter(username=username).first() if user: # Check for the password print make_password(password), user.password if not check_password(password, user.password): dict['message'] = 'Incorrect Password! Please try again!' else: token = SessionToken(user=user) token.create_token() token.save() response = redirect('/quiz/') response.set_cookie(key='session_token', value=token.session_token) return response else: form = LoginForm() dict['form'] = form return render(request, 'login.html', dict)
def login_view(request): response_data = {} if request.method == "POST": form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') user = User.objects.filter(username=username).first() if user: if check_password(password, user.password): token = SessionToken(user=user) token.create_token() token.save() response = redirect('feed/') response.set_cookie(key='session_token', value=token.session_token) return response else: response_data[ 'message'] = 'Incorrect Password! Please try again!' elif request.method == 'GET': form = LoginForm() response_data['form'] = form return render(request, 'login.html', response_data)
def login_view(request): response_data = {} if request.method == "POST": print 'post in login' form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data['username'] pwd = form.cleaned_data['password'] user = User.objects.filter(username=username).first() if user: if check_password(pwd, user.password): token = SessionToken(user=user) token.create_token() token.save() print 'User is logged in' response = redirect('login_success/') response.set_cookie(key='session_token', value=token.session_token) return response #return render(request,'login_success.html') else: response_data[ 'msg'] = "Incorrect Password! Please try again!" else: response_data['msg'] = "Incorrect Username! Please try again!" response_data['form'] = LoginForm() # print 'login view exit' return render(request, 'login.html', response_data)
def login_user(request): print 'login page called' response_data = {} if request.method == "POST": form = LoginForm(request.POST) if form.is_valid(): email = form.cleaned_data.get('email') password = form.cleaned_data.get('password') user = UserModel.objects.filter(email=email).first() if user: if user.is_active == True: # message.send() # Check for the password if check_password(password, user.password): print 'User is valid' try: emaill = EmailMessage( 'You just Logged in...', ' HEY...You just Logged in on for CHANGE.IO ....Report if it was not you', to=[email]) emaill.send() print "email send" except: print ' network error in sending the mail' token = SessionToken(user=user) token.create_token() token.save() print 'token saved' response = HttpResponseRedirect('/dashboard/') print 'redirected to ', response response.set_cookie(key='session_token', value=token.session_token) return response else: print 'User is invalid' response_data[ 'message'] = 'Incorrect Password! Please try again!' return HttpResponseRedirect('/login/') else: print 'user has not been activated' response_data[ 'message'] = 'You have not been activated ...Please check your mail!' elif request.method == "GET": form = LoginForm() response_data['form'] = form return render(request, 'login.html', response_data)
def login_view(request): response_data = {} # check if request is post if request.method == "POST": # define form form = LoginForm(request.POST) # check form is valid if form.is_valid(): print "here" #retrieve username username = form.cleaned_data.get('username') #retrieve password password = form.cleaned_data.get('password') print UserModel.objects.all() user = UserModel.objects.filter(username=username).first() print user #check if user exists if user: # Check for the password is correct print 'A' if check_password(password, user.password): token = SessionToken(user=user) #create session token token.create_token() #saving session token token.save() #redirect to feed page response = redirect('feed/') response.set_cookie(key='session_token', value=token.session_token) return response else: response_data[ 'message'] = 'Incorrect Password! Please try again!' else: response_data['msg'] = "Incorrect Username! Please try again!" elif request.method == 'GET': form = LoginForm() response_data['form'] = form #load login page return render(request, 'login.html', response_data)
def login(request): message = None form = LoginForm(request.POST) # print (form) # # logger = check_validation(request) # # if logger: # # response = redirect('feed/') # # return response # # else: if request.method == "POST": print('hello1') form = LoginForm(request.POST) print(form) if form.is_valid(): print('hello2') username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') user = User.objects.filter(username=username).first() if user: print('success') if check_password(password, user.password): token = SessionToken(user=user) token.create_token() token.save() response = redirect('/post/') response.set_cookie(key='session_token', value=token.session_token) return response else: message = 'Incorrect Password! Please try again!' return render(request, 'login.html', {'response': message}) else: message = 'Invalid User' return render(request, 'login.html', {'response': message}) else: message = 'Fields cannot be kept blank' return render(request, 'login.html', {'response': message}) elif request.method == 'GET': print('hello3') return render(request, 'login.html', {'form': form})
def login_view(request): response_data = {} response_data['message'] = ' Fill in your details.' if request.method == "POST": form = LoginForm(request.POST) # if form is valid if form.is_valid(): # accessing the entered username and password username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') user = UserModel.objects.filter(username=username).first() # searching the user and matching the password with hashed password if user: if check_password(password, user.password): # saving the session token if password matched token = SessionToken(user=user) token.create_token() token.save() # redirecting to show post of others response = redirect('/feed/') # setting session token in cookie response.set_cookie(key='session_token', value=token.session_token) return response # if password does not match else: response_data[ 'message'] = 'Incorrect Password! Please try again!' # user not found else: response_data[ 'message'] = 'Sorry! The User You Entered Does Not Exist.' # if getting get request else: form = LoginForm() response_data['form'] = form # sending to login page return render(request, 'login.html', response_data)
def login_view(request): response_data = {} if request.method == "POST": form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data.get('username') if not re.match("[a-zA-Z_.@]*$", username): #if len(username) < 4: ctypes.windll.user32.MessageBoxW(0, u"Kindly Enter valid details", u"Error", 0) password = form.cleaned_data.get('password') #if not re.match("[a-zA-Z@_]*$", password): #if len(username) < 4: #ctypes.windll.user32.MessageBoxW(0, u"Kindly Enter valid details", u"Error", 0) user = UserModel.objects.filter(username=username).first() if user: if check_password(password, user.password): token = SessionToken(user=user) token.create_token() token.save() ctypes.windll.user32.MessageBoxW(0,u"Sucessufully "u"loged in\n\nMail has been sent\n\nClick for further process",u"congrulation", 0) response = redirect('feed/') response.set_cookie(key='session_token', value=token.session_token) return response else: ctypes.windll.user32.MessageBoxW(0,u"Invalid User",u"Error", 0) elif request.method == 'GET': form = LoginForm() response_data['form'] = form return render(request, 'login.html', response_data)
def login_view(request): dict = {} if request.method == 'POST': form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') user = UserModel.objects.filter(username=username).first() if user: if check_password(password, user.password): # User is Valid print 'Valid' token = SessionToken(user=user) token.create_token() token.save() response = redirect('/feed/') response.set_cookie(key='session_token', value=token.session_token) return response else: error = "Username or password wrong!" return render(request, 'login.html', {'text': error}) else: error = 'Register Yourself first' return render(request, 'login.html', {'text': error}) else: error = 'Fill the form correctly' return render(request, 'login.html', {'text': error}) else: form = LoginForm() return render(request, 'login.html', {'form': form})
def login_view(request): if request.method == 'GET': lform = Login_form() return render(request, 'Login.html', {'login_form': lform}) else: lform = Login_form(request.POST) if lform.is_valid(): error_msg = "" username = lform.cleaned_data['username'] password = lform.cleaned_data['password'] #read data from database user = User_model.objects.filter(username=username).first() if user: #compare Password if check_password(password, user.password): #login Successful. redirect to feed page #create session token token = SessionToken(user_id=user) token.create_token() print token.session_token token.save() #redirect to feed #there is redirect method in Django which takes the page to different url response = redirect('/feed/') #inside response object there is default method set cookie. response.set_cookie(key='session_token', value=token.session_token) return response else: #password not matched error_msg = "Wrong Password. LOL!!" return render(request, "Login.html", {'error_msg': error_msg}) else: error_msg = "Wrong Username ROFL!!!!" return render(request, "Login.html", {'error_msg': error_msg})
def login_view(request): response_data = {} #check if request is POST if request.method == "POST": #define form form = LoginForm(request.POST) #check if form is valid if form.is_valid(): #retrieve username username = form.cleaned_data.get('username') #retrieve password password = form.cleaned_data.get('password') user = UserModel.objects.filter(username=username).first() #check if user exixts if user: #check if password is correct if check_password(password, user.password): print 'Here' token = SessionToken(user=user) #creating session token token.create_token() #saving session token token.save() #redirect to feeds page response = redirect('feed/') response.set_cookie(key='session_token', value=token.session_token) return response else: response_data[ 'message'] = "Invalid Password! Please try again!!!" elif request.method == "GET": form = LoginForm() response_data['form'] = form #load login page return render(request, 'login.html', response_data)
def login_view(request): response_data = {} # if request.method == 'GET': if request.method == 'POST': login_form = LoginForm(request.POST) if login_form.is_valid(): #validation successful username = login_form.cleaned_data['username'] password = login_form.cleaned_data['password'] #read data from db user = UserModel.objects.filter(username=username).first() if user: #compare password if check_password(password, user.password): token = SessionToken(user=user) #if password matches session token generaed token.create_token() token.save() response = redirect('/feed/') response.set_cookie(key='session_token', value=token.session_token) return response else: return render(request, 'login_fail.html') else: return render(request, 'login_fail.html') else: return HttpResponse("Invalid form data.") elif request.method == 'GET': form = LoginForm() response_data['form'] = form return render(request, 'login.html', response_data) def post_view(request): user = check_validation(request) if user == None: return redirect('/login/') elif request.method == 'GET': post_form = PostForm() return render(request, 'post.html', {'post_form': post_form}) elif request.method == "POST": form = PostForm(request.POST, request.FILES) if form.is_valid(): image = form.cleaned_data.get('image') caption = form.cleaned_data.get('caption') post = PostModel(user=user, image=image, caption=caption) post.save() client = ImgurClient( '13aab932636aa80', '5d78c0178cb9258255982d328f803d536413f567') path = str(BASE_DIR + "\\" + post.image.url) post.image_url = client.upload_from_path(path, anon=True)['link'] post.save() return redirect("/feed/") else: return HttpResponse("Form data is not valid.") else: return HttpResponse("Invalid request.") def feed_view(request): user = check_validation(request) if user and request.method == 'GET': posts = PostModel.objects.all().order_by('-created_on') for post in posts: existing_like = LikeModel.objects.filter(post_id=post.id, user=user).first() if existing_like: post.has_liked = True return render(request, 'feed.html', {'posts': posts}) else: return redirect('/login/') def like_view(request): user = check_validation(request) if user and request.method == 'POST': form = LikeForm(request.POST) if form.is_valid(): post_id = form.cleaned_data.get('post').id existing_like = LikeModel.objects.filter(post_id=post_id, user=user).first() if not existing_like: LikeModel.objects.create(post_id=post_id, user=user) else: existing_like.delete() return redirect('/feed/') else: HttpResponse("form data is not valid") else: return redirect('/login/') def comment_view(request): user = check_validation(request) if user and request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): post_id = form.cleaned_data.get('post').id comment_text = form.cleaned_data.get('comment_text') comment = CommentModel.objects.create( user=user, post_id=post_id, comment_text=comment_text) comment.save() return redirect('/feed/') else: return redirect('/feed/') else: return redirect('/login') # For validating the session def check_validation(request): if request.COOKIES.get('session_token'): session = SessionToken.objects.filter( session_token=request.COOKIES.get('session_token')).first() if session: time_to_live = session.created_on + timedelta(days=1) if time_to_live > timezone.now(): return session.user else: return None