Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
folix-01 committed Oct 23, 2024
1 parent 1c43ccc commit 0976f08
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 59 deletions.
75 changes: 18 additions & 57 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,32 +1,3 @@
.. This README is meant for consumption by humans and PyPI. PyPI can render rst files so please do not use Sphinx features.
If you want to learn more about writing documentation, please check out: http://docs.plone.org/about/documentation_styleguide.html
This text does not appear on PyPI or github. It is a comment.
.. image:: https://github.com/collective/collective.formsupport.counter/actions/workflows/plone-package.yml/badge.svg
:target: https://github.com/collective/collective.formsupport.counter/actions/workflows/plone-package.yml

.. image:: https://coveralls.io/repos/github/collective/collective.formsupport.counter/badge.svg?branch=main
:target: https://coveralls.io/github/collective/collective.formsupport.counter?branch=main
:alt: Coveralls

.. image:: https://codecov.io/gh/collective/collective.formsupport.counter/branch/master/graph/badge.svg
:target: https://codecov.io/gh/collective/collective.formsupport.counter

.. image:: https://img.shields.io/pypi/v/collective.formsupport.counter.svg
:target: https://pypi.python.org/pypi/collective.formsupport.counter/
:alt: Latest Version

.. image:: https://img.shields.io/pypi/status/collective.formsupport.counter.svg
:target: https://pypi.python.org/pypi/collective.formsupport.counter
:alt: Egg Status

.. image:: https://img.shields.io/pypi/pyversions/collective.formsupport.counter.svg?style=plastic :alt: Supported - Python Versions

.. image:: https://img.shields.io/pypi/l/collective.formsupport.counter.svg
:target: https://pypi.python.org/pypi/collective.formsupport.counter/
:alt: License


==============================
collective.formsupport.counter
==============================
Expand All @@ -36,28 +7,7 @@ Counter integration for collective.volto.formsupport
Features
--------

- Can be bullet points


Examples
--------

This add-on can be seen in action at the following sites:
- Is there a page on the internet where everybody can see the features?


Documentation
-------------

Full documentation for end users can be found in the "docs" folder, and is also available online at http://docs.plone.org/foo/bar


Translations
------------

This product has been translated into

- Klingon (thanks, K'Plai)
- Form counter for collective.volto.formsupport


Installation
Expand All @@ -75,20 +25,31 @@ Install collective.formsupport.counter by adding it to your buildout::

and then running ``bin/buildout``

REST API
========

Here is the list of available REST API endpoints and how to use them.

1. **Reset form counter**

- **Endpoint**: `/<document>/reset-counter`
- **Method**: `PATCH`
- **Description**: Reset form counter.
- **Request**: No parameters required.
- **Response**:

- **Status Code**: `204 No Content`

Authors
-------

Provided by awesome people ;)
RedTurtle


Contributors
------------

Put your name here, you deserve it!

- ?

- folix-01

Contribute
----------
Expand All @@ -102,7 +63,7 @@ Support
-------

If you are having issues, please let us know.
We have a mailing list located at: project@example.com
We have a mailing list located at: info@redturtle.it


License
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"z3c.jbot",
"plone.api>=1.8.4",
"plone.app.dexterity",
"collective.volto.formsupport",
],
extras_require={
"test": [
Expand Down
54 changes: 54 additions & 0 deletions src/collective/formsupport/counter/adapters/__init__.py
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 src/collective/formsupport/counter/adapters/configure.zcml
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>

3 changes: 3 additions & 0 deletions src/collective/formsupport/counter/config.py
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"
4 changes: 4 additions & 0 deletions src/collective/formsupport/counter/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
<!-- has to be loaded before permissions are used, so keep above views aso. -->
<include file="permissions.zcml" />

<include package=".adapters" />
<include package=".browser" />
<include package=".datamanager" />
<include package=".events" />
<include package=".restapi" />

<genericsetup:registerProfile
name="default"
Expand Down
53 changes: 53 additions & 0 deletions src/collective/formsupport/counter/datamanager/__init__.py
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 src/collective/formsupport/counter/datamanager/configure.zcml
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>
21 changes: 21 additions & 0 deletions src/collective/formsupport/counter/events/__init__.py
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
13 changes: 13 additions & 0 deletions src/collective/formsupport/counter/events/configure.zcml
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>

4 changes: 2 additions & 2 deletions src/collective/formsupport/counter/interfaces.py
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.
8 changes: 8 additions & 0 deletions src/collective/formsupport/counter/restapi/configure.zcml
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 src/collective/formsupport/counter/restapi/services/configure.zcml
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>
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)

0 comments on commit 0976f08

Please sign in to comment.