Added working foundations for announcements templates. CSS style necessary

parent 1c3111e9
{% if Announcement %}
<h2>{{Announcement.announcement_title}}</h2>
<h3>by {{Announcement.author.first_name}} {{Announcement.author.last_name}}, {{Announcement.pub_date}}</h3>
<h4>{{Announcement.announcement_body}}</h4>
{% for reaction in reactions %}
<h4>{{reaction.reaction_name}}: {{reaction.tally}}</h4>
{% endfor %}
{% else %}
<p>Announcement not found.</p>
{% endif %}
\ No newline at end of file
{% if announcement_list %}
<h1>Announcement Board</h1>
<h2>Important Announcements:</h2>
<ul>
{% for Announcement in announcement_list %}
<li><a href="/announcements/{{Announcement.id}}/details">{{Announcement.announcement_title}}, by {{Announcement.author.last_name}}, {{Announcement.author.first_name}}, dated {{Announcement.pub_date}}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No Announcements are available</p>
{% endif %}
\ No newline at end of file
......@@ -2,5 +2,6 @@ from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name="announcements")
path('', views.index, name="announcements"),
path('<int:announcement_id>/details/', views.details, name="details"),
]
from django.http import HttpResponse
from array import array
from django.http import HttpResponse, Http404
from announcements.models import Announcement, Reaction, WidgetUser
from django.shortcuts import render
from django.template import loader
# Create your views here.
def index(request):
announcementEntries = Announcement.objects.all().count()
n = 1
announcement = "ANNOUNCEMENTS:<br/>"
while(n<=announcementEntries):
announcement += getAnnouncementHeader(n)
announcement += getReactions(n)
n += 1
return HttpResponse(announcement)
# announcementEntries = Announcement.objects.all().count()
# n = 1
# announcement = "ANNOUNCEMENTS:<br/>"
# while(n<=announcementEntries):
# announcement += getAnnouncementHeader(n)
# announcement += getReactions(n)
# n += 1
# return HttpResponse(announcement)
announcement_list = Announcement.objects.order_by("pub_date")
context = {
"announcement_list": announcement_list,
}
return render(request, "announcements/index.html", context)
# helper function that iterates through every reaction of an announcement and returns it
# helper function that iterates through every reaction of an announcement, adds it to an array, then returns it
def getReactions(pk):
announcementReactions = ""
# announcementReactions = ""
# for reaction in Announcement.objects.get(pk=pk).announcements.all():
# announcementReactions += "{}: {}<br/>" .format(reaction.reaction_name, reaction.tally)
# announcementReactions += "<br/> "
# return announcementReactions
# announcementReactions = ""
announcementReactions = []
for reaction in Announcement.objects.get(pk=pk).announcements.all():
announcementReactions += "{}: {}<br/>" .format(reaction.reaction_name, reaction.tally)
announcementReactions += "<br/> "
announcementReactions.append(reaction)
return announcementReactions
# helper function that takes the data in an announcement and creates the header of the announcement
# (First and Second lines)
def getAnnouncementHeader(pk):
announcementFirstLine = ""
announcementSecondLine = ""
announcementHeader = ""
# def getAnnouncementHeader(pk):
# announcementFirstLine = ""
# announcementSecondLine = ""
# announcementHeader = ""
# announcementTitle = Announcement.objects.get(pk=pk).announcement_title
# authorFirstName = Announcement.objects.get(pk=pk).author.first_name
# authorLastName = Announcement.objects.get(pk=pk).author.last_name
# publicationDate = Announcement.objects.get(pk=pk).pub_date
# announcementBody = Announcement.objects.get(pk=pk).announcement_body
# announcementFirstLine = "{} by {} {} dated {}:<br/>" .format(announcementTitle, authorFirstName, authorLastName, publicationDate)
# announcementSecondLine = "{}<br/>" .format(announcementBody)
# announcementHeader += announcementFirstLine + announcementSecondLine
announcementTitle = Announcement.objects.get(pk=pk).announcement_title
authorFirstName = Announcement.objects.get(pk=pk).author.first_name
authorLastName = Announcement.objects.get(pk=pk).author.last_name
publicationDate = Announcement.objects.get(pk=pk).pub_date
announcementBody = Announcement.objects.get(pk=pk).announcement_body
announcementFirstLine = "{} by {} {} dated {}:<br/>" .format(announcementTitle, authorFirstName, authorLastName, publicationDate)
announcementSecondLine = "{}<br/>" .format(announcementBody)
announcementHeader += announcementFirstLine + announcementSecondLine
# return announcementHeader
return announcementHeader
def details(request, announcement_id):
try:
announcement = Announcement.objects.get(pk=announcement_id)
except Announcement.DoesNotExist:
raise Http404("Announcement does not exist!")
try:
reactions = getReactions(pk=announcement_id)
except Reaction.DoesNotExist:
raise Http404("Reaction does not exist")
context = {
"Announcement": announcement,
"reactions": reactions
}
return render(request, "announcements/details.html", context)
\ No newline at end of file
......@@ -31,6 +31,9 @@ DEBUG = True
ALLOWED_HOSTS = []
# Change Date Format
DATE_FORMAT = "m-d-Y"
USE_L10N = False
# Application definition
......
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