Skip to content

Commit

Permalink
Order posts by category and creation date
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Feb 25, 2020
1 parent f6c8913 commit 9be4595
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
10 changes: 7 additions & 3 deletions scraper/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

class Post(BaseModel):

TYPE_ETP = "ETP"
TYPE_EPS = "EPS"
TYPE_TWP = "TWP"

TYPE_CHOICES = (
("ETP", "ELTIEMPO"),
("EPS", "ELPAIS"),
("TWP", "TWP"),
(TYPE_ETP, "ELTIEMPO"),
(TYPE_EPS, "ELPAIS"),
(TYPE_TWP, "TWP"),
)

title = models.CharField(max_length=255)
Expand Down
11 changes: 8 additions & 3 deletions scraper/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ class PostViewSet(viewsets.GenericViewSet, mixins.RetrieveModelMixin, mixins.Lis
serializer_class = PostSerializer

def list(self, request, *args, **kwargs):
queryset = self.get_queryset()[:10]
post_types = [post_type[0] for post_type in Post.TYPE_CHOICES]
posts = {post_type:[] for post_type in post_types}

serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
for post_type in post_types:
queryset = Post.objects.filter(post_type=post_type).order_by('-created_at')[:4]
serialized_posts = self.get_serializer(queryset, many=True).data
posts[post_type] = serialized_posts

return Response(posts)

0 comments on commit 9be4595

Please sign in to comment.