Commit 0ebf8de9 authored by justin's avatar justin

staging: merged announcement board

parents e88e23d6 2b522e43
from django.db import models from django.db import models
from dashboard.models import WidgetUser from dashboard.models import WidgetUser
from django.urls import reverse
class Announcement(models.Model): class Announcement(models.Model):
...@@ -14,12 +15,16 @@ class Announcement(models.Model): ...@@ -14,12 +15,16 @@ class Announcement(models.Model):
def __str__(self): def __str__(self):
return self.title return self.title
# get corresponding detail page
def get_absolute_url(self):
return(reverse('announcement_board:announcementdetail', kwargs={'pk': self.pk}))
class Reaction(models.Model): class Reaction(models.Model):
name = models.CharField(max_length=50) name = models.CharField(max_length=50)
tally = models.IntegerField(default=0) 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): def __str__(self):
return self.name return self.name
{% extends 'base.html' %}
{% load static %}
{% block title %} Add Announcement {% endblock %}
{% block content %}
<h1> Add a new announcement: </h1>
<form method="POST">
{% csrf_token %}
{# iterate through fields in the form #}
{% for field in form%}
<b>{{ field.label }}:</b> {{ field }} <br><br>
{% endfor %}
<button type="submit">Add Announcement</button>
</form>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %} {{ object.title }} {% endblock %}
{% block content %}
{# announcement title #}
<h1> {{ object.title }} </h1>
{# announcement author using displayName method in model #}
<h2> by {{ object.author.displayName }} </h2>
{# format datetime with tags #}
<h3> {{ object.pub_datetime|date:"m/d/Y, h:i A" }} </h3>
{# announcement body #}
<p> {{ object.body }} </p> <br>
{# iterate through reactions, show name: tally #}
Reactions:
<ul>
{% for reaction in object.reactlist.all %}
<li> <b>{{ reaction.name}}:</b> {{ reaction.tally }} </li>
{% endfor %}
</ul>
<form action="../edit">
<button type="Submit">Edit Announcement</button>
</form>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %} Edit Announcement {% endblock %}
{% block content %}
<h1> Edit announcement: </h1>
<form method="POST">
{% csrf_token %}
{# iterate through fields #}
{% for field in form%}
<b>{{ field.label }}:</b> {{ field }} <br><br>
{% endfor %}
<button type="submit">Save Changes to Announcement</button>
</form>
{% endblock %}
\ No newline at end of file
{% 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>
{# iterate through announcement_list from context, use get url function to link to detail page #}
<ul>
{% for announcement in announcement_list %}
<li><a href="{{ announcement.get_absolute_url }}">{{ announcement.title }} by {{ announcement.author.displayName }}</a></li>
{% endfor %}
</ul>
<br>
<form action="./add">
<button type="Submit">New Announcement</button>
</form>
<br>
<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
...@@ -2,7 +2,17 @@ from django.urls import path ...@@ -2,7 +2,17 @@ from django.urls import path
from . import views from . import views
urlpatterns = [ urlpatterns = [
# main page listing announcements
path("", views.announcements, name="announcements"), path("", views.announcements, name="announcements"),
# detail page for each announcement
path("<int:pk>/details/", views.AnnouncementDetailView.as_view(), name="announcementdetail"),
# create page
path("add/", views.AnnouncementCreateView.as_view(), name="announcementadd"),
# edit page for each announcement
path("<int:pk>/edit/", views.AnnouncementUpdateView.as_view(), name="announcementupdate"),
] ]
......
...@@ -2,38 +2,29 @@ from django.http import HttpResponse ...@@ -2,38 +2,29 @@ from django.http import HttpResponse
from .models import Announcement, Reaction from .models import Announcement, Reaction
from datetime import datetime from datetime import datetime
from django.utils import timezone 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): 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 # use context to pass announcements sorted by pubdate
reactions = announcement.reaction_set.all() context = {
"announcement_list" : Announcement.objects.all().order_by('-pub_datetime'),
}
# response proper return render(request, 'announcement_board/announcements.html', context)
response = (
response
+ "<br>"
+ announcement.title
+ " by "
+ announcement.author.displayName()
+ " published "
+ datetime
+ ": <br>"
)
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 class AnnouncementCreateView(CreateView):
for reaction in reactions: model = Announcement
response = response + reaction.name + ": " + str(reaction.tally) + "<br>" 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