Skip to content

Commit

Permalink
Merge pull request #1262 from digitalfabrik/develop
Browse files Browse the repository at this point in the history
Deploy fixed region cloning, FCM messages, page form & translation status
  • Loading branch information
svenseeberg authored Mar 6, 2022
2 parents dc42dbe + 8ba5afb commit 06069c9
Show file tree
Hide file tree
Showing 45 changed files with 777 additions and 287 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
UNRELEASED
----------

* [ [#1086](https://github.com/digitalfabrik/integreat-cms/issues/1086) ] Provide correct URL for POI
* [ [#1247](https://github.com/digitalfabrik/integreat-cms/issues/1247) ] Update translation status on source status changes
* [ [#1251](https://github.com/digitalfabrik/integreat-cms/issues/1251) ] Fix change detection in page form
* [ [#1260](https://github.com/digitalfabrik/integreat-cms/issues/1260) ] Fix Firebase messaging
* [ [#1259](https://github.com/digitalfabrik/integreat-cms/issues/1259) ] Fix cloning of regions


2022.2.4
--------
Expand Down
2 changes: 1 addition & 1 deletion integreat_cms/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
path("parents/", parents, name="parents"),
path("pdf/", pdf_export, name="pdf_export"),
path(
"sent_push_notifications/",
"fcm/",
sent_push_notifications,
name="sent_push_notifications",
),
Expand Down
2 changes: 1 addition & 1 deletion integreat_cms/api/v3/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def transform_event_translation(event_translation):
if event.location:
location_translation = (
event.location.get_public_translation(event_translation.language.slug)
or event.location.default_translation
or event.location.best_translation
)
else:
location_translation = None
Expand Down
5 changes: 2 additions & 3 deletions integreat_cms/api/v3/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def pages(request, region_slug, language_slug):
# requested from the database
for page in region.pages.filter(explicitly_archived=False).cache_tree(
archived=False, language_slug=language_slug
)[0]:
):
page_translation = page.get_public_translation(language_slug)
if page_translation:
result.append(transform_page(page_translation))
Expand Down Expand Up @@ -234,8 +234,7 @@ def children(request, region_slug, language_slug):
# like in wordpress depth = 0 will return no results in this case
depth = depth - 1
result = []
# pylint: disable=unused-variable
public_region_pages, skipped_pages = request.region.pages.filter(
public_region_pages = request.region.pages.filter(
explicitly_archived=False, tree_id__in=[page.tree_id for page in root_pages]
).cache_tree(archived=False, language_slug=language_slug)
for root in root_pages:
Expand Down
72 changes: 72 additions & 0 deletions integreat_cms/cms/fixtures/test_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,21 @@
"last_updated": "2019-08-12T07:52:21.400Z"
}
},
{
"model": "cms.language",
"pk": 5,
"fields": {
"slug": "es",
"bcp47_tag": "es-es",
"english_name": "Spanish",
"native_name": "Español",
"text_direction": "RIGHT_TO_LEFT",
"primary_country_code": "es",
"table_of_contents": "Índice de contenidos",
"created_date": "2019-08-12T07:52:21.399Z",
"last_updated": "2019-08-12T07:52:21.400Z"
}
},
{
"model": "cms.languagetreenode",
"pk": 1,
Expand Down Expand Up @@ -2460,5 +2475,62 @@
"text": "Ignorierter Link",
"ignore": true
}
},
{
"model": "cms.pushnotification",
"pk": 1,
"fields": {
"region": 2,
"channel": "news",
"draft": false,
"sent_date": "2022-03-05T10:32:33.118Z",
"created_date": "2022-03-05T10:31:33.118Z",
"mode": "ONLY_AVAILABLE"
}
},
{
"model": "cms.pushnotificationtranslation",
"pk": 1,
"fields": {
"title": "Test DE",
"text": "Test DE Inhalt",
"language": 1,
"push_notification": 1,
"last_updated": "2022-03-05T10:31:33.130Z"
}
},
{
"model": "cms.pushnotificationtranslation",
"pk": 2,
"fields": {
"title": "Test EN",
"text": "Test EN content",
"language": 2,
"push_notification": 1,
"last_updated": "2022-03-05T10:31:33.145Z"
}
},
{
"model": "cms.pushnotification",
"pk": 2,
"fields": {
"region": 2,
"channel": "news",
"draft": true,
"sent_date": null,
"created_date": "2022-03-05T10:35:33.118Z",
"mode": "ONLY_AVAILABLE"
}
},
{
"model": "cms.pushnotificationtranslation",
"pk": 3,
"fields": {
"title": "Nicht gesendet",
"text": "Nicht gesendeter Inhalt",
"language": 1,
"push_notification": 2,
"last_updated": "2022-03-05T10:35:33.130Z"
}
}
]
6 changes: 4 additions & 2 deletions integreat_cms/cms/forms/custom_tree_node_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,16 @@ def _get_position_ref_node(self, instance):
logger.debug(
"Node %r is now referenced left to node %r", instance, next_sibling
)
return {"_ref_node_id": next_sibling.id, "_position": "left"}
return {"_ref_node_id": str(next_sibling.id), "_position": "left"}
# If the page is the only root page of this region, do not reference other nodes
logger.debug(
"Node %r is the only root node of its region and now referenced to no other node",
instance,
)
return {"_ref_node_id": "", "_position": "first-child"}
return super()._get_position_ref_node(instance)
# Convert initial data to string to fix the change detection
initial_data = super()._get_position_ref_node(instance)
return {key: str(value) for key, value in initial_data.items()}

@classmethod
def mk_dropdown_tree(cls, model, for_node=None):
Expand Down
39 changes: 17 additions & 22 deletions integreat_cms/cms/forms/language_tree/language_tree_node_form.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import logging

from django import forms
from django.utils.text import capfirst
from django.utils.translation import ugettext_lazy as _

from cacheops import invalidate_obj

from ...constants import position
from ...models import Language, LanguageTreeNode
from ..custom_model_form import CustomModelForm
from ..custom_tree_node_form import CustomTreeNodeForm
from ...models import Language, LanguageTreeNode


logger = logging.getLogger(__name__)
Expand All @@ -19,12 +19,6 @@ class LanguageTreeNodeForm(CustomModelForm, CustomTreeNodeForm):
Form for creating and modifying language tree node objects
"""

parent = forms.ModelChoiceField(
queryset=LanguageTreeNode.objects.all(),
required=False,
label=capfirst(LanguageTreeNode._meta.get_field("parent").verbose_name),
)

class Meta:
"""
This class contains additional meta configuration of the form class, see the :class:`django.forms.ModelForm`
Expand All @@ -34,7 +28,7 @@ class Meta:
#: The model of this :class:`django.forms.ModelForm`
model = LanguageTreeNode
#: The fields of the model which should be handled by this form
fields = ["language", "visible", "active"]
fields = ["language", "visible", "active", "parent"]

def __init__(self, **kwargs):
r"""
Expand All @@ -44,18 +38,12 @@ def __init__(self, **kwargs):
:type \**kwargs: dict
"""

if "data" in kwargs:
# Copy QueryDict because it is immutable
data = kwargs.pop("data").copy()
# Use the parent node as value for the ref node
data["_ref_node_id"] = data["parent"]
data["_position"] = "first-child"
# Set the kwargs to updated POST data again
kwargs["data"] = data

# Instantiate CustomModelForm
# Instantiate CustomModelForm and CustomTreeNodeForm
super().__init__(**kwargs)

# Make position field optional because we're overriding that value anyway
self.fields["_position"].required = False

parent_queryset = self.instance.region.language_tree_nodes

if self.instance.id:
Expand All @@ -66,13 +54,13 @@ def __init__(self, **kwargs):
)
]
parent_queryset = parent_queryset.exclude(id__in=descendant_ids)
self.fields["parent"].initial = self.instance.parent_id
excluded_languages = [
language.id
for language in self.instance.region.languages
if language != self.instance.language
]
else:
# Make sure it's not possible to create multiple nodes for the same language
excluded_languages = [
language.id for language in self.instance.region.languages
]
Expand All @@ -96,6 +84,12 @@ def clean(self):
:rtype: dict
"""
cleaned_data = super().clean()

# Ignore the value that is submitted as ref node and just use the parent field
parent = cleaned_data.get("parent")
cleaned_data["_ref_node_id"] = str(parent.id) if parent else ""
cleaned_data["_position"] = position.FIRST_CHILD

default_language = self.instance.region.default_language
# There are two cases in which this error is thrown.
# Both cases include that the parent field is None.
Expand Down Expand Up @@ -125,17 +119,18 @@ def clean(self):
def save(self, commit=True):
"""
This method extends the default ``save()``-method of the base :class:`~django.forms.ModelForm` to flush
the cache after commiting.
the cache after committing.
:param commit: Whether or not the changes should be written to the database
:type commit: bool
:return: The saved page translation object
:rtype: ~integreat_cms.cms.models.pages.page_translation.PageTranslation
"""
# Save CustomModelForm and flush Cache
# Save CustomModelForm and CustomTreeNodeForm
result = super().save(commit=commit)

# Flush cache of content objects
for page in self.instance.region.pages.all():
invalidate_obj(page)
for poi in self.instance.region.pois.all():
Expand Down
26 changes: 12 additions & 14 deletions integreat_cms/cms/forms/pages/page_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from django import forms
from django.contrib.auth import get_user_model
from django.db.models import Q
from django.utils.text import capfirst
from django.utils.translation import ugettext_lazy as _

from ...constants import mirrored_page_first, position
Expand All @@ -21,12 +20,6 @@ class PageForm(CustomModelForm, CustomTreeNodeForm):
Form for creating and modifying page objects
"""

parent = forms.ModelChoiceField(
queryset=Page.objects.all(),
required=False,
widget=ParentFieldWidget(),
label=capfirst(Page._meta.get_field("parent").verbose_name),
)
editors = forms.ModelChoiceField(
queryset=get_user_model().objects.all(),
required=False,
Expand Down Expand Up @@ -56,11 +49,18 @@ class Meta:
#: The model of this :class:`django.forms.ModelForm`
model = Page
#: The fields of the model which should be handled by this form
fields = ["icon", "mirrored_page", "mirrored_page_first", "organization"]
fields = [
"icon",
"mirrored_page",
"mirrored_page_first",
"organization",
"parent",
]
#: The widgets for the fields if they differ from the standard widgets
widgets = {
"mirrored_page_first": forms.Select(choices=mirrored_page_first.CHOICES),
"icon": IconWidget(),
"parent": ParentFieldWidget(),
}

def __init__(self, **kwargs):
Expand All @@ -71,7 +71,7 @@ def __init__(self, **kwargs):
:type \**kwargs: dict
"""

# Instantiate CustomModelForm
# Instantiate CustomModelForm and CustomTreeNodeForm
super().__init__(**kwargs)

# Pass form object to ParentFieldWidget
Expand Down Expand Up @@ -117,8 +117,6 @@ def __init__(self, **kwargs):
tree_id=self.instance.tree_id,
lft__range=(self.instance.lft, self.instance.rgt - 1),
)
# Set initial value for parent field
self.fields["parent"].initial = self.instance.parent_id
# Exclude the current page from the possible options for mirrored pages
mirrored_page_queryset = mirrored_page_queryset.exclude(id=self.instance.id)
else:
Expand All @@ -128,21 +126,21 @@ def __init__(self, **kwargs):
self.fields["_ref_node_id"].initial = last_root_page.id
self.fields["_position"].initial = position.RIGHT
else:
# If no page exists, treebeard expects the value "0" as reference node id
# If no page exists, treebeard expects the value "" as reference node id
self.fields["_ref_node_id"].initial = ""
self.fields["_position"].initial = position.FIRST_CHILD

# Set choices of mirrored_page field manually to make use of cache_tree()
logger.debug("Set choices for mirrored page field:")
self.fields["mirrored_page"].choices = [
(page.id, str(page)) for page in mirrored_page_queryset.cache_tree()[0]
(page.id, str(page)) for page in mirrored_page_queryset.cache_tree()
]

# Set choices of parent and _ref_node_id fields manually to make use of cache_tree()
logger.debug("Set choices for parent field:")
cached_parent_choices = [("", "---------")]
cached_parent_choices.extend(
[(page.id, str(page)) for page in parent_queryset.cache_tree()[0]]
[(page.id, str(page)) for page in parent_queryset.cache_tree()]
)
self.fields["parent"].choices = cached_parent_choices
self.fields["_ref_node_id"].choices = cached_parent_choices
Expand Down
Loading

0 comments on commit 06069c9

Please sign in to comment.