Commit 3255bb11 authored by Javi Ng's avatar Javi Ng

created template for announcements page, updated urls, added new views,...

created template for announcements page, updated urls, added new views, created get_reverse_url for announcement model
parent 3c204a86
from django.db import models
from dashboard.models import WidgetUser
from django.urls import reverse
class Announcement(models.Model):
......@@ -13,13 +14,16 @@ class Announcement(models.Model):
def __str__(self):
return self.title
def get_absolute_url(self):
reverse('announcement-details', kwargs={'pk': self.pk})
class Reaction(models.Model):
name = models.CharField(max_length=50)
tally = models.IntegerField(default=0)
announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE)
announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE, related_name='reactlist')
def __str__(self):
return self.name
{% extends 'base.html' %}
{% load static %}
{% block title %} Widget's Announcement Board {% endblock %}
{% block content %}
<h1>Welcome to Widget's Announcement Board!</h1>
<h2>Announcements:</h2>
<ul>
{% for announcement in announcement_list %}
<li><a href="{{ announcement.get_absolute_url }}">{{ announcement.title }} by {{ announcement.author.displayName }}</a></li>
{% endfor %}
</ul>
<form action="./add">
<button type="Submit">New Announcement</button>
</form>
<a href="../dashboard/">Dashboard</a> <br>
<a href="../forum/">Forum</a> <br>
<a href="../assignments/">Assignments</a> <br>
<a href="../calendar/">Calendar</a> <br>
{% endblock %}
\ No newline at end of file
......@@ -3,6 +3,9 @@ from . import views
urlpatterns = [
path("", views.announcements, name="announcements"),
path("<int:pk>/details/", views.AnnouncementDetailView.as_view(), name="announcement-details"),
path("add/", views.AnnouncementCreateView.as_view(), name="announcement-add"),
path("<int:pk>/edit/", views.AnnouncementUpdateView.as_view(), name="announcement-update"),
]
......
......@@ -2,38 +2,27 @@ from django.http import HttpResponse
from .models import Announcement, Reaction
from datetime import datetime
from django.utils import timezone
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from django.shortcuts import render
def announcements(request):
# retrieve all announcement entries in a list
announcementList = Announcement.objects.all()
# build response string
response = "Widget's Announcement Board <br><br>" + "Announcements:"
for announcement in announcementList:
# timezone conversion and string formatting for date-time
localdatetime = timezone.localtime(announcement.pub_datetime)
datetime = localdatetime.strftime("%m/%d/%Y, %I:%M %p")
# list of reactions for announcement
reactions = announcement.reaction_set.all()
context = {
"announcement_list" : Announcement.objects.all().order_by('pub_date'),
}
# response proper
response = (
response
+ "<br>"
+ announcement.title
+ " by "
+ announcement.author.displayName()
+ " published "
+ datetime
+ ": <br>"
)
return render(request, 'announcement_board/announcements.html', context)
response = response + announcement.body + "<br>"
class AnnouncementDetailView(DetailView):
model = Announcement
template_name = 'announcement_board/announcement-details.html'
# for each reaction, add line with reaction and tally
for reaction in reactions:
response = response + reaction.name + ": " + str(reaction.tally) + "<br>"
class AnnouncementCreateView(CreateView):
model = Announcement
template_name = 'announcement_board/announcement-add.html'
fields = ['title', 'body', 'author']
return HttpResponse(response)
class AnnouncementUpdateView(UpdateView):
model = Announcement
template_name = 'announcement_board/announcement-edit.html'
fields = ['title', 'body', 'author']
\ No newline at end of file
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