Skip to content

Commit cbb1809

Browse files
committed
feat: added-types
1 parent 517a499 commit cbb1809

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

api/metadata/models.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from urllib.parse import urlparse
2-
2+
from typing import cast
33
from django.contrib.contenttypes.fields import GenericForeignKey
44
from django.contrib.contenttypes.models import ContentType
55
from django.db import models
@@ -29,42 +29,42 @@ class FieldType(models.TextChoices):
2929

3030
class MetadataField(AbstractBaseExportableModel):
3131
"""This model represents a metadata field(specific to an organisation) that can be attached to any model"""
32-
33-
name = models.CharField(max_length=255)
34-
type = models.CharField(
32+
id: models.AutoField[int, int]
33+
name: models.CharField[str, str] = models.CharField(max_length=255)
34+
type: models.CharField[str, str] = models.CharField(
3535
max_length=255, choices=FieldType.choices, default=FieldType.STRING
3636
)
37-
description = models.TextField(blank=True, null=True)
38-
organisation = models.ForeignKey(Organisation, on_delete=models.CASCADE)
37+
description: models.TextField[str | None, str | None] = models.TextField(blank=True, null=True)
38+
organisation: models.ForeignKey[Organisation, Organisation] = models.ForeignKey(Organisation, on_delete=models.CASCADE)
3939

4040
def is_field_value_valid(self, field_value: str) -> bool:
4141
if len(field_value) > FIELD_VALUE_MAX_LENGTH:
4242
return False
43-
return self.__getattribute__(f"validate_{self.type}")(field_value) # type: ignore[no-any-return]
43+
return cast(bool, self.__getattribute__(f"validate_{self.type}")(field_value))
4444

45-
def validate_int(self, field_value: str): # type: ignore[no-untyped-def]
45+
def validate_int(self, field_value: str) -> bool:
4646
try:
4747
int(field_value)
4848
except ValueError:
4949
return False
5050
return True
5151

52-
def validate_bool(self, field_value: str): # type: ignore[no-untyped-def]
52+
def validate_bool(self, field_value: str) -> bool:
5353
if field_value.lower() in ["true", "false"]:
5454
return True
5555
return False
5656

57-
def validate_url(self, field_value: str): # type: ignore[no-untyped-def]
57+
def validate_url(self, field_value: str) -> bool:
5858
try:
5959
result = urlparse(field_value)
6060
return all([result.scheme, result.netloc])
6161
except ValueError:
6262
return False
6363

64-
def validate_str(self, field_value: str): # type: ignore[no-untyped-def]
64+
def validate_str(self, field_value: str) -> bool:
6565
return True
6666

67-
def validate_multiline_str(self, field_value: str): # type: ignore[no-untyped-def]
67+
def validate_multiline_str(self, field_value: str) -> bool:
6868
return True
6969

7070
class Meta:
@@ -116,9 +116,9 @@ class Meta:
116116
unique_together = ("model_field", "content_type", "object_id")
117117

118118
def deep_clone_for_new_entity(self, cloned_entity: models.Model, content_type: ContentType) -> "Metadata":
119-
return Metadata.objects.create(
119+
return cast(Metadata, Metadata.objects.create(
120120
model_field=self.model_field,
121121
content_type=content_type,
122122
object_id=cloned_entity.pk,
123123
field_value=self.field_value,
124-
)
124+
))

0 commit comments

Comments
 (0)