from django.http import Http404
from .models import Announcement, Reaction
from django.shortcuts import render
from django.views.generic import CreateView

# Create your views here.
def index(request):
    announcement_list = Announcement.objects.order_by("-pub_date")
    context = {
        "announcement_list": announcement_list,
    }

    return render(request, "announcements/index.html", context)

def details(request, announcement_id):
    try:
        announcement = Announcement.objects.get(pk=announcement_id)
        reaction_list = Reaction.objects.order_by("reaction_name")
    except Announcement.DoesNotExist:
        raise Http404("Announcement does not exist!")
    output = {
        "announcement": announcement,
        "reaction_list": reaction_list,
    }
    return render(request, "announcements/detail.html", output)

class addAnnouncement(CreateView):
    model = Announcement
    template_name = "announcements/add.html"
    fields = ['announcement_title', 'announcement_body', 'author', 'announcement_image']

