diff --git a/pepfar_mle/common/admin.py b/pepfar_mle/common/admin.py index 3d50f82a..5a002d90 100644 --- a/pepfar_mle/common/admin.py +++ b/pepfar_mle/common/admin.py @@ -4,6 +4,13 @@ class BaseAdmin(admin.ModelAdmin): + readonly_fields = ( + "created", + "created_by", + "updated", + "updated_by", + ) + def save_model(self, request, obj, form, change): if not change: obj.created_by = request.user.pk diff --git a/pepfar_mle/common/migrations/0005_auto_20210714_1633.py b/pepfar_mle/common/migrations/0005_auto_20210714_1633.py new file mode 100644 index 00000000..4b9c2cd5 --- /dev/null +++ b/pepfar_mle/common/migrations/0005_auto_20210714_1633.py @@ -0,0 +1,18 @@ +# Generated by Django 3.1.12 on 2021-07-14 13:33 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('common', '0004_auto_20210714_1339'), + ] + + operations = [ + migrations.AlterField( + model_name='facilityattachment', + name='size', + field=models.IntegerField(blank=True, help_text='The size of the attachment in bytes', null=True), + ), + ] diff --git a/pepfar_mle/common/models.py b/pepfar_mle/common/models.py index 11edb4fc..c146e8b0 100644 --- a/pepfar_mle/common/models.py +++ b/pepfar_mle/common/models.py @@ -318,7 +318,9 @@ class Attachment(AbstractBase): data = models.FileField(upload_to=get_directory, max_length=65535) title = models.CharField(max_length=255) creation_date = models.DateTimeField(default=timezone.now) - size = models.IntegerField(help_text="The size of the attachment in bytes") + size = models.IntegerField( + help_text="The size of the attachment in bytes", null=True, blank=True + ) description = models.TextField(null=True, blank=True) aspect_ratio = models.CharField(max_length=50, blank=True, null=True) @@ -328,7 +330,10 @@ def validate_image_size(self): """Ensure that the supplied image size matches the actual file.""" if not is_image_type(self.content_type): return + image = Image.open(self.data) + self.size = len(image.fp.read()) + width, height = image.size msg_template = ( "Your image has a {axis} of {actual_size} {extra_text} "