-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create api files for IPR #38
Changes from 1 commit
f4ee39e
b5bbdfe
f079680
066bd1a
8fb3254
33d51ce
2a6c661
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from django_filters import rest_framework as filters | ||
|
||
from ipr.models import IPR | ||
|
||
|
||
class IPRFilter(filters.FilterSet): | ||
executor = filters.CharFilter() | ||
|
||
class Meta: | ||
model = IPR | ||
fields = ["executor"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from django.contrib.auth import get_user_model | ||
from django.shortcuts import get_object_or_404 | ||
from rest_framework import permissions | ||
|
||
from users.models import MiddleUsersTeams, Team | ||
|
||
User = get_user_model() | ||
|
||
|
||
class BossPermission(permissions.BasePermission): | ||
def has_permission(self, request, view): | ||
user = request.user | ||
executor_id = request.data.get("executor") | ||
if ( | ||
user.is_authenticated | ||
and request.method in permissions.SAFE_METHODS | ||
): | ||
return True | ||
if user.is_authenticated: | ||
team_id = get_object_or_404(Team, boss_id=request.user.id) | ||
if MiddleUsersTeams.objects.filter( | ||
MiddleUsersTeams, | ||
team_id=team_id, | ||
user_id=executor_id, | ||
): | ||
return True | ||
return False | ||
|
||
|
||
class TeamBossPermission(permissions.BasePermission): | ||
def has_permission(self, request, view): | ||
user = request.user | ||
executor_id = view.kwargs.get("user_id") | ||
executor = get_object_or_404(User, id=executor_id) | ||
if ( | ||
user.is_authenticated | ||
and user.get_team() == executor.get_team() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тоже самое, |
||
and user.is_boss() | ||
): | ||
return True | ||
return False |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from django.contrib.auth import get_user_model | ||
from rest_framework import serializers | ||
|
||
from ipr.models import IPR | ||
|
||
User = get_user_model() | ||
|
||
|
||
class IPRSerializer(serializers.ModelSerializer): | ||
creator = serializers.SlugRelatedField( | ||
slug_field="username", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Почему тут |
||
read_only=True, | ||
) | ||
executor = serializers.RelatedField(read_only=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это что делает? Почему не |
||
|
||
class Meta: | ||
fields = ( | ||
"title", | ||
"creator", | ||
"creation_date", | ||
"start_date", | ||
"end_date", | ||
"status", | ||
"executor", | ||
) | ||
model = IPR |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from django.shortcuts import get_object_or_404 | ||
from django_filters.rest_framework import DjangoFilterBackend | ||
from rest_framework import permissions | ||
from rest_framework.decorators import action | ||
from rest_framework.response import Response | ||
from rest_framework.viewsets import ModelViewSet | ||
|
||
from api.v1.filters import IPRFilter | ||
from api.v1.permissions import TeamBossPermission | ||
from api.v1.serializers.ipr_serializers import IPRSerializer | ||
from ipr.models import IPR | ||
from users.models import User | ||
|
||
|
||
class IPRViewSet(ModelViewSet): | ||
serializer_class = IPRSerializer | ||
filter_backends = [DjangoFilterBackend] | ||
filterset_class = IPRFilter | ||
|
||
class Meta: | ||
ordering = ["-creation_date"] | ||
|
||
def get_queryset(self): | ||
return IPR.objects.select_related("executor", "creator") | ||
|
||
def perform_create(self, serializer): | ||
executor_id = self.kwargs.get("user_id") | ||
executor = get_object_or_404(User, id=executor_id) | ||
serializer.save(creator=self.request.user, executor=executor) | ||
|
||
def get_permissions(self): | ||
if self.request.method in permissions.SAFE_METHODS: | ||
self.permission_classes = [permissions.IsAuthenticated] | ||
else: | ||
self.permission_classes = [ | ||
TeamBossPermission, | ||
] | ||
return super(IPRViewSet, self).get_permissions() | ||
|
||
@action( | ||
detail=True, | ||
methods=["get"], | ||
permission_classes=[permissions.IsAuthenticated], | ||
) | ||
def status(self, request, ipr_id): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Сюда бы докстринг |
||
ipr = get_object_or_404(IPR, id=ipr_id) | ||
executor_id = self.kwargs.get("user_id") | ||
executor = get_object_or_404(User, id=executor_id) | ||
tasks_without_trail = ipr.tasks.exclude(status="trail").count() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут можно не хардкодить trail и complete, а взять из статусов. |
||
tasks_is_complete = ipr.tasks.filter(status="complete").count() | ||
progress = tasks_without_trail / 100 * tasks_is_complete | ||
context = { | ||
"request": request, | ||
"progress": progress, | ||
"executor": executor, | ||
} | ||
serializer = IPRSerializer(ipr, context=context) | ||
return Response(serializer.data) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,41 +3,37 @@ | |
|
||
User = get_user_model() | ||
|
||
|
||
STATUSES = [ | ||
("STATUS_ABSENT", "Отсутствует"), | ||
("STATUS_COMPLETED", "Выполнен"), | ||
("STATUS_NOT_COMPLETED", "Не выполнен"), | ||
("STATUS_IN_PROGRESS", "В работе"), | ||
("STATUS_CANCELLED", "Отменен"), | ||
("STATUS_DELAYED", "Отстает"), | ||
("complete", "Выполнен"), | ||
("in_progress", "В работе"), | ||
("canceled", "Отменен"), | ||
("trail", "Отстает"), | ||
] | ||
|
||
|
||
class IPR(models.Model): | ||
title = models.CharField("Название ИПР", max_length=100) | ||
description = models.CharField("Описание ИПР", max_length=500) | ||
creator = models.ForeignKey( | ||
User, | ||
on_delete=models.CASCADE, | ||
verbose_name="Создатель ИПР", | ||
related_name="created_ipr", | ||
) | ||
creation_date = models.DateField("Дата создания ИПР", auto_now_add=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Дату создания не надо удалять в модели. |
||
start_date = models.DateField("Дата начала работ по ИПР") | ||
start_date = models.DateField( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Почему тут появились |
||
"Дата начала работ по ИПР", blank=True, null=True | ||
) | ||
end_date = models.DateField("Дедлайн ИПР") | ||
status = models.CharField("Статус ИПР", max_length=20, choices=STATUSES) | ||
status = models.CharField( | ||
"Статус ИПР", max_length=20, choices=STATUSES, default=STATUSES[0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
) | ||
executor = models.ForeignKey( | ||
User, | ||
verbose_name="Исполнитель ИПР", | ||
on_delete=models.CASCADE, | ||
related_name="ipr", | ||
) | ||
usability = models.PositiveSmallIntegerField( | ||
verbose_name="Удобство использования ИПР", default=0 | ||
) | ||
ease_of_creation = models.PositiveSmallIntegerField( | ||
verbose_name="Удобство создания ИПР", default=0 | ||
) | ||
|
||
class Meta: | ||
verbose_name = "ИПР" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3 дня назад упростил модель
user
, там уже нету моделиMiddleUsersTeams