|
1 | 1 | from urllib.parse import urlparse
|
2 |
| - |
| 2 | +from typing import cast |
3 | 3 | from django.contrib.contenttypes.fields import GenericForeignKey
|
4 | 4 | from django.contrib.contenttypes.models import ContentType
|
5 | 5 | from django.db import models
|
@@ -29,42 +29,42 @@ class FieldType(models.TextChoices):
|
29 | 29 |
|
30 | 30 | class MetadataField(AbstractBaseExportableModel):
|
31 | 31 | """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( |
35 | 35 | max_length=255, choices=FieldType.choices, default=FieldType.STRING
|
36 | 36 | )
|
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) |
39 | 39 |
|
40 | 40 | def is_field_value_valid(self, field_value: str) -> bool:
|
41 | 41 | if len(field_value) > FIELD_VALUE_MAX_LENGTH:
|
42 | 42 | 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)) |
44 | 44 |
|
45 |
| - def validate_int(self, field_value: str): # type: ignore[no-untyped-def] |
| 45 | + def validate_int(self, field_value: str) -> bool: |
46 | 46 | try:
|
47 | 47 | int(field_value)
|
48 | 48 | except ValueError:
|
49 | 49 | return False
|
50 | 50 | return True
|
51 | 51 |
|
52 |
| - def validate_bool(self, field_value: str): # type: ignore[no-untyped-def] |
| 52 | + def validate_bool(self, field_value: str) -> bool: |
53 | 53 | if field_value.lower() in ["true", "false"]:
|
54 | 54 | return True
|
55 | 55 | return False
|
56 | 56 |
|
57 |
| - def validate_url(self, field_value: str): # type: ignore[no-untyped-def] |
| 57 | + def validate_url(self, field_value: str) -> bool: |
58 | 58 | try:
|
59 | 59 | result = urlparse(field_value)
|
60 | 60 | return all([result.scheme, result.netloc])
|
61 | 61 | except ValueError:
|
62 | 62 | return False
|
63 | 63 |
|
64 |
| - def validate_str(self, field_value: str): # type: ignore[no-untyped-def] |
| 64 | + def validate_str(self, field_value: str) -> bool: |
65 | 65 | return True
|
66 | 66 |
|
67 |
| - def validate_multiline_str(self, field_value: str): # type: ignore[no-untyped-def] |
| 67 | + def validate_multiline_str(self, field_value: str) -> bool: |
68 | 68 | return True
|
69 | 69 |
|
70 | 70 | class Meta:
|
@@ -116,9 +116,9 @@ class Meta:
|
116 | 116 | unique_together = ("model_field", "content_type", "object_id")
|
117 | 117 |
|
118 | 118 | 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( |
120 | 120 | model_field=self.model_field,
|
121 | 121 | content_type=content_type,
|
122 | 122 | object_id=cloned_entity.pk,
|
123 | 123 | field_value=self.field_value,
|
124 |
| - ) |
| 124 | + )) |
0 commit comments