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

Jazzband Checklist #32

Merged
merged 14 commits into from
Sep 11, 2024
4 changes: 4 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[flake8]
max-line-length = 120
exclude = .git, .venv, __pycache__, migrations, tests
ignore = E203, E501, W605, W504, E265, W503
30 changes: 30 additions & 0 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Python package

on:
push:
branches: '**' #[ $default-branch ]
pull_request:
branches: '**'

jobs:
build:

runs-on: ubuntu-latest
strategy:
fail-fast: false
# matrix:
# python-version: ["3.3.7", "3.4.10", "3.5.10"]

steps:
- uses: actions/checkout@v4
# - name: Set up Python ${{ matrix.python-version }}
# uses: actions/setup-python@v3
# with:
# python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 tox
- name: Run tests and flake8 with tox
run: |
tox
15 changes: 15 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: debug-statements
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 24.8.0
hooks:
- id: black
- repo: https://github.com/PyCQA/flake8
rev: 7.1.1
hooks:
- id: flake8
4 changes: 2 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Changelog
* Fix formset rendering in Django 1.9. `#17`_
* Add support for Django 1.9's ``get_bound_field``. `#18`_

.. _#17: https://github.com/gregmuellegger/django-superform/pull/17
.. _#18: https://github.com/gregmuellegger/django-superform/pull/18
.. _#17: https://github.com/jazzband/django-superform/pull/17
.. _#18: https://github.com/jazzband/django-superform/pull/18

0.3.1
-----
Expand Down
5 changes: 5 additions & 0 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.. image:: https://jazzband.co/static/img/jazzband.svg
:target: https://jazzband.co/
:alt: Jazzband

This is a `Jazzband <https://jazzband.co>`_ project. By contributing, you agree to abide by the `Contributor Code of Conduct <https://jazzband.co/about/conduct>`_ and follow the `guidelines <https://jazzband.co/about/guidelines>`_.
13 changes: 5 additions & 8 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ django-superform

**Less sucking formsets.**

|build| |package| |gitter|
|build| |package| |jazzband|

Documentation_ | Changelog_ | Requirements_ | Installation_

Expand Down Expand Up @@ -126,7 +126,7 @@ Development

- Clone django-superform::

git clone [email protected]:gregmuellegger/django-superform.git
git clone [email protected]:jazzband/django-superform.git

- ``cd`` into the repository::

Expand Down Expand Up @@ -157,13 +157,10 @@ Full documentation is available on Read the Docs: https://django-superform.readt

.. |build| image:: https://travis-ci.org/gregmuellegger/django-superform.svg?branch=master
:alt: Build Status
:scale: 100%
:target: https://travis-ci.org/gregmuellegger/django-superform
.. |package| image:: https://badge.fury.io/py/django-superform.svg
:alt: Package Version
:scale: 100%
:target: http://badge.fury.io/py/django-superform
.. |gitter| image:: https://badges.gitter.im/JoinChat.svg
:alt: Gitter Chat, discuss django-superform with others
:scale: 100%
:target: https://gitter.im/gregmuellegger/django-superform
.. |jazzband| image:: https://jazzband.co/static/img/badge.svg
:target: https://jazzband.co
:alt: Jazzband
25 changes: 19 additions & 6 deletions django_superform/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,29 @@
See http://django-superform.readthedocs.org/en/latest/ for complete docs.
"""
from .fields import (
FormField, ModelFormField, ForeignKeyFormField, FormSetField,
ModelFormSetField, InlineFormSetField)
FormField,
ModelFormField,
ForeignKeyFormField,
FormSetField,
ModelFormSetField,
InlineFormSetField,
)
from .forms import SuperForm, SuperModelForm
from .widgets import FormWidget, FormSetWidget


__version__ = '0.4.0.dev1'
__version__ = "0.4.0.dev1"


__all__ = (
'FormField', 'ModelFormField', 'ForeignKeyFormField', 'FormSetField',
'ModelFormSetField', 'InlineFormSetField', 'SuperForm', 'SuperModelForm',
'FormWidget', 'FormSetWidget')
"FormField",
"ModelFormField",
"ForeignKeyFormField",
"FormSetField",
"ModelFormSetField",
"InlineFormSetField",
"SuperForm",
"SuperModelForm",
"FormWidget",
"FormSetWidget",
)
106 changes: 63 additions & 43 deletions django_superform/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@ class BaseCompositeField(object):
# order.
creation_counter = 0

def __init__(self, required=True, widget=None, label=None, help_text='',
localize=False, disabled=False):
def __init__(
self,
required=True,
widget=None,
label=None,
help_text="",
localize=False,
disabled=False,
):
self.required = required
self.label = label
self.help_text = help_text
Expand Down Expand Up @@ -52,7 +59,7 @@ class CompositeField(BaseCompositeField):
This field cannot be used directly, use a subclass of it.
"""

