-
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.
* Add app ratings, create test for ratings and tasks * Some refactoring * Delete ipr/migrations directory * Исправил комментарии с собрания v2 * Fix isort
- Loading branch information
1 parent
486ae52
commit 997ccba
Showing
14 changed files
with
317 additions
and
160 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from rest_framework import serializers | ||
|
||
from ratings.models import Rating | ||
|
||
|
||
class RatingSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Rating | ||
fields = [ | ||
"id", | ||
"content_type", | ||
"object_id", | ||
"user", | ||
"rating", | ||
"created_at", | ||
] | ||
read_only_fields = ["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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from rest_framework import generics, status | ||
from rest_framework.generics import get_object_or_404 | ||
from rest_framework.response import Response | ||
|
||
from api.v1.serializers.ratings import RatingSerializer | ||
from ipr.models import IPR | ||
from ratings.models import Rating | ||
from tasks.models import Task | ||
|
||
|
||
class RatingCreateListView(generics.CreateAPIView, generics.ListAPIView): | ||
queryset = Rating.objects.all() | ||
serializer_class = RatingSerializer | ||
content_model = None | ||
|
||
def get_content_object(self): | ||
object_id = self.kwargs.get(f"{self.content_model}_id") | ||
return get_object_or_404(self.content_model, id=object_id) | ||
|
||
def get_queryset(self): | ||
content_object = self.get_content_object() | ||
return Rating.objects.filter( | ||
content_type__model=self.content_model, object_id=content_object.id | ||
) | ||
|
||
def post(self, request, *args, **kwargs): | ||
content_object = self.get_content_object() | ||
|
||
serializer = self.get_serializer(data=request.data) | ||
if serializer.is_valid(): | ||
serializer.save( | ||
content_object=content_object, user=self.request.user | ||
) | ||
return Response(serializer.data, status=status.HTTP_201_CREATED) | ||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
|
||
class TaskRatingCreateView(RatingCreateListView): | ||
content_model = Task | ||
|
||
|
||
class IPRRatingCreateView(RatingCreateListView): | ||
content_model = IPR |
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 was deleted.
Oops, something went wrong.
File renamed without changes.
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,17 @@ | ||
from django.contrib import admin | ||
|
||
from ratings.models import Rating | ||
|
||
|
||
class RatingAdmin(admin.ModelAdmin): | ||
list_display = ( | ||
"content_type", | ||
"object_id", | ||
"content_object", | ||
"user", | ||
"rating", | ||
"created_at", | ||
) | ||
|
||
|
||
admin.site.register(Rating, RatingAdmin) |
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,7 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class RatingsConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "ratings" | ||
verbose_name = "Оценка" |
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,28 @@ | ||
from django.conf import settings | ||
from django.contrib.auth import get_user_model | ||
from django.contrib.contenttypes.fields import GenericForeignKey | ||
from django.contrib.contenttypes.models import ContentType | ||
from django.db import models | ||
|
||
User = get_user_model() | ||
|
||
|
||
class Rating(models.Model): | ||
content_type = models.ForeignKey( | ||
ContentType, | ||
on_delete=models.CASCADE, | ||
limit_choices_to={"model__in": ["task", "ipr"]}, | ||
) | ||
object_id = models.PositiveIntegerField() | ||
content_object = GenericForeignKey("content_type", "object_id") | ||
|
||
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True) | ||
rating = models.IntegerField(choices=settings.RATING_CHOICES) | ||
created_at = models.DateTimeField(auto_now_add=True) | ||
|
||
class Meta: | ||
verbose_name = "Оценки" | ||
verbose_name_plural = "Оценки" | ||
|
||
def __str__(self): | ||
return f"{self.content_object} - {self.rating} от {self.user}" |
Oops, something went wrong.