Esempio n. 1
0
def login(response):
    if response.method == "POST":
        form = LogInForm(response.POST)
        if form.is_valid():
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            data_email = None
            data_password = None
            data = database(
                'db.sqlite3', "SELECT email, password FROM User WHERE email = ? and password = ?;", (email, password))
            if data != None:
                data_email = data[0]
                data_password = data[1]
            if data_email == email and data_password == password:
                database(
                    'db.sqlite3', "UPDATE User SET logged_in = True WHERE email = ? and password = ?;", (email, password))
                return redirect('authentication:profile')
            else:
                if data == None:
                    return redirect('authentication:register')
                elif password != data_password:
                    context_vars = {'form': form, 'heading': 'The passwords do not match.',
                                    'method': 'post', 'action': '/register/login/', 'val': 'Login'}
                    return render(response, 'authentication/login.html', context_vars)
    else:
        form = LogInForm()
        context_vars = {'form': form, 'heading': 'Please sign in to your account here.',
                        'action': '/register/login/', 'method': 'post', 'val': 'Login'}
        return render(response, 'authentication/login.html', context_vars)
Esempio n. 2
0
class TestLogInForm(TestCase):
    """To test the login form"""
    data = {
        'username': '******',
        'password': '******',
    }

    def test_if_invalid_email(self):
        """To test when an incorrect email is given"""
        self.new_data = self.data.copy()
        self.new_data['username'] = '******'
        self.form = LogInForm(self.new_data)
        self.assertFalse(self.form.is_valid())

    def test_if_invalid_password(self):
        """To test when an incorrect password is given"""
        self.new_data = self.data.copy()
        self.new_data['password'] = '******'
        self.form = LogInForm(self.new_data)
        self.assertFalse(self.form.is_valid())

    def test_when_missing_required_fields(self):
        """To check the response when a required fields is missing"""
        for field in ('username', 'password'):
            with self.subTest(field=field):
                my_data = self.data.copy()
                my_data[field] = ''
                form = LogInForm(my_data)
                self.assertFalse(form.is_valid())
Esempio n. 3
0
def login_view(request):
    if request.method == 'POST':
        print ("Login form validating.")
        form = LogInForm(request.POST)
        if not form.is_valid():
            print ("Login form is not valid.")
            return render(request, 'auth/login.html',
                      {'form': LogInForm()})

        else:
            # user = form.save(commit=False)
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            user = authenticate(username=username, password=password)
            if user is not None:
                if user.is_active:
                    print(str(user.profile.account_type)+" Logged in.")
                    login(request,user)
                    return redirect_user(request, user)

            else:
                messages.add_message(request,
                                     messages.ERROR,
                                     "Please enter valid username & password.")


    print ("Rendering get form")
    return render(request, 'auth/login.html',
                      {'form': LogInForm()})
Esempio n. 4
0
 def test_when_missing_required_fields(self):
     """To check the response when a required fields is missing"""
     for field in ('username', 'password'):
         with self.subTest(field=field):
             my_data = self.data.copy()
             my_data[field] = ''
             form = LogInForm(my_data)
             self.assertFalse(form.is_valid())
Esempio n. 5
0
def login_view(request):
    form = LogInForm()
    if request.method == "POST":
        form = LogInForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            username = data.get("username")
            password = data.get("password")
            user = authenticate(request, username=username, password=password)

            if user is not None:
                login(request, user)
                return HttpResponseRedirect(reverse("home"))
    return render(request, "login.html", {"form": form})
Esempio n. 6
0
 def test_if_invalid_password(self):
     """To test when an incorrect password is given"""
     self.new_data = self.data.copy()
     self.new_data['password'] = '******'
     self.form = LogInForm(self.new_data)
     self.assertFalse(self.form.is_valid())
Esempio n. 7
0
 def test_if_invalid_email(self):
     """To test when an incorrect email is given"""
     self.new_data = self.data.copy()
     self.new_data['username'] = '******'
     self.form = LogInForm(self.new_data)
     self.assertFalse(self.form.is_valid())