prefix_name = 'composite'
prefix_name = "composite"

def __init__(self, *args, **kwargs):
super(CompositeField, self).__init__(*args, **kwargs)
Expand All @@ -68,10 +75,11 @@ def get_prefix(self, form, name):
"""
Return the prefix that is used for the formset.
"""
return '{form_prefix}{prefix_name}-{field_name}'.format(
form_prefix=form.prefix + '-' if form.prefix else '',
return "{form_prefix}{prefix_name}-{field_name}".format(
form_prefix=form.prefix + "-" if form.prefix else "",
prefix_name=self.prefix_name,
field_name=name)
field_name=name,
)

def get_initial(self, form, name):
"""
Expand All @@ -80,7 +88,7 @@ def get_initial(self, form, name):
given.
"""

if hasattr(form, 'initial'):
if hasattr(form, "initial"):
return form.initial.get(name, None)
return None

Expand All @@ -89,8 +97,8 @@ def get_kwargs(self, form, name):
Return the keyword arguments that are used to instantiate the formset.
"""
kwargs = {
'prefix': self.get_prefix(form, name),
'initial': self.get_initial(form, name),
"prefix": self.get_prefix(form, name),
"initial": self.get_initial(form, name),
}
kwargs.update(self.default_kwargs)
return kwargs
Expand Down Expand Up @@ -148,7 +156,7 @@ class RegistrationForm(SuperForm):
The first method (using ``kwargs``) will take precedence.
"""

prefix_name = 'form'
prefix_name = "form"
widget = FormWidget

def __init__(self, form_class, kwargs=None, **field_kwargs):
Expand Down Expand Up @@ -176,7 +184,8 @@ def get_form(self, form, name):
composite_form = form_class(
data=form.data if form.is_bound else None,
files=form.files if form.is_bound else None,
**kwargs)
**kwargs
)
return composite_form


Expand Down Expand Up @@ -246,8 +255,8 @@ def get_kwargs(self, form, name):
"""
kwargs = super(ModelFormField, self).get_kwargs(form, name)
instance = self.get_instance(form, name)
kwargs.setdefault('instance', instance)
kwargs.setdefault('empty_permitted', not self.required)
kwargs.setdefault("instance", instance)
kwargs.setdefault("empty_permitted", not self.required)
return kwargs

def shall_save(self, form, name, composite_form):
Expand Down Expand Up @@ -279,20 +288,20 @@ def save(self, form, name, composite_form, commit):


class ForeignKeyFormField(ModelFormField):
def __init__(self, form_class, kwargs=None, field_name=None, blank=None,
**field_kwargs):
super(ForeignKeyFormField, self).__init__(form_class, kwargs,
**field_kwargs)
def __init__(
self, form_class, kwargs=None, field_name=None, blank=None, **field_kwargs
):
super(ForeignKeyFormField, self).__init__(form_class, kwargs, **field_kwargs)
self.field_name = field_name
self.blank = blank

def get_kwargs(self, form, name):
kwargs = super(ForeignKeyFormField, self).get_kwargs(form, name)
if 'instance' not in kwargs:
kwargs.setdefault('instance', self.get_instance(form, name))
if 'empty_permitted' not in kwargs:
if "instance" not in kwargs:
kwargs.setdefault("instance", self.get_instance(form, name))
if "empty_permitted" not in kwargs:
if self.allow_blank(form, name):
kwargs['empty_permitted'] = True
kwargs["empty_permitted"] = True
return kwargs

def get_field_name(self, form, name):
Expand Down Expand Up @@ -323,16 +332,17 @@ def save(self, form, name, composite_form, commit):
if composite_form.empty_permitted and not composite_form.has_changed():
saved_obj = composite_form.instance
else:
saved_obj = super(ForeignKeyFormField, self).save(form, name,
composite_form,
commit)
saved_obj = super(ForeignKeyFormField, self).save(
form, name, composite_form, commit
)
setattr(form.instance, self.get_field_name(form, name), saved_obj)
if commit:
form.instance.save()
else:
raise NotImplementedError(
'ForeignKeyFormField cannot yet be used with non-commiting '
'form saves.')
"ForeignKeyFormField cannot yet be used with non-commiting "
"form saves."
)
return saved_obj


Expand All @@ -345,7 +355,7 @@ class FormSetField(CompositeField):
are used when the ``formset_class`` is instantiated.
"""

