Skip to content

Commit

Permalink
refactor: replace user utils
Browse files Browse the repository at this point in the history
  • Loading branch information
AmooHashem committed Nov 24, 2024
1 parent aa07b8d commit 7b7e784
Show file tree
Hide file tree
Showing 15 changed files with 20 additions and 51 deletions.
7 changes: 6 additions & 1 deletion apps/accounts/serializers/custom_token_obtain.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ def validate(self, attrs):

def get_token(self, user):
refresh = RefreshToken.for_user(user)
access_token = refresh.access_token

refresh["origin"] = user.origin
access_token["origin"] = user.origin

return {
'refresh': str(refresh),
'access': str(refresh.access_token),
'access': str(access_token),
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import string
import secrets
from django.db.models import Q
from rest_framework.exceptions import ParseError
from django.contrib.auth.hashers import make_password, check_password

from apps.accounts.serializers.user_serializer import UserSerializer
from apps.accounts.models import User, UserWebsite, UserWebsiteLogin
from apps.accounts.models import User, UserWebsite
from apps.accounts.serializers.custom_token_obtain import CustomTokenObtainSerializer
from errors.error_codes import serialize_error
from apps.fsm.models import RegistrationForm, RegistrationReceipt, Team, AnswerSheet
from apps.fsm.serializers.answer_sheet_serializers import MinimalRegistrationReceiptSerializer
from proxies.instant_messaging_service.main import InstantMessagingServiceProxy


def generate_tokens_for_user(user):
Expand Down Expand Up @@ -162,36 +159,3 @@ def create_team(**data):
team.save()

return team


def generate_secure_password(length=12):
"""
Generate a secure random password with:
- Minimum length of 12 characters
- At least one lowercase letter
- At least one uppercase letter
- At least one digit
- At least one special character
"""
# Define character sets
lowercase = string.ascii_lowercase
uppercase = string.ascii_uppercase
digits = string.digits
special = "!@#$%^&*"

# Ensure at least one character from each set
password = [
secrets.choice(lowercase),
secrets.choice(uppercase),
secrets.choice(digits),
secrets.choice(special),
]

# Fill the rest with random characters from all sets
all_characters = lowercase + uppercase + digits + special
password.extend(secrets.choice(all_characters) for _ in range(length - 4))

# Shuffle the password characters
secrets.SystemRandom().shuffle(password)

return ''.join(password)
2 changes: 1 addition & 1 deletion apps/accounts/views/base_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from apps.accounts.models import UserWebsiteLogin
from apps.accounts.serializers.user_serializer import UserSerializer
from apps.accounts.utils import create_or_get_user, find_user_in_website, generate_tokens_for_user
from apps.accounts.utils.user_management import create_or_get_user, find_user_in_website, generate_tokens_for_user


class BaseLoginView(APIView):
Expand Down
2 changes: 1 addition & 1 deletion apps/accounts/views/change_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from rest_framework.response import Response

from apps.accounts.serializers.user_serializer import PhoneNumberVerificationCodeSerializer, UserSerializer
from apps.accounts.utils import find_user_in_website
from apps.accounts.utils.user_management import find_user_in_website


class ChangePasswordView(GenericAPIView):
Expand Down
2 changes: 1 addition & 1 deletion apps/accounts/views/change_phone_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from rest_framework.response import Response

from apps.accounts.serializers.user_serializer import PhoneNumberVerificationCodeSerializer
from apps.accounts.utils import find_user
from apps.accounts.utils.user_management import find_user
from errors.error_codes import serialize_error


Expand Down
2 changes: 1 addition & 1 deletion apps/accounts/views/google_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from apps.accounts.models import UserWebsiteLogin
from apps.accounts.serializers.user_serializer import UserSerializer
from apps.accounts.utils import find_user_in_website, generate_tokens_for_user
from apps.accounts.utils.user_management import find_user_in_website, generate_tokens_for_user


class GoogleLogin(TokenObtainPairView):
Expand Down
2 changes: 1 addition & 1 deletion apps/accounts/views/institute_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from apps.accounts.permissions import IsInstituteOwner, IsInstituteAdmin
from apps.accounts.serializers.user_serializer import UserSerializer
from apps.accounts.serializers.institute_serializer import InstituteSerializer
from apps.accounts.utils import find_user_in_website
from apps.accounts.utils.user_management import find_user_in_website
from errors.error_codes import serialize_error


Expand Down
2 changes: 1 addition & 1 deletion apps/accounts/views/simple_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from apps.accounts.models import UserWebsiteLogin
from apps.accounts.serializers.user_serializer import UserSerializer
from apps.accounts.serializers.custom_token_obtain import CustomTokenObtainSerializer
from apps.accounts.utils import can_user_login, create_or_get_user, find_user_in_website, generate_tokens_for_user
from apps.accounts.utils.user_management import can_user_login, create_or_get_user, find_user_in_website, generate_tokens_for_user
from errors.error_codes import serialize_error


Expand Down
2 changes: 1 addition & 1 deletion apps/accounts/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from apps.accounts.models import User, UserWebsiteLogin
from apps.accounts.permissions import IsHimself
from apps.accounts.serializers.user_serializer import PhoneNumberVerificationCodeSerializer, UserSerializer
from apps.accounts.utils import create_or_get_user, find_user_in_website, generate_tokens_for_user
from apps.accounts.utils.user_management import create_or_get_user, find_user_in_website, generate_tokens_for_user
from errors.error_codes import serialize_error


Expand Down
2 changes: 1 addition & 1 deletion apps/accounts/views/uuid_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from rest_framework.response import Response

from apps.accounts.serializers.user_serializer import UserSerializer
from apps.accounts.utils import create_or_get_user, find_user_in_website
from apps.accounts.utils.user_management import create_or_get_user, find_user_in_website
from apps.accounts.views.base_login import BaseLoginView


Expand Down
2 changes: 1 addition & 1 deletion apps/fsm/views/fsm_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from django_filters import rest_framework as filters

from apps.accounts.serializers.user_serializer import UserSerializer
from apps.accounts.utils import find_user_in_website
from apps.accounts.utils.user_management import find_user_in_website
from apps.fsm.models.fsm import State
from apps.fsm.pagination import StandardPagination
from apps.fsm.serializers.papers.state_serializer import StateSerializer
Expand Down
2 changes: 1 addition & 1 deletion apps/fsm/views/program_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from apps.fsm.permissions import ProgramAdminPermission
from apps.fsm.serializers.program_serializers import ProgramSerializer, ProgramSummarySerializer
from apps.accounts.serializers.user_serializer import UserSerializer
from apps.accounts.utils import find_user_in_website
from apps.accounts.utils.user_management import find_user_in_website
from apps.fsm.utils import add_admin_to_program
from errors.error_codes import serialize_error
from utils.cache_enabled_model_viewset import CacheEnabledModelViewSet
Expand Down
2 changes: 1 addition & 1 deletion apps/fsm/views/registration_admin_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet

from apps.accounts.utils import create_or_get_user, find_user_in_website, update_or_create_team, update_or_create_registration_receipt
from apps.accounts.utils.user_management import create_or_get_user, find_user_in_website, update_or_create_team, update_or_create_registration_receipt
from apps.fsm.utils import register_user_in_program
from apps.fsm.models import RegistrationForm, transaction
from apps.fsm.permissions import IsRegistrationFormModifier
Expand Down
2 changes: 1 addition & 1 deletion apps/fsm/views/team_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from django_filters import rest_framework as filters

from apps.accounts.serializers.user_serializer import PhoneNumberSerializer
from apps.accounts.utils import find_user_in_website
from apps.accounts.utils.user_management import find_user_in_website
from errors.error_codes import serialize_error
from apps.fsm import permissions as customPermissions
from apps.fsm.models import Team, Invitation, RegistrationReceipt
Expand Down
2 changes: 1 addition & 1 deletion apps/sale/views/discount_code_view.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from rest_framework.viewsets import ModelViewSet
from apps.accounts.models import DiscountCode, Merchandise
from apps.accounts.permissions import IsDiscountCodeModifier
from apps.accounts.utils import find_user_in_website
from apps.accounts.utils.user_management import find_user_in_website
from apps.sale.serializers.discount_code import DiscountCodeSerializer
from django.db import transaction
from rest_framework import status
Expand Down

0 comments on commit 7b7e784

Please sign in to comment.