Skip to content

Commit

Permalink
Update comment endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
folix-01 committed Jul 25, 2024
1 parent 5d60a83 commit a341fa2
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Changelog
1.1.4 (unreleased)
------------------

- Add feedback update endpoint.
[folix-01]

- Add read field to the comment.
[folix-01]

Expand Down
4 changes: 4 additions & 0 deletions src/collective/feedback/permissions.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
id="collective.feedback.ShowDeletedFeedbacks"
title="collective.feedback: Show Deleted Feedbacks"
/>
<permission
id="collective.feedback.UpdateFeedbacks"
title="collective.feedback: Update Feedbacks"
/>

</configure>

Expand Down
7 changes: 6 additions & 1 deletion src/collective/feedback/profiles/default/rolemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
<role name="Manager" />
<role name="Site Administrator" />
</permission>


<permission acquire="True" name="collective.feedback: Update Feedbacks">
<role name="Manager" />
<role name="Site Administrator" />
</permission>

</permissions>
</rolemap>
8 changes: 8 additions & 0 deletions src/collective/feedback/restapi/services/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,13 @@
layer="collective.feedback.interfaces.ICollectiveFeedbackLayer"
name="@feedback-delete"
/>
<plone:service
method="PATCH"
factory=".update.FeedbackUpdate"
for="plone.app.layout.navigation.interfaces.INavigationRoot"
permission="collective.feedback.FeedbacksOverview"
layer="collective.feedback.interfaces.ICollectiveFeedbackLayer"
name="@feedback"
/>

</configure>
3 changes: 3 additions & 0 deletions src/collective/feedback/restapi/services/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ def get_data(self):
uid = feedback._attrs.get("uid", "")
date = feedback._attrs.get("date", "")
vote = feedback._attrs.get("vote", "")
id = feedback.id

if uid not in feedbacks:
obj = self.get_commented_obj(uid=uid)
if not obj and not api.user.has_permission(
Expand All @@ -148,6 +150,7 @@ def get_data(self):
"comments": 0,
"title": feedback._attrs.get("title", ""),
"uid": uid,
"id": id,
}

if obj:
Expand Down
79 changes: 79 additions & 0 deletions src/collective/feedback/restapi/services/update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from collective.feedback.interfaces import ICollectiveFeedbackStore
from plone import api
from plone.protect.interfaces import IDisableCSRFProtection
from plone.restapi.deserializer import json_body
from plone.restapi.services import Service
from zExceptions import BadRequest, NotFound
from zope.component import getUtility
from zope.interface import alsoProvides, implementer
from zope.publisher.interfaces import IPublishTraverse


@implementer(IPublishTraverse)
class FeedbackUpdate(Service):
"""
Service for update feedback to object, you can only update `read` field
"""

def __init__(self, context, request):
super().__init__(context, request)
self.params = []

def publishTraverse(self, request, id):
# Consume any path segments after /@users as parameters
self.params.append(id)
return self

def reply(self):
alsoProvides(self.request, IDisableCSRFProtection)

if self.params:
id = self.params[0]

comment = tool.get(id)

if comment.get("error", "") == "NotFound":
raise NotFound(f"Object with id={id} not found")

form_data = json_body(self.request)

self.validate_form(form_data=form_data)

form_data = self.extract_data(form_data)

tool = getUtility(ICollectiveFeedbackStore)

try:
res = tool.update(id, form_data)
except ValueError as e:
self.request.response.setStatus(500)
return dict(
error=dict(
type="InternalServerError",
message=getattr(e, "message", e.__str__()),
)
)

if res == None:
return self.reply_no_content()

self.request.response.setStatus(500)

return dict(
error=dict(
type="InternalServerError",
message="Unable to add. Contact site manager.",
)
)

def extract_data(sefl, form_data):
return {"read": form_data.get("read")}

def validate_form(self, form_data):
"""
check all required fields and parameters
"""
for field in ["read"]:
value = form_data.get(field, "")
if not value:
raise BadRequest("Campo obbligatorio mancante: {}".format(field))
16 changes: 12 additions & 4 deletions src/collective/feedback/storage/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ def parse_query_params(self, index, value):
# return "{} in any('{}')".format(index, value)
else:
return Eq(index, value)
if isinstance(value, int):
return "{} == {}".format(index, value)
else:
return "{} == '{}'".format(index, value)
# if isinstance(value, int):
# return "{} == {}".format(index, value)
# else:
# return "{} == '{}'".format(index, value)

def get_record(self, id):
if isinstance(id, six.text_type) or isinstance(id, str):
Expand All @@ -113,6 +113,7 @@ def update(self, id, data):

else:
record.attrs[k] = v

self.soup.reindex(records=[record])

def delete(self, id):
Expand All @@ -123,5 +124,12 @@ def delete(self, id):
return {"error": "NotFound"}
del self.soup[record]

def get(self, id):
try:
return self.soup.get(id)
except KeyError:
logger.error('[GET] Subscription with id "{}" not found.'.format(id))
return {"error": "NotFound"}

def clear(self):
self.soup.clear()
10 changes: 10 additions & 0 deletions src/collective/feedback/upgrades.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,14 @@
handler=".upgrades.update_actions"
/>
</genericsetup:upgradeSteps>
<genericsetup:upgradeSteps
profile="collective.feedback:default"
source="1201"
destination="1202"
>
<genericsetup:upgradeStep
title="Add feedback update permission"
handler=".upgrades.update_rolemap"
/>
</genericsetup:upgradeSteps>
</configure>

0 comments on commit a341fa2

Please sign in to comment.