Parcourir la source

Cache image width and height in the database

Baptiste Jonglez il y a 10 ans
Parent
commit
7c2c31f544
3 fichiers modifiés avec 42 ajouts et 4 suppressions
  1. 3 1
      panorama/admin.py
  2. 32 0
      panorama/migrations/0004_auto_20150310_1906.py
  3. 7 3
      panorama/models.py

+ 3 - 1
panorama/admin.py

@@ -17,7 +17,9 @@ class PanoramaAdmin(admin.ModelAdmin):
     model = Panorama
     inlines = (ReferenceInline, )
     list_display = ('name', 'latitude', 'longitude', 'altitude', 'loop')
-    fields = ('name', 'image', 'loop', ('latitude', 'longitude'), 'altitude')
+    fields = ('name', ('image', 'image_width', 'image_height'),
+              'loop', ('latitude', 'longitude'), 'altitude')
+    readonly_fields = ('image_width', 'image_height')
 
 
 @admin.register(ReferencePoint)

+ 32 - 0
panorama/migrations/0004_auto_20150310_1906.py

@@ -0,0 +1,32 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('panorama', '0003_auto_20150310_1853'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='panorama',
+            name='image_height',
+            field=models.PositiveIntegerField(default=0),
+            preserve_default=True,
+        ),
+        migrations.AddField(
+            model_name='panorama',
+            name='image_width',
+            field=models.PositiveIntegerField(default=0),
+            preserve_default=True,
+        ),
+        migrations.AlterField(
+            model_name='panorama',
+            name='image',
+            field=models.ImageField(height_field='image_height', upload_to='pano', width_field='image_width', verbose_name='image'),
+            preserve_default=True,
+        ),
+    ]

+ 7 - 3
panorama/models.py

@@ -106,7 +106,11 @@ class ReferencePoint(Point):
 class Panorama(ReferencePoint):
     loop = models.BooleanField(default=False, verbose_name="360° panorama",
                                help_text="Whether the panorama loops around the edges")
-    image = models.ImageField(verbose_name="image", upload_to="pano")
+    image = models.ImageField(verbose_name="image", upload_to="pano",
+                              width_field="image_width",
+                              height_field="image_height")
+    image_width = models.PositiveIntegerField(default=0)
+    image_height = models.PositiveIntegerField(default=0)
     # Set of references, i.e. reference points with information on how
     # they relate to this panorama.
     references = models.ManyToManyField(ReferencePoint, through='Reference',
@@ -160,8 +164,8 @@ class Reference(models.Model):
         if self.panorama.pk == self.reference_point.pk:
             raise ValidationError("A panorama can't reference itself.")
         # Check than the position is within the bounds of the image.
-        w = self.panorama.image.width
-        h = self.panorama.image.height
+        w = self.panorama.image_width
+        h = self.panorama.image_height
         if self.x >= w or self.y >= h:
             raise ValidationError("Position ({x}, {y}) is outside the bounds "
                                   "of the image ({width}, {height}).".format(