Skip to content

Commit

Permalink
Merge pull request #163 from KhaledBousrih/feature-set-default-max-le…
Browse files Browse the repository at this point in the history
…ngth-for-url-fields

Set a default max length to 1024 for URLFields
  • Loading branch information
lcognat committed Jun 19, 2023
2 parents b80d030 + 1d5f040 commit 24b9bfa
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

### Changed

- nothing changed
- Set a default max length to 1024 for URLFields

### Removed

Expand Down
2 changes: 2 additions & 0 deletions concrete_datastore/concrete/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,8 @@ class Meta:
args['null'] = False
args.setdefault('blank', True)
args.setdefault('default', "")
elif field.f_type == 'URLField':
args.setdefault('max_length', settings.URL_FIELD_MAX_LENGTH)
elif field.f_type in ('IntegerField', 'BigIntegerField'):
args['null'] = False
args.setdefault('blank', True)
Expand Down
2 changes: 2 additions & 0 deletions concrete_datastore/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,3 +656,5 @@
#: The TOTP issuer used for the QR-Code. Leave to None to use the same value
#: as PLATFORM_NAME
OTP_TOTP_ISSUER = None

URL_FIELD_MAX_LENGTH = 1024
5 changes: 5 additions & 0 deletions tests/datamodel/unittest-datamodel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ manifest:
datatype: file
attributes: {}
description: Photo of the project
- name: storage_url
datatype: url
attributes:
allow_empty: true
description: Url of the project storgae
uid: 7158d6af-8c79-4613-82cb-c1b0b20400ab
- name: Category
description: Category
Expand Down
18 changes: 18 additions & 0 deletions tests/migrations/0013_project_storage_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.19 on 2023-06-19 13:53

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('concrete', '0012_auto_20221115_0644'),
]

operations = [
migrations.AddField(
model_name='project',
name='storage_url',
field=models.URLField(blank=True, max_length=1024, null=True),
),
]
23 changes: 23 additions & 0 deletions tests/test_fields_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.test import TestCase
from concrete_datastore.concrete.models import Project
from django.db import DataError


class UrlValidationTestCase(TestCase):
#: URL length validator is 1024
def test_nominal_case(self):
url = "http://domain.ext/" + "a/" * 491
self.assertEqual(len(url), 1000)
# 1000 < 1024
self.assertEqual(Project.objects.count(), 0)
project = Project.objects.create(storage_url=url)
self.assertEqual(Project.objects.count(), 1)
self.assertEqual(len(project.storage_url), 1000)

def test_validation_error(self):
url = "http://domain.ext/" + "a/" * 991
self.assertEqual(len(url), 2000)
# 2000 > 1024
self.assertEqual(Project.objects.count(), 0)
with self.assertRaises(DataError):
Project.objects.create(storage_url=url)

0 comments on commit 24b9bfa

Please sign in to comment.