taxonomy fix
This commit is contained in:
@@ -242,24 +242,24 @@ class GenerateContentFunction(BaseAIFunction):
|
|||||||
tag_name = tag_name.strip()
|
tag_name = tag_name.strip()
|
||||||
if tag_name:
|
if tag_name:
|
||||||
try:
|
try:
|
||||||
# Get or create tag taxonomy term
|
tag_slug = slugify(tag_name)
|
||||||
|
# Get or create tag taxonomy term using site + slug + type for uniqueness
|
||||||
tag_obj, created = ContentTaxonomy.objects.get_or_create(
|
tag_obj, created = ContentTaxonomy.objects.get_or_create(
|
||||||
site=task.site,
|
site=task.site,
|
||||||
name=tag_name,
|
slug=tag_slug,
|
||||||
taxonomy_type='tag',
|
taxonomy_type='tag',
|
||||||
defaults={
|
defaults={
|
||||||
'slug': slugify(tag_name),
|
'name': tag_name,
|
||||||
'sector': task.sector,
|
'sector': task.sector,
|
||||||
'account': task.account,
|
'account': task.account,
|
||||||
'description': '', # Required by database
|
'description': '',
|
||||||
'external_taxonomy': '', # Required by database
|
'external_taxonomy': '',
|
||||||
'sync_status': '', # Required by database
|
'count': 0,
|
||||||
'count': 0, # Required by database
|
'metadata': {},
|
||||||
'metadata': {}, # Required by database
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
content_record.taxonomy_terms.add(tag_obj)
|
content_record.taxonomy_terms.add(tag_obj)
|
||||||
logger.info(f"{'Created' if created else 'Found'} and linked tag: {tag_name} (ID: {tag_obj.id})")
|
logger.info(f"{'Created' if created else 'Found'} and linked tag: {tag_name} (ID: {tag_obj.id}, Slug: {tag_slug})")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to add tag '{tag_name}': {e}", exc_info=True)
|
logger.error(f"Failed to add tag '{tag_name}': {e}", exc_info=True)
|
||||||
|
|
||||||
@@ -271,24 +271,24 @@ class GenerateContentFunction(BaseAIFunction):
|
|||||||
category_name = category_name.strip()
|
category_name = category_name.strip()
|
||||||
if category_name:
|
if category_name:
|
||||||
try:
|
try:
|
||||||
# Get or create category taxonomy term
|
category_slug = slugify(category_name)
|
||||||
|
# Get or create category taxonomy term using site + slug + type for uniqueness
|
||||||
category_obj, created = ContentTaxonomy.objects.get_or_create(
|
category_obj, created = ContentTaxonomy.objects.get_or_create(
|
||||||
site=task.site,
|
site=task.site,
|
||||||
name=category_name,
|
slug=category_slug,
|
||||||
taxonomy_type='category',
|
taxonomy_type='category',
|
||||||
defaults={
|
defaults={
|
||||||
'slug': slugify(category_name),
|
'name': category_name,
|
||||||
'sector': task.sector,
|
'sector': task.sector,
|
||||||
'account': task.account,
|
'account': task.account,
|
||||||
'description': '', # Required by database
|
'description': '',
|
||||||
'external_taxonomy': '', # Required by database
|
'external_taxonomy': '',
|
||||||
'sync_status': '', # Required by database
|
'count': 0,
|
||||||
'count': 0, # Required by database
|
'metadata': {},
|
||||||
'metadata': {}, # Required by database
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
content_record.taxonomy_terms.add(category_obj)
|
content_record.taxonomy_terms.add(category_obj)
|
||||||
logger.info(f"{'Created' if created else 'Found'} and linked category: {category_name} (ID: {category_obj.id})")
|
logger.info(f"{'Created' if created else 'Found'} and linked category: {category_name} (ID: {category_obj.id}, Slug: {category_slug})")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to add category '{category_name}': {e}", exc_info=True)
|
logger.error(f"Failed to add category '{category_name}': {e}", exc_info=True)
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,16 @@
|
|||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from igny8_core.admin.base import SiteSectorAdminMixin
|
from igny8_core.admin.base import SiteSectorAdminMixin
|
||||||
from .models import Tasks, Images, Content
|
from .models import Tasks, Images, Content
|
||||||
from igny8_core.business.content.models import ContentTaxonomy, ContentAttribute
|
from igny8_core.business.content.models import ContentTaxonomy, ContentAttribute, ContentTaxonomyRelation
|
||||||
|
|
||||||
|
|
||||||
|
class ContentTaxonomyInline(admin.TabularInline):
|
||||||
|
"""Inline admin for managing content taxonomy relationships"""
|
||||||
|
model = ContentTaxonomyRelation
|
||||||
|
extra = 1
|
||||||
|
autocomplete_fields = ['taxonomy']
|
||||||
|
verbose_name = 'Taxonomy Term'
|
||||||
|
verbose_name_plural = 'Taxonomy Terms (Tags & Categories)'
|
||||||
|
|
||||||
|
|
||||||
@admin.register(Tasks)
|
@admin.register(Tasks)
|
||||||
@@ -90,8 +99,8 @@ class ContentAdmin(SiteSectorAdminMixin, admin.ModelAdmin):
|
|||||||
list_filter = ['content_type', 'content_structure', 'source', 'status', 'site', 'sector', 'created_at']
|
list_filter = ['content_type', 'content_structure', 'source', 'status', 'site', 'sector', 'created_at']
|
||||||
search_fields = ['title', 'content_html', 'external_url']
|
search_fields = ['title', 'content_html', 'external_url']
|
||||||
ordering = ['-created_at']
|
ordering = ['-created_at']
|
||||||
readonly_fields = ['created_at', 'updated_at', 'word_count']
|
readonly_fields = ['created_at', 'updated_at', 'word_count', 'get_tags_display', 'get_categories_display']
|
||||||
# Note: taxonomy_terms removed from filter_horizontal because it uses a through model
|
inlines = [ContentTaxonomyInline]
|
||||||
|
|
||||||
fieldsets = (
|
fieldsets = (
|
||||||
('Basic Info', {
|
('Basic Info', {
|
||||||
@@ -100,8 +109,11 @@ class ContentAdmin(SiteSectorAdminMixin, admin.ModelAdmin):
|
|||||||
('Content Classification', {
|
('Content Classification', {
|
||||||
'fields': ('content_type', 'content_structure', 'source')
|
'fields': ('content_type', 'content_structure', 'source')
|
||||||
}),
|
}),
|
||||||
# Note: taxonomy_terms field removed from fieldsets because it uses ContentTaxonomyAssociation through model
|
('Taxonomies (Read-only - manage below)', {
|
||||||
# Taxonomy associations can be managed via the inline admin or separately
|
'fields': ('get_tags_display', 'get_categories_display'),
|
||||||
|
'classes': ('collapse',),
|
||||||
|
'description': 'View tags and categories. To add/remove, use the Taxonomy Terms section below.'
|
||||||
|
}),
|
||||||
('Content', {
|
('Content', {
|
||||||
'fields': ('content_html', 'word_count')
|
'fields': ('content_html', 'word_count')
|
||||||
}),
|
}),
|
||||||
@@ -124,6 +136,22 @@ class ContentAdmin(SiteSectorAdminMixin, admin.ModelAdmin):
|
|||||||
return obj.taxonomy_terms.count()
|
return obj.taxonomy_terms.count()
|
||||||
get_taxonomy_count.short_description = 'Taxonomy Count'
|
get_taxonomy_count.short_description = 'Taxonomy Count'
|
||||||
|
|
||||||
|
def get_tags_display(self, obj):
|
||||||
|
"""Display tags"""
|
||||||
|
tags = obj.taxonomy_terms.filter(taxonomy_type='tag')
|
||||||
|
if tags.exists():
|
||||||
|
return ', '.join([tag.name for tag in tags])
|
||||||
|
return 'No tags'
|
||||||
|
get_tags_display.short_description = 'Tags'
|
||||||
|
|
||||||
|
def get_categories_display(self, obj):
|
||||||
|
"""Display categories"""
|
||||||
|
categories = obj.taxonomy_terms.filter(taxonomy_type='category')
|
||||||
|
if categories.exists():
|
||||||
|
return ', '.join([cat.name for cat in categories])
|
||||||
|
return 'No categories'
|
||||||
|
get_categories_display.short_description = 'Categories'
|
||||||
|
|
||||||
def get_site_display(self, obj):
|
def get_site_display(self, obj):
|
||||||
"""Safely get site name"""
|
"""Safely get site name"""
|
||||||
try:
|
try:
|
||||||
@@ -142,17 +170,27 @@ class ContentAdmin(SiteSectorAdminMixin, admin.ModelAdmin):
|
|||||||
|
|
||||||
@admin.register(ContentTaxonomy)
|
@admin.register(ContentTaxonomy)
|
||||||
class ContentTaxonomyAdmin(SiteSectorAdminMixin, admin.ModelAdmin):
|
class ContentTaxonomyAdmin(SiteSectorAdminMixin, admin.ModelAdmin):
|
||||||
list_display = ['name', 'taxonomy_type', 'slug', 'external_id', 'external_taxonomy', 'site', 'sector']
|
list_display = ['name', 'taxonomy_type', 'slug', 'count', 'external_id', 'external_taxonomy', 'site', 'sector']
|
||||||
list_filter = ['taxonomy_type', 'site', 'sector']
|
list_filter = ['taxonomy_type', 'site', 'sector']
|
||||||
search_fields = ['name', 'slug', 'external_taxonomy']
|
search_fields = ['name', 'slug', 'external_taxonomy']
|
||||||
ordering = ['taxonomy_type', 'name']
|
ordering = ['taxonomy_type', 'name']
|
||||||
|
readonly_fields = ['count', 'created_at', 'updated_at']
|
||||||
|
|
||||||
fieldsets = (
|
fieldsets = (
|
||||||
('Basic Info', {
|
('Basic Info', {
|
||||||
'fields': ('name', 'slug', 'taxonomy_type', 'site', 'sector')
|
'fields': ('name', 'slug', 'taxonomy_type', 'description', 'site', 'sector')
|
||||||
|
}),
|
||||||
|
('Usage', {
|
||||||
|
'fields': ('count',),
|
||||||
|
'description': 'Number of content items using this term'
|
||||||
}),
|
}),
|
||||||
('WordPress Sync', {
|
('WordPress Sync', {
|
||||||
'fields': ('external_id', 'external_taxonomy')
|
'fields': ('external_id', 'external_taxonomy', 'metadata'),
|
||||||
|
'classes': ('collapse',)
|
||||||
|
}),
|
||||||
|
('Timestamps', {
|
||||||
|
'fields': ('created_at', 'updated_at'),
|
||||||
|
'classes': ('collapse',)
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user