-
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
2 changed files
with
32 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from collective.formsupport.counter.config import COUNTER_ANNOTATIONS_NAME | ||
from collective.formsupport.counter.config import COUNTER_BLOCKS_FIELD_ID | ||
from collective.formsupport.counter.config import COUNTER_ENABLED_FORM_FLAG_NAME | ||
from collective.formsupport.counter.interfaces import ICollectiveFormsupportCounterLayer | ||
from collective.volto.formsupport.interfaces import IDataAdapter | ||
from zope.annotation.interfaces import IAnnotations | ||
from zope.component import adapter | ||
from zope.interface import implementer | ||
from zope.interface import Interface | ||
|
||
|
||
@implementer(IDataAdapter) | ||
@adapter(Interface, ICollectiveFormsupportCounterLayer) | ||
class DataAdapterWithCounter: | ||
def get_block(self, block_id): | ||
blocks = getattr(self.context, "blocks", {}) | ||
if not blocks: | ||
return | ||
for id, block in blocks.items(): | ||
if block.get("@type", "") == "form": | ||
if not block_id or block_id == id: | ||
return block | ||
|
||
def __call__(self, result, block_id=None): | ||
block = self.get_block(block_id) | ||
if block and block.get(COUNTER_ENABLED_FORM_FLAG_NAME): | ||
annotations = IAnnotations(self.context) | ||
result[COUNTER_BLOCKS_FIELD_ID] = annotations.get( | ||
COUNTER_ANNOTATIONS_NAME, {} | ||
).get(block_id, 0) | ||
return result |