Skip to main content

collective.beancounter

Author:

$Author: seletz $

Date:

$Date: 2007-11-14 19:46:16 +0100 (Wed, 14 Nov 2007) $

Revision:

$Revision: 53844 $

Abstract

This package scratches an itch of mine in providing a very simple viewlet which displays the percentage a content is filled by an user.

setup stuff

>>> class Mock(object):
...    def __init__(self, **kw): self.__sict__.update(kw)

Blurb

First we define an interface to be used to mark bean-countable content:

>>> from zope import interface
>>> class IBeanContable(interface.Interface):
...     """ a content which is bean countable """

The counting itself is very simple and done by an adapter. We simply count which fields in the default schemata are filled. We there count only the writable fields. From that we calculate a percentage.

Lets define a interface for that functionality:

>>> class IBeanCounter(interface.Interface):
...     percentage = interface.Attribute(u"The percentage filled")

Now lets create some content class to test our stuff:

>>> _ = self.folder.invokeFactory("Document", "doc")
>>> doc = self.folder.get(_)

Count the fields which are in the default schemata and are rewd/write:

>>> len([f for f in doc.Schema().fields() if f.schemata=="default" and f.mode =="rw"])
4

Ok, now how many of them are filled?:

>>> l = [f for f in doc.Schema().fields() if f.schemata=="default" and f.mode =="rw"]
>>> [f.getName() for f in l if f.get(doc)]
['id']

Ok, fair enough. Now lets do the opposite:

>>> [f.getName() for f in l if not f.get(doc)]
['title', 'description', 'text']

Ok, thats enough. Lets wrap it up.

Implementation

We have an adapter:

>>> from collective.beancounter.adapter import ATBeanCounter
>>> ct = ATBeanCounter(doc)
>>> print ct.percentage
25.0

Fill out completely:

>>> doc.update( title="muha", text="haha", description="desc")
>>> ct = ATBeanCounter(doc)
>>> print ct.percentage
100.0

Yay.

Field filter

You may provide an adapter to specify a filter to decide which fields you consider to be “countable”:

>>> from collective.beancounter.interfaces import IBeanCounterFieldFilter

We provide adefault adapter for AT objects:

>>> from collective.beancounter.adapter import ATFieldFilter
That adapter provides a filter which filters out fields which:
  • are not user settable

  • are not in the “default” schemata

  • are not in the special plone field blacklist

  • are not boolean fields (these are true or false, i.e. always “filled”)

>>> IBeanCounterFieldFilter(doc)
<collective.beancounter.adapter.ATFieldFilter object at ...>

Lets test that:

>>> from collective.beancounter.adapter import countable_fields
>>> sorted([f.getName() for f in countable_fields(doc)])
['description', 'id', 'text', 'title']

Ok, that filters nothing, which is fine. Now lets provide an adapter which does filter out the “title”,”id” and “description” fields:

>>> from zope import component
>>> from Products.Archetypes.interfaces import IBaseObject
>>> class TestFilter(object):
...     component.adapts(IBaseObject)
...     interface.implements(IBeanCounterFieldFilter)
...     def __init__(self,context): self.context = context
...     def __call__(self, field):
...         return field.getName() not in "title id description".split()
>>> component.provideAdapter(TestFilter)

We should now get our adapter:

>>> IBeanCounterFieldFilter(doc)
<TestFilter object at ...>

We should now get ALL fields but the ones we filtered out:

>>> set("title id description".split()) & set([f.getName() for f in countable_fields(doc)])
set([])
vim: set ft=rst tw=75 nocin nosi ai sw=4 ts=4 expandtab:

Release files for collective.beancounter 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for collective.beancounter 0.3.1
File Size Uploaded
collective.beancounter-0.3.1.tar.gz 11.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for collective.beancounter 0.3.1
File Interpreter ABI Platform
collective.beancounter-0.3.1-py2.4.egg Legacy Egg format - - Details

Total release size: 24.0 kB

Release files / collective.beancounter-0.3.1.tar.gz

Download URL collective.beancounter-0.3.1.tar.gz
Size 11.0 kB
Tags Source
SHA-256 checksum
How to use checksums
55385d43d4a69268540739234e890abdae5e96e219bd16375424261e20ec1f98
BLAKE2b-256 checksum
How to use checksums
ae721d6f91a3e53464a3e75366c7e29efb5f2021a6b93ecca3a7f0baaac60fc3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / collective.beancounter-0.3.1-py2.4.egg

Download URL collective.beancounter-0.3.1-py2.4.egg
Size 13.1 kB
Tags Egg
SHA-256 checksum
How to use checksums
e751e86d1d4ca14ba622e7dd287ded1dd1ad84b0c1d464074ee3002e67991fd0
BLAKE2b-256 checksum
How to use checksums
e4a03150b16ce52b307c82032e11fd1e7f6ee6b0caba6d6921515bf3a6335735
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 release files

0.3

2 release files

0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page