-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from RedTurtle/feedbacks_list_update
Feedbacks list update
- Loading branch information
Showing
7 changed files
with
244 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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 | ||
|
||
from collective.feedback.interfaces import ICollectiveFeedbackStore | ||
|
||
|
||
class FeedbacListkUpdate(Service): | ||
""" | ||
Service for update feedback to object, you can only update `read` field | ||
""" | ||
|
||
def __init__(self, context, request): | ||
super().__init__(context, request) | ||
|
||
def reply(self): | ||
alsoProvides(self.request, IDisableCSRFProtection) | ||
|
||
tool = getUtility(ICollectiveFeedbackStore) | ||
|
||
form_data = self.extract_data(json_body(self.request)) | ||
|
||
for id, value in form_data.items(): | ||
comment = tool.get(id) | ||
|
||
if comment.get("error", "") == "NotFound": | ||
raise NotFound() | ||
|
||
try: | ||
tool.update(id, value) | ||
except ValueError as e: | ||
self.request.response.setStatus(500) | ||
return dict( | ||
error=dict( | ||
type="InternalServerError", | ||
message=getattr(e, "message", e.__str__()), | ||
) | ||
) | ||
|
||
return form_data | ||
|
||
def extract_data(self, form_data): | ||
data = {} | ||
|
||
for id, value in form_data.items(): | ||
try: | ||
self.validate_data(value) | ||
data[int(id)] = {"read": value.get("read")} | ||
except ValueError: | ||
raise BadRequest(f"Bad id={id} format provided") | ||
|
||
return data | ||
|
||
def validate_data(self, data): | ||
""" | ||
check all required fields and parameters | ||
""" | ||
for field in ["read"]: | ||
value = data.get(field, None) | ||
if value is None: | ||
raise BadRequest("Campo obbligatorio mancante: {}".format(field)) |
122 changes: 122 additions & 0 deletions
122
src/collective/feedback/tests/test_restapi_services_list_upgdate.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# -*- coding: utf-8 -*- | ||
import unittest | ||
|
||
import transaction | ||
from plone import api | ||
from plone.app.testing import ( | ||
SITE_OWNER_NAME, | ||
SITE_OWNER_PASSWORD, | ||
TEST_USER_ID, | ||
setRoles, | ||
) | ||
from plone.restapi.testing import RelativeSession | ||
from zope.component import getUtility | ||
|
||
from collective.feedback.interfaces import ICollectiveFeedbackStore | ||
from collective.feedback.testing import RESTAPI_TESTING | ||
|
||
|
||
class TestAdd(unittest.TestCase): | ||
layer = RESTAPI_TESTING | ||
|
||
def setUp(self): | ||
self.app = self.layer["app"] | ||
self.portal = self.layer["portal"] | ||
self.portal_url = self.portal.absolute_url() | ||
setRoles(self.portal, TEST_USER_ID, ["Manager"]) | ||
|
||
api.user.create( | ||
email="[email protected]", | ||
username="memberuser", | ||
password="secret!!", | ||
) | ||
|
||
self.document = api.content.create( | ||
title="Document", container=self.portal, type="Document" | ||
) | ||
api.content.transition(obj=self.document, transition="publish") | ||
|
||
self.private_document = api.content.create( | ||
title="restricted document", container=self.portal, type="Document" | ||
) | ||
transaction.commit() | ||
|
||
self.api_session = RelativeSession(self.portal_url) | ||
self.api_session.headers.update({"Accept": "application/json"}) | ||
self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD) | ||
self.anon_api_session = RelativeSession(self.portal_url) | ||
self.anon_api_session.headers.update({"Accept": "application/json"}) | ||
|
||
self.url = "{}/@feedback-add".format(self.document.absolute_url()) | ||
self.url_private_document = "{}/@feedback-add".format( | ||
self.private_document.absolute_url() | ||
) | ||
|
||
def tearDown(self): | ||
self.api_session.close() | ||
self.anon_api_session.close() | ||
|
||
def test_correctly_update_data(self): | ||
self.anon_api_session.post( | ||
self.url, | ||
json={"vote": 3, "comment": "i disagree", "honey": ""}, | ||
) | ||
self.anon_api_session.post( | ||
self.url, | ||
json={"vote": 2, "comment": "i disagree", "honey": ""}, | ||
) | ||
transaction.commit() | ||
tool = getUtility(ICollectiveFeedbackStore) | ||
feedbacks = tool.search() | ||
|
||
self.assertEqual(len(feedbacks), 2) | ||
|
||
self.api_session.patch( | ||
api.portal.get().absolute_url() + "/@feedback-list", | ||
json={str(feedbacks[0].intid): {"read": True}}, | ||
) | ||
transaction.commit() | ||
|
||
self.assertTrue(tool.get(feedbacks[0].intid).attrs.get("read")) | ||
|
||
def test_unknown_id(self): | ||
self.anon_api_session.post( | ||
self.url, | ||
json={"vote": 3, "comment": "i disagree", "honey": ""}, | ||
) | ||
transaction.commit() | ||
|
||
tool = getUtility(ICollectiveFeedbackStore) | ||
feedbacks = tool.search() | ||
|
||
self.assertEqual(len(feedbacks), 1) | ||
|
||
resp = self.api_session.patch( | ||
api.portal.get().absolute_url() + "/@feedback-list", | ||
json={"1111111111": {"read": True}}, | ||
) | ||
|
||
transaction.commit() | ||
|
||
self.assertEqual(resp.status_code, 404) | ||
|
||
def test_bad_id(self): | ||
self.anon_api_session.post( | ||
self.url, | ||
json={"vote": 3, "comment": "i disagree", "honey": ""}, | ||
) | ||
transaction.commit() | ||
|
||
tool = getUtility(ICollectiveFeedbackStore) | ||
feedbacks = tool.search() | ||
|
||
self.assertEqual(len(feedbacks), 1) | ||
|
||
resp = self.api_session.patch( | ||
api.portal.get().absolute_url() + "/@feedback-list", | ||
json={"fffffffff": {"read": True}}, | ||
) | ||
|
||
transaction.commit() | ||
|
||
self.assertEqual(resp.status_code, 400) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters