Skip to content

Commit

Permalink
Fixed Compatibility with Django-3.1 (#48)
Browse files Browse the repository at this point in the history
* Fixed Compatibility with Django-3.1

* Update django from 3.1 to 3.1.1

* Blacked Correctly

* Fixed Flake8 issue
  • Loading branch information
raratiru authored Oct 5, 2020
1 parent 2d971b9 commit 5f062cb
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 31 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)

[![Python Versions](https://img.shields.io/badge/Python-3.5%20|%203.6%20|%203.7%20|%203.8-%236600cc)](https://docs.djangoproject.com/en/dev/faq/install/#what-python-version-can-i-use-with-django)
[![Django Versions](https://img.shields.io/badge/Django-2.2%20|%203.0-brown.svg)](https://www.djangoproject.com/download/)
[![Django Versions](https://img.shields.io/badge/Django-2.2%20|%203.1-brown.svg)](https://www.djangoproject.com/download/)
[![Database Window Functions](https://img.shields.io/badge/Database-Window%20Functions-important.svg)](https://www.sql-workbench.eu/dbms_comparison.html)

Let's Agree
Expand Down Expand Up @@ -46,7 +46,7 @@ Prerequisites
-------

* Python 3.5, 3.6, 3.7, 3.8
* Django 2.2, 3.0
* Django 2.2, 3.1
* [Django Admin Site](https://docs.djangoproject.com/en/dev/ref/contrib/admin/) (enabled by default in Django)
* A database with [Window Functions support](https://www.sql-workbench.eu/dbms_comparison.html)
* [`django-translated-fields`](https://github.com/matthiask/django-translated-fields)
Expand Down Expand Up @@ -252,6 +252,8 @@ Unfortunatelly, the test suite is rather complicated. Sorry!

Changelog
---------
1.1.6: Fixed compatibility with Django-3.1

1.1.5: Fixed bug in LETSAGREE_LOGOUT_URL setting.

1.1.4: Deprecated `LETSAGREE_LOGOUT_APP_NAME` in favor of `LETSAGREE_LOGOUT_URL`
Expand Down
25 changes: 13 additions & 12 deletions letsagree/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
#
# Creation Date : Mon 25 Feb 2019 05:44:28 PM EET (17:44)
#
# Last Modified : Mon 08 Apr 2019 03:16:38 PM EEST (15:16)
# Last Modified : Mon 05 Oct 2020 07:06:40 PM EEST (19:06)
#
# ==============================================================================

from django import forms
from django.conf import settings
from django.utils.translation import gettext_lazy as _
from letsagree import models
from translated_fields import to_attribute


class PendingConsentForm(forms.ModelForm):
Expand Down Expand Up @@ -44,15 +43,17 @@ class Media:
js = getattr(settings, "LETSAGREE_JS", tuple())

class Meta:
model = models.Term
fields = (
"date_created",
to_attribute("summary"),
to_attribute("content"),
"agree",
)
"""
The fields here are not fine-grained based on the active language because
get_language() has contenxt only within the request/response cycle.
In this case, it happens within the View insance where modelformset_factory
is initialized with the appropriate fields.
If needed, the default language should be explicitly queried from the
settings.DEFAULT_LANGUAGE.
PendingAgreementFormSet = forms.modelformset_factory(
models.Term, form=PendingConsentForm, extra=0
)
In this case, however, the modelform is not enabled in the admin.
"""

model = models.Term
fields = "__all__"
4 changes: 2 additions & 2 deletions letsagree/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ def consent_is_required(self):

def get_next_parameter(self):
"""
If next parameter exists in request, set it also in the redirect
unless it equals the url of the consent form.
If next parameter exists in request, set it also in the redirect
unless it equals the url of the consent form.
"""
return (
"?next={0}".format(self.get_next)
Expand Down
12 changes: 6 additions & 6 deletions letsagree/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#
# Creation Date : Sat 23 Mar 2019 08:42:45 PM EET (20:42)
#
# Last Modified : Tue 18 Aug 2020 11:26:18 AM EEST (11:26)
# Last Modified : Mon 05 Oct 2020 08:07:46 PM EEST (20:07)
#
# ==============================================================================

Expand All @@ -19,7 +19,7 @@
from django.conf import settings
from django.test import RequestFactory
from django.urls import reverse
from letsagree import views, models, forms
from letsagree import views, models # , forms

pytestmark = pytest.mark.django_db

Expand Down Expand Up @@ -132,10 +132,10 @@ def test_view_post(queries, admin_client, settings):
key_name = "form-{0}-{1}".format(count, key)
data[key_name] = value

# Test formset is valid and formset does not save to db
formset = forms.PendingAgreementFormSet(data=data)
assert formset.is_valid()
assert formset.save() == [None, None, None]
# # Test formset is valid and formset does not save to db
# formset = forms.PendingAgreementFormSet(data=data)
# assert formset.is_valid()
# assert formset.save() == [None, None, None]

# Create the post request
factory = RequestFactory()
Expand Down
28 changes: 25 additions & 3 deletions letsagree/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,54 @@
#
# Creation Date : Sun 27 Jan 2019 07:54:42 PM EET (19:54)
#
# Last Modified : Tue 18 Aug 2020 11:15:32 AM EEST (11:15)
# Last Modified : Mon 05 Oct 2020 07:06:53 PM EEST (19:06)
#
# ==============================================================================

from django.db import transaction
from django.conf import settings
from django.core.cache import cache
from django.forms import modelformset_factory
from django.http import Http404
from django.utils.translation import gettext_lazy as _
from django.views.generic import FormView
from translated_fields import to_attribute
from letsagree import models
from letsagree.forms import PendingAgreementFormSet
from letsagree.forms import PendingConsentForm
from letsagree.helpers import get_logout_url


class PendingView(FormView):
http_method_names = ["get", "post"]
template_name = "letsagree/pending.html"
form_class = PendingAgreementFormSet

def dispatch(self, request, *args, **kwargs):
if not request.user.is_authenticated:
raise Http404()
self.success_url = request.GET.get("next")
return super().dispatch(request, *args, **kwargs)

def get_form_class(self):
"""
Initialize modelformset_factory within the FormView instance instead of
the form_class because get_language() has a context only within the
request/response cycle.
https://code.djangoproject.com/ticket/31911#ticket
https://github.com/matthiask/django-translated-fields/issues/24#issuecomment-678069602
"""
return modelformset_factory(
models.Term,
form=PendingConsentForm,
extra=0,
fields=(
"date_created",
to_attribute("summary"),
to_attribute("content"),
"agree",
),
)

def get_form_kwargs(self):
"""
Pass to the modelformset_factory a queryset argument to create the
Expand Down
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#
# Creation Date : Mon 08 Apr 2019 07:00:40 PM EEST (19:00)
#
# Last Modified : Tue 18 Aug 2020 11:27:52 AM EEST (11:27)
# Last Modified : Mon 05 Oct 2020 07:35:24 PM EEST (19:35)
#
# ==============================================================================

Expand All @@ -22,7 +22,7 @@

setup(
name="django-letsagree",
version="1.1.5",
version="1.1.6",
python_requires=">=3.5",
description=(
"A django application that associates Groups with Terms "
Expand All @@ -35,14 +35,14 @@
license="BSD 3-Clause License",
packages=find_packages(exclude=("tests",)),
include_package_data=True,
install_requires=["Django>=2.2,<3.1", "django-translated-fields"],
install_requires=["Django>=2.2", "django-translated-fields"],
zip_safe=False,
classifiers=[
"Development Status :: 5 - Production/Stable",
"Environment :: Web Environment",
"Framework :: Django",
"Framework :: Django :: 2.2",
"Framework :: Django :: 3.0",
"Framework :: Django :: 3.1",
"Intended Audience :: Developers",
"License :: OSI Approved :: BSD License",
"Operating System :: OS Independent",
Expand Down
2 changes: 1 addition & 1 deletion test_setup/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Django==3.1
Django==3.1.1
django-translated-fields==0.10.2
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ deps =
pytest-django
pytest-cov
pytest-factoryboy
django3: Django>=3.0,<3.1
django3: Django>=3.1
django22: Django>=2.2,<2.3
postgres: psycopg2-binary
mariadb: mysqlclient
Expand Down

0 comments on commit 5f062cb

Please sign in to comment.