Skip to content

Commit

Permalink
Add endpoint / query_url for (smartweb) topics
Browse files Browse the repository at this point in the history
  • Loading branch information
boulch committed Feb 26, 2025
1 parent af95d00 commit 7a567db
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/imio/smartweb/core/contents/rest/campaign/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,14 @@
layer="imio.smartweb.core.interfaces.IImioSmartwebCoreLayer"
/>

<plone:service
name="@topics"
method="GET"
accept="application/json"
for="imio.smartweb.core.contents.ICampaignView"
factory=".endpoint.TopicsEndpointGet"
permission="zope2.View"
layer="imio.smartweb.core.interfaces.IImioSmartwebCoreLayer"
/>

</configure>
39 changes: 33 additions & 6 deletions src/imio/smartweb/core/contents/rest/campaign/endpoint.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
# -*- coding: utf-8 -*-

from imio.smartweb.common.utils import get_vocabulary
from imio.smartweb.core.contents.rest.base import BaseEndpoint
from imio.smartweb.core.contents.rest.base import BaseService
from imio.smartweb.core.utils import get_basic_auth_json
from imio.smartweb.core.utils import get_ts_api_url
from plone import api
from plone.restapi.interfaces import IExpandableElement
from zope.component import adapter
from zope.i18n import translate
from zope.interface import implementer
from zope.interface import Interface

import base64
import requests


def get_ideabox_basic_auth_header() -> str:
user = api.portal.get_registry_record("smartweb.iaideabox_api_username")
pwd = api.portal.get_registry_record("smartweb.iaideabox_api_password")
usrPass = f"{user}:{pwd}".encode("utf-8")
b64Val = base64.b64encode(usrPass)
return f"Basic {b64Val.decode('utf-8')}"


@implementer(IExpandableElement)
@adapter(Interface, Interface)
class CampaignEndpoint(BaseEndpoint):
Expand Down Expand Up @@ -107,6 +117,26 @@ def query_url(self):
return url


@implementer(IExpandableElement)
@adapter(Interface, Interface)
class TopicsEndpoint(BaseEndpoint):

def __call__(self):
topics_vocabulary = get_vocabulary("imio.smartweb.vocabulary.Topics")
current_lang = api.portal.get_current_language()[:2]
json_res = {
"items": [
{
"value": term.value,
"title": translate(term.title, target_language=current_lang),
}
for term in topics_vocabulary
],
"items_total": len(topics_vocabulary),
}
return json_res


class CampaignEndpointGet(BaseService):
def reply(self):
return CampaignEndpoint(self.context, self.request)()
Expand All @@ -122,9 +152,6 @@ def reply(self):
return ZonesEndpoint(self.context, self.request)()


def get_ideabox_basic_auth_header() -> str:
user = api.portal.get_registry_record("smartweb.iaideabox_api_username")
pwd = api.portal.get_registry_record("smartweb.iaideabox_api_password")
usrPass = f"{user}:{pwd}".encode("utf-8")
b64Val = base64.b64encode(usrPass)
return f"Basic {b64Val.decode('utf-8')}"
class TopicsEndpointGet(BaseService):
def reply(self):
return TopicsEndpoint(self.context, self.request)()
1 change: 1 addition & 0 deletions src/imio/smartweb/core/contents/rest/campaign/view.pt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<div class="full-width">
<smartweb-campaign tal:attributes="query-url view/local_query_url;
query-zones-url view/local_query_zones_url;
query-topics-url view/local_query_topics_url;
batch-size view/batch_size;
propose-url view/propose_url;
current-language view/current_language;
Expand Down
7 changes: 6 additions & 1 deletion src/imio/smartweb/core/contents/rest/campaign/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CampaignViewView(BaseRestView):
@property
def propose_url(self):
return self.context.propose_project_url

@property
def display_map(self):
return self.context.display_map
Expand All @@ -22,3 +22,8 @@ def display_map(self):
def local_query_zones_url(self):
base_url = self.context.absolute_url()
return f"{base_url}/@zones"

@property
def local_query_topics_url(self):
base_url = self.context.absolute_url()
return f"{base_url}/@topics"

0 comments on commit 7a567db

Please sign in to comment.