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

62 verify related fields #68

Merged
merged 2 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions edtf/fields.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pickle

from django.core import checks
from django.core.exceptions import FieldDoesNotExist
from django.db import models
from django.db.models import signals
Expand Down Expand Up @@ -188,3 +189,46 @@ def contribute_to_class(self, cls, name, **kwargs):
# Only run post-initialization values update on non-abstract models
if not cls._meta.abstract:
signals.post_init.connect(self.update_values, sender=cls)

def check(self, **kwargs):
errors = super().check(**kwargs)

for field_alias in [
"direct_input_field",
"lower_fuzzy_field",
"lower_strict_field",
"natural_text_field",
"upper_fuzzy_field",
"upper_strict_field",
]:
errors.extend(self._check_field(field_alias))

return errors

def _check_field(self, field_alias):
field_name = getattr(self, field_alias, None)

# Check if the alias value has been provided in the field definition
if not field_name:
return [
checks.Error(
f"You must specify a '{field_alias}' for EDTFField",
hint=None,
obj=self,
id="python-edtf.EDTF01",
)
]

# Check if the field that is referenced actually exists
try:
self.model._meta.get_field(field_name)
except FieldDoesNotExist:
return [
checks.Error(
f"'{self.name}' refers to a non-existent '{field_alias}' field: '{field_name}'",
hint=None,
obj=self,
id="python-edtf.EDTF02",
)
]
return []
31 changes: 31 additions & 0 deletions edtf_django_tests/edtf_integration/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,34 @@ def test_comparison(self):
self.event2.date_edtf,
"2019-11 is less than 2021-05-06",
)

def test_field_related_field_specification(self):
edtf_field_on_model = TestEvent._meta.get_field("date_edtf")
required_fields = (
"direct_input_field",
"lower_fuzzy_field",
"lower_strict_field",
"natural_text_field",
"upper_fuzzy_field",
"upper_strict_field",
)
for field_alias in required_fields:
# Remove the alias from the edtf_field
orig_value = getattr(edtf_field_on_model, field_alias)
setattr(edtf_field_on_model, field_alias, None)
errors = edtf_field_on_model.check()
self.assertEqual(len(errors), 1)
self.assertTrue(field_alias in errors[0].msg)
# Should be an 'alias not specified' error
self.assertEqual(errors[0].id, "python-edtf.EDTF01")

# Point the alias to a non-existent field
setattr(edtf_field_on_model, field_alias, "fake")
errors = edtf_field_on_model.check()
self.assertEqual(len(errors), 1)
self.assertTrue(field_alias in errors[0].msg)
# Should be a 'non-eixstent field' error
self.assertEqual(errors[0].id, "python-edtf.EDTF02")

# Repair the field so later tests can still work
setattr(edtf_field_on_model, field_alias, orig_value)
Loading