Commit bbb15e13 authored by Tom van Bussel's avatar Tom van Bussel
Browse files

Ignore duplicate photos in albums using digests

Photo krijgt nu een extra veld, digest, waarin een SHA1 hash van de foto
staat. Deze digest wordt gebruikt om te zorgen dat er geen dubbele
foto's worden geüpload. Omdat het compressen pas de save() van de foto
gebeurd, wordt het foto object eerst opgeslagen en daarna weer
verwijderd als de foto dubbel is.
parent e3a14e0c
......@@ -67,6 +67,12 @@ def save_photo(request, archive_file, photo, album):
else:
photo_obj.save()
if Photo.objects.filter(album=album, _digest=photo_obj._digest)\
.exclude(pk=photo_obj.pk).exists():
messages.add_message(request, messages.WARNING,
"{} is duplicate.".format(photo_filename))
photo_obj.delete()
class AlbumAdmin(admin.ModelAdmin):
list_display = ('title', 'date', 'hidden', 'shareable')
......
# -*- coding: utf-8 -*-
# Generated by Django 1.10.4 on 2017-03-08 18:26
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('photos', '0007_auto_20170218_2142'),
]
operations = [
migrations.AddField(
model_name='photo',
name='_digest',
field=models.CharField(
default='',
blank=True,
max_length=40,
verbose_name='digest'),
preserve_default=False,
),
]
# -*- coding: utf-8 -*-
# Generated by Django 1.10.4 on 2017-03-08 19:03
from __future__ import unicode_literals
import hashlib
from django.db import migrations
def add_digests(apps, schema_editor):
Photo = apps.get_model('photos', 'Photo')
for photo in Photo.objects.all():
hash_sha1 = hashlib.sha1()
for chunk in iter(lambda: photo.file.read(4096), b""):
hash_sha1.update(chunk)
photo._digest = hash_sha1.hexdigest()
photo.save()
class Migration(migrations.Migration):
dependencies = [
('photos', '0008_photo__digest'),
]
operations = [
migrations.RunPython(add_digests),
]
# -*- coding: utf-8 -*-
# Generated by Django 1.10.4 on 2017-03-08 19:03
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('photos', '0009_generate_digests'),
]
operations = [
migrations.AlterField(
model_name='Photo',
name='_digest',
field=models.CharField(
max_length=40,
verbose_name='digest')),
]
......@@ -40,6 +40,11 @@ class Photo(models.Model):
default=False
)
_digest = models.CharField(
'digest',
max_length=40,
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.file:
......@@ -61,6 +66,11 @@ class Photo(models.Model):
image.save(image_path, "JPEG")
self._orig_file = self.file.path
hash_sha1 = hashlib.sha1()
for chunk in iter(lambda: self.file.read(4096), b""):
hash_sha1.update(chunk)
self._digest = hash_sha1.hexdigest()
class Meta:
ordering = ('file', )
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment