This repository has been archived on 2023-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
python-blog/src/articles/views/api.py

28 lines
1 KiB
Python
Raw Normal View History

2021-12-28 22:31:53 +01:00
from typing import Any
2020-12-29 13:16:54 +01:00
from django.contrib.auth.decorators import login_required
2021-12-28 22:31:53 +01:00
from django.http import HttpRequest, HttpResponse
2020-12-29 13:16:54 +01:00
from django.shortcuts import render
from django.views.decorators.http import require_POST
2021-03-06 14:43:04 +01:00
from articles.models import Article, Tag
2020-12-29 13:16:54 +01:00
@login_required
@require_POST
2021-12-28 22:31:53 +01:00
def render_article(request: HttpRequest, article_pk: int) -> HttpResponse:
2020-12-29 13:16:54 +01:00
template = "articles/article_detail.html"
article = Article.objects.get(pk=article_pk)
article.content = request.POST.get("content", article.content)
article.title = request.POST.get("title", article.title)
article.custom_css = request.POST.get("custom_css", article.custom_css)
has_code = request.POST.get("has_code")
if has_code is not None:
article.has_code = has_code == "true"
2021-12-28 22:31:53 +01:00
context: dict[str, Any] = {"article": article}
2021-03-06 15:11:17 +01:00
tags = request.POST.get("tag_ids")
if tags:
2021-12-28 22:31:53 +01:00
context["tags"] = Tag.objects.filter(pk__in=map(int, tags.split(",")))
2021-03-06 15:11:17 +01:00
html = render(request, template, context=context)
2020-12-29 13:16:54 +01:00
return HttpResponse(html)