feat: 💻 add json output of users' latest posts

This commit is contained in:
m5ka 2024-03-25 23:44:57 +00:00
parent 4d6dc2263e
commit 11ed6bd2a1
2 changed files with 33 additions and 1 deletions

View file

@ -20,7 +20,7 @@ from django.contrib import admin
from django.urls import include, path from django.urls import include, path
from moku.views.auth import LoginView, LogoutView 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 ( from moku.views.recipe import (
DeleteRecipeView, DeleteRecipeView,
DeleteStepView, DeleteStepView,
@ -44,6 +44,7 @@ urlpatterns = [
path("privacy", PrivacyView.as_view(), name="privacy"), path("privacy", PrivacyView.as_view(), name="privacy"),
path("terms", TermsView.as_view(), name="terms"), path("terms", TermsView.as_view(), name="terms"),
path("user/<str:username>", ProfileView.as_view(), name="profile"), path("user/<str:username>", ProfileView.as_view(), name="profile"),
path("user/<str:username>/json", LatestPostJSONView.as_view(), name="json"),
path("recipes", IndexRecipeView.as_view(), name="recipe.index"), path("recipes", IndexRecipeView.as_view(), name="recipe.index"),
path("recipes/new", NewRecipeView.as_view(), name="recipe.new"), path("recipes/new", NewRecipeView.as_view(), name="recipe.new"),
path("recipes/<str:uuid>", ShowRecipeView.as_view(), name="recipe.show"), path("recipes/<str:uuid>", ShowRecipeView.as_view(), name="recipe.show"),

View file

@ -1,7 +1,12 @@
import json
from django.conf import settings
from django.contrib import messages from django.contrib import messages
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.http import HttpResponse
from django.shortcuts import redirect from django.shortcuts import redirect
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from django.views.generic import View as BaseView
from moku.constants import EMOJI_CATEGORIES, Verbs from moku.constants import EMOJI_CATEGORIES, Verbs
from moku.forms.post import PostForm from moku.forms.post import PostForm
@ -52,3 +57,29 @@ class FeedView(FormView):
if self.request.user.is_authenticated: if self.request.user.is_authenticated:
form.fields["recipe"].queryset = Recipe.objects.filter(created_by=self.request.user) form.fields["recipe"].queryset = Recipe.objects.filter(created_by=self.request.user)
return form 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")