Beispiel #1
0
 def create_alive_token(self):
     session_token = SessionToken(user=self.user.key,
                                  expires_on=datetime.now() +
                                  self.expires_on,
                                  status=SessionStatus.ACTIVE)
     session_token.put()
     return session_token
 def create_alive_token(self):
     session_token = SessionToken(
         user=self.user.key,
         expires_on=datetime.now() + self.expires_on,
         status=SessionStatus.ACTIVE
     )
     session_token.put()
     return session_token
Beispiel #3
0
    def post(self):
        # create a new session
        sessionToken = SessionToken()
        sessionToken.username = request.json.get('username')

        flask_session['username'] = sessionToken.username

        return sessionToken.to_json()
Beispiel #4
0
    def get(self, sessionid):
        if sessionid is None:
            abort(401)

        username = flask_session.get('username', None)

        sessionToken = SessionToken()
        sessionToken.username = username

        return sessionToken.to_json()
Beispiel #5
0
    def get_alive_token(self):
        token = SessionToken.query(
            ndb.AND(SessionToken.user == self.user.key,
                    SessionToken.status == SessionStatus.ACTIVE)).get()

        if not token or token.expires_on < datetime.now():
            return None
        return token
    def get_alive_token(self):
        token = SessionToken.query(ndb.AND(
            SessionToken.user == self.user.key,
            SessionToken.status == SessionStatus.ACTIVE
        )).get()

        if not token or token.expires_on < datetime.now():
            return None
        return token
Beispiel #7
0
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)
Beispiel #8
0
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)
Beispiel #9
0
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)
Beispiel #10
0
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)
Beispiel #11
0
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})
Beispiel #12
0
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)
Beispiel #13
0
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')
Beispiel #14
0
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})
Beispiel #15
0
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})
Beispiel #16
0
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_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)
Beispiel #18
0
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})
Beispiel #19
0
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)
Beispiel #20
0
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})
Beispiel #21
0
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')
Beispiel #22
0
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)
Beispiel #23
0
    def check_authentication(self, *args, **kwargs):
        try:
            session_token = read_token(self)

            if not ValidateSessionTokenCommand(session_token=session_token).execute():
                raise AuthenticationException("Invalid Session Token provided")

            # Totally authenticated and permitted
            self.user = SessionToken.get_user_from_session_token(
                session_token_id=session_token
            )

        except AuthenticationException, e:
            self.abort(401)
Beispiel #24
0
    def execute(self):
        """
            Will check validity of a token according to:
            1 - token.status
            2 - token.expiration_date
        """

        if not self.session_token:
            return False

        session_token_entity = SessionToken.get_by_unique_id(self.session_token)
        if not session_token_entity:
            return False
        if session_token_entity.expires_on <= datetime.now():
            return False
        if session_token_entity.status != SessionStatus.ACTIVE:
            return False
        return True
Beispiel #25
0
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)
Beispiel #26
0
        def check_permissions(self, *args, **kwargs):

            try:
                invite_id = read_parameter_from_request('invite_id', self, kwargs=kwargs,safe=False)
                invite = Invite.get_by_unique_id(invite_id)
                if not invite:
                    raise Exception(
                        "This is not a valid Event. "
                        "Please check the url you're trying to access"
                    )

                invite_attendee_id = None
                if InvitePermission.Attendee in permissions or InvitePermission.Organizer in permissions:
                    invite_attendee_id = read_parameter_from_request('invite_attendee_id', self, kwargs)

                current_user = None

                session_token = read_token(self)

                if session_token and ValidateSessionTokenCommand(
                    session_token=session_token
                ).execute():
                    current_user = SessionToken.get_user_from_session_token(
                        session_token_id=session_token
                    )

                if not ValidateInvitePermissionsCommand(
                    invite,
                    current_user=current_user,
                    invite_attendee_id=invite_attendee_id,
                    permissions=permissions
                ).execute():
                    raise AuthenticationException("You dont have the permissions to modify the current Event")

                self.user = current_user
            except AuthenticationException, e:
                self.abort(401)
                raise e
Beispiel #27
0
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)
Beispiel #28
0
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)
Beispiel #29
0
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})
Beispiel #30
0
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)
Beispiel #31
0
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})
Beispiel #32
0
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})
Beispiel #33
0
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)
Beispiel #34
0
 def expire_all_user_tokens(self):
     user_tokens = SessionToken.all_user_tokens(self.user)
     for x in user_tokens:
         x.key.delete()
 def expire_all_user_tokens(self):
     user_tokens = SessionToken.all_user_tokens(self.user)
     for x in user_tokens:
         x.key.delete()