From 11ed6bd2a1ecb6dac4d12bd90b4e92d65b68f98f Mon Sep 17 00:00:00 2001 From: m5ka Date: Mon, 25 Mar 2024 23:44:57 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=92=BB=20add=20json=20output=20of?= =?UTF-8?q?=20users'=20latest=20posts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- moku/config/urls.py | 3 ++- moku/views/post.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/moku/config/urls.py b/moku/config/urls.py index eb9db44..277d3d9 100644 --- a/moku/config/urls.py +++ b/moku/config/urls.py @@ -20,7 +20,7 @@ from django.contrib import admin from django.urls import include, path from moku.views.auth import LoginView, LogoutView -from moku.views.post import FeedView +from moku.views.post import FeedView, LatestPostJSONView from moku.views.recipe import ( DeleteRecipeView, DeleteStepView, @@ -44,6 +44,7 @@ urlpatterns = [ path("privacy", PrivacyView.as_view(), name="privacy"), path("terms", TermsView.as_view(), name="terms"), path("user/", ProfileView.as_view(), name="profile"), + path("user//json", LatestPostJSONView.as_view(), name="json"), path("recipes", IndexRecipeView.as_view(), name="recipe.index"), path("recipes/new", NewRecipeView.as_view(), name="recipe.new"), path("recipes/", ShowRecipeView.as_view(), name="recipe.show"), diff --git a/moku/views/post.py b/moku/views/post.py index ea4670b..e0e0d86 100644 --- a/moku/views/post.py +++ b/moku/views/post.py @@ -1,7 +1,12 @@ +import json + +from django.conf import settings from django.contrib import messages from django.core.exceptions import PermissionDenied +from django.http import HttpResponse from django.shortcuts import redirect from django.utils.translation import gettext as _ +from django.views.generic import View as BaseView from moku.constants import EMOJI_CATEGORIES, Verbs from moku.forms.post import PostForm @@ -52,3 +57,29 @@ class FeedView(FormView): if self.request.user.is_authenticated: form.fields["recipe"].queryset = Recipe.objects.filter(created_by=self.request.user) return form + + +class LatestPostJSONView(BaseView): + def get(self, request, *args, **kwargs): + post = Post.objects.prefetch_related("recipe__steps") \ + .filter(created_by__username=kwargs.get("username")) \ + .order_by("-created_at").first() + if not post: + return HttpResponse(json.dumps({"post": None}), content_type="application/json") + post_data = { + "date": str(post.created_at), + "text": post.get_verb_display() % {"user": f"@{post.created_by.username}", "food": post.food}, + "food": post.food, + "verb": { + "id": post.verb, + "pattern": post.get_verb_display() % {"user": "$1", "food": "$2"}, + }, + "image": f"{settings.SITE_ROOT_URL}{post.image.url}" if post.image else None, + "user": { + "username": post.created_by.username, + "url": f"{settings.SITE_ROOT_URL}{post.created_by.get_absolute_url()}", + }, + } + if post.recipe: + post_data["recipe"] = [step.instructions for step in post.recipe.steps.all()] + return HttpResponse(json.dumps(post_data), content_type="application/json")