Commit 01829092 authored by JayCay's avatar JayCay

Email Signup, accepts only obf, error on confirmation

parent 125ce2c5
......@@ -136,3 +136,5 @@ STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
LOGIN_REDIRECT_URL = 'reviews_index'
LOGOUT_REDIRECT_URL = 'reviews_index'
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
\ No newline at end of file
......@@ -25,6 +25,8 @@ from profs import views as profs_views
urlpatterns = [
url(r'^signup/$', accounts_views.signup, name='signup'),
url(r'^account_activation_sent/$', accounts_views.account_activation_sent, name='account_activation_sent'),
url(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',accounts_views.activate, name='activate'),
url(r'^login/$', auth_views.LoginView.as_view(template_name='templates/login.html'), name='login'),
url(r'^logout/$', auth_views.LogoutView.as_view(), name='logout'),
url(r'^admin/', admin.site.urls),
......
......@@ -3,7 +3,14 @@ from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class SignUpForm(UserCreationForm):
email = forms.CharField(max_length=254, required=True, widget=forms.EmailInput())
email = forms.EmailField(max_length=254, required=True, widget=forms.EmailInput(), help_text='Required. Inform a valid email address.')
def clean_email(self):
data = self.cleaned_data['email']
if "@obf.ateneo.edu" not in data: # any check you need
raise forms.ValidationError("Please use your obf email.")
return data
class Meta:
model = User
fields = ('username', 'email', 'password1', 'password2')
\ No newline at end of file
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
email_confirmed = models.BooleanField(default=False)
@receiver(post_save, sender=User)
def update_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
instance.profile.save()
# Create your models here.
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.utils import six
class AccountActivationTokenGenerator(PasswordResetTokenGenerator):
def _make_hash_value(self, user, timestamp):
return(
six.text_type(user.pk) + six.text_type(timestamp) + six.text_type(user.profile.email_confirmed)
)
account_activation_token = AccountActivationTokenGenerator()
\ No newline at end of file
from django.contrib.sites.shortcuts import get_current_site
from django.contrib.auth import login as auth_login
from django.shortcuts import render, redirect
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode
from django.template.loader import render_to_string
from .forms import SignUpForm
from accounts.forms import SignUpForm
from accounts.tokens import account_activation_token
#@login_required
#def home(request):
# return render(request, 'reviews_index')
def signup(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
user = form.save()
auth_login(request, user)
return redirect('reviews_index')
user = form.save(commit=False)
user.is_active = False
user.save()
current_site = get_current_site(request)
subject = 'Activate your Academe Account'
message = render_to_string('account_activation_email.html', {
'user': user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': account_activation_token.make_token(user),
})
user.email_user(subject, message)
return redirect('account_activation_sent')
else:
form = SignUpForm()
return render(request, 'templates/signup.html', {'form' : form})
def account_activation_sent(request):
return render(request, 'account_activation_sent.html')
def activate(request, uidb64, token):
try:
uid = force_text(urlsafe_base64_decode(uidb64))
user = User.objects.get(pk=uid)
except (TypeError, OverflowError, User.DoesNotExist):
user = None
if user is not None and account_activation_token.check_token(user, token):
user.is_active = True
user.profile.email_confirmed= True
user.save()
login(request, user)
return redirect('reviews_index')
else:
return render(request, 'account_activation_invalid.html')
No preview for this file type
{% autoescape off %}
Hi {{ user.username }},
Please click on the link below to confirm your registration:
http://{{ domain }}{% url 'activate' uidb64=uid token=token %}
{% endautoescape %}
\ No newline at end of file
{% extends templates/base.html %}
{% block content %}
<p>The confirmation link was invalid, possibly because it has already been used.</p>
{% endblock %}
\ No newline at end of file
{% extends templates/base.html %}
{% block content %}
<p>Please confirm your email address to complete the registration.</p>
{% endblock %}
\ No newline at end of file
......@@ -12,6 +12,8 @@
<li><a href="{% url 'reviews_index' %}">Home</a></li>
<li><a href="{% url 'profs_index' %}">Profs</a></li>
<li><a href="{% url 'login' %}">Log in</a></li>
<li><a href="{% url 'logout' %}">Log out</a></li
<li><a href="{% url 'signup' %}">Sign Up</a></li>
<li>{{ user.username }}</li>
</ul>
</nav>
......
Markdown is supported
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