mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-08-04 20:55:19 +02:00
feat: add unique constraint for immich_id per user in AdventureImage model and enhance Immich integration image retrieval
This commit is contained in:
parent
06787bccf6
commit
0838a41156
4 changed files with 133 additions and 45 deletions
|
@ -0,0 +1,19 @@
|
|||
# Generated by Django 5.2.1 on 2025-06-02 02:31
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('adventures', '0032_remove_adventureimage_image_xor_immich_id'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddConstraint(
|
||||
model_name='adventureimage',
|
||||
constraint=models.UniqueConstraint(fields=('immich_id', 'user_id'), name='unique_immich_id_per_user'),
|
||||
),
|
||||
]
|
|
@ -0,0 +1,17 @@
|
|||
# Generated by Django 5.2.1 on 2025-06-02 02:44
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('adventures', '0033_adventureimage_unique_immich_id_per_user'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveConstraint(
|
||||
model_name='adventureimage',
|
||||
name='unique_immich_id_per_user',
|
||||
),
|
||||
]
|
|
@ -11,7 +11,7 @@ from django.contrib.postgres.fields import ArrayField
|
|||
from django.forms import ValidationError
|
||||
from django_resized import ResizedImageField
|
||||
from worldtravel.models import City, Country, Region, VisitedCity, VisitedRegion
|
||||
from adventures.geocoding import reverse_geocode
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.utils import timezone
|
||||
|
||||
def background_geocode_and_assign(adventure_id: str):
|
||||
|
@ -799,19 +799,25 @@ class AdventureImage(models.Model):
|
|||
is_primary = models.BooleanField(default=False)
|
||||
|
||||
def clean(self):
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
# Normalize empty values to None
|
||||
|
||||
# One of image or immich_id must be set, but not both
|
||||
has_image = bool(self.image and str(self.image).strip())
|
||||
has_immich_id = bool(self.immich_id and str(self.immich_id).strip())
|
||||
|
||||
# Exactly one must be provided
|
||||
|
||||
if has_image and has_immich_id:
|
||||
raise ValidationError("Cannot have both image file and Immich ID. Please provide only one.")
|
||||
|
||||
if not has_image and not has_immich_id:
|
||||
raise ValidationError("Must provide either an image file or an Immich ID.")
|
||||
|
||||
# Enforce: immich_id may only be used by a single user
|
||||
if has_immich_id:
|
||||
# Check if this immich_id is already used by a *different* user
|
||||
from adventures.models import AdventureImage
|
||||
conflict = AdventureImage.objects.filter(immich_id=self.immich_id).exclude(user_id=self.user_id)
|
||||
|
||||
if conflict.exists():
|
||||
raise ValidationError("This Immich ID is already used by another user.")
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
# Clean empty strings to None for proper database storage
|
||||
if not self.image:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue