Commit 8b4b7445 authored by ViolanteCodes's avatar ViolanteCodes
Browse files

started process of breaking forms apart, need to add form_valid tests before continuing

parent 6c959822
......@@ -2,9 +2,10 @@ from django import forms
from django.forms import ModelForm, modelformset_factory, BaseModelFormSet
from django.conf import settings
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.urls import reverse
import gitlab
import random
from django.core.exceptions import ValidationError
from .models import UserIdentifier, Project, Issue, Note, GitlabAccountRequest
# Initialize GitLab Object
......@@ -12,12 +13,13 @@ gl = gitlab.Gitlab(settings.GITLAB_URL, private_token=settings.GITLAB_SECRET_TOK
class LoginForm(forms.Form):
"""A form that allows users to enter in their keycodes to login."""
word_1 = forms.CharField(max_length=9)
word_2 = forms.CharField(max_length=9)
word_3 = forms.CharField(max_length=9)
word_4 = forms.CharField(max_length=9)
word_5 = forms.CharField(max_length=9)
word_6 = forms.CharField(max_length=9)
word_1 = forms.CharField(max_length=9, required=False)
word_2 = forms.CharField(max_length=9, required=False)
word_3 = forms.CharField(max_length=9, required=False)
word_4 = forms.CharField(max_length=9, required=False)
word_5 = forms.CharField(max_length=9, required=False)
word_6 = forms.CharField(max_length=9, required=False)
login_string = forms.CharField(max_length=255, required=False)
def join_words(self):
"""Pull cleaned data from form and join into code_phrase"""
......@@ -31,6 +33,54 @@ class LoginForm(forms.Form):
join_key = '-'
code_phrase = join_key.join(word_list)
return code_phrase
def build_code_phrase(self, cleaned_word_data=[]):
"""Join form cleaned data into code_phrase"""
join_key = '-'
code_phrase = join_key.join(cleaned_word_data)
return code_phrase
def clean(self):
"""Custom clean method for form"""
# Call the parent method
cleaned_data = super().clean()
# Set two flags for strings and all words filled.
string_filled = False
all_words_filled = True
# Grab the login string field
if cleaned_data.get('login_string') != '':
login_string = cleaned_data.get('login_string')
string_filled = True
# Now get the individual word login fields - first set up a list called word_counter
word_counter = ['word_1', 'word_2', 'word_3', 'word_4', 'word_5', 'word_6']
# iterate through the list and save the values to a new list to pass to build code phrase function.
cleaned_word_data = []
for word in word_counter:
value = cleaned_data.get(word)
print('testing: cleaned_word_data')
# if any of the words are left blank, flip the flag for all_words_filled.
if value == '':
all_words_filled = False
if string_filled == True:
if all_words_filled == True:
raise ValidationError(
"""It looks like you've filled out both the login words and the login string/phrase fields.
Please choose one or the other.""")
if all_words_filled == False:
if string_filled == False:
if all_words_filled == False:
raise ValidationError(
"""Make sure to fill out either all words of your user identifier above, or paste the string
version of your user identifier below.""")
user_identifier = self.build_code_phrase(cleaned_word_data=cleaned_word_data)
self.cleaned_data['user_identifier'] = user_identifier
# Forms relating to User Objects:
......@@ -300,9 +300,10 @@ def login_view(request):
# if the LoginForm is filled out, clean data and join the words together
# using the forms join_words function (defined in the form.)
if form.is_valid():
results = form.join_words()
user_identifier = form.cleaned_data['user_identifier']
# redirect to user-landing view, passing results dictionary as kwarg
return redirect('user-landing', user_identifier = results)
return redirect('user-landing', user_identifier = user_identifier)
# if no valid post request, display the form
form = LoginForm
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment