Skip to content

Commit

Permalink
data adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
mamico committed Nov 11, 2024
1 parent 2ebcf07 commit 99c767f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/collective/formsupport/counter/adapters/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
>

<adapter factory=".post.PostAdapterWithCounter" />
<adapter factory=".data.DataAdapterWithCounter" />

</configure>

31 changes: 31 additions & 0 deletions src/collective/formsupport/counter/adapters/data.py
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

0 comments on commit 99c767f

Please sign in to comment.