|
1 | | -from drf_yasg import openapi |
2 | | -from drf_yasg.utils import swagger_auto_schema |
3 | | -from rest_framework import status |
4 | | -from rest_framework.request import Request |
| 1 | +from django.contrib.contenttypes.models import ContentType |
| 2 | +from django.db.models import Q |
| 3 | +from rest_framework.generics import CreateAPIView |
5 | 4 | from rest_framework.response import Response |
6 | 5 | from rest_framework.views import APIView |
7 | 6 |
|
8 | | -from feed.constants import SupportedModel, SupportedQuerySet, FeedItemType, model_mapping |
9 | | -from feed.helpers import collect_querysets, paginate_serialize_feed, add_pagination |
10 | 7 | from feed.pagination import FeedPagination |
11 | 8 |
|
| 9 | +from news.models import News |
| 10 | +from news.serializers import NewsFeedListSerializer |
| 11 | +from projects.models import Project |
| 12 | +from vacancy.models import Vacancy |
12 | 13 |
|
13 | | -class FeedList(APIView): |
14 | | - pagination_class = FeedPagination |
15 | 14 |
|
16 | | - @swagger_auto_schema( |
17 | | - responses={ |
18 | | - 200: openapi.Response( |
19 | | - description="List of some news: new projects, vacancies, project, users and program news", |
20 | | - schema=openapi.Schema( |
21 | | - type=openapi.TYPE_ARRAY, |
22 | | - items=openapi.Schema( |
23 | | - type=openapi.TYPE_OBJECT, |
24 | | - description="Feed item", |
25 | | - properties={ |
26 | | - "type": openapi.TYPE_STRING, |
27 | | - "content": openapi.TYPE_OBJECT, |
28 | | - }, |
29 | | - ), |
30 | | - ), |
31 | | - ) |
32 | | - } |
33 | | - ) |
34 | | - def get(self, request: Request, *args, **kwargs) -> Response: |
35 | | - prepared_data, sum_pages = self.paginate_serialize_data( |
36 | | - self.get_response_data(self.get_request_data()) |
37 | | - ) |
38 | | - for obj in prepared_data: |
39 | | - obj["type_model"] = obj["type_model"].lower() |
40 | | - return Response( |
41 | | - status=status.HTTP_200_OK, |
42 | | - data=add_pagination(prepared_data, sum_pages), |
43 | | - ) |
| 15 | +class NewSimpleFeed(APIView): |
| 16 | + serializator_class = NewsFeedListSerializer |
| 17 | + pagination_class = FeedPagination |
44 | 18 |
|
45 | | - def get_request_data(self) -> list[SupportedModel]: |
| 19 | + def get_filter_data(self): |
46 | 20 | filter_queries = self.request.query_params.get("type") |
47 | 21 | filter_queries = filter_queries if filter_queries else "" # existence check |
48 | 22 |
|
49 | | - models = [ |
50 | | - model_mapping[model_name] |
51 | | - for model_name in model_mapping.keys() |
52 | | - if model_name.lower() in filter_queries |
53 | | - ] |
54 | | - return models |
| 23 | + news_types = filter_queries.split("|") |
| 24 | + if "news" in news_types: |
| 25 | + news_types.append("customuser") |
| 26 | + return news_types |
55 | 27 |
|
56 | | - def get_response_data( |
57 | | - self, models: list[SupportedModel] |
58 | | - ) -> dict[FeedItemType, SupportedQuerySet]: |
59 | | - return {model.__name__: collect_querysets(model) for model in models} |
| 28 | + def get_queryset(self): |
| 29 | + filters = self.get_filter_data() |
| 30 | + queryset = ( |
| 31 | + News.objects.select_related("content_type") |
| 32 | + .prefetch_related("content_object", "files") |
| 33 | + .filter(content_type__model__in=filters) |
| 34 | + .order_by("-datetime_created") |
| 35 | + ) |
| 36 | + # временное удаление постов для проектов с текстом |
| 37 | + return queryset.exclude(~Q(text=""), content_type__model="project") |
60 | 38 |
|
61 | | - def paginate_serialize_data( |
62 | | - self, get_model_data: dict[FeedItemType, SupportedQuerySet] |
63 | | - ) -> tuple[list[dict], int]: |
| 39 | + def get(self, *args, **kwargs): |
64 | 40 | paginator = self.pagination_class() |
65 | | - return paginate_serialize_feed(get_model_data, paginator, self.request, self) |
| 41 | + paginated_data = paginator.paginate_queryset(self.get_queryset(), self.request) |
| 42 | + serializer = NewsFeedListSerializer(paginated_data, many=True) |
| 43 | + |
| 44 | + new_data = [] |
| 45 | + # временная подстройка данных под фронт |
| 46 | + for data in serializer.data: |
| 47 | + if data["type_model"] in ["project", "vacancy", None]: |
| 48 | + fomated_data = { |
| 49 | + "type_model": data["type_model"], |
| 50 | + "content": data["content_object"], |
| 51 | + } |
| 52 | + elif data["type_model"] == "news": |
| 53 | + del data["type_model"] |
| 54 | + fomated_data = {"type_model": "news", "content": data} |
| 55 | + new_data.append(fomated_data) |
| 56 | + |
| 57 | + return paginator.get_paginated_response(new_data) |
| 58 | + |
| 59 | + |
| 60 | +class DevScript(CreateAPIView): |
| 61 | + def create(self, request): |
| 62 | + content_type = ContentType.objects.filter(model="project").first() |
| 63 | + for project in Project.objects.filter(draft=False): |
| 64 | + if not News.objects.filter( |
| 65 | + content_type=content_type, object_id=project.id |
| 66 | + ).exists(): |
| 67 | + News.objects.create( |
| 68 | + content_type=content_type, |
| 69 | + object_id=project.id, |
| 70 | + datetime_created=project.datetime_created, |
| 71 | + ) |
| 72 | + |
| 73 | + content_type = ContentType.objects.filter(model="vacancy").first() |
| 74 | + for vacancy in Vacancy.objects.filter(is_active=True): |
| 75 | + if not News.objects.filter( |
| 76 | + content_type=content_type, object_id=vacancy.id |
| 77 | + ).exists(): |
| 78 | + News.objects.create( |
| 79 | + content_type=content_type, |
| 80 | + object_id=vacancy.id, |
| 81 | + datetime_created=vacancy.datetime_created, |
| 82 | + ) |
| 83 | + return Response({"status": "success"}, status=201) |
0 commit comments