-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
233 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from copy import deepcopy | ||
|
||
from zope.interface import implementer | ||
from zope.component import adapter | ||
from zope.interface import Interface | ||
from zope.annotation.interfaces import IAnnotations | ||
|
||
from collective.volto.formsupport.interfaces import IFormData | ||
from collective.volto.formsupport.adapters import FormDataAdapter | ||
|
||
from collective.formsupport.counter import _ | ||
from collective.formsupport.counter.config import ( | ||
COUNTER_ENABLED_FORM_FLAG_NAME, | ||
COUNTER_ANNOTATIONS_NAME, | ||
COUNTER_BLOCKS_FIELD_ID, | ||
) | ||
from collective.formsupport.counter.interfaces import ICollectiveFormsupportCounterLayer | ||
|
||
|
||
@implementer(IFormData) | ||
@adapter(Interface, ICollectiveFormsupportCounterLayer) | ||
class FormDataAdapterWithCounter(FormDataAdapter): | ||
|
||
_block = {} | ||
|
||
@property | ||
def block(self): | ||
return self._block | ||
|
||
@block.setter | ||
def block(self, new_value): | ||
block = deepcopy(new_value) | ||
# if block.get(COUNTER_ENABLED_FORM_FLAG_NAME): | ||
block["subblocks"].append({"field_id": COUNTER_BLOCKS_FIELD_ID}) | ||
|
||
self._block = block | ||
|
||
def extract_data_from_request(self): | ||
form_data = super().extract_data_from_request() | ||
|
||
if not self.block.get(COUNTER_ENABLED_FORM_FLAG_NAME): | ||
return form_data | ||
|
||
annotations = IAnnotations(self.context) | ||
|
||
form_data["data"].append( | ||
{ | ||
"field_id": COUNTER_BLOCKS_FIELD_ID, | ||
"label": _("Form counter"), | ||
"value": annotations.get(COUNTER_ANNOTATIONS_NAME, 0) + 1, | ||
} | ||
) | ||
|
||
return form_data |
10 changes: 10 additions & 0 deletions
10
src/collective/formsupport/counter/adapters/configure.zcml
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,10 @@ | ||
<configure | ||
xmlns="http://namespaces.zope.org/zope" | ||
xmlns:plone="http://namespaces.plone.org/plone" | ||
i18n_domain="collective.formsupport.counter" | ||
> | ||
|
||
<adapter factory=".FormDataAdapterWithCounter" /> | ||
|
||
</configure> | ||
|
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,3 @@ | ||
COUNTER_ANNOTATIONS_NAME = "collective.formsupport.counter.form_counter" | ||
COUNTER_ENABLED_FORM_FLAG_NAME = "counter_enabled" | ||
COUNTER_BLOCKS_FIELD_ID = "form_counter" |
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
53 changes: 53 additions & 0 deletions
53
src/collective/formsupport/counter/datamanager/__init__.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,53 @@ | ||
from copy import deepcopy | ||
|
||
from plone.dexterity.interfaces import IDexterityContent | ||
from zope.component import adapter | ||
from zope.interface import implementer | ||
from plone.dexterity.interfaces import IDexterityContent | ||
|
||
from collective.volto.formsupport.datamanager.catalog import FormDataStore | ||
from collective.volto.formsupport.interfaces import IFormDataStore | ||
from collective.volto.formsupport.utils import get_blocks | ||
|
||
from collective.formsupport.counter.interfaces import ICollectiveFormsupportCounterLayer | ||
from collective.formsupport.counter.config import ( | ||
COUNTER_ENABLED_FORM_FLAG_NAME, | ||
COUNTER_BLOCKS_FIELD_ID, | ||
) | ||
|
||
|
||
@implementer(IFormDataStore) | ||
@adapter(IDexterityContent, ICollectiveFormsupportCounterLayer) | ||
class FormDataStoreWithCounter(FormDataStore): | ||
def get_form_fields(self): | ||
blocks = get_blocks(self.context) | ||
|
||
if not blocks: | ||
return {} | ||
|
||
form_block = {} | ||
|
||
for id, block in blocks.items(): | ||
|
||
if id != self.block_id: | ||
continue | ||
|
||
block_type = block.get("@type", "") | ||
|
||
if block_type == "form": | ||
form_block = deepcopy(block) | ||
|
||
if not form_block: | ||
return {} | ||
|
||
subblocks = form_block.get("subblocks", []) | ||
|
||
if form_block.get(COUNTER_ENABLED_FORM_FLAG_NAME): | ||
subblocks.append({"field_id": COUNTER_BLOCKS_FIELD_ID}) | ||
|
||
# Add the 'custom_field_id' field back in as this isn't stored with each subblock | ||
for index, field in enumerate(subblocks): | ||
if form_block.get(field["field_id"]): | ||
subblocks[index]["custom_field_id"] = form_block.get(field["field_id"]) | ||
|
||
return subblocks |
9 changes: 9 additions & 0 deletions
9
src/collective/formsupport/counter/datamanager/configure.zcml
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,9 @@ | ||
<configure | ||
xmlns="http://namespaces.zope.org/zope" | ||
xmlns:plone="http://namespaces.plone.org/plone" | ||
i18n_domain="collective.formsupport.counter" | ||
> | ||
|
||
<adapter factory=".FormDataStoreWithCounter" /> | ||
|
||
</configure> |
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,21 @@ | ||
from zope.annotation.interfaces import IAnnotations | ||
|
||
from collective.formsupport.counter.config import ( | ||
COUNTER_ANNOTATIONS_NAME, | ||
COUNTER_ENABLED_FORM_FLAG_NAME, | ||
) | ||
|
||
|
||
def add_counter(context, event): | ||
"""Add forms counter on the context if form requires""" | ||
|
||
if not event.form.get(COUNTER_ENABLED_FORM_FLAG_NAME): | ||
return | ||
|
||
annotations = IAnnotations(context) | ||
counter = annotations.get(COUNTER_ANNOTATIONS_NAME) | ||
|
||
if counter is None: | ||
annotations[COUNTER_ANNOTATIONS_NAME] = 0 | ||
|
||
annotations[COUNTER_ANNOTATIONS_NAME] += 1 |
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,13 @@ | ||
<configure | ||
xmlns="http://namespaces.zope.org/zope" | ||
i18n_domain="collective.formsupport.counter" | ||
> | ||
|
||
<subscriber | ||
for="Products.CMFCore.interfaces.IContentish | ||
collective.volto.formsupport.interfaces.IFormSubmittedEvent" | ||
handler=".add_counter" | ||
/> | ||
|
||
</configure> | ||
|
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Module where all interfaces, events and exceptions live.""" | ||
|
||
from zope.publisher.interfaces.browser import IDefaultBrowserLayer | ||
from collective.volto.formsupport.interfaces import ICollectiveVoltoFormsupportLayer | ||
|
||
|
||
class ICollectiveFormsupportCounterLayer(IDefaultBrowserLayer): | ||
class ICollectiveFormsupportCounterLayer(ICollectiveVoltoFormsupportLayer): | ||
"""Marker interface that defines a browser layer.""" |
Empty file.
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,8 @@ | ||
<configure | ||
xmlns="http://namespaces.zope.org/zope" | ||
xmlns:zcml="http://namespaces.zope.org/zcml" | ||
> | ||
|
||
<include package=".services" /> | ||
|
||
</configure> |
Empty file.
17 changes: 17 additions & 0 deletions
17
src/collective/formsupport/counter/restapi/services/configure.zcml
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,17 @@ | ||
<configure | ||
xmlns="http://namespaces.zope.org/zope" | ||
xmlns:cache="http://namespaces.zope.org/cache" | ||
xmlns:plone="http://namespaces.plone.org/plone" | ||
xmlns:zcml="http://namespaces.zope.org/zcml" | ||
> | ||
|
||
<plone:service | ||
method="PATCH" | ||
factory=".reset_counter.CounterReset" | ||
for="plone.dexterity.interfaces.IDexterityContent" | ||
permission="cmf.ModifyPortalContent" | ||
layer="collective.formsupport.counter.interfaces.ICollectiveFormsupportCounterLayer" | ||
name="reset-counter" | ||
/> | ||
|
||
</configure> |
20 changes: 20 additions & 0 deletions
20
src/collective/formsupport/counter/restapi/services/reset_counter.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,20 @@ | ||
from plone.restapi.services import Service | ||
from zope.annotation.interfaces import IAnnotations | ||
|
||
from collective.formsupport.counter.config import ( | ||
COUNTER_ANNOTATIONS_NAME, | ||
) | ||
|
||
|
||
class CounterReset(Service): | ||
def reply(self): | ||
annotations = IAnnotations(self.context) | ||
counter = annotations.get(COUNTER_ANNOTATIONS_NAME) | ||
|
||
if counter is None: | ||
self.request.response.setStatus(204) | ||
return | ||
|
||
annotations[COUNTER_ANNOTATIONS_NAME] = 0 | ||
|
||
self.request.response.setStatus(204) |