Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Automatic reading of image dates.

MadoctheHadoc3590c5851f94
diff --git a/wger/gallery/forms.py b/wger/gallery/forms.py
index 05e547b68..c4b4d42ff 100644
--- a/wger/gallery/forms.py
+++ b/wger/gallery/forms.py
@@ -10,9 +10,12 @@ from wger.gallery.models import Image
from wger.utils.constants import DATE_FORMATS
from wger.utils.widgets import Html5DateInput
+# Python Imaging Library to extract data
+from PIL import Image as PilImage
+from datetime import datetime
class ImageForm(ModelForm):
- date = DateField(input_formats=DATE_FORMATS, widget=Html5DateInput())
+ date = DateField(input_formats=DATE_FORMATS, widget=Html5DateInput(), required=False)
class Meta:
model = Image
@@ -20,3 +23,29 @@ class ImageForm(ModelForm):
widgets = {
'user': widgets.HiddenInput(),
}
+
+ def save(self, commit=True):
+ instance = super().save(commit=False)
+
+ # Tries to extract date from EXIF metadata
+ if 'image' in self.changed_data:
+ exif_date = self._get_exif_date(self.cleaned_data['image'])
+ if exif_date:
+ instance.date = exif_date
+
+ if commit:
+ instance.save()
+ return instance
+
+ def _get_exif_date(self, image_file):
+ try:
+ img = PilImage.open(image_file)
+ exif_data = img._getexif()
+ if exif_data:
+ # EXIF constant index for date and datetime format
+ date_str = exif_data.get(36867)
+ if date_str:
+ return datetime.strptime(date_str, '%Y:%m:%d %H:%M:%S').date()
+ except Exception as e:
+ pass
+ return None