Skip to content

Commit

Permalink
PRO-389: User already exists
Browse files Browse the repository at this point in the history
When registering a new user via tilde, the
functionality works as before, but if a user with such an
email is already in the system, then no edits are made to
his profile, he is added to the program with the sent data.
  • Loading branch information
pavuchara committed Aug 5, 2024
1 parent 8b87b4a commit 955c663
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 77 deletions.
46 changes: 25 additions & 21 deletions partner_programs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ def get(self, request, *args, **kwargs):

class PartnerProgramCreateUserAndRegister(generics.GenericAPIView):
"""
Create new user and register him to program and save additional data
Create new user and register him to program and save additional data.
If a user with such an email already exists in the system, then his profile
remains the same, but he registers in the program with the specified data.
"""

permission_classes = [AllowAny]
Expand Down Expand Up @@ -93,32 +95,34 @@ def post(self, request, *args, **kwargs):
"patronymic",
"city",
)
try:
user = User.objects.create(
user, created = User.objects.get_or_create(
email=email,
defaults={
"birthday": date_to_iso(data.get("birthday", "01-01-1900")),
"is_active": True, # bypass email verification
"onboarding_stage": None, # bypass onboarding
"verification_date": timezone.now(), # bypass ClickUp verification
**{field_name: data.get(field_name, "") for field_name in user_fields},
birthday=date_to_iso(data.get("birthday", "01-01-1900")),
is_active=True, # bypass email verification
onboarding_stage=None, # bypass onboarding
verification_date=timezone.now(), # bypass ClickUp verification
email=email,
}
)
if created: # Only when registering a new user.
user.set_password(password)
user.save()

user_profile_program_data = {
k: v for k, v in data.items() if k not in user_fields and k != "password"
}
try:
PartnerProgramUserProfile.objects.create(
partner_program_data=user_profile_program_data,
user=user,
partner_program=program,
)
except IntegrityError:
return Response(
data={"detail": "User with this email already exists."},
data={"detail": "User has already registered in this program."},
status=status.HTTP_400_BAD_REQUEST,
)

user.set_password(password)
user.save()

user_profile_program_data = {
k: v for k, v in data.items() if k not in user_fields and k != "password"
}
PartnerProgramUserProfile.objects.create(
partner_program_data=user_profile_program_data,
user=user,
partner_program=program,
)
return Response(status=status.HTTP_201_CREATED)

def get(self, request, *args, **kwargs):
Expand Down
25 changes: 24 additions & 1 deletion projects/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,34 @@
from industries.models import Industry
from projects.models import Project, Achievement, Collaborator, ProjectNews
from projects.validators import validate_project
from vacancy.serializers import ProjectVacancyListSerializer
from vacancy.models import Vacancy

User = get_user_model()


class ProjectVacancyListSerializer(serializers.ModelSerializer):
datetime_closed = serializers.DateTimeField(read_only=True)
response_count = serializers.SerializerMethodField(read_only=True)
required_skills = SkillToObjectSerializer(many=True, read_only=True)

class Meta:
model = Vacancy
fields = [
"id",
"role",
"required_skills",
"description",
"project",
"is_active",
"datetime_closed",
"response_count",
]

def get_response_count(self, obj):
"""Returns count non status responses."""
return obj.vacancy_requests.filter(is_approved=None).count()


class AchievementListSerializer(serializers.ModelSerializer):
class Meta:
model = Achievement
Expand Down
56 changes: 1 addition & 55 deletions vacancy/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

from core.models import Skill, SkillToObject
from core.serializers import SkillToObjectSerializer
from core.services import get_views_count
from files.models import UserFile
from files.serializers import UserFileSerializer
from projects.models import Project
from projects.validators import validate_project
from projects.serializers import ProjectListSerializer
from users.serializers import UserDetailSerializer
from vacancy.models import Vacancy, VacancyResponse

Expand Down Expand Up @@ -36,25 +35,6 @@ def get_response_count(self, obj):
return obj.vacancy_requests.filter(is_approved=None).count()


class ProjectVacancyListSerializer(
serializers.ModelSerializer,
AbstractVacancyReadOnlyFields,
RequiredSkillsSerializerMixin[Vacancy],
):
class Meta:
model = Vacancy
fields = [
"id",
"role",
"required_skills",
"description",
"project",
"is_active",
"datetime_closed",
"response_count",
]


class ProjectForVacancySerializer(serializers.ModelSerializer[Project]):
class Meta:
model = Project
Expand Down Expand Up @@ -112,40 +92,6 @@ class Meta:
]


class ProjectListSerializer(serializers.ModelSerializer):
views_count = serializers.SerializerMethodField(method_name="count_views")
short_description = serializers.SerializerMethodField()

@classmethod
def count_views(cls, project):
return get_views_count(project)

@classmethod
def get_short_description(cls, project):
return project.get_short_description()

class Meta:
model = Project
fields = [
"id",
"name",
"leader",
"short_description",
"image_address",
"industry",
"views_count",
]

read_only_fields = ["leader", "views_count"]

def is_valid(self, *, raise_exception=False):
return super().is_valid(raise_exception=raise_exception)

def validate(self, data):
super().validate(data)
return validate_project(data)


class ProjectVacancyCreateListSerializer(
serializers.ModelSerializer,
AbstractVacancyReadOnlyFields,
Expand Down

0 comments on commit 955c663

Please sign in to comment.