Skip to content

Commit

Permalink
prepare 0.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
barseghyanartur committed Mar 26, 2019
1 parent 8e9eb74 commit d6e68f4
Show file tree
Hide file tree
Showing 20 changed files with 163 additions and 84 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changelog
=========

0.3.1
-----
2019-03-26

- More tests.
- Addition to docs.

0.3
---
2019-03-25
Expand Down
13 changes: 13 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,25 @@ Usage
**Sample view**

With function-based views:

.. code-block:: python
def person_list(request):
filter = PersonFilter(request.GET, queryset=Person.objects())
return render(request, "dfm_app/person_list.html", {"objects": filter.qs})
Or class-based views:

.. code-block:: python
from django_mongoengine_filter.views import FilterView
class PersonListView(FilterView):
filterset_class = PersonFilter
template_name = "dfm_app/person_list.html"
**Sample template**

.. code-block:: html
Expand Down
12 changes: 6 additions & 6 deletions django_mongoengine_filter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from .filterset import FilterSet
from .filters import *

VERSION = (0, 2)
VERSION = (0, 3, 1)

__title__ = 'django-mongoengine-filter'
__version__ = '.'.join([str(_i) for _i in VERSION])
__author__ = 'Artur Barseghyan <[email protected]>'
__copyright__ = '2019 Artur Barseghyan'
__license__ = 'GPL 2.0/LGPL 2.1'
__title__ = "django-mongoengine-filter"
__version__ = ".".join([str(_i) for _i in VERSION])
__author__ = "Artur Barseghyan <[email protected]>"
__copyright__ = "2019 Artur Barseghyan"
__license__ = "GPL 2.0/LGPL 2.1"
9 changes: 2 additions & 7 deletions django_mongoengine_filter/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@

from .widgets import RangeWidget, LookupTypeWidget

__all__ = (
"Lookup",
"LookupTypeField",
"RangeField",
)
__all__ = ("Lookup", "LookupTypeField", "RangeField")


