-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from Pet-projects-CodePET/feature/profile
[+] Модель профиля
- Loading branch information
Showing
23 changed files
with
669 additions
and
20 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from rest_framework import permissions | ||
|
||
|
||
class IsOwnerOrReadOnly(permissions.BasePermission): | ||
def has_object_permission(self, request, view, obj): | ||
if request.method in permissions.SAFE_METHODS: | ||
return True | ||
|
||
return obj.owner or obj.creator == request.user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from rest_framework import generics, serializers | ||
|
||
from apps.profile.models import Profile, UserSkill, UserSpecialization | ||
from apps.projects.models import Skill, Specialist | ||
|
||
|
||
class ProfileSerializer(serializers.Serializer): | ||
"""Модель сериализатора на просмотр профиля с учетом выбора видимости контактов""" | ||
|
||
class Meta: | ||
model = Profile | ||
fields = "__all__" | ||
|
||
def to_representation(self, instance): | ||
user = self.context["request"].user | ||
visible_status_contacts = instance.visible_status_contacts | ||
if visible_status_contacts in [Profile.NOBODY] or ( | ||
not user.is_organizer | ||
and visible_status_contacts in [Profile.CREATOR_ONLY] | ||
): | ||
self.fields.pop("phone_number") | ||
self.fields.pop("email") | ||
self.fields.pop("telegram") | ||
|
||
return super().to_representation(instance) | ||
|
||
|
||
class ProfileUpdateSerializer(serializers.ModelSerializer): | ||
"""Сериализатор на редактирование профиля пользователя.""" | ||
|
||
user = serializers.HiddenField(default=serializers.CurrentUserDefault()) | ||
|
||
class Meta: | ||
model = Profile | ||
fields = "__all__" | ||
|
||
|
||
class UserSkillSerializer(serializers.ModelSerializer): | ||
skills = serializers.PrimaryKeyRelatedField( | ||
queryset=Skill.objects.all(), many=True | ||
) | ||
|
||
class Meta: | ||
model = UserSkill | ||
fields = ["user", "skill"] | ||
validators = [ | ||
serializers.UniqueTogetherValidator( | ||
queryset=UserSkill.objects.all(), | ||
fields=["user", "skill"], | ||
message="Этот навык вами уже был выбран", | ||
) | ||
] | ||
|
||
|
||
class UserSpecializationSerializer(serializers.ModelSerializer): | ||
specialization = serializers.PrimaryKeyRelatedField( | ||
queryset=Specialist.objects.all(), many=True | ||
) | ||
|
||
class Meta: | ||
model = UserSpecialization | ||
fields = ["user", "specialization"] | ||
validators = [ | ||
serializers.UniqueTogetherValidator( | ||
queryset=UserSpecialization.objects.all(), | ||
fields=["user", "specialization"], | ||
message="Этот специализация вами уже была выбрана", | ||
) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from django.contrib.auth.models import User | ||
from django.db.models.signals import post_save | ||
from django.dispatch import receiver | ||
|
||
from apps.profile.models import Profile | ||
|
||
|
||
@receiver(post_save, sender=User) | ||
def create_user_profile(sender, instance, created, **kwargs): | ||
"""Функция автоматического создания профиля при создании пользователя""" | ||
if created: | ||
Profile.objects.create(user=instance) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from django.urls import include, path | ||
|
||
from api.v1.profile.views import ProfileListAPIView, ProfileView | ||
|
||
urlpatterns = [ | ||
path("profiles/", ProfileListAPIView.as_view(), name="profile-list"), | ||
path("profiles/<pk>", ProfileView.as_view(), name="profile"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from django.db.models import Q | ||
from rest_framework import generics | ||
|
||
from api.v1.profile.permissions import IsOwnerOrReadOnly | ||
from api.v1.profile.serializers import ( | ||
ProfileSerializer, | ||
ProfileUpdateSerializer, | ||
) | ||
from apps.profile.models import Profile | ||
from apps.projects.models import Project | ||
|
||
|
||
class ProfileView(generics.UpdateAPIView): | ||
"""Представление на изменение данных в профиле""" | ||
|
||
queryset = Profile.objects.all() | ||
serializer_class = ProfileUpdateSerializer | ||
permission_classes = [IsOwnerOrReadOnly] | ||
|
||
|
||
class ProfileListAPIView(generics.ListAPIView): | ||
"""Список профилей в зависимости от их видимости""" | ||
|
||
serializer_class = ProfileSerializer | ||
|
||
def get_queryset(self): | ||
user = self.request.user | ||
queryset = Profile.objects.all() | ||
is_organizer = user.is_authenticated and user.is_organizer | ||
if is_organizer: | ||
queryset = queryset.filter( | ||
visible_status__in=[Profile.ALL, Profile.CREATOR_ONLY] | ||
) | ||
else: | ||
queryset = queryset.filter(visible_status=Profile.ALL) | ||
|
||
return queryset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
BOOL_CHOICES = [(True, "Готов"), (False, "Не готов")] | ||
MAX_LENGTH_NAME = 30 | ||
MAX_LENGTH_COUNTRY = 255 | ||
MAX_LENGTH_CITY = 255 | ||
MAX_LENGTH_ABOUT = 750 | ||
MAX_LENGTH_URL = 256 | ||
MIN_LENGTH_NAME = 2 | ||
MIN_LENGTH_ABOUT = 50 | ||
MIN_LENGTH_PORTFOLIO = 5 | ||
VISIBLE_CHOICES = [ | ||
(1, "All"), | ||
(2, "Only creator"), | ||
(3, "Nobody"), | ||
] |
Oops, something went wrong.