Skip to content

Commit

Permalink
Add deleteAnnotation
Browse files Browse the repository at this point in the history
  • Loading branch information
thefunny42 committed Oct 17, 2014
1 parent ec61a52 commit e556d6d
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 6 deletions.
7 changes: 5 additions & 2 deletions CHANGES.txt
Expand Up @@ -4,8 +4,11 @@ Changes
1.4 (unreleased)
----------------

- Add ``queryAnnotation()`` that will not create an annotation if it didn't
exist before, preventing unneccessary databse writes.
- Add ``queryAnnotation()`` to return an annotation. Return None if it
doesn't exists. This helper will never do any write operation in the
database.

- Add ``deleteAnnotation()`` to delete an annotation (if it exists).

1.3 (2012-05-01)
----------------
Expand Down
3 changes: 3 additions & 0 deletions README.txt
Expand Up @@ -75,6 +75,9 @@ Base classes
interface. Return the annotation if found, None otherwise. This
will not *make* any write operation.

``deleteAnnotation(model, interface)``
Look for the given annotation and delete it from the model.

In addition, the ``grokcore.annotation`` package exposes the
`grokcore.component`_ API.

Expand Down
1 change: 1 addition & 0 deletions src/grokcore/annotation/__init__.py
Expand Up @@ -18,6 +18,7 @@

from grokcore.annotation.components import Annotation, Model
from grokcore.annotation.components import queryAnnotation
from grokcore.annotation.components import deleteAnnotation

# BBB These two functions are meant for test fixtures and should be
# imported from grok.testing, not from grok.
Expand Down
19 changes: 17 additions & 2 deletions src/grokcore/annotation/components.py
Expand Up @@ -45,12 +45,19 @@ def __init__(self, factory, name):
self.factory = factory
self.name = name

def get(self, context):
def query(self, context):
"""Return None if the annotation doesn't exists.
"""
annotations = IAnnotations(context)
return annotations.get(self.name)

def delete(self, context):
annotations = IAnnotations(context)
if self.name in annotations:
del annotations[self.name]
return True
return False

def __call__(self, context):
annotations = IAnnotations(context)
try:
Expand All @@ -71,4 +78,12 @@ def queryAnnotation(context, interface):
factory = manager.adapters.lookup((providedBy(context),), interface)
if not IAnnotationFactory.providedBy(factory):
return None
return factory.get(context)
return factory.query(context)


def deleteAnnotation(context, interface):
manager = getSiteManager()
factory = manager.adapters.lookup((providedBy(context),), interface)
if not IAnnotationFactory.providedBy(factory):
return False
return factory.delete(context)
9 changes: 7 additions & 2 deletions src/grokcore/annotation/interfaces.py
Expand Up @@ -27,11 +27,15 @@ class IAnnotationFactory(interface.Interface):
factory = interface.Attribute('Class to create a new annotation')
name = interface.Attribute('Name of the annotation')

def get(context):
def query(context):
"""Return the existing annotation or None if no annotaion
was created before.
"""

def delete(context):
"""Delete the existing annotation on the context.
"""

def __call__(context):
"""Return the existing annotation or create a new one.
"""
Expand All @@ -40,4 +44,5 @@ def __call__(context):
class IGrokcoreAnnotationAPI(IBaseClasses):
"""grokcore.annotation API description.
"""
queryAnnotation = interface.Attribute('Function to query an annotation')
queryAnnotation = interface.Attribute('Query an annotation or return None')
deleteAnnotation = interface.Attribute('Delete an annotation if it exists')
8 changes: 8 additions & 0 deletions src/grokcore/annotation/tests/annotation/annotation.py
Expand Up @@ -42,6 +42,14 @@
['mine', 'yours']
>>>
And you can delete an annotation:
>>> grok.deleteAnnotation(manfred, IBranding)
True
>>> grok.queryAnnotation(manfred, IBranding) is None
True
"""

Expand Down

0 comments on commit e556d6d

Please sign in to comment.