Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#Excluding database

*.sqlite3

#Excluding vscode

.vscode
Empty file removed db.sqlite3
Empty file.
Binary file added email_auth/__pycache__/forms.cpython-36.pyc
Binary file not shown.
Binary file modified email_auth/__pycache__/models.cpython-36.pyc
Binary file not shown.
Binary file added email_auth/__pycache__/tokens.cpython-36.pyc
Binary file not shown.
Binary file modified email_auth/__pycache__/urls.cpython-36.pyc
Binary file not shown.
Binary file modified email_auth/__pycache__/views.cpython-36.pyc
Binary file not shown.
8 changes: 8 additions & 0 deletions email_auth/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class SignupForm(UserCreationForm):
email = forms.EmailField(max_length=200, help_text='Required')
class Meta:
model = User
fields = ('username', 'email', 'password1', 'password2')
24 changes: 24 additions & 0 deletions email_auth/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 2.1.7 on 2019-12-13 08:26

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='MyAppUser',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
Binary file not shown.
6 changes: 5 additions & 1 deletion email_auth/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from django.db import models

from django.contrib.auth.models import User
# Create your models here.

class MyAppUser (models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
#user = models.OneToOneField(User, on_delete=models.CASCADE)
14 changes: 14 additions & 0 deletions email_auth/tokens.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.contrib.auth.tokens import PasswordResetTokenGenerator
#from django.utils import six
#from .models.MyAppUser import user #added here
import six
class TokenGenerator(PasswordResetTokenGenerator):
def _make_hash_value(self, user, timestamp):
return (
six.text_type(user.pk) + six.text_type(timestamp) +
six.text_type(user.is_active)
)
#return (six.text_type(user.pk) + six.text_type(timestamp) + six.text_type(user.username))
account_activation_token = TokenGenerator()
print(account_activation_token)

7 changes: 6 additions & 1 deletion email_auth/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from django.urls import path
from django.conf.urls import url
from .views import *

urlpatterns=[
path('',home,name='home'),
path('email',email,name='email1')
#path('email',email,name='email1'),
url('signup', signup, name='signup'),
#path(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',activate, name='activate')
path('activate/(?P<uidb64>\d+)/(?P<token>\d+)/$',activate,name='activate')

]
69 changes: 65 additions & 4 deletions email_auth/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,71 @@
from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.contrib.auth import login, authenticate
from .forms import SignupForm
from django.contrib.sites.shortcuts import get_current_site
from django.utils.encoding import force_bytes, force_text
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
from django.template.loader import render_to_string
from .tokens import account_activation_token
from django.contrib.auth.models import User
from django.core.mail import EmailMessage
from . import models
def signup(request):
if request.method == 'POST':
form = SignupForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.is_active = False
user.save()
current_site = get_current_site(request)
mail_subject = 'Activate your blog account.'
message = render_to_string('acc_active_email.html', {
'user': user,
'domain': current_site.domain,
'uid':urlsafe_base64_encode(force_bytes(user.pk)),
'token':account_activation_token.make_token(user)
})
to_email = form.cleaned_data.get('email')
email = EmailMessage(
mail_subject, message, to=[to_email]
)
email.send()
return HttpResponse('Please confirm your email address to complete the registration')
else:
form = SignupForm()
return render(request, 'signup.html', {'form': form})

def activate(request, uidb64, token):
#print(user)#added here
try:
uid = force_text(urlsafe_base64_decode(uidb64))
print(uid)
user = User.objects.get(pk=uid)
#user=models.MyAppUser.objects.get(pk=uid)
#print(user)#added here
#except (TypeError, ValueError, OverflowError, User.DoesNotExist):
except(ValueError):
#print(user)
user = None
print(user)#added here
if user is not None and account_activation_token.check_token(user, token):
user.is_active = True
user.save()
#login(request, user)
# return redirect('home')
return HttpResponse('Thank you for your email confirmation. Now you can login your account.')
else:
return HttpResponse('Activation link is invalid!')





# Create your views here.
def home(request):
return render(request,'home.html')


def email(request):
email=request.POST['email']
print(email)
#def email(request):
#email=request.POST['email']
#print(email)
Binary file modified email_authentication/__pycache__/settings.cpython-36.pyc
Binary file not shown.
12 changes: 11 additions & 1 deletion email_authentication/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,20 @@
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
],``
},
},
]

WSGI_APPLICATION = 'email_authentication.wsgi.application'

#EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'email@example.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587


# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
Expand All @@ -81,6 +88,9 @@
}
}

#AUTHENTICATION_BACKENDS = (
# ('django.contrib.auth.backends.ModelBackend'),
#)

# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators
Expand Down
5 changes: 5 additions & 0 deletions templates/acc_active_email.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% autoescape off %}
Hi {{ user.username }},
Please click on the link to confirm your registration,
http://{{ domain }}{% url 'activate' uidb64=uid token=token %}
{% endautoescape %}
8 changes: 4 additions & 4 deletions templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% block content %}

<h1 style="font-size: xx-large;" align='center'>Enter your details</h1>
<div align=center style="font-weight: bolder;">
<!--div align=center style="font-weight: bolder;">
<form action="email" method="POST">

{% csrf_token %}
Expand All @@ -21,10 +21,10 @@ <h5>Confirm Password</h5>

</form>

</div>


</div-->

<a href="{% url 'signup' %}">Sign Up</a>
<a href="{% url 'signin' %}">Sign In</a>


{% endblock %}
21 changes: 21 additions & 0 deletions templates/signup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% extends 'base.html' %}

{% block content %}
<h2>Sign up</h2>
<form method="post">
{% csrf_token %}
{% for field in form %}
<p>
{{ field.label_tag }}<br>
{{ field }}
{% if field.help_text %}
<small style="display: none">{{ field.help_text }}</small>
{% endif %}
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
</p>
{% endfor %}
<button type="submit">Sign up</button>
</form>
{% endblock %}