mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 13:11:08 -05:00
series of tests added with built-in django test support
This commit is contained in:
@@ -5,6 +5,9 @@ from django.contrib.contenttypes.models import ContentType
|
||||
from django.utils.text import slugify
|
||||
from django.conf import settings
|
||||
import os
|
||||
from PIL import Image, ExifTags
|
||||
from PIL.ExifTags import TAGS
|
||||
from datetime import datetime
|
||||
from .storage import MediaStorage
|
||||
from rides.models import Ride
|
||||
from django.utils import timezone
|
||||
@@ -56,6 +59,7 @@ class Photo(models.Model):
|
||||
is_approved = models.BooleanField(default=False) # New field for approval status
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
date_taken = models.DateTimeField(null=True, blank=True)
|
||||
uploaded_by = models.ForeignKey(
|
||||
settings.AUTH_USER_MODEL,
|
||||
on_delete=models.SET_NULL,
|
||||
@@ -76,8 +80,29 @@ class Photo(models.Model):
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.content_type} - {self.content_object} - {self.caption or 'No caption'}"
|
||||
|
||||
def extract_exif_date(self) -> Optional[datetime]:
|
||||
"""Extract the date taken from image EXIF data"""
|
||||
try:
|
||||
with Image.open(self.image) as img:
|
||||
exif = img.getexif()
|
||||
if exif:
|
||||
# Find the DateTime tag ID
|
||||
for tag_id in ExifTags.TAGS:
|
||||
if ExifTags.TAGS[tag_id] == 'DateTimeOriginal':
|
||||
if tag_id in exif:
|
||||
# EXIF dates are typically in format: '2024:02:15 14:30:00'
|
||||
date_str = exif[tag_id]
|
||||
return datetime.strptime(date_str, '%Y:%m:%d %H:%M:%S')
|
||||
return None
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def save(self, *args: Any, **kwargs: Any) -> None:
|
||||
# Extract EXIF date if this is a new photo
|
||||
if not self.pk and not self.date_taken:
|
||||
self.date_taken = self.extract_exif_date()
|
||||
|
||||
# Set default caption if not provided
|
||||
if not self.caption and self.uploaded_by:
|
||||
current_time = timezone.now()
|
||||
|
||||
Reference in New Issue
Block a user