Skip to content

Commit

Permalink
Merge pull request #264 from Georiviere/fix_custom_contrib_null_values
Browse files Browse the repository at this point in the history
fix custom contribution none values
  • Loading branch information
submarcos authored Jul 1, 2024
2 parents fa71710 + 3de9981 commit 3d4deb8
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 24 deletions.
26 changes: 13 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ FROM ubuntu:focal as base
# stage with general requirements
ARG UID=1000

ENV DEBIAN_FRONTEND noninteractive
ENV PYTHONUNBUFFERED 1
ENV LANG C.UTF-8
ENV TZ UTC

ENV SERVER_NAME localhost
ENV CONVERSION_HOST convertit
ENV CAPTURE_HOST screamshotter
ENV POSTGRES_HOST postgres
ENV PGPORT 5432
ENV CUSTOM_SETTINGS_FILE /opt/georiviere-admin/var/conf/custom.py
ENV DEBIAN_FRONTEND=noninteractive
ENV PYTHONUNBUFFERED=1
ENV LANG=C.UTF-8
ENV TZ=UTC

ENV SERVER_NAME=localhost
ENV CONVERSION_HOST=convertit
ENV CAPTURE_HOST=screamshotter
ENV POSTGRES_HOST=postgres
ENV PGPORT=5432
ENV CUSTOM_SETTINGS_FILE=/opt/georiviere-admin/var/conf/custom.py

RUN mkdir -p /opt/georiviere-admin/var

Expand Down Expand Up @@ -76,7 +76,7 @@ RUN apt-get update -qq && apt-get install -y -qq \
USER django

RUN python3.9 -m venv /opt/venv
RUN /opt/venv/bin/pip install --no-cache-dir pip setuptools wheel -U
RUN /opt/venv/bin/pip install --no-cache-dir pip==24.0 setuptools wheel -U
# geotrek setup fix : it required django before being installed... TODO: fix it in geotrek setup.py
RUN /opt/venv/bin/pip install --no-cache-dir django==2.2.*

Expand All @@ -92,7 +92,7 @@ CMD ./manage.py runserver 0.0.0.0:8000

FROM base as prod
# stage with prod requirements only
ENV GUNICORN_CMD_ARGS "--workers 1 --timeout 3600 --bind 0.0.0.0:8000 --timeout 3600"
ENV GUNICORN_CMD_ARGS="--workers 1 --timeout 3600 --bind 0.0.0.0:8000 --timeout 3600"

USER root

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
deps:
docker compose run --rm web bash -c "pip-compile -q && pip-compile -q dev-requirements.in"
docker compose run --rm web bash -c "pip-compile --strip-extras -q && pip-compile -q dev-requirements.in --strip-extras"
2 changes: 1 addition & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This file is autogenerated by pip-compile with Python 3.9
# by the following command:
#
# pip-compile dev-requirements.in
# pip-compile --strip-extras dev-requirements.in
#
alabaster==0.7.16
# via sphinx
Expand Down
8 changes: 8 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
CHANGELOG
=========

1.4.1 (2024-07-01)
---------------------

**Bug fix**

- Fix null values send on non required fields in custom contribution API


1.4.0 (2024-06-17)
---------------------

Expand Down
23 changes: 21 additions & 2 deletions georiviere/portal/tests/test_views/test_contribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
CustomContributionTypeFactory,
CustomContributionTypeStringFieldFactory,
CustomContributionTypeBooleanFieldFactory,
CustomContributionFactory, )
CustomContributionFactory,
CustomContributionTypeFloatFieldFactory, )
from georiviere.main.models import Attachment
from georiviere.observations.tests.factories import StationFactory
from georiviere.portal.tests.factories import PortalFactory
Expand Down Expand Up @@ -334,6 +335,10 @@ def setUpTestData(cls):
custom_type=cls.custom_contribution_type,
label="Field boolean",
)
CustomContributionTypeFloatFieldFactory(
custom_type=cls.custom_contribution_type,
label="Field float",
)
cls.contribution_validated = CustomContributionFactory(custom_type=cls.custom_contribution_type,
station=cls.station, validated=True, portal=cls.portal)
cls.contribution_unvalidated = CustomContributionFactory(custom_type=cls.custom_contribution_type,
Expand Down Expand Up @@ -367,9 +372,23 @@ def test_create(self):
"field_boolean": True,
"contributed_at": "2020-01-01T00:00"
}
response = self.client.post(self.get_contribution_url(), data=data)
response = self.client.post(self.get_contribution_url(), data=data, format='json')
data = response.json()
self.assertEqual(response.status_code, 201, data)

def test_null_values_on_not_required(self):
"""Null values should be accepted on non required fields"""
data = {
"station": self.station.pk,
"field_string": "string",
"field_boolean": None,
"field_float": 1.1,
"contributed_at": "2020-01-01T00:00"
}
response = self.client.post(self.get_contribution_url(), data=data, format='json')
data = response.json()
self.assertEqual(response.status_code, 201, data)
self.assertEqual(data['field_float'], 1.1)

def test_validated_not_in_list(self):
response = self.client.get(self.get_contribution_url())
Expand Down
4 changes: 3 additions & 1 deletion georiviere/portal/views/contribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ def create_contribution(self, request, *args, **kwargs):
context = self.get_serializer_context()
custom_type = self.get_object()
context["custom_type"] = custom_type
serializer = self.get_serializer(data=request.data, context=context)
# as data come from form, we need to clean None values in creation mode
data = {k: v for k, v in request.data.items() if v is not None}
serializer = self.get_serializer(data=data, context=context)
if not serializer.is_valid():
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
extra_save_params = {}
Expand Down
15 changes: 9 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# This file is autogenerated by pip-compile with Python 3.9
# by the following command:
#
# pip-compile
# pip-compile --strip-extras
#
amqp==5.0.5
amqp==5.2.0
# via kombu
appy==1.0.9
# via django-appypod
Expand All @@ -16,7 +16,7 @@ attrs==20.3.0
# jsonschema
beautifulsoup4==4.10.0
# via mapentity
billiard==3.6.3.0
billiard==3.6.4.0
# via celery
btrees==4.9.2
# via zodb
Expand All @@ -27,7 +27,7 @@ cairocffi==1.3.0
# weasyprint
cairosvg==2.5.2
# via weasyprint
celery[redis]==5.0.5
celery==5.1.0
# via
# django-celery-results
# geotrek
Expand Down Expand Up @@ -107,7 +107,7 @@ django-appconf==1.0.4
# via django-compressor
django-appypod==2.0.7
# via mapentity
django-autocomplete-light[gfk]==3.8.2
django-autocomplete-light==3.8.2
# via -r requirements.in
django-celery-results==2.0.1
# via geotrek
Expand Down Expand Up @@ -220,7 +220,7 @@ jsonschema==4.17.3
# via
# -r requirements.in
# drf-spectacular
kombu==5.0.2
kombu==5.3.7
# via celery
landez==2.5.0
# via geotrek
Expand Down Expand Up @@ -349,6 +349,8 @@ tinycss2==1.1.1
# weasyprint
transaction==3.0.1
# via zodb
typing-extensions==4.12.2
# via kombu
uritemplate==3.0.1
# via
# coreapi
Expand All @@ -364,6 +366,7 @@ vine==5.0.0
# via
# amqp
# celery
# kombu
wcwidth==0.2.5
# via prompt-toolkit
weasyprint==52.5
Expand Down

0 comments on commit 3d4deb8

Please sign in to comment.