Skip to content

Commit

Permalink
Add push page content API end point, fixes #1099
Browse files Browse the repository at this point in the history
  • Loading branch information
svenseeberg committed Mar 5, 2022
1 parent 56c3eec commit 88d063f
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 4 deletions.
50 changes: 50 additions & 0 deletions integreat_cms/api/v3/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
This module includes functions related to the pages API endpoint.
"""
import logging
import json
from django.conf import settings
from django.core.exceptions import MultipleObjectsReturned
from django.http import JsonResponse, Http404
from django.shortcuts import get_object_or_404

from ...cms.models import Page
from ...cms.forms import PageTranslationForm
from ..decorators import json_response, matomo_tracking

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -302,3 +304,51 @@ def get_public_ancestor_translations(current_page, language_slug):
raise Http404("No Page matches the given url or id.")
result.append(transform_page(public_translation))
return result


@json_response
# pylint: disable=unused-argument
def push_content(request, region_slug, language_slug):
"""
Retrieves all ancestors (parent and all nodes up to the root node) of a page
:param request: The request that has been sent to the Django server
:type request: ~django.http.HttpRequest
:param region_slug: Slug defining the region
:type region_slug: str
:param language_slug: Code to identify the desired language
:type language_slug: str
:raises ~django.http.Http404: HTTP status 404 if the request is malformed or no page with the given id or url exists.
:return: JSON with the requested page ancestors
:rtype: ~django.http.JsonResponse
"""
try:
data = json.loads(request.body)
except json.decoder.JSONDecodeError:
data = {}
if not all(key in data for key in ["page_id", "content", "token"]):
logger.error("Push Content: failed to parse JSON.")
return JsonResponse({"status": "error"}, safe=False)

page = request.region.pages.get(api_token=data["token"])
if not page or data["token"] == "":
return JsonResponse({"status": "denied"}, safe=False)

translation = page.get_translation("language_slug")
data = {
"content": data["content"],
"title": translation.title,
"slug": translation.slug,
"status": translation.status,
"minor_edit": False,
}
page_translation_form = PageTranslationForm(data=data)
if page_translation_form.is_valid():
page_translation_form.save()
return JsonResponse({"status": "success"}, safe=False)
logger.error("Push Content: failed to validate page translation.")
return JsonResponse({"status": "error"}, safe=False)
24 changes: 24 additions & 0 deletions integreat_cms/cms/migrations/0010_page_api_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 3.2.12 on 2022-03-05 15:04

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("cms", "0009_alter_translation_model_ordering"),
]

operations = [
migrations.AddField(
model_name="page",
name="api_token",
field=models.TextField(
blank=True,
help_text="API token to allow writing content to translations.",
max_length=64,
null=True,
verbose_name="api_token",
),
),
]
7 changes: 7 additions & 0 deletions integreat_cms/cms/models/pages/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ class Page(AbstractTreeNode, AbstractBasePage):
"This allows all members of the organization to edit and publish this page."
),
)
api_token = models.TextField(
null=True,
blank=True,
max_length=64,
verbose_name=_("api_token"),
help_text=_("API token to allow writing content to translations."),
)

#: Custom model manager to inherit methods from tree manager as well as the custom content queryset
objects = PageManager()
Expand Down
16 changes: 12 additions & 4 deletions integreat_cms/locale/de/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: 1.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2022-03-05 11:16+0000\n"
"POT-Creation-Date: 2022-03-05 15:26+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: Integreat <info@integreat-app.de>\n"
"Language-Team: Integreat <info@integreat-app.de>\n"
Expand Down Expand Up @@ -1297,7 +1297,7 @@ msgstr "Aktiv"
msgid "Hidden"
msgstr "Versteckt"

#: cms/constants/region_status.py:18 cms/models/pages/page.py:298
#: cms/constants/region_status.py:18 cms/models/pages/page.py:305
#: cms/models/regions/region.py:597 cms/templates/events/event_form.html:70
#: cms/templates/pages/page_form.html:93
#: cms/templates/pages/page_tree_archived_node.html:78
Expand Down Expand Up @@ -2569,11 +2569,19 @@ msgstr ""
"Dies erlaubt allen Mitgliedern der Organisation diese Seite zu bearbeiten "
"und veröffentlichen"

#: cms/models/pages/page.py:319 cms/models/pages/page_translation.py:30
#: cms/models/pages/page.py:195
msgid "api_token"
msgstr ""

#: cms/models/pages/page.py:196
msgid "API token to allow writing content to translations."
msgstr ""

#: cms/models/pages/page.py:326 cms/models/pages/page_translation.py:30
msgid "page"
msgstr "Seite"

#: cms/models/pages/page.py:321
#: cms/models/pages/page.py:328
msgid "pages"
msgstr "Seiten"

Expand Down

0 comments on commit 88d063f

Please sign in to comment.