Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache products and content filters for article #2486

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion apps/content_filters/content_filter/content_filter_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# AUTHORS and LICENSE files distributed with this source code, or
# at https://www.sourcefabric.org/superdesk/license

import flask
import logging

from superdesk.services import CacheableService
Expand Down Expand Up @@ -195,7 +196,15 @@ def _get_elastic_query(self, doc, matching=True):
def does_match(self, content_filter, article, filters=None):
if not content_filter:
return True # a non-existing filter matches every thing

cache_id = "filter-match-{filter}-{article}".format(
filter=content_filter.get("_id") or content_filter.get("name"),
article=article.get("_id") or article.get("guid"),
)
if not hasattr(flask.g, cache_id):
setattr(flask.g, cache_id, self._does_match(content_filter, article, filters))
return getattr(flask.g, cache_id)

def _does_match(self, content_filter, article, filters):
filter_condition_service = get_resource_service("filter_conditions")
expressions = []
for index, expression in enumerate(content_filter.get("content_filter", [])):
Expand Down
2 changes: 1 addition & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ typing_extensions>=3.7.4
moto[sqs]

-e .
-e git+https://github.com/superdesk/superdesk-planning.git@develop#egg=superdesk-planning
-e git+https://github.com/superdesk/superdesk-planning.git@v2.6.2#egg=superdesk-planning
-e git+https://github.com/superdesk/sams.git@develop#egg=sams_client&subdirectory=src/clients/python/
15 changes: 12 additions & 3 deletions superdesk/publish/formatters/ninjs_newsroom_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
# at https://www.sourcefabric.org/superdesk/license


from .ninjs_formatter import NINJSFormatter
import flask
import superdesk
import elasticapm

from .ninjs_formatter import NINJSFormatter


class NewsroomNinjsFormatter(NINJSFormatter):
name = "Newsroom NINJS"
Expand All @@ -32,8 +34,15 @@ def _format_products(self, article):
:param article:
:return:
"""
result = superdesk.get_resource_service("product_tests").test_products(article)
return [{"code": p["product_id"], "name": p.get("name")} for p in result if p.get("matched", False)]
cache_id = "article-products-{_id}".format(_id=article.get("_id") or article.get("guid"))
if not hasattr(flask.g, cache_id):
matches = superdesk.get_resource_service("product_tests").test_products(article)
setattr(
flask.g,
cache_id,
[{"code": p["product_id"], "name": p.get("name")} for p in matches if p.get("matched", False)],
)
return getattr(flask.g, cache_id)

@elasticapm.capture_span()
def _transform_to_ninjs(self, article, subscriber, recursive=True):
Expand Down
2 changes: 1 addition & 1 deletion tests/publish/formatters/ninjs_newsroom_formatter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def test_products(self):
"products": [{"code": 1, "name": "p-1"}],
}
self.assertEqual(json.loads(doc), expected)
article["urgency"] = 1
article.update(urgency=1, _id="v2")
seq, doc = self.formatter.format(article, {"name": "Test Subscriber"})[0]
expected = {
"guid": "tag:aap.com.au:20150613:12345",
Expand Down
Loading