diff --git a/partner_programs/views.py b/partner_programs/views.py index 36acf5cb..c520ac3b 100644 --- a/partner_programs/views.py +++ b/partner_programs/views.py @@ -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] @@ -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): diff --git a/projects/serializers.py b/projects/serializers.py index dccfb049..05371f30 100644 --- a/projects/serializers.py +++ b/projects/serializers.py @@ -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 diff --git a/vacancy/serializers.py b/vacancy/serializers.py index 8e936254..5b774b4a 100644 --- a/vacancy/serializers.py +++ b/vacancy/serializers.py @@ -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 @@ -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 @@ -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,