-
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 4 commits
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,19 @@ | ||
from django.contrib.auth import get_user_model | ||
from django.shortcuts import get_object_or_404 | ||
from rest_framework import permissions | ||
|
||
User = get_user_model() | ||
|
||
|
||
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.team == executor.team | ||
and user.is_boss() | ||
): | ||
return True | ||
return False |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from django.contrib.auth import get_user_model | ||
from rest_framework import serializers | ||
|
||
from api.v1.serializers.api.users_serializer import CustomUserSerializer | ||
from api.v1.serializers.task import TaskSerializer | ||
from core.statuses_for_ipr_tests import Status | ||
from ipr.models import IPR | ||
|
||
User = get_user_model() | ||
|
||
|
||
class ExecutorSerializer(CustomUserSerializer): | ||
class Meta: | ||
fields = ( | ||
"first_name", | ||
"last_name", | ||
"patronymic", | ||
"position", | ||
"userpic", | ||
) | ||
model = User | ||
|
||
|
||
class IPRSerializer(serializers.ModelSerializer): | ||
tasks = TaskSerializer(many=True, read_only=True) | ||
creator = serializers.PrimaryKeyRelatedField(read_only=True) | ||
executor = ExecutorSerializer(read_only=True) | ||
status = serializers.SerializerMethodField() | ||
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. Не понимаю, зачем тут переопределять 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. Creato - переопределил, что бы при создании ИПР он не запрашивался, а автоматически проставлялся в методе performe_create во view. Status - что бы у статуса было актуальное значение. Но с учетом того что мы делаем это через сигналы, то я могу либо оставить пока как есть либо удалить и ждать пока будут готовы сигналы 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. В смысле, у тебя же для создания ИПР другой сериализатор. 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. Да, а этот для отображения при GET запросе, при создании статус ставится автоматически в работе 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 = ( | ||
"id", | ||
"title", | ||
"creator", | ||
"executor", | ||
"creation_date", | ||
"start_date", | ||
"end_date", | ||
"status", | ||
"tasks", | ||
) | ||
model = IPR | ||
|
||
def get_status(self, obj): | ||
if obj.status == Status.IN_PROGRESS and obj.start_date > obj.end_date: | ||
obj.status = Status.TRAIL | ||
return obj.status | ||
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. Это лучше сделать через сигналы, @greenpandorik взялся это делать. 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. Таким образом, без 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. мне тогда пока оставить как есть? как появятся сигналы поменяю 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. ок 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 IPRSerializerPost(serializers.ModelSerializer): | ||
creator = serializers.PrimaryKeyRelatedField(read_only=True) | ||
executor = serializers.PrimaryKeyRelatedField(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 = ( | ||
"id", | ||
"title", | ||
"creator", | ||
"executor", | ||
"creation_date", | ||
"start_date", | ||
"end_date", | ||
"status", | ||
) | ||
model = IPR |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
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.api.ipr_serializers import ( | ||
IPRSerializer, | ||
IPRSerializerPost, | ||
) | ||
from core.statuses_for_ipr_tests import Status | ||
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): | ||
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:
ordering = ["-creation_date"] 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. Так же во viewset можно ограничить количество доступных HTTP-методов. |
||
return IPR.objects.select_related("executor", "creator") | ||
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. При запросе на |
||
|
||
def get_serializer_class(self): | ||
if self.request.method == "POST" or self.request.method == "PATCH": | ||
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. Можно короче: if self.request.method in ("POST", "PATH"): |
||
return IPRSerializerPost | ||
return IPRSerializer | ||
|
||
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: | ||
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. У нас весь проект на if self.request.method not in permissions.SAFE_METHODS:
self.permission_classes = [TeamBossPermission]
return super(IPRViewSet, self).get_permissions() |
||
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], | ||
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. Тут тоже можно не указывать |
||
) | ||
def status(self, request, ipr_id): | ||
"""Дополнительный эндпоинт и View-функция | ||
для отображения прогресса выполнения ИПР""" | ||
ipr = get_object_or_404(IPR, id=ipr_id) | ||
tasks_without_trail = ipr.tasks.exclude(status=Status.TRAIL).count() | ||
tasks_is_complete = ipr.tasks.filter(status=Status.COMPLETE).count() | ||
progress = tasks_without_trail / 100 * tasks_is_complete | ||
|
||
context = { | ||
"request": request, | ||
"progress": progress, | ||
} | ||
serializer = IPRSerializer(ipr, context=context) | ||
return Response(serializer.data) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from django.db import models | ||
|
||
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 Status(models.TextChoices): | ||
COMPLETE = "complete", "Выполнен" | ||
IN_PROGRESS = "in_progress", "В работе" | ||
CANCEL = "cancel", "Отменен" | ||
TRAIL = "trail", "Отстает" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,24 +7,22 @@ | |
class IPRAdmin(admin.ModelAdmin): | ||
list_display = ( | ||
"title", | ||
"description", | ||
"creator", | ||
"creation_date", | ||
"start_date", | ||
"end_date", | ||
"status", | ||
"executor", | ||
) | ||
search_fields = ("title", "executor", "author") | ||
list_filter = ( | ||
"creation_date", | ||
"start_date", | ||
"status", | ||
"creator", | ||
"executor", | ||
"title", | ||
"end_date", | ||
) | ||
ordering = ("-creation_date",) | ||
ordering = ("-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. В админку тоже можно вернуть |
||
def get_queryset(self, request): | ||
qs = super().get_queryset(request) | ||
|
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.
надо добавить поле id