Skip to content
Merged
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
26 changes: 26 additions & 0 deletions users/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from django.conf import settings

ADMIN = 0
MEMBER = 1
MENTOR = 2
EXPERT = 3
INVESTOR = 4

VERBOSE_USER_TYPES = (
(MEMBER, "Участник"),
(MENTOR, "Ментор"),
(EXPERT, "Эксперт"),
(INVESTOR, "Инвестор"),
)

VERBOSE_ROLE_TYPES = (
(MENTOR, "Ментор"),
(EXPERT, "Эксперт"),
(INVESTOR, "Инвестор"),
)

VERIFY_EMAIL_REDIRECT_URL = "https://app.procollab.ru/auth/verification/"

PROTOCOL = "https"
if settings.DEBUG:
PROTOCOL = "http"
28 changes: 1 addition & 27 deletions users/helpers.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,9 @@
from django.conf import settings
from rest_framework_simplejwt.tokens import RefreshToken
from django.contrib.sites.shortcuts import get_current_site
from django.urls import reverse

from core.utils import Email

ADMIN = 0
MEMBER = 1
MENTOR = 2
EXPERT = 3
INVESTOR = 4

VERBOSE_USER_TYPES = (
(MEMBER, "Участник"),
(MENTOR, "Ментор"),
(EXPERT, "Эксперт"),
(INVESTOR, "Инвестор"),
)

VERBOSE_ROLE_TYPES = (
(MENTOR, "Ментор"),
(EXPERT, "Эксперт"),
(INVESTOR, "Инвестор"),
)

REDIRECT_URL = "https://app.procollab.ru/auth/verification/"

PROTOCOL = "https"

if settings.DEBUG:
PROTOCOL = "http"
from users.constants import PROTOCOL


def reset_email(user, request):
Expand Down
2 changes: 1 addition & 1 deletion users/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.contrib.auth.models import UserManager
from django.db.models import Manager

from users.helpers import MEMBER
from users.constants import MEMBER


class CustomUserManager(UserManager):
Expand Down
2 changes: 1 addition & 1 deletion users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.db.models.signals import post_save
from django.dispatch import receiver

from users.helpers import (
from users.constants import (
ADMIN,
EXPERT,
INVESTOR,
Expand Down
6 changes: 6 additions & 0 deletions users/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
LikedProjectList,
RegisteredEventsList,
SetUserOnboardingStage,
ResendVerifyEmail,
)

app_name = "users"
Expand All @@ -38,6 +39,11 @@
path("users/achievements/", AchievementList.as_view()),
path("users/achievements/<int:pk>/", AchievementDetail.as_view()),
path("logout/", LogoutView.as_view()),
path(
"resend_email/",
ResendVerifyEmail.as_view(),
name="account_email_verification_resent",
),
re_path(
r"^account-confirm-email/",
VerifyEmail.as_view(),
Expand Down
38 changes: 32 additions & 6 deletions users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@
from events.serializers import EventsListSerializer
from projects.serializers import ProjectListSerializer
from users.helpers import (
VERBOSE_ROLE_TYPES,
VERBOSE_USER_TYPES,
reset_email,
verify_email,
)
from users.constants import (
VERBOSE_ROLE_TYPES,
VERBOSE_USER_TYPES,
VERIFY_EMAIL_REDIRECT_URL,
)
from users.models import UserAchievement, LikesOnProject
from users.permissions import IsAchievementOwnerOrReadOnly
from users.serializers import (
Expand Down Expand Up @@ -160,7 +163,7 @@ class VerifyEmail(GenericAPIView):

def get(self, request):
token = request.GET.get("token")
REDIRECT_URL = "https://app.procollab.ru/auth/verification/"

try:
payload = jwt.decode(jwt=token, key=settings.SECRET_KEY, algorithms=["HS256"])
user = User.objects.get(id=payload["user_id"])
Expand All @@ -172,20 +175,20 @@ def get(self, request):
user.save()

return redirect(
f"{REDIRECT_URL}?access_token={access_token}&refresh_token={refresh_token}",
f"{VERIFY_EMAIL_REDIRECT_URL}?access_token={access_token}&refresh_token={refresh_token}",
status=status.HTTP_200_OK,
message="Succeed",
)

except jwt.ExpiredSignatureError:
return redirect(
REDIRECT_URL,
VERIFY_EMAIL_REDIRECT_URL,
status=status.HTTP_400_BAD_REQUEST,
message="Activate Expired",
)
except jwt.DecodeError:
return redirect(
REDIRECT_URL,
VERIFY_EMAIL_REDIRECT_URL,
status=status.HTTP_400_BAD_REQUEST,
message="Decode error",
)
Expand Down Expand Up @@ -360,3 +363,26 @@ def put(self, request: Request, pk):
return Response(
status=status.HTTP_400_BAD_REQUEST, data={"error": "Something went wrong"}
)


class ResendVerifyEmail(GenericAPIView):
permission_classes = [AllowAny]
serializer_class = UserDetailSerializer
queryset = User.objects.all()

def post(self, request, *args, **kwargs):
try:
email = request.data["email"]
user = User.objects.get(email=email)

if not user.is_active:
verify_email(user, request)
return Response("Email sent!", status=status.HTTP_200_OK)

return Response("User already verified!", status=status.HTTP_200_OK)
except User.DoesNotExist:
return Response(
"User with given email does not exists!", status=status.HTTP_404_NOT_FOUND
)
except Exception:
return Response(status=status.HTTP_400_BAD_REQUEST)