Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow include/exclude query parameters and delimiter to be configurable in settings #33

Merged
merged 3 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
set -xe
pip install ".[dev]"
run: pip install ".[dev]"

- name: Run tests for ${{ matrix.python-version }}
run: python -m pytest --cov=drf_queryfields

- name: Upload coverage to Codecov
uses: codecov/codecov-action@main
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}

tests-27:
name: Python 2.7 on ubuntu-20.04
Expand All @@ -45,12 +45,12 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Install dependencies
run: |
set -xe
pip install ".[dev]"
run: pip install ".[dev]"

- name: Run tests for Python 2.7 on Ubuntu 20.04
run: python -m pytest --cov=drf_queryfields

- name: Upload coverage to Codecov
uses: codecov/codecov-action@main
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
10 changes: 10 additions & 0 deletions docs/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ A:

Now request like ``GET /things/?exclude=key2|key3`` instead of the default ``GET /things/?fields!=key2,key3``.

*New in v1.1.0*: The Django settings module may also be used, for example:

.. code-block:: python

# in settings.py
DRF_QUERYFIELDS_INCLUDE_ARG_NAME = 'gimme'
DRF_QUERYFIELDS_EXCLUDE_ARG_NAME = 'omit'
DRF_QUERYFIELDS_DELIMITER = '+'


Q:
This thing broke, you suck... / Hey, wouldn't it be cool if...
A:
Expand Down
18 changes: 18 additions & 0 deletions drf_queryfields/mixins.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from django.conf import settings


class QueryFieldsMixin(object):

# If using Django filters in the API, these labels mustn't conflict with any model field names.
Expand All @@ -10,6 +13,21 @@ class QueryFieldsMixin(object):

def __init__(self, *args, **kwargs):
super(QueryFieldsMixin, self).__init__(*args, **kwargs)
self.include_arg_name = getattr(
settings,
'DRF_QUERYFIELDS_INCLUDE_ARG_NAME',
self.include_arg_name,
)
self.exclude_arg_name = getattr(
settings,
'DRF_QUERYFIELDS_EXCLUDE_ARG_NAME',
self.exclude_arg_name,
)
self.delimiter = getattr(
settings,
'DRF_QUERYFIELDS_DELIMITER',
self.delimiter,
)

try:
request = self.context['request']
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

setup(
name='djangorestframework-queryfields',
version='1.0.0',
version='1.1.0',
description='Serialize a partial subset of fields in the API',
long_description=long_description,
packages=['drf_queryfields'],
Expand Down
2 changes: 2 additions & 0 deletions test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
)

ROOT_URLCONF = 'tests.app.urls'

USE_TZ = True
21 changes: 21 additions & 0 deletions tests/test_custom_settings_override.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from django.test import override_settings
from rest_framework.test import APIClient

from tests.utils import decode_content


@override_settings(DRF_QUERYFIELDS_EXCLUDE_ARG_NAME="omit")
def test_list_response_filtered_excludes():
response = APIClient().get('/quotes/?omit=line')
expected = [
{
'character': 'Customer',
'sketch': 'CHEESE SHOP',
},
{
'character': 'The Black Knight',
'sketch': 'HOLY GRAIL',
},
]
content = decode_content(response)
assert content == expected