#6 Verifikacija emaila zavrsena.
This commit is contained in:
@@ -1,3 +1,10 @@
|
||||
from django.contrib import admin
|
||||
from .models import EmailConfirmation
|
||||
|
||||
# Register your models here.
|
||||
|
||||
class EmailConfirmationAdmin(admin.ModelAdmin):
|
||||
list_display= ['email', 'uuid', 'created_at']
|
||||
|
||||
|
||||
|
||||
admin.site.register(EmailConfirmation, EmailConfirmationAdmin)
|
||||
|
||||
24
backend/accounts/migrations/0001_initial.py
Normal file
24
backend/accounts/migrations/0001_initial.py
Normal file
@@ -0,0 +1,24 @@
|
||||
# Generated by Django 5.1.3 on 2025-02-13 12:37
|
||||
|
||||
import uuid
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='EmailConfirmation',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('email', models.EmailField(max_length=254, unique=True)),
|
||||
('uuid', models.UUIDField(default=uuid.uuid4, unique=True)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
],
|
||||
),
|
||||
]
|
||||
@@ -1,3 +1,12 @@
|
||||
from django.db import models
|
||||
import uuid
|
||||
from django.utils.timezone import now
|
||||
from datetime import timedelta
|
||||
|
||||
# Create your models here.
|
||||
class EmailConfirmation(models.Model):
|
||||
email = models.EmailField(unique=True)
|
||||
uuid = models.UUIDField(default=uuid.uuid4, unique=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
def is_expired(self):
|
||||
return now() > (self.created_at + timedelta(days=1))
|
||||
@@ -0,0 +1,8 @@
|
||||
{% extends "base_login.html" %}
|
||||
|
||||
|
||||
<h2>Link has expired! </h2>
|
||||
<form method="POST">
|
||||
{% csrf_token %}
|
||||
<button type="submit">Resend link</button>
|
||||
</form>
|
||||
@@ -0,0 +1,5 @@
|
||||
{% extends "base_login.html" %}
|
||||
|
||||
|
||||
<h1>Email Confirmed!</h1>
|
||||
<p>Your email {{ email }} has been successfully verified.</p>
|
||||
@@ -10,4 +10,6 @@ urlpatterns = [
|
||||
),
|
||||
path('logout/', LogoutView.as_view(), name='logout'),
|
||||
path('signup/', v.SignUpView.as_view(), name='signup'),
|
||||
path('confirm/<uuid:uuid>/', v.confirm_email, name='confirm_email'),
|
||||
path('resend/<str:email>/', v.resend_confirmation, name='resend_confirmation'),
|
||||
]
|
||||
|
||||
24
backend/accounts/utils.py
Normal file
24
backend/accounts/utils.py
Normal file
@@ -0,0 +1,24 @@
|
||||
from django.core.mail import send_mail
|
||||
from django.urls import reverse
|
||||
from .models import EmailConfirmation
|
||||
import uuid
|
||||
from django.conf import settings
|
||||
from django.utils.timezone import now
|
||||
|
||||
|
||||
def send_confirmation_email(email):
|
||||
confirmation, created = EmailConfirmation.objects.get_or_create(email=email)
|
||||
|
||||
if not created:
|
||||
confirmation.uuid = uuid.uuid4()
|
||||
confirmation.created_at = now()
|
||||
confirmation.save()
|
||||
|
||||
confirmation_link = f"http://127.0.0.1:8000{reverse('confirm_email', args=[confirmation.uuid])}"
|
||||
|
||||
send_mail(
|
||||
subject="Confirm your e-mail address",
|
||||
message=f"Please click on the link to confirm your e-mail address: {confirmation_link}",
|
||||
from_email= settings.EMAIL_HOST_USER,
|
||||
recipient_list=[email]
|
||||
)
|
||||
@@ -1,9 +1,27 @@
|
||||
from django.urls import reverse_lazy
|
||||
from django.views.generic import CreateView
|
||||
from backend.accounts.forms import SignupForm
|
||||
from .models import EmailConfirmation
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.http import HttpResponse
|
||||
from backend.accounts.utils import send_confirmation_email
|
||||
|
||||
|
||||
class SignUpView(CreateView):
|
||||
form_class = SignupForm
|
||||
success_url = reverse_lazy('login')
|
||||
template_name = 'accounts/signup.html'
|
||||
|
||||
|
||||
def confirm_email(request,uuid):
|
||||
confirmation = get_object_or_404(EmailConfirmation, uuid=uuid)
|
||||
|
||||
if confirmation.is_expired():
|
||||
return render(request,'confirmation_expired.html', {'email': confirmation.email})
|
||||
|
||||
return HttpResponse("Email is confirmed")
|
||||
|
||||
def resend_confirmation(request,email):
|
||||
if request.method == 'POST':
|
||||
send_confirmation_email(email)
|
||||
return HttpResponse("Confirmation email resent")
|
||||
Reference in New Issue
Block a user