class RangeField(forms.MultiValueField):
Expand Down Expand Up @@ -41,7 +37,6 @@ def __init__(self, field, lookup_choices, *args, **kwargs):
def compress(self, data_list):
if len(data_list) == 2:
return Lookup(
value=data_list[0],
lookup_type=data_list[1] or "exact"
value=data_list[0], lookup_type=data_list[1] or "exact"
)
return Lookup(value=None, lookup_type="exact")
17 changes: 13 additions & 4 deletions django_mongoengine_filter/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,15 @@ def __init__(
def field(self):
if not hasattr(self, "_field"):
help_text = _("This is an exclusion filter") if self.exclude else ""
if self.lookup_type is None or isinstance(self.lookup_type, (list, tuple)):
if self.lookup_type is None or isinstance(
self.lookup_type, (list, tuple)
):
if self.lookup_type is None:
lookup = [(x, x) for x in LOOKUP_TYPES]
else:
lookup = [(x, x) for x in LOOKUP_TYPES if x in self.lookup_type]
lookup = [
(x, x) for x in LOOKUP_TYPES if x in self.lookup_type
]
self._field = LookupTypeField(
self.field_class(
required=self.required, widget=self.widget, **self.extra
Expand Down Expand Up @@ -175,7 +179,9 @@ def filter(self, qs, value):
if value:
start_lookup = "%s__gte" % self.name
stop_lookup = "%s__lte" % self.name
return qs.filter(**{start_lookup: value.start, stop_lookup: value.stop})
return qs.filter(
**{start_lookup: value.start, stop_lookup: value.stop}
)
return qs


Expand Down Expand Up @@ -207,7 +213,10 @@ class DateRangeFilter(ChoiceFilter):
3: (
_("This month"),
lambda qs, name: qs.filter(
**{"%s__year" % name: now().year, "%s__month" % name: now().month}
**{
"%s__year" % name: now().year,
"%s__month" % name: now().month,
}
),
),
4: (
Expand Down
37 changes: 28 additions & 9 deletions django_mongoengine_filter/filterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,16 @@ def __new__(cls, name, bases, attrs):
# We are defining FilterSet itself here
parents = None
declared_filters = get_declared_filters(bases, attrs, False)
new_class = super(FilterSetMetaclass, cls).__new__(cls, name, bases, attrs)
new_class = super(FilterSetMetaclass, cls).__new__(
cls, name, bases, attrs
)

if not parents:
return new_class

opts = new_class._meta = FilterSetOptions(getattr(new_class, "Meta", None))
opts = new_class._meta = FilterSetOptions(
getattr(new_class, "Meta", None)
)
if opts.model:
filters = filters_for_model(
opts.model,
Expand All @@ -161,7 +165,8 @@ def __new__(cls, name, bases, attrs):

if None in filters.values():
raise TypeError(
"Meta.fields contains a field that isn't defined " "on this FilterSet"
"Meta.fields contains a field that isn't defined "
"on this FilterSet"
)

new_class.declared_filters = declared_filters
Expand Down Expand Up @@ -258,7 +263,9 @@ def qs(self):
pass

if ordered_value in EMPTY_VALUES and self.strict:
ordered_value = self.form.fields[self.order_by_field].choices[0][0]
ordered_value = self.form.fields[
self.order_by_field
].choices[0][0]

if ordered_value:
qs = qs.order_by(*self.get_order_by(ordered_value))
Expand All @@ -274,11 +281,16 @@ def count(self):
def form(self):
if not hasattr(self, "_form"):
fields = OrderedDict(
[(name, filter_.field) for name, filter_ in six.iteritems(self.filters)]
[
(name, filter_.field)
for name, filter_ in six.iteritems(self.filters)
]
)
fields[self.order_by_field] = self.ordering_field
Form = type(
str("%sForm" % self.__class__.__name__), (self._meta.form,), fields
str("%sForm" % self.__class__.__name__),
(self._meta.form,),
fields,
)
if self.is_bound:
self._form = Form(self.data, prefix=self.form_prefix)
Expand Down Expand Up @@ -312,11 +324,16 @@ def get_ordering_field(self):
(fltr.name or f, fltr.label or capfirst(f)),
(
"-%s" % (fltr.name or f),
_("%s (descending)" % (fltr.label or capfirst(f))),
_(
"%s (descending)"
% (fltr.label or capfirst(f))
),
),
]
)
return forms.ChoiceField(label="Ordering", required=False, choices=choices)
return forms.ChoiceField(
label="Ordering", required=False, choices=choices
)

@property
def ordering_field(self):
Expand Down Expand Up @@ -383,6 +400,8 @@ class FilterSet(six.with_metaclass(FilterSetMetaclass, BaseFilterSet)):
def filterset_factory(model):
meta = type(str("Meta"), (object,), {"model": model})
filterset = type(
str("%sFilterSet" % model._meta.object_name), (FilterSet,), {"Meta": meta}
str("%sFilterSet" % model._meta.object_name),
(FilterSet,),
{"Meta": meta},
)
return filterset
7 changes: 1 addition & 6 deletions django_mongoengine_filter/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@

from .filterset import filterset_factory

__all__ = (
"BaseFilterView",
"FilterMixin",
"FilterView",
"object_filter",
)
__all__ = ("BaseFilterView", "FilterMixin", "FilterView", "object_filter")


class FilterMixin(object):
Expand Down
6 changes: 1 addition & 5 deletions django_mongoengine_filter/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _

__all__ = (
"LinkWidget",
"LookupTypeWidget",
"RangeWidget",
)
__all__ = ("LinkWidget", "LookupTypeWidget", "RangeWidget")


class LinkWidget(forms.Widget):
Expand Down
7 changes: 7 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changelog
=========

0.3.1
-----
2019-03-26

- More tests.
- Addition to docs.

0.3
---
2019-03-25
Expand Down
11 changes: 2 additions & 9 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ class Settings(object):

docs_settings = Settings()
docs_settings.DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": ":memory:"
}
"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}
}

docs_settings.SECRET_KEY = "top-secret"
Expand Down Expand Up @@ -75,11 +72,7 @@ class Settings(object):
}
]

