39 lines
1.4 KiB
Python
39 lines
1.4 KiB
Python
import os
|
|
from django.shortcuts import render, redirect, get_object_or_404
|
|
from django.utils import timezone
|
|
from .models import Announcement, Comment, Event, Resource
|
|
from django.contrib import messages
|
|
|
|
def index(request):
|
|
"""Render the Barangay Portal landing page."""
|
|
announcements = Announcement.objects.prefetch_related('comments').all()
|
|
upcoming_events = Event.objects.filter(start_date__gte=timezone.now())[:5]
|
|
resources = Resource.objects.all()
|
|
|
|
context = {
|
|
"announcements": announcements,
|
|
"upcoming_events": upcoming_events,
|
|
"resources": resources,
|
|
"project_name": "Barangay Portal",
|
|
}
|
|
return render(request, "core/index.html", context)
|
|
|
|
def add_comment(request, announcement_id):
|
|
"""Handle adding a comment to an announcement."""
|
|
if request.method == "POST":
|
|
announcement = get_object_or_404(Announcement, id=announcement_id)
|
|
author_name = request.POST.get("author_name", "Anonymous")
|
|
content = request.POST.get("content")
|
|
|
|
if content:
|
|
Comment.objects.create(
|
|
announcement=announcement,
|
|
author_name=author_name,
|
|
content=content
|
|
)
|
|
messages.success(request, "Your comment has been posted!")
|
|
else:
|
|
messages.error(request, "Comment content cannot be empty.")
|
|
|
|
return redirect("index")
|