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

Use psycopg2-binary in order to remove Django warnings and fix a typo in docs #25

Merged
merged 12 commits into from
Mar 27, 2019
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ env:
matrix:
- DJANGO='1.11'
- DJANGO='2.0'
- DJANGO='2.1'

matrix:
include:
Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Define a type and add it to a model:

```python
from django.db import models
from postgres_composite_type import CompositeType
from postgres_composite_types import CompositeType

class Address(CompositeType):
"""An address."""
Expand Down Expand Up @@ -144,6 +144,17 @@ Lookups and indexes are not implemented yet
([bug #9](https://github.com/danni/django-postgres-composite-types/issues/9),
[bug #10](https://github.com/danni/django-postgres-composite-types/issues/10)).

Running Tests
-------------------
Clone the repository, go to it's base directory and run the following commands.

pip install tox
tox

Or if you want a specific environment

tox -e py35-dj2.0

Authors
-------

Expand Down
14 changes: 12 additions & 2 deletions postgres_composite_types/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@
import logging
from collections import OrderedDict

from django import forms
from django import VERSION, forms
from django.contrib.postgres.utils import prefix_validation_error
from django.utils.translation import ugettext as _

from . import CompositeType

LOGGER = logging.getLogger(__name__)

DJANGO21 = VERSION >= (2, 1)


class CompositeBoundField(forms.BoundField):
"""
Expand Down Expand Up @@ -144,9 +146,13 @@ def clean(self, value):
try:
cleaned_data[name] = field.clean(value.get(name))
except forms.ValidationError as error:
if DJANGO21:
prefix = '%(label)s:'
else:
prefix = '%(label)s: '
errors.append(prefix_validation_error(
error, code='field_invalid',
prefix='%(label)s: ', params={'label': field.label}))
prefix=prefix, params={'label': field.label}))
if errors:
raise forms.ValidationError(errors)
value = self.model(**cleaned_data)
Expand Down Expand Up @@ -218,6 +224,10 @@ def value_from_datadict(self, data, files, name):
for subname, widget in self.widgets.items()
}

def value_omitted_from_data(self, data, files, name):
prefix = '{}-'.format(name)
return not any(key.startswith(prefix) for key in data)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


def id_for_label(self, id_):
"""
Wrapper around the field widget's `id_for_label` method.
Expand Down
2 changes: 1 addition & 1 deletion requirements.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Django >= 1.11
psycopg2
psycopg2-binary
15 changes: 15 additions & 0 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,21 @@ def test_null_initial_data(self):
""",
str(form['simple_field']))

def test_value_omission_check_inside_widget(self):
"""
Assert that CompositeTypeWidget.value_omitted_from_data function
will return False when passing valid data.
"""
form = self.SimpleForm()
widget = form.fields['simple_field'].widget
self.assertFalse(
widget.value_omitted_from_data(
data=self.simple_valid_data,
files=[],
name='simple_field',
)
)

# pylint:disable=invalid-name
def assertHTMLContains(self, text, content, count=None, msg=None):
"""
Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tox]
skipsdist = True
envlist =
py{35,36}-dj{1.11,2.0}
py{35,36}-dj{1.11,2.0,2.1}
pycodestyle,isort,pylint

[testenv]
Expand All @@ -15,6 +15,7 @@ deps =
-rtest_requirements.in
dj1.11: Django~=1.11.0
dj2.0: Django~=2.0.0
dj2.1: Django~=2.1.0

[testenv:flake8]
usedevelop = True
Expand Down