docs_settings.ALLOWED_HOSTS = [
'*',
'127.0.0.1',
'localhost',
]
docs_settings.ALLOWED_HOSTS = ["*", "127.0.0.1", "localhost"]

# -- Django configuration ------------------------------------------------------
from django.conf import settings
Expand Down
13 changes: 13 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,25 @@ Usage
**Sample view**

With function-based views:

.. code-block:: python
def person_list(request):
filter = PersonFilter(request.GET, queryset=Person.objects())
return render(request, "dfm_app/person_list.html", {"objects": filter.qs})
Or class-based views:

.. code-block:: python
from django_mongoengine_filter.views import FilterView
class PersonListView(FilterView):
filterset_class = PersonFilter
template_name = "dfm_app/person_list.html"
**Sample template**

.. code-block:: html
Expand Down
10 changes: 5 additions & 5 deletions scripts/black.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

black django_mongoengine_filter/
black tests/
black setup.py
black runtests.py
black docs/
black django_mongoengine_filter/ --line-length 80
black tests/ --line-length 80
black setup.py --line-length 80
black runtests.py --line-length 80
black docs/ --line-length 80
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name="django-mongoengine-filter",
version="0.3",
version="0.3.1",
description=(
"django-mongoengine-filter is a reusable Django application inspired "
"from django-filter for allowing mongoengine users to filter querysets "
Expand All @@ -28,9 +28,9 @@
"later (LGPLv2+)",
"Operating System :: OS Independent",
"Programming Language :: Python",
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Framework :: Django",
],
include_package_data=True,
Expand Down
5 changes: 4 additions & 1 deletion tests/dfm_app/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
PROFILE_TYPE_FREE = "free"
PROFILE_TYPE_MEMBER = "member"
PROFILE_TYPES = (PROFILE_TYPE_FREE, PROFILE_TYPE_MEMBER)
PROFILE_TYPES_CHOICES = ((PROFILE_TYPE_FREE, "Free"), (PROFILE_TYPE_MEMBER, "Member"))
PROFILE_TYPES_CHOICES = (
(PROFILE_TYPE_FREE, "Free"),
(PROFILE_TYPE_MEMBER, "Member"),
)

GENDER_MALE = "male"
GENDER_FEMALE = "female"
Expand Down
11 changes: 9 additions & 2 deletions tests/dfm_app/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class Person(document.Document):
required=True, max_length=255, default="Robot", verbose_name="Name"
)
age = fields.IntField(required=True, verbose_name="Age")
num_fingers = fields.IntField(required=False, verbose_name="Number of fingers")
num_fingers = fields.IntField(
required=False, verbose_name="Number of fingers"
)
profile_type = fields.StringField(
required=False,
blank=False,
Expand All @@ -19,8 +21,13 @@ class Person(document.Document):
default=PROFILE_TYPE_FREE,
)
gender = fields.StringField(
required=False, blank=False, null=False, choices=GENDERS, default=GENDER_MALE
required=False,
blank=False,
null=False,
choices=GENDERS,
default=GENDER_MALE,
)
agnostic = fields.BooleanField(default=True)

def __str__(self):
return self.name
5 changes: 3 additions & 2 deletions tests/dfm_app/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ class PersonFilter(django_mongoengine_filter.FilterSet):
ten_fingers = django_mongoengine_filter.MethodFilter(
action="ten_fingers_filter"
)
# agnostic = django_mongoengine_filter.BooleanFilter()
# gender = django_mongoengine_filter.StringFilter()
# contract_type = django_mongoengine_filter.StringFilter()
# type = django_mongoengine_filter.StringFilter()
# work_think_level = django_mongoengine_filter.StringFilter()

class Meta:
model = Person
fields = ["profile_type", "ten_fingers"]
fields = ["profile_type", "ten_fingers", "agnostic"]

def ten_fingers_filter(self, queryset, name, value):
if value == 'yes':
if value == "yes":
return queryset.filter(num_fingers=10)
return queryset
Loading

0 comments on commit d6e68f4

Please sign in to comment.