#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")
|
||||
@@ -5,7 +5,6 @@
|
||||
<div class="col">
|
||||
<h1 class="pt-4 mt-5 mb-4">Thank you.</h1>
|
||||
We will send the document to {{ email }} when it is ready.
|
||||
<a href="{{ document_link }}">View Your Document</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -13,10 +13,6 @@ def extract_risk_factors(organization):
|
||||
risk_data[field.name] = value
|
||||
return risk_data
|
||||
|
||||
from openai import OpenAI
|
||||
from django.conf import settings
|
||||
from .models import Risk
|
||||
|
||||
def get_top_risk(organization):
|
||||
client = OpenAI(api_key=settings.OPENAI_API_KEY)
|
||||
|
||||
|
||||
@@ -5,6 +5,8 @@ from .forms import OrganizationForm
|
||||
from .models import Organization,Document,Risk
|
||||
from backend.core.utils import get_top_risk
|
||||
from django.urls import reverse
|
||||
from backend.accounts.utils import send_confirmation_email
|
||||
|
||||
# @login_required
|
||||
# def index(request):
|
||||
# return HttpResponse('<h1>Django</h1><p>Página simples.</p>')
|
||||
@@ -44,6 +46,8 @@ def signup(request):
|
||||
|
||||
document.add_segment('body',f"Identified Risks: \n\n{risk_content}")
|
||||
|
||||
send_confirmation_email(form.data['email'])
|
||||
|
||||
return render(request, 'thankyou.html', {
|
||||
'email': form.data['email'],
|
||||
'document_link': reverse('core:document', args=[str(document.id)])
|
||||
|
||||
@@ -18,6 +18,7 @@ import os
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv()
|
||||
|
||||
|
||||
#API key
|
||||
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
||||
|
||||
@@ -131,6 +132,17 @@ USE_THOUSAND_SEPARATOR = True
|
||||
|
||||
DECIMAL_SEPARATOR = ','
|
||||
|
||||
#EMAIL SMTP
|
||||
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
|
||||
EMAIL_HOST = 'smtp.gmail.com'
|
||||
EMAIL_PORT = 587
|
||||
EMAIL_USE_TLS = True
|
||||
EMAIL_HOST_USER = os.getenv("CONF_MAIL")
|
||||
EMAIL_HOST_PASSWORD = os.getenv("CONF_MAIL_PASSWORD")
|
||||
|
||||
print(f"Email: {EMAIL_HOST_USER}")
|
||||
print(f"Password: {EMAIL_HOST_PASSWORD}")
|
||||
|
||||
|
||||
# Static files (CSS, JavaScript, Images)
|
||||
# https://docs.djangoproject.com/en/5.1/howto/static-files/
|
||||
|
||||
Reference in New Issue
Block a user