Skip to content

Commit

Permalink
Добавил роли для users.models
Browse files Browse the repository at this point in the history
  • Loading branch information
VitalRu committed Sep 22, 2023
1 parent d2fd84b commit 164b75d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 27 deletions.
45 changes: 18 additions & 27 deletions backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django_filters.rest_framework import DjangoFilterBackend
from djoser.serializers import SetPasswordSerializer
from djoser.views import UserViewSet as DjoserViewSet
from rest_framework import exceptions, status, viewsets
from rest_framework import status, viewsets
from rest_framework.decorators import action
from rest_framework.generics import get_object_or_404
from rest_framework.pagination import PageNumberPagination
Expand Down Expand Up @@ -72,33 +72,24 @@ def subscriptions(self, request):
)
def subscribe(self, request, id=None):
author = get_object_or_404(User, id=id)
if request.method == 'POST':
if request.user.id == author.id:
raise exceptions.ValidationError(
'Unable to subscribe to yourself'
)
else:
serializer = FollowSerializer(
Follow.objects.create(user=request.user, author=author),
context={'request': request},
)
return Response(
serializer.data,
status=status.HTTP_201_CREATED
)
elif request.method == 'DELETE':
follow_bm = Follow.objects.filter(
user=request.user,
author=author,
user = request.user
follower = user.follower.filter(author=author)
if ((request.method == 'POST') and (user.id != author.id)
and not follower.exists()):
serializer = FollowSerializer(
Follow.objects.create(user=user, author=author),
context={'request': request},
)
return Response(
serializer.data,
status=status.HTTP_201_CREATED,
)
if follow_bm.exists():
follow_bm.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
else:
return Response(
{'errors': 'No such author'},
status=status.HTTP_400_BAD_REQUEST,
)
if request.method == 'DELETE':
get_object_or_404(
Follow, user=user, author=author
).delete()
return Response(status=status.HTTP_204_NO_CONTENT)

return Response(
{'errors': 'Invalid operation'},
status=status.HTTP_400_BAD_REQUEST,
Expand Down
18 changes: 18 additions & 0 deletions backend/users/migrations/0008_user_role.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2 on 2023-09-22 05:59

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('users', '0007_alter_follow_options'),
]

operations = [
migrations.AddField(
model_name='user',
name='role',
field=models.CharField(choices=[('admin', 'ADMIN'), ('user', 'USER')], default='user', max_length=15),
),
]
15 changes: 15 additions & 0 deletions backend/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
class User(AbstractUser):
"""Кастомная модель пользователя."""

ADMIN = 'admin'
MODERATOR = 'moderator'
USER = 'user'

ROLE_CHOICES = (
(ADMIN, 'ADMIN'), (USER, 'USER')
)

email = models.EmailField('email адрес', unique=True)
username = models.CharField(
'имя пользователя',
Expand All @@ -16,6 +24,9 @@ class User(AbstractUser):
first_name = models.CharField('имя', max_length=150)
last_name = models.CharField('фамилия', max_length=150)
password = models.CharField('пароль', max_length=150)
role = models.CharField(
max_length=15, choices=ROLE_CHOICES, default='user'
)

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username', 'password', 'first_name', 'last_name']
Expand All @@ -26,6 +37,10 @@ def get_full_name(self):

return f'{self.first_name} {self.last_name}'

@property
def is_admin(self):
return self.role == 'admin'

class Meta:
db_table = 'auth_user'
ordering = ['id']
Expand Down

0 comments on commit 164b75d

Please sign in to comment.