Commit 5d13d77d authored by Emilio Gentolia's avatar Emilio Gentolia

changed function to class view for announcements

parent ad41407b
from django.http import HttpResponse
from .models import Announcement, Reaction
from django.shortcuts import render
from django.views import View
from django.views.generic.detail import DetailView
from .models import Announcement
# View for announcements page.
def index(request):
return HttpResponse(display_announcementboard(Announcement.objects.all(), Reaction.objects.all()))
def display_announcementboard(announcement_data, reaction_data):
display_output = "ANNOUNCEMENTS: <br>"
for object in announcement_data:
announcement_title = object.announcement_title
first_name = object.author.first_name
last_name = object.author.last_name
pub_date = str(object.pub_date)
announcement_body = object.announcement_body
like_count = Reaction.objects.filter(announcement_id=object.id).filter(reaction_name="Like").first().tally
love_count = Reaction.objects.filter(announcement_id=object.id).filter(reaction_name="Love").first().tally
angry_count = Reaction.objects.filter(announcement_id=object.id).filter(reaction_name="Angry").first().tally
display_output += f'''
{announcement_title} by {first_name} {last_name} dated {pub_date}:<br>
{announcement_body}<br>
Like: {like_count}<br>
Love: {love_count}<br>
Angry: {angry_count}<br>
<br>
'''
return display_output
class AnnouncementView(View):
def get(self, request):
objects_set = {
"all_announcements": [obj for obj in Announcement.objects.all().order_by('last_name')]
}
return render(request, 'homepage/index.html', objects_set)
class AnnouncementDetailView(DetailView):
model = Announcement
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment