Add: proper function for importing post titles
This commit is contained in:
parent
82f630789a
commit
06b0df63f9
1 changed files with 68 additions and 7 deletions
|
@ -20,6 +20,9 @@ from apps.archive.models import (
|
||||||
CategoryModel,
|
CategoryModel,
|
||||||
CreatorModel,
|
CreatorModel,
|
||||||
PostModel,
|
PostModel,
|
||||||
|
TitleModel,
|
||||||
|
CreatorTitle,
|
||||||
|
PostTitle,
|
||||||
DescriptionModel,
|
DescriptionModel,
|
||||||
CreatorDescription,
|
CreatorDescription,
|
||||||
PostDescription,
|
PostDescription,
|
||||||
|
@ -150,6 +153,7 @@ class BaseImporter(ABC):
|
||||||
if not file_instance.thumbnail:
|
if not file_instance.thumbnail:
|
||||||
generate_video_thumbnail.delay(file_instance.id)
|
generate_video_thumbnail.delay(file_instance.id)
|
||||||
|
|
||||||
|
# Process PDF thumbnails
|
||||||
if file_instance.file_type in ["pdf"]:
|
if file_instance.file_type in ["pdf"]:
|
||||||
if not file_instance.thumbnail:
|
if not file_instance.thumbnail:
|
||||||
generate_pdf_thumbnail.delay(file_instance.id)
|
generate_pdf_thumbnail.delay(file_instance.id)
|
||||||
|
@ -174,6 +178,55 @@ class BaseImporter(ABC):
|
||||||
self.log_error(f"Error importing file {file_path}: {str(e)}")
|
self.log_error(f"Error importing file {file_path}: {str(e)}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def add_title(
|
||||||
|
self,
|
||||||
|
title_text: str,
|
||||||
|
date_str: str,
|
||||||
|
date_format: str,
|
||||||
|
owner_instance,
|
||||||
|
owner_type: str,
|
||||||
|
file_date,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
Add title to a post or creator.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
title_text: The title text to add
|
||||||
|
owner_instance: The post or creator instance
|
||||||
|
owner_type: Either 'post' or 'creator'
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
title_hash = compute_string_hash_blake3(title_text, logger=self.command)
|
||||||
|
|
||||||
|
title_instance, created = TitleModel.objects.get_or_create(hash=title_hash)
|
||||||
|
|
||||||
|
if created:
|
||||||
|
title_instance.content = title_text
|
||||||
|
title_instance.date_created = timezone.make_aware(
|
||||||
|
datetime.strptime(date_str, date_format)
|
||||||
|
)
|
||||||
|
title_instance.save()
|
||||||
|
|
||||||
|
if owner_type == "creator":
|
||||||
|
relation, created = CreatorTitle.objects.get_or_create(
|
||||||
|
creator=owner_instance, title=title_instance
|
||||||
|
)
|
||||||
|
else: # post
|
||||||
|
relation, created = PostTitle.objects.get_or_create(
|
||||||
|
post=owner_instance, title=title_instance
|
||||||
|
)
|
||||||
|
|
||||||
|
relation.date_imported = timezone.make_aware(
|
||||||
|
datetime.fromtimestamp(file_date)
|
||||||
|
)
|
||||||
|
relation.save()
|
||||||
|
|
||||||
|
if owner_type == "post":
|
||||||
|
owner_instance.title.add(title_instance)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
self.log_error(f"Error adding description: {str(e)}")
|
||||||
|
|
||||||
def add_description(
|
def add_description(
|
||||||
self,
|
self,
|
||||||
description_text: str,
|
description_text: str,
|
||||||
|
@ -516,15 +569,23 @@ class FurAffinityImporter(BaseImporter):
|
||||||
rating = data.get("rating", "").lower()
|
rating = data.get("rating", "").lower()
|
||||||
post_instance.mature = rating in ["mature", "adult"]
|
post_instance.mature = rating in ["mature", "adult"]
|
||||||
|
|
||||||
# Add description (title + description)
|
# Add title
|
||||||
title = data.get("title", "")
|
title_text = data.get("title", "")
|
||||||
description = data.get("description", "")
|
if title_text:
|
||||||
|
self.add_title(
|
||||||
|
title_text=title_text,
|
||||||
|
date_str=data["date"],
|
||||||
|
date_format="%Y-%m-%d %H:%M:%S",
|
||||||
|
owner_instance=post_instance,
|
||||||
|
owner_type="post",
|
||||||
|
file_date=os.path.getmtime(file_path_json),
|
||||||
|
)
|
||||||
|
|
||||||
full_description = f"{title}\n\n{description}" if title else description
|
# Add description
|
||||||
|
description_text = data.get("description", "")
|
||||||
if full_description:
|
if description_text:
|
||||||
self.add_description(
|
self.add_description(
|
||||||
description_text=full_description,
|
description_text=description_text,
|
||||||
date_str=data["date"],
|
date_str=data["date"],
|
||||||
date_format="%Y-%m-%d %H:%M:%S",
|
date_format="%Y-%m-%d %H:%M:%S",
|
||||||
owner_instance=post_instance,
|
owner_instance=post_instance,
|
||||||
|
|
Loading…
Add table
Reference in a new issue