Skip to content

Commit

Permalink
fix: Removal of optional sign next to New Password (#967)
Browse files Browse the repository at this point in the history
* removal of optional sign next to New Password

* Merge branch 'master' into independent_edit_account_fix

* Re-arrangement of a few pieces of code

* Merge branch 'independent_edit_account_fix' of https://github.com/ocadotechnology/codeforlife-portal into independent_edit_account_fix

* Small syntax fix

* Spacing and Cyclomatic complexity fix

* More spacing fixes

* Fixing functions

* indentation fix

* Indentation fix

* Email verification fix

* Name change fix

* Merge branch 'master' into independent_edit_account_fix

* Spacing Changes

* Merge branch 'independent_edit_account_fix' of https://github.com/ocadotechnology/codeforlife-portal into independent_edit_account_fix

* Revert "Spacing Changes"

This reverts commit 634bb36.

* Used FormView instead of VanillaView

* Merge branch 'master' into independent_edit_account_fix

* Spacing changes and getting rid of prints

* Merge branch 'independent_edit_account_fix' of https://github.com/ocadotechnology/codeforlife-portal into independent_edit_account_fix

* Spacing fix

* Refactored get_form method

* Similar blocks of code changed

* Indentation fix

* Form_Valid unrefactored

* Merge branch 'master' into independent_edit_account_fix

* typo fix

* Merge branch 'master' into independent_edit_account_fix

* Merge branch 'master' into independent_edit_account_fix

* Add verification success url

* Fix test issues

* Refactored process form function

* Restructured student views, added docstrings and constant

* Refactor stuff

* More refactoring

* Even more refactoring

* Makes methods private

* Remove blank lines
  • Loading branch information
JackD03 authored and faucomte97 committed Jul 10, 2019
1 parent da59b85 commit e7661bf
Show file tree
Hide file tree
Showing 15 changed files with 453 additions and 401 deletions.
71 changes: 0 additions & 71 deletions portal/forms/auth_forms.py

This file was deleted.

66 changes: 37 additions & 29 deletions portal/forms/play.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@
# copyright notice and these terms. You must not misrepresent the origins of this
# program; modified versions of the program must be marked as such and not
# identified as the original program.
from datetime import timedelta
import re
from datetime import timedelta

from captcha.fields import ReCaptchaField
from django import forms
from django.contrib.auth import authenticate
from django.contrib.auth.models import User
from django.utils import timezone

from portal.helpers.password import form_clean_password
from portal.models import Student, Class, stripStudentName
from portal.helpers.password import password_strength_test
from captcha.fields import ReCaptchaField


class StudentLoginForm(forms.Form):
Expand Down Expand Up @@ -98,6 +98,28 @@ def check_for_errors(self, name, access_code, password):


class StudentEditAccountForm(forms.Form):
password = forms.CharField(
label="New password", required=True, widget=forms.PasswordInput
)
confirm_password = forms.CharField(
label="Confirm new password", required=True, widget=forms.PasswordInput
)
current_password = forms.CharField(
label="Current password", widget=forms.PasswordInput
)

def __init__(self, user, *args, **kwargs):
self.user = user
super(StudentEditAccountForm, self).__init__(*args, **kwargs)

def clean_password(self):
return form_clean_password(self, forms, "password")

def clean(self):
return clean_confirm_password(self)


class IndependentStudentEditAccountForm(forms.Form):
name = forms.CharField(
label="Name",
max_length=100,
Expand All @@ -110,7 +132,7 @@ class StudentEditAccountForm(forms.Form):
widget=forms.EmailInput(attrs={"placeholder": "[email protected]"}),
)
password = forms.CharField(
label="New password (optional)", required=False, widget=forms.PasswordInput
label="New password", required=False, widget=forms.PasswordInput
)
confirm_password = forms.CharField(
label="Confirm new password", required=False, widget=forms.PasswordInput
Expand All @@ -121,7 +143,7 @@ class StudentEditAccountForm(forms.Form):

def __init__(self, user, *args, **kwargs):
self.user = user
super(StudentEditAccountForm, self).__init__(*args, **kwargs)
super(IndependentStudentEditAccountForm, self).__init__(*args, **kwargs)

def clean_name(self):
name = self.cleaned_data.get("name", None)
Expand All @@ -137,27 +159,18 @@ def clean_name(self):
return name

def clean_password(self):
password = self.cleaned_data.get("password", None)
return form_clean_password(self, forms, "password")

if password and not password_strength_test(
password, length=8, upper=False, lower=False, numbers=False
):
raise forms.ValidationError(
"Password not strong enough, consider using at least 8 characters, upper and lower case letters, and numbers"
)
def clean(self):
return clean_confirm_password(self)

return password

def clean(self):
def clean_confirm_password(self):
password = self.cleaned_data.get("password", None)
confirm_password = self.cleaned_data.get("confirm_password", None)
current_password = self.cleaned_data.get("current_password", None)

if (
password is not None
and (password or confirm_password)
and password != confirm_password
):
if are_password_and_confirm_password_different(password, confirm_password):
raise forms.ValidationError("Your new passwords do not match")

if current_password and not self.user.check_password(current_password):
Expand All @@ -166,6 +179,10 @@ def clean(self):
return self.cleaned_data


def are_password_and_confirm_password_different(password, confirm_password):
return password is not None and password != confirm_password


class IndependentStudentSignupForm(forms.Form):
name = forms.CharField(
label="Name",
Expand Down Expand Up @@ -218,16 +235,7 @@ def clean_username(self):
return username

def clean_password(self):
password = self.cleaned_data.get("password", None)

if password and not password_strength_test(
password, length=8, upper=False, lower=False, numbers=False
):
raise forms.ValidationError(
"Password not strong enough, consider using at least 8 characters, upper and lower case letters, and numbers"
)

return password
return form_clean_password(self, forms, "password")

def clean(self):
password = self.cleaned_data.get("password", None)
Expand Down
29 changes: 7 additions & 22 deletions portal/forms/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,25 @@
# copyright notice and these terms. You must not misrepresent the origins of this
# program; modified versions of the program must be marked as such and not
# identified as the original program.
from captcha.fields import ReCaptchaField
from django import forms
from django.core.mail import EmailMultiAlternatives
from django.contrib.auth import forms as django_auth_forms
from django.contrib.auth import get_user_model
from django.contrib.auth.models import User
from django.contrib.auth import forms as django_auth_forms
from django.contrib.auth.tokens import default_token_generator
from django.contrib.sites.shortcuts import get_current_site
from django.core.mail import EmailMultiAlternatives
from django.template import loader
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode
from django.utils.translation import ugettext, ugettext_lazy as _

from portal.helpers.password import form_clean_password
from portal.models import Student, Teacher
from portal.helpers.password import password_strength_test
from captcha.fields import ReCaptchaField


class PasswordResetSetPasswordForm(django_auth_forms.SetPasswordForm):
def __init__(self, user, *args, **kwags):
super(PasswordResetSetPasswordForm, self).__init__(user, *args, **kwags)
def __init__(self, user, *args, **kwargs):
super(PasswordResetSetPasswordForm, self).__init__(user, *args, **kwargs)
self.fields["new_password1"].label = "Enter your new password"
self.fields["new_password1"].widget.attrs[
"placeholder"
Expand All @@ -64,21 +63,7 @@ def __init__(self, user, *args, **kwags):
] = "Please repeat your new password"

def clean_new_password1(self):
new_password1 = self.cleaned_data.get("new_password1", None)
if hasattr(self.user, "teacher"):
if not password_strength_test(new_password1):
raise forms.ValidationError(
"Password not strong enough, consider using at least 8 characters, upper and "
+ "lower case letters, and numbers"
)
elif hasattr(self.user, "student"):
if not password_strength_test(
new_password1, length=8, upper=False, lower=False, numbers=False
):
raise forms.ValidationError(
"Password not strong enough, consider using at least 8 characters, upper and lower case letters, and numbers"
)
return new_password1
return form_clean_password(self, forms, "new_password1")


class TeacherPasswordResetForm(forms.Form):
Expand Down
38 changes: 6 additions & 32 deletions portal/forms/teach.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@
# identified as the original program.
import re

from captcha.fields import ReCaptchaField
from django import forms
from django.contrib.auth import authenticate
from django.contrib.auth.models import User

from portal.models import Student, Teacher, stripStudentName
from portal.helpers.password import password_strength_test
from captcha.fields import ReCaptchaField

from portal.helpers.password import form_clean_password
from portal.models import Student, stripStudentName

choices = [
("Miss", "Miss"),
Expand Down Expand Up @@ -89,14 +88,7 @@ class TeacherSignupForm(forms.Form):
captcha = ReCaptchaField()

def clean_teacher_password(self):
password = self.cleaned_data.get("teacher_password", None)

if password and not password_strength_test(password):
raise forms.ValidationError(
"Password not strong enough, consider using at least 8 characters, upper and lower case letters, and numbers"
)

return password
return form_clean_password(self, forms, "teacher_password")

def clean(self):
if any(self.errors):
Expand Down Expand Up @@ -147,15 +139,7 @@ def __init__(self, user, *args, **kwargs):
super(TeacherEditAccountForm, self).__init__(*args, **kwargs)

def clean_password(self):
password = self.cleaned_data.get("password", None)

if password and not password_strength_test(password):
raise forms.ValidationError(
"Password not strong enough, consider using at least 8 characters, upper and lower "
+ "case letters, and numbers"
)

return password
return form_clean_password(self, forms, "password")

def clean(self):
if any(self.errors):
Expand Down Expand Up @@ -356,16 +340,7 @@ class TeacherSetStudentPass(forms.Form):
)

def clean_password(self):
password = self.cleaned_data.get("password", None)

if password and not password_strength_test(
password, length=8, upper=False, lower=False, numbers=False
):
raise forms.ValidationError(
"Password not strong enough, consider using at least 8 characters, upper and lower case letters, and numbers"
)

return password
return form_clean_password(self, forms, "password")

def clean(self):
password = self.cleaned_data.get("password", None)
Expand Down Expand Up @@ -437,7 +412,6 @@ def find_illegal_characters(names, validationErrors):
def check_passwords(password, confirm_password):
if (
password is not None
and (password or confirm_password)
and password != confirm_password
):
raise forms.ValidationError(
Expand Down
23 changes: 16 additions & 7 deletions portal/helpers/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,20 @@
# copyright notice and these terms. You must not misrepresent the origins of this
# program; modified versions of the program must be marked as such and not
# identified as the original program.
from uuid import uuid4
from datetime import timedelta
from requests import post
from requests.exceptions import RequestException
from uuid import uuid4

from django.conf import settings
from django.utils import timezone
from django.core.mail import EmailMultiAlternatives
from django.template import loader
from django.utils import timezone
from requests import post
from requests.exceptions import RequestException

from portal.models import EmailVerification
from portal import app_settings
from portal.email_messages import emailVerificationNeededEmail
from portal.email_messages import emailChangeNotificationEmail
from portal.email_messages import emailChangeVerificationEmail
from portal.email_messages import emailVerificationNeededEmail
from portal.models import EmailVerification

NOTIFICATION_EMAIL = "Code For Life Notification <" + app_settings.EMAIL_ADDRESS + ">"
VERIFICATION_EMAIL = "Code For Life Verification <" + app_settings.EMAIL_ADDRESS + ">"
Expand Down Expand Up @@ -148,3 +147,13 @@ def add_to_salesforce(first_name, last_name, email):
post(url, data=data)
except RequestException:
return


def update_email(user, request, data):
changing_email = False
new_email = data["email"]
if new_email != "" and new_email != user.email:
# new email to set and verify
changing_email = True
send_verification_email(request, user, new_email)
return changing_email, new_email
Loading

0 comments on commit e7661bf

Please sign in to comment.