Skip to content

Commit

Permalink
apps/bplan: remove char limit on name and description and instead tru…
Browse files Browse the repository at this point in the history
…ncate the input if too long
  • Loading branch information
goapunk authored and m4ra committed Jan 6, 2025
1 parent 6081050 commit b87bc83
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 3 deletions.
2 changes: 2 additions & 0 deletions changelog/8543.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@
- update the bplan api documentation
- add a `bplan_id` field which will replace the `identifier` field once the
switch to diplan is done
- remove char limit for name and description field, instead truncate text if
too long
4 changes: 2 additions & 2 deletions docs/bplan_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ The following fields need to be provided:

- *name*: string
- Name of the BPLAN (e.g. used as the title of the project tile)
- Maximum length of 120 chars
- Maximum length of 120 chars, however longer input is accepted and will be cut to 120
- *(imperia only) identifier*: string
- Identifier that clearly identifies the BPLAN, needs to be the same as in the FIS Broker (e.g. `VIII - 329`)
- Maximum length of 120 chars
Expand All @@ -85,7 +85,7 @@ The following fields need to be provided:
- Maximum length of 120 chars
- *description*: string
- Description of the BPLAN shown in the project tile
- Maximum length of 250 chars
- Maximum length of 250 chars, however longer input is accepted and will be cut to 250
- *url*: string
- URL of the external site the BPLAN is embedded on
- *office_worker_email*: string
Expand Down
31 changes: 30 additions & 1 deletion meinberlin/apps/bplan/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,20 @@
'max-height: 100vh" src="{}" frameborder="0"></iframe>'
)
DOWNLOAD_IMAGE_SIZE_LIMIT_BYTES = 10 * 1024 * 1024
NAME_MAX_LENGTH = project_models.Project._meta.get_field("name").max_length
DESCRIPTION_MAX_LENGTH = project_models.Project._meta.get_field(
"description"
).max_length


class BplanSerializer(serializers.ModelSerializer):
id = serializers.IntegerField(required=False)
name = serializers.CharField(
write_only=True,
)
description = serializers.CharField(
write_only=True,
)

# make write_only for consistency reasons
start_date = serializers.DateTimeField(write_only=True)
Expand All @@ -47,7 +57,7 @@ class BplanSerializer(serializers.ModelSerializer):
required=False,
write_only=True,
)
# can't use a4 field as must be serilizer field
# can't use a4 field as must be serializer field
image_alt_text = serializers.CharField(
required=False,
write_only=True,
Expand Down Expand Up @@ -121,6 +131,14 @@ def create(self, validated_data):
orga = orga_model.objects.get(pk=orga_pk)
validated_data["organisation"] = orga

# Ellipsizing/char limit is handled by us, diplan always sends full text
if len(validated_data["name"]) > NAME_MAX_LENGTH:
validated_data["name"] = validated_data["name"][:NAME_MAX_LENGTH]
if len(validated_data["description"]) > DESCRIPTION_MAX_LENGTH:
validated_data["description"] = validated_data["description"][
:DESCRIPTION_MAX_LENGTH
]

# mark as diplan, will make removal of old bplans easier
# TODO: remove this check and the is_diplan field once transition to diplan is completed
if "bplan_id" in validated_data or "point" in validated_data:
Expand Down Expand Up @@ -175,6 +193,17 @@ def update(self, instance, validated_data):
if end_date and end_date > timezone.localtime(timezone.now()):
instance.is_archived = False

# Ellipsizing/char limit is handled by us, diplan always sends full text
if "name" in validated_data and len(validated_data["name"]) > NAME_MAX_LENGTH:
validated_data["name"] = validated_data["name"][:NAME_MAX_LENGTH]
if (
"description" in validated_data
and len(validated_data["description"]) > DESCRIPTION_MAX_LENGTH
):
validated_data["description"] = validated_data["description"][
:DESCRIPTION_MAX_LENGTH
]

# mark as diplan, will make removal of old bplans easier
# TODO: remove this check and the is_diplan field once transition to diplan is completed
if "bplan_id" in validated_data or "point" in validated_data:
Expand Down
48 changes: 48 additions & 0 deletions tests/bplan/test_bplan_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,3 +618,51 @@ def test_bplan_api_rejects_invalid_base64_image(
with django_capture_on_commit_callbacks(execute=True):
response = apiclient.post(url, data, format="json")
assert response.status_code == status.HTTP_400_BAD_REQUEST


@pytest.mark.django_db
def test_bplan_api_accepts_long_name_and_truncates_it(
apiclient, districts, organisation
):
url = reverse("bplan-list", kwargs={"organisation_pk": organisation.pk})
data = {
"name": "bplan-" + "a" * 120,
"description": "desc",
"url": "https://bplan.net",
"start_date": "2013-01-01 18:00",
"identifier": "1-234",
"point": "[0,0]",
"end_date": "2021-01-01 18:00",
}
user = organisation.initiators.first()
apiclient.force_authenticate(user=user)
response = apiclient.post(url, data, format="json")
assert response.status_code == status.HTTP_201_CREATED
bplan = bplan_models.Bplan.objects.first()
assert bplan.is_diplan is True
assert len(bplan.name) == 120
assert bplan.name == "bplan-" + "a" * 114


@pytest.mark.django_db
def test_bplan_api_accepts_long_description_and_truncates_it(
apiclient, districts, organisation
):
url = reverse("bplan-list", kwargs={"organisation_pk": organisation.pk})
data = {
"name": "bplan-long-desc",
"description": "desc" + "a" * 250,
"url": "https://bplan.net",
"start_date": "2013-01-01 18:00",
"identifier": "1-234",
"point": "[0,0]",
"end_date": "2021-01-01 18:00",
}
user = organisation.initiators.first()
apiclient.force_authenticate(user=user)
response = apiclient.post(url, data, format="json")
assert response.status_code == status.HTTP_201_CREATED
bplan = bplan_models.Bplan.objects.first()
assert bplan.is_diplan is True
assert len(bplan.description) == 250
assert bplan.description == "desc" + "a" * 246

0 comments on commit b87bc83

Please sign in to comment.