prefix_name = 'formset'
prefix_name = "formset"
widget = FormSetWidget

def __init__(self, formset_class, kwargs=None, **field_kwargs):
Expand Down Expand Up @@ -373,7 +383,8 @@ def get_formset(self, form, name):
formset = formset_class(
form.data if form.is_bound else None,
form.files if form.is_bound else None,
**kwargs)
**kwargs
)
return formset


Expand Down Expand Up @@ -434,8 +445,14 @@ class Meta:
extra=1)
"""

def __init__(self, parent_model=None, model=None, formset_class=None,
kwargs=None, **factory_kwargs):
def __init__(
self,
parent_model=None,
model=None,
formset_class=None,
kwargs=None,
**factory_kwargs
):
"""
You need to either provide the ``formset_class`` or the ``model``
argument.
Expand All @@ -448,25 +465,27 @@ def __init__(self, parent_model=None, model=None, formset_class=None,
# Make sure that all standard arguments will get passed through to the
# parent's __init__ method.
field_kwargs = {}
for arg in ['required', 'widget', 'label', 'help_text', 'localize']:
for arg in ["required", "widget", "label", "help_text", "localize"]:
if arg in factory_kwargs:
field_kwargs[arg] = factory_kwargs.pop(arg)

self.parent_model = parent_model
self.model = model
self.formset_factory_kwargs = factory_kwargs
super(InlineFormSetField, self).__init__(formset_class, kwargs=kwargs,
**field_kwargs)
super(InlineFormSetField, self).__init__(
formset_class, kwargs=kwargs, **field_kwargs
)
if (
self.formset_class is None and
'form' not in self.formset_factory_kwargs and
'fields' not in self.formset_factory_kwargs and
'exclude' not in self.formset_factory_kwargs):
self.formset_class is None
and "form" not in self.formset_factory_kwargs
and "fields" not in self.formset_factory_kwargs
and "exclude" not in self.formset_factory_kwargs
):
raise ValueError(
'You need to either specify the `formset_class` argument or '
'one of `form`/`fields`/`exclude` arguments '
'when creating a {0}.'
.format(self.__class__.__name__))
"You need to either specify the `formset_class` argument or "
"one of `form`/`fields`/`exclude` arguments "
"when creating a {0}.".format(self.__class__.__name__)
)

def get_model(self, form, name):
return self.model
Expand All @@ -487,10 +506,11 @@ def get_formset_class(self, form, name):
formset_class = inlineformset_factory(
self.get_parent_model(form, name),
self.get_model(form, name),
**self.formset_factory_kwargs)
**self.formset_factory_kwargs
)
return formset_class

def get_kwargs(self, form, name):
kwargs = super(InlineFormSetField, self).get_kwargs(form, name)
kwargs.setdefault('instance', form.instance)
kwargs.setdefault("instance", form.instance)
return kwargs
Loading