Skip to content

Commit

Permalink
Update README with the new custom operator feature
Browse files Browse the repository at this point in the history
  • Loading branch information
versae committed May 26, 2013
1 parent 5fac71a commit c8ac9be
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ Enable autocompletion tool (work in progress, not enabled yet)::

QBE_AUTOCOMPLETE = True

Enable an Exhibit faceted navigation for results (not yet implemented)::

QBE_EXHIBIT = False

Admin module name to add admin urls in results::

QBE_ADMIN = "admin"
Expand All @@ -121,58 +117,70 @@ Path to QBE formats export file, in order to add custom export formats::

QBE_FORMATS_EXPORT = "qbe_formats"

Path to custom QBE operators for the criteria::

QBE_CUSTOM_OPERATORS = "qbe_operators"

Custom Operators
--------

Use Custom Operators only if you know what you are doing and at your own risks!

If you need to define custom operators, in one of your ``models.py``, you need to create a new class that extends ``django_qbe.operators.CustomOperator``::
If you need to define custom operators, in a file ``qbe_operators.py`` in your
project root, you need to create a new class that extends
``django_qbe.operators.CustomOperator``::

import datetime
from django.utils import timezone
from django_qbe.operators import CustomOperator


class SinceDaysAgo(CustomOperator):
slug = 'since-days-ago' # REQUIRED and must be unique
label = 'Since Days Ago' # REQUIRED
slug = 'since-days-ago' # REQUIRED and must be unique
label = 'Since Days Ago' # REQUIRED

def get_params(self):
if len(self.params):
return self.params

now = timezone.now()
today = now.replace(hour=0, minute=0, second=0, microsecond=0)
tomorrow = today + datetime.timedelta(days=1)

date_since = today - datetime.timedelta(days=int(self.value))

operator = "gt"
lookup_since = self._get_lookup(operator, str(date_since))
lookup_until = self._get_lookup(operator, str(tomorrow))

self.params.append(lookup_since)
self.params.append(lookup_until)

return self.params

def get_wheres(self):
if len(self.wheres):
return self.wheres

lookup_cast = self._db_operations.lookup_cast
for operator in ["gte", "lt"]:
db_operator = self._db_operators[operator]
self.wheres.append(u"%s %s" \
% (lookup_cast(operator) % self.db_field,
db_operator))
self.wheres.append(u"%s %s" % (
lookup_cast(operator) % self.db_field,
db_operator)
)

return self.wheres

Your custom operator must have 2 attributes, ``slug`` and ``label`` in order to be displayed in the Criteria dropdown.
Your custom operator must have 2 attributes, ``slug`` and ``label`` in order
to be displayed in the Criteria dropdown.

The ``get_params`` and ``get_wheres`` methods must return an iterable instance (eg. list), otherwise it gets converted to a list.
The ``get_params`` and ``get_wheres`` methods must return an iterable instance
(eg. list), otherwise it gets converted to a list.

If you dont want to write it in your ``models.py`` make sure that it is imported in one of the files that are evaluated at runtime (eg. ``models.py`` or ``urls.py``) in order to register your Custom Operator.
If you dont want to write it in your ``models.py`` make sure that it is
imported in one of the files that are evaluated at runtime (eg. ``models.py``
or ``urls.py``) in order to register your Custom Operator.

.. _QBE: http://www.google.com/url?sa=t&source=web&ct=res&cd=2&ved=0CB4QFjAB&url=http%3A%2F%2Fpages.cs.wisc.edu%2F~dbbook%2FopenAccess%2FthirdEdition%2Fqbe.pdf&ei=_UD5S5WSBYP5-Qb-18i8CA&usg=AFQjCNHMv-Pua285zhWT8DevuZFj2gfYKA&sig2=-sTEDWjJhnTaixh2iJfsAw
.. _PyPI: http://pypi.python.org/pypi/django_qbe/
Expand Down

0 comments on commit c8ac9be

Please sign in to comment.