diff --git a/README.md b/README.md index ef8be83..1d8c0fe 100644 --- a/README.md +++ b/README.md @@ -51,17 +51,40 @@ create a superuser (admin) account which can be used for Django Admin and Wagtai uv run python manage.py createsuperuser ``` -### 4. Run Development Server +### 4. Generate Test Data (optional but recommended) + +To see how blog and events pages look with content, +generate dummy blog posts and events: + +```bash +# Generate default test data (25 blog posts, 20 events) +uv run python manage.py create_dummy_posts + +# Or specify custom amounts +uv run python manage.py create_dummy_posts --blog-posts=30 --events=25 + +# Delete any existing dummy data +uv run python manage.py create_dummy_posts --delete +``` + +This command creates: +- Blog posts distributed across multiple years (for testing year filters) +- Events with both past and upcoming dates +- Random tags, authors, and excerpts +- All posts are automatically published and visible + +### 5. Run Development Server ```bash # Start Django development server uv run python manage.py runserver ``` -## Testing the Homepage Migration +## Available Pages - **Homepage (Django)**: [http://127.0.0.1:8000](http://127.0.0.1:8000) -- **Blog (Wagtail)**: [http://127.0.0.1:8000/blog/](http://127.0.0.1:8000/blog/) +- **Blog Index**: [http://127.0.0.1:8000/blog/](http://127.0.0.1:8000/blog/) +- **Events Index**: [http://127.0.0.1:8000/events/](http://127.0.0.1:8000/events/) - **Wagtail Admin**: [http://127.0.0.1:8000/cms/](http://127.0.0.1:8000/cms/) - **Django Admin**: [http://127.0.0.1:8000/admin/](http://127.0.0.1:8000/admin/) diff --git a/core/views.py b/core/views.py index 79d33c5..17b85f4 100644 --- a/core/views.py +++ b/core/views.py @@ -50,7 +50,14 @@ def home(request): # Fetch recent packages from YAML recent_packages = get_recent_packages(count=3) - + + # Fetch recent blog posts from Wagtail + recent_blog_posts = ( + BlogPage.objects.live() + .select_related('author') + .order_by('-date')[:3] + ) + context = { 'page_title': 'Welcome to pyOpenSci', 'hero_title': 'We make it easier for scientists to create, find, maintain, and contribute to reusable code and software.', @@ -59,6 +66,8 @@ def home(request): 'recent_contributors': recent_contributors, # Used for the "Recently Accepted Python Packages" section on the home page 'recent_packages': recent_packages, + # Used for the "Recent blog posts & updates" section on the home page + 'recent_blog_posts': recent_blog_posts, } return render(request, 'core/home.html', context) @@ -203,7 +212,37 @@ def serve_blog_page(request, slug): HttpResponse Rendered blog page using Wagtail's serve mechanism """ - page = get_object_or_404(BlogPage.objects.live().select_related('author'), slug=slug) + page = get_object_or_404(BlogPage.objects.live().select_related('author').prefetch_related('tags'), slug=slug) + + # Get related posts based on tag overlap + page_tags = page.tags.all() + related_posts = [] + + if page_tags: + # Find posts with overlapping tags + from django.db.models import Count + related_posts = ( + BlogPage.objects.live() + .select_related('author') + .prefetch_related('tags') + .filter(tags__in=page_tags) + .exclude(pk=page.pk) + .annotate(same_tags=Count('pk')) + .order_by('-same_tags', '-date')[:3] + ) + + # Fallback to recent posts if no tag matches + if not related_posts: + related_posts = ( + BlogPage.objects.live() + .select_related('author') + .exclude(pk=page.pk) + .order_by('-date')[:3] + ) + + # Add related_posts to the page context + page.related_posts = related_posts + return page.serve(request) @@ -224,4 +263,34 @@ def serve_event_page(request, slug): Rendered event page using Wagtail's serve mechanism """ page = get_object_or_404(EventPage.objects.live().select_related('author').prefetch_related('tags'), slug=slug) + + # Get related events based on tag overlap + page_tags = page.tags.all() + related_events = [] + + if page_tags: + # Find events with overlapping tags + from django.db.models import Count + related_events = ( + EventPage.objects.live() + .select_related('author') + .prefetch_related('tags') + .filter(tags__in=page_tags) + .exclude(pk=page.pk) + .annotate(same_tags=Count('pk')) + .order_by('-same_tags', '-start_date')[:3] + ) + + # Fallback to recent events if no tag matches + if not related_events: + related_events = ( + EventPage.objects.live() + .select_related('author') + .exclude(pk=page.pk) + .order_by('-start_date')[:3] + ) + + # Add related_events to the page context + page.related_events = related_events + return page.serve(request) diff --git a/templates/core/blog_index.html b/templates/core/blog_index.html index b1f3701..fb5edfb 100644 --- a/templates/core/blog_index.html +++ b/templates/core/blog_index.html @@ -21,19 +21,28 @@

{{ hero_title }}

Recent pyOpenSci blog posts

- + {% if available_years %} -
- - All Years - - {% for year in available_years %} - - {{ year.year }} - - {% endfor %} +
+ +
+ +
+ + + +
+
{% endif %}
diff --git a/templates/core/events_index.html b/templates/core/events_index.html index 931d105..823802f 100644 --- a/templates/core/events_index.html +++ b/templates/core/events_index.html @@ -87,19 +87,28 @@

Upcoming Events

Past Events

Browse our complete event archive

- + {% if available_years %} -
- - All Years - - {% for year in available_years %} - - {{ year.year }} - - {% endfor %} +
+ +
+ +
+ + + +
+
{% endif %}
diff --git a/templates/core/home.html b/templates/core/home.html index 6fb5221..f2b17b4 100644 --- a/templates/core/home.html +++ b/templates/core/home.html @@ -209,15 +209,40 @@

Recent blog posts & updates

- - {% for i in "123" %} - diff --git a/templates/publications/blog_page.html b/templates/publications/blog_page.html index db410ee..b214860 100644 --- a/templates/publications/blog_page.html +++ b/templates/publications/blog_page.html @@ -94,5 +94,42 @@

Comments

Comments system integration placeholder

{% endif %} + + + {% if page.related_posts %} +
+

You May Also Enjoy

+ +
+ {% endif %}
{% endblock %} \ No newline at end of file diff --git a/templates/publications/event_page.html b/templates/publications/event_page.html index f0b9da3..abb6eaa 100644 --- a/templates/publications/event_page.html +++ b/templates/publications/event_page.html @@ -139,5 +139,42 @@

Comments

Comments system integration placeholder

{% endif %} + + + {% if page.related_events %} +
+

You May Also Enjoy

+
+ {% for event in page.related_events %} + + + {% if event.header_image %} +
+ {{ event.header_image_alt|default:event.title }} +
+ {% else %} +
+ {% endif %} + +
+

{{ event.title }}

+ + +

+ {{ event.start_date|date:"F j, Y" }} +

+ + + {% if event.excerpt %} +

{{ event.excerpt|truncatewords:15 }}

+ {% endif %} +
+
+ {% endfor %} +
+
+ {% endif %} {% endblock %} \ No newline at end of file