Skip to content

Commit 756e0da

Browse files
committed
fix urls.py
1 parent f545e50 commit 756e0da

6 files changed

Lines changed: 114 additions & 1 deletion

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from django.db import models
2+
from django.contrib.auth.models import User
3+
4+
class RegistrationProfile(models.Model):
5+
user = models.OneToOneField(User, on_delete=models.CASCADE)
6+
activation_key = models.CharField(max_length=40)
7+
8+
def save(self, *args, **kwargs):
9+
# Generate a unique activation key when saving the model
10+
if not self.activation_key:
11+
self.activation_key = self.generate_activation_key()
12+
super(RegistrationProfile, self).save(*args, **kwargs)
13+
14+
def generate_activation_key(self):
15+
# Implement a function to generate a unique activation key (you may use a library like uuid)
16+
# This is a simple example, you may want to customize this based on your requirements
17+
import uuid
18+
return str(uuid.uuid4())

server/balancer_backend/settings.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
'django.contrib.sessions',
3939
'django.contrib.messages',
4040
'django.contrib.staticfiles',
41+
'registration',
4142
]
4243

4344
MIDDLEWARE = [
@@ -104,6 +105,16 @@
104105
},
105106
]
106107

108+
ACCOUNT_ACTIVATION_DAYS = 7
109+
REGISTRATION_AUTO_LOGIN = True
110+
REGISTRATION_EMAIL_SUBJECT_PREFIX = 'Balancer'
111+
REGISTRATION_SALT = 'TEST'
112+
113+
EMAIL_USE_TLS = True
114+
EMAIL_HOST = 'smtp.gmail.com'
115+
EMAIL_HOST_USER = os.environ.get("EMAIL_USER")
116+
EMAIL_HOST_PASSWORD = os.environ.get("EMAIL_PASSWORD")
117+
EMAIL_PORT = 587
107118

108119
# Internationalization
109120
# https://docs.djangoproject.com/en/4.2/topics/i18n/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Account Activation</title>
7+
</head>
8+
<body>
9+
<p>
10+
Hi {{ user.username }},<br><br>
11+
12+
Thank you for registering on our website! Please click the link below to activate your account:
13+
<br><br>
14+
15+
<a href="{{ activation_link }}" target="_blank">{{ activation_link }}</a>
16+
<br><br>
17+
18+
If you did not request this, please ignore this email.
19+
</p>
20+
</body>
21+
</html>

server/balancer_backend/urls.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,9 @@
1919

2020
urlpatterns = [
2121
path("admin/", admin.site.urls),
22-
path("", include("views.urls", prefix="api/")),
22+
path("api/", include(balancer_backend.views.auth.urls)),
23+
path("api/", include(balancer_backend.views.chatgpt.urls)),
24+
path("api/", include(balancer_backend.views.jira.urls)),
25+
path("api/", include(balancer_backend.views.listDrugs.urls)),
26+
path("api/", include(balancer_backend.views.risk.urls)),
2327
]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.urls import path
2+
from balancer_backend.views.auth import views
3+
4+
urlpatterns = [
5+
path("auth/register_user/", views.register_user, name="register_user"),
6+
path("auth/activate/<str:activation_key>/", views.activate_account, name="activate_account")
7+
]
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from django.core.mail import send_mail
2+
from django.template.loader import render_to_string
3+
from django.utils.html import strip_tags
4+
from rest_framework import status
5+
from rest_framework.decorators import api_view
6+
from django.http import JsonResponse
7+
from balancer_backend.models.users import RegistrationProfile
8+
import requests
9+
import json
10+
import os
11+
12+
# XXX: remove csrf_exempt usage before production
13+
from django.views.decorators.csrf import csrf_exempt
14+
15+
16+
@csrf_exempt
17+
def send_verification_email(user):
18+
subject = 'Activate Your Account'
19+
activation_key = user.registrationprofile.activation_key
20+
message = render_to_string('email/verification_email.html', {'activation_key': activation_key, 'user': user})
21+
plain_message = strip_tags(message) # This is the plain text version of the HTML content
22+
23+
send_mail(subject, plain_message, 'your_email@example.com', [user.email], html_message=message)
24+
25+
@csrf_exempt
26+
def register_user(request: str) -> JsonResponse:
27+
data: dict[str, str] = json.loads(request.body)
28+
email: str = data["email"]
29+
password: str = data["password"]
30+
31+
if not email or not password:
32+
return JsonResponse({"error": "Email and password are required."})
33+
34+
if RegistrationProfile.objects.filter(user__email=email).exists():
35+
return JsonResponse({"error": "Email is already registred."})
36+
37+
user = User.objects.create_user(email, email, password)
38+
user.is_active = False
39+
user.save()
40+
41+
send_verification_email(user)
42+
43+
return JsonResponse({"message": "Registration successful. Check your email for verification."})
44+
45+
@csrf_exempt
46+
def activate_account(request, activation_key):
47+
user = RegistrationProfile.objects.activate_user(activation_key)
48+
49+
if user:
50+
return JsonResponse({"message": "Account activated successfully."})
51+
else:
52+
return JsonResponse({"error": "Invalid or expired activation key."})

0 commit comments

Comments
 (0)