-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pro-111: started working on feed, create view and helpers
- Loading branch information
Showing
10 changed files
with
81 additions
and
2 deletions.
There are no files selected for viewing
Empty file.
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,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class FeedConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "feed" |
Empty file.
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,19 @@ | ||
from projects.models import Project | ||
|
||
|
||
def get_n_random_projects(num: int) -> list[Project]: | ||
tries = 3 | ||
projects = set() | ||
|
||
while len(projects) < num and tries > 0: | ||
project = Project.objects.filter(draft=False).order_by("?").first() | ||
|
||
if project not in projects: | ||
projects.add(project) | ||
else: | ||
tries -= 1 | ||
return list(projects) | ||
|
||
|
||
def get_n_latest_created_projects(num: int) -> list[Project]: | ||
return list(Project.objects.filter(draft=False).order_by("-datetime_created")[:num]) |
Empty file.
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,6 @@ | ||
from rest_framework import serializers | ||
|
||
|
||
class FeedListSerializer(serializers.Serializer): | ||
# todo: add type for feed news item | ||
pass |
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,9 @@ | ||
from django.urls import path | ||
|
||
from feed.views import FeedList | ||
|
||
app_name = "feed" | ||
|
||
urlpatterns = [ | ||
path("", FeedList.as_view()), | ||
] |
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,39 @@ | ||
from drf_yasg import openapi | ||
from drf_yasg.utils import swagger_auto_schema | ||
from rest_framework import status | ||
from rest_framework.request import Request | ||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
|
||
from feed.helpers import get_n_random_projects, get_n_latest_created_projects | ||
from projects.serializers import ProjectListSerializer | ||
|
||
|
||
class FeedList(APIView): | ||
@swagger_auto_schema( | ||
responses={ | ||
200: openapi.Response( | ||
description="List of some news: new projects, vacancies, project, users and program news", | ||
schema=openapi.Schema( | ||
type=openapi.TYPE_ARRAY, | ||
items=openapi.Schema( | ||
type=openapi.TYPE_OBJECT, | ||
description="Feed item", | ||
), | ||
), | ||
) | ||
} | ||
) | ||
def get(self, request: Request, *args, **kwargs) -> Response: | ||
return Response(status=status.HTTP_200_OK, data=collect_feed()) | ||
|
||
|
||
def collect_feed() -> list: | ||
n_random_projects = get_n_random_projects(3) | ||
n_latest_created_projects = get_n_latest_created_projects(3) | ||
serializer = ProjectListSerializer( | ||
data=set(n_random_projects + n_latest_created_projects), many=True | ||
) | ||
|
||
serializer.is_valid() | ||
return serializer.data |
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