You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
486 lines
22 KiB
486 lines
22 KiB
"""
|
|
Django Management Command: populate_books
|
|
|
|
This command creates 20 book objects with related categories and collections.
|
|
|
|
Usage:
|
|
python manage.py populate_books
|
|
"""
|
|
|
|
from django.core.management.base import BaseCommand
|
|
from django.utils.text import slugify
|
|
from apps.library.models import Book, Category, BookCollection
|
|
from dj_language.models import Language
|
|
import random
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Create 20 book objects with categories and collections'
|
|
|
|
def handle(self, *args, **options):
|
|
# Get or create English language
|
|
language, created = Language.objects.get_or_create(
|
|
code='en',
|
|
defaults={'name': 'English'}
|
|
)
|
|
|
|
# Define book data
|
|
book_data = [
|
|
{
|
|
'title': 'The Art of Programming',
|
|
'slogan': 'Master the fundamentals of coding',
|
|
'summary_title': 'Learning to Code',
|
|
'summary': 'A comprehensive guide to programming fundamentals',
|
|
'description': 'This book covers essential programming concepts and best practices.',
|
|
'publisher': 'Tech Books Publishing',
|
|
'year_of_publication': '2023',
|
|
'author': 'John Smith',
|
|
'isbn': '978-1234567890',
|
|
'numnber_of_volume': '1',
|
|
'main_themes': ['programming', 'education', 'technology'],
|
|
'notable_works': ['Chapter 1: Basics', 'Chapter 2: Advanced'],
|
|
'pages_count': '350',
|
|
'file_type': 'pdf',
|
|
},
|
|
{
|
|
'title': 'Web Development Essentials',
|
|
'slogan': 'Build modern web applications',
|
|
'summary_title': 'Web Development Guide',
|
|
'summary': 'Everything you need to know about web development',
|
|
'description': 'Learn HTML, CSS, JavaScript and modern frameworks.',
|
|
'publisher': 'Digital Press',
|
|
'year_of_publication': '2023',
|
|
'author': 'Jane Doe',
|
|
'isbn': '978-1234567891',
|
|
'numnber_of_volume': '1',
|
|
'main_themes': ['web', 'frontend', 'backend'],
|
|
'notable_works': ['HTML Basics', 'CSS Layouts'],
|
|
'pages_count': '420',
|
|
'file_type': 'pdf',
|
|
},
|
|
{
|
|
'title': 'Database Design Principles',
|
|
'slogan': 'Design efficient databases',
|
|
'summary_title': 'Database Fundamentals',
|
|
'summary': 'Master database design and optimization',
|
|
'description': 'Complete guide to relational and NoSQL databases.',
|
|
'publisher': 'Data Press',
|
|
'year_of_publication': '2022',
|
|
'author': 'Michael Chen',
|
|
'isbn': '978-1234567892',
|
|
'numnber_of_volume': '1',
|
|
'main_themes': ['database', 'sql', 'optimization'],
|
|
'notable_works': ['Normalization', 'Indexing'],
|
|
'pages_count': '380',
|
|
'file_type': 'pdf',
|
|
},
|
|
{
|
|
'title': 'Python for Data Science',
|
|
'slogan': 'Analyze data with Python',
|
|
'summary_title': 'Data Science with Python',
|
|
'summary': 'Learn Python for data analysis and visualization',
|
|
'description': 'Comprehensive guide to Python libraries for data science.',
|
|
'publisher': 'Science Books Ltd',
|
|
'year_of_publication': '2023',
|
|
'author': 'Sarah Johnson',
|
|
'isbn': '978-1234567893',
|
|
'numnber_of_volume': '1',
|
|
'main_themes': ['python', 'data science', 'analytics'],
|
|
'notable_works': ['Pandas', 'NumPy', 'Matplotlib'],
|
|
'pages_count': '450',
|
|
'file_type': 'pdf',
|
|
},
|
|
{
|
|
'title': 'Machine Learning Basics',
|
|
'slogan': 'Introduction to ML',
|
|
'summary_title': 'ML Fundamentals',
|
|
'summary': 'Get started with machine learning algorithms',
|
|
'description': 'Learn supervised and unsupervised learning techniques.',
|
|
'publisher': 'AI Publishing',
|
|
'year_of_publication': '2023',
|
|
'author': 'Alex Kumar',
|
|
'isbn': '978-1234567894',
|
|
'numnber_of_volume': '1',
|
|
'main_themes': ['machine learning', 'algorithms', 'AI'],
|
|
'notable_works': ['Classification', 'Regression'],
|
|
'pages_count': '520',
|
|
'file_type': 'pdf',
|
|
},
|
|
{
|
|
'title': 'Cloud Computing Guide',
|
|
'slogan': 'Deploy applications to the cloud',
|
|
'summary_title': 'Cloud Architecture',
|
|
'summary': 'Master cloud platforms and deployment',
|
|
'description': 'Complete guide to AWS, Azure, and Google Cloud.',
|
|
'publisher': 'Cloud Press',
|
|
'year_of_publication': '2023',
|
|
'author': 'Emma Wilson',
|
|
'isbn': '978-1234567895',
|
|
'numnber_of_volume': '1',
|
|
'main_themes': ['cloud', 'devops', 'infrastructure'],
|
|
'notable_works': ['AWS', 'Docker', 'Kubernetes'],
|
|
'pages_count': '480',
|
|
'file_type': 'pdf',
|
|
},
|
|
{
|
|
'title': 'REST API Design',
|
|
'slogan': 'Build scalable APIs',
|
|
'summary_title': 'API Development',
|
|
'summary': 'Design and implement RESTful web services',
|
|
'description': 'Learn API best practices and design patterns.',
|
|
'publisher': 'Web Services Publishing',
|
|
'year_of_publication': '2022',
|
|
'author': 'David Brown',
|
|
'isbn': '978-1234567896',
|
|
'numnber_of_volume': '1',
|
|
'main_themes': ['api', 'rest', 'web services'],
|
|
'notable_works': ['HTTP Methods', 'Authentication'],
|
|
'pages_count': '340',
|
|
'file_type': 'pdf',
|
|
},
|
|
{
|
|
'title': 'Security Best Practices',
|
|
'slogan': 'Secure your applications',
|
|
'summary_title': 'Application Security',
|
|
'summary': 'Essential security practices for developers',
|
|
'description': 'Learn to protect your applications from attacks.',
|
|
'publisher': 'Security Press',
|
|
'year_of_publication': '2023',
|
|
'author': 'Lisa Anderson',
|
|
'isbn': '978-1234567897',
|
|
'numnber_of_volume': '1',
|
|
'main_themes': ['security', 'encryption', 'authentication'],
|
|
'notable_works': ['OWASP', 'Cryptography'],
|
|
'pages_count': '400',
|
|
'file_type': 'pdf',
|
|
},
|
|
{
|
|
'title': 'Mobile App Development',
|
|
'slogan': 'Build iOS and Android apps',
|
|
'summary_title': 'Mobile Development',
|
|
'summary': 'Cross-platform mobile development guide',
|
|
'description': 'Learn to build native and cross-platform mobile apps.',
|
|
'publisher': 'Mobile Press',
|
|
'year_of_publication': '2023',
|
|
'author': 'Tom Martinez',
|
|
'isbn': '978-1234567898',
|
|
'numnber_of_volume': '1',
|
|
'main_themes': ['mobile', 'ios', 'android'],
|
|
'notable_works': ['React Native', 'Flutter'],
|
|
'pages_count': '490',
|
|
'file_type': 'pdf',
|
|
},
|
|
{
|
|
'title': 'Git and Version Control',
|
|
'slogan': 'Master version control',
|
|
'summary_title': 'Git Workflow',
|
|
'summary': 'Complete guide to Git and collaborative development',
|
|
'description': 'Learn Git commands and best practices.',
|
|
'publisher': 'DevOps Books',
|
|
'year_of_publication': '2022',
|
|
'author': 'Chris Lee',
|
|
'isbn': '978-1234567899',
|
|
'numnber_of_volume': '1',
|
|
'main_themes': ['git', 'version control', 'collaboration'],
|
|
'notable_works': ['Branching', 'Merging'],
|
|
'pages_count': '280',
|
|
'file_type': 'pdf',
|
|
},
|
|
{
|
|
'title': 'Advanced Python Techniques',
|
|
'slogan': 'Level up your Python skills',
|
|
'summary_title': 'Python Advanced',
|
|
'summary': 'Deep dive into advanced Python concepts',
|
|
'description': 'Explore decorators, generators, and more.',
|
|
'publisher': 'Python Publishers',
|
|
'year_of_publication': '2023',
|
|
'author': 'Rachel Green',
|
|
'isbn': '978-1234567900',
|
|
'numnber_of_volume': '2',
|
|
'main_themes': ['python', 'advanced', 'patterns'],
|
|
'notable_works': ['Decorators', 'Generators', 'Meta-programming'],
|
|
'pages_count': '410',
|
|
'file_type': 'pdf',
|
|
},
|
|
{
|
|
'title': 'TypeScript Guide',
|
|
'slogan': 'Write type-safe JavaScript',
|
|
'summary_title': 'TypeScript Basics',
|
|
'summary': 'Learn TypeScript for robust web development',
|
|
'description': 'Complete TypeScript guide with practical examples.',
|
|
'publisher': 'JavaScript Press',
|
|
'year_of_publication': '2023',
|
|
'author': 'Kevin White',
|
|
'isbn': '978-1234567901',
|
|
'numnber_of_volume': '1',
|
|
'main_themes': ['typescript', 'javascript', 'types'],
|
|
'notable_works': ['Interfaces', 'Generics'],
|
|
'pages_count': '360',
|
|
'file_type': 'pdf',
|
|
},
|
|
{
|
|
'title': 'React Deep Dive',
|
|
'slogan': 'Master React framework',
|
|
'summary_title': 'React Advanced',
|
|
'summary': 'Advanced React concepts and patterns',
|
|
'description': 'Learn hooks, context, and performance optimization.',
|
|
'publisher': 'Frontend Press',
|
|
'year_of_publication': '2023',
|
|
'author': 'Nina Patel',
|
|
'isbn': '978-1234567902',
|
|
'numnber_of_volume': '1',
|
|
'main_themes': ['react', 'javascript', 'frontend'],
|
|
'notable_works': ['Hooks', 'Context API', 'Performance'],
|
|
'pages_count': '470',
|
|
'file_type': 'pdf',
|
|
},
|
|
{
|
|
'title': 'Django for Beginners',
|
|
'slogan': 'Build web apps with Django',
|
|
'summary_title': 'Django Basics',
|
|
'summary': 'Learn Django framework from scratch',
|
|
'description': 'Complete beginner guide to Django web development.',
|
|
'publisher': 'Django Press',
|
|
'year_of_publication': '2023',
|
|
'author': 'Oscar Torres',
|
|
'isbn': '978-1234567903',
|
|
'numnber_of_volume': '1',
|
|
'main_themes': ['django', 'python', 'web development'],
|
|
'notable_works': ['Models', 'Views', 'Templates'],
|
|
'pages_count': '440',
|
|
'file_type': 'pdf',
|
|
},
|
|
{
|
|
'title': 'Testing and QA',
|
|
'slogan': 'Write better tests',
|
|
'summary_title': 'Testing Strategies',
|
|
'summary': 'Unit testing, integration testing and automation',
|
|
'description': 'Comprehensive guide to software testing.',
|
|
'publisher': 'QA Press',
|
|
'year_of_publication': '2022',
|
|
'author': 'Patricia Davis',
|
|
'isbn': '978-1234567904',
|
|
'numnber_of_volume': '1',
|
|
'main_themes': ['testing', 'qa', 'automation'],
|
|
'notable_works': ['Unit Tests', 'Integration Tests'],
|
|
'pages_count': '350',
|
|
'file_type': 'pdf',
|
|
},
|
|
{
|
|
'title': 'Docker and Containerization',
|
|
'slogan': 'Containerize your applications',
|
|
'summary_title': 'Docker Essentials',
|
|
'summary': 'Docker fundamentals and best practices',
|
|
'description': 'Learn containerization with Docker and Compose.',
|
|
'publisher': 'Container Press',
|
|
'year_of_publication': '2023',
|
|
'author': 'Victor Lopez',
|
|
'isbn': '978-1234567905',
|
|
'numnber_of_volume': '1',
|
|
'main_themes': ['docker', 'containers', 'devops'],
|
|
'notable_works': ['Images', 'Containers', 'Docker Compose'],
|
|
'pages_count': '400',
|
|
'file_type': 'pdf',
|
|
},
|
|
{
|
|
'title': 'Microservices Architecture',
|
|
'slogan': 'Build scalable systems',
|
|
'summary_title': 'Microservices Design',
|
|
'summary': 'Design and implement microservices',
|
|
'description': 'Guide to building distributed microservice systems.',
|
|
'publisher': 'Architecture Press',
|
|
'year_of_publication': '2023',
|
|
'author': 'Wendy Harris',
|
|
'isbn': '978-1234567906',
|
|
'numnber_of_volume': '1',
|
|
'main_themes': ['microservices', 'architecture', 'distributed'],
|
|
'notable_works': ['Service Design', 'Communication Patterns'],
|
|
'pages_count': '510',
|
|
'file_type': 'pdf',
|
|
},
|
|
{
|
|
'title': 'GraphQL Mastery',
|
|
'slogan': 'Query language for APIs',
|
|
'summary_title': 'GraphQL Guide',
|
|
'summary': 'Learn GraphQL query language and execution',
|
|
'description': 'Complete guide to GraphQL API development.',
|
|
'publisher': 'GraphQL Press',
|
|
'year_of_publication': '2023',
|
|
'author': 'Xavier Knight',
|
|
'isbn': '978-1234567907',
|
|
'numnber_of_volume': '1',
|
|
'main_themes': ['graphql', 'api', 'query language'],
|
|
'notable_works': ['Queries', 'Mutations', 'Subscriptions'],
|
|
'pages_count': '380',
|
|
'file_type': 'pdf',
|
|
},
|
|
{
|
|
'title': 'Performance Optimization',
|
|
'slogan': 'Make your code faster',
|
|
'summary_title': 'Optimization Techniques',
|
|
'summary': 'Optimize application performance',
|
|
'description': 'Strategies for improving code and system performance.',
|
|
'publisher': 'Performance Press',
|
|
'year_of_publication': '2023',
|
|
'author': 'Yvonne Robinson',
|
|
'isbn': '978-1234567908',
|
|
'numnber_of_volume': '1',
|
|
'main_themes': ['performance', 'optimization', 'caching'],
|
|
'notable_works': ['Profiling', 'Caching', 'Monitoring'],
|
|
'pages_count': '360',
|
|
'file_type': 'pdf',
|
|
},
|
|
{
|
|
'title': 'Clean Code Practices',
|
|
'slogan': 'Write maintainable code',
|
|
'summary_title': 'Code Quality',
|
|
'summary': 'Best practices for writing clean code',
|
|
'description': 'Learn coding standards and refactoring techniques.',
|
|
'publisher': 'Code Press',
|
|
'year_of_publication': '2023',
|
|
'author': 'Zoe Walker',
|
|
'isbn': '978-1234567909',
|
|
'numnber_of_volume': '1',
|
|
'main_themes': ['clean code', 'best practices', 'refactoring'],
|
|
'notable_works': ['SOLID Principles', 'Design Patterns'],
|
|
'pages_count': '430',
|
|
'file_type': 'pdf',
|
|
},
|
|
{
|
|
'title': 'Artificial Intelligence Fundamentals',
|
|
'slogan': 'Understanding AI and ML',
|
|
'summary_title': 'AI Basics',
|
|
'summary': 'Foundational concepts of artificial intelligence',
|
|
'description': 'Introduction to AI, machine learning and deep learning.',
|
|
'publisher': 'AI Academy',
|
|
'year_of_publication': '2023',
|
|
'author': 'Benjamin Foster',
|
|
'isbn': '978-1234567910',
|
|
'numnber_of_volume': '1',
|
|
'main_themes': ['ai', 'machine learning', 'neural networks'],
|
|
'notable_works': ['Neural Networks', 'Deep Learning'],
|
|
'pages_count': '520',
|
|
'file_type': 'pdf',
|
|
},
|
|
]
|
|
|
|
# Book cover images (cycling through 4 images)
|
|
image_paths = [
|
|
'seeds/images/lib-book1.jpg',
|
|
'seeds/images/lib-book2.jpg',
|
|
'seeds/images/lib-book3.jpg',
|
|
'seeds/images/lib-book4.jpg',
|
|
]
|
|
|
|
# Category IDs and Collection IDs
|
|
category_ids = [1, 2, 3, 4, 5, 15, 16, 17, 18, 19, 20]
|
|
collection_ids = [1, 2, 3, 4, 5, 6, 13, 14, 15]
|
|
|
|
created_books = []
|
|
image_index = 0
|
|
|
|
self.stdout.write(self.style.SUCCESS('Starting book creation...'))
|
|
|
|
# Create books
|
|
for idx, book_info in enumerate(book_data):
|
|
try:
|
|
# Create slug if not present
|
|
slug = slugify(book_info['title'])
|
|
|
|
# Create the book
|
|
book = Book.objects.create(
|
|
title=book_info['title'],
|
|
slug=slug,
|
|
slogan=book_info['slogan'],
|
|
summary_title=book_info['summary_title'],
|
|
summary=book_info['summary'],
|
|
description=book_info['description'],
|
|
publisher=book_info['publisher'],
|
|
year_of_publication=book_info['year_of_publication'],
|
|
author=book_info['author'],
|
|
isbn=book_info['isbn'],
|
|
numnber_of_volume=book_info['numnber_of_volume'],
|
|
language=language, # Pass Language instance
|
|
main_themes=book_info['main_themes'],
|
|
notable_works=book_info['notable_works'],
|
|
pages_count=book_info['pages_count'],
|
|
file_type=book_info['file_type'],
|
|
status=True,
|
|
pin=True,
|
|
thumbnail=image_paths[image_index % len(image_paths)],
|
|
)
|
|
|
|
# Cycle through images
|
|
image_index += 1
|
|
created_books.append(book)
|
|
|
|
self.stdout.write(
|
|
self.style.SUCCESS(f'✓ Created book: {book.title}')
|
|
)
|
|
|
|
except Exception as e:
|
|
self.stdout.write(
|
|
self.style.ERROR(f'✗ Error creating book {book_info["title"]}: {str(e)}')
|
|
)
|
|
continue
|
|
|
|
# Connect books with categories
|
|
self.stdout.write(self.style.SUCCESS('\nConnecting books to categories...'))
|
|
for idx, book in enumerate(created_books):
|
|
try:
|
|
# Select 2-3 random categories for each book
|
|
num_categories = random.randint(2, 3)
|
|
selected_categories = random.sample(category_ids, num_categories)
|
|
|
|
for category_id in selected_categories:
|
|
try:
|
|
category = Category.objects.get(id=category_id)
|
|
book.categories.add(category)
|
|
except Category.DoesNotExist:
|
|
self.stdout.write(
|
|
self.style.WARNING(f' ⚠ Category with ID {category_id} not found')
|
|
)
|
|
|
|
self.stdout.write(
|
|
self.style.SUCCESS(f' ✓ Connected {book.title} to categories')
|
|
)
|
|
|
|
except Exception as e:
|
|
self.stdout.write(
|
|
self.style.ERROR(f' ✗ Error connecting categories for {book.title}: {str(e)}')
|
|
)
|
|
continue
|
|
|
|
# Connect books with collections
|
|
self.stdout.write(self.style.SUCCESS('\nConnecting books to collections...'))
|
|
for idx, book in enumerate(created_books):
|
|
try:
|
|
# Select 1-2 random collections for each book
|
|
num_collections = random.randint(1, 2)
|
|
selected_collections = random.sample(collection_ids, num_collections)
|
|
|
|
for collection_id in selected_collections:
|
|
try:
|
|
collection = BookCollection.objects.get(id=collection_id)
|
|
book.collections.add(collection)
|
|
except BookCollection.DoesNotExist:
|
|
self.stdout.write(
|
|
self.style.WARNING(f' ⚠ Collection with ID {collection_id} not found')
|
|
)
|
|
|
|
self.stdout.write(
|
|
self.style.SUCCESS(f' ✓ Connected {book.title} to collections')
|
|
)
|
|
|
|
except Exception as e:
|
|
self.stdout.write(
|
|
self.style.ERROR(f' ✗ Error connecting collections for {book.title}: {str(e)}')
|
|
)
|
|
continue
|
|
|
|
# Summary
|
|
self.stdout.write(self.style.SUCCESS('\n' + '='*50))
|
|
self.stdout.write(
|
|
self.style.SUCCESS(f'Successfully created {len(created_books)} books!')
|
|
)
|
|
self.stdout.write(self.style.SUCCESS('='*50))
|