Commit e521c58b authored by Ysabella Panghulan's avatar Ysabella Panghulan

Merge branch 'announcementsv2'

parents fcc9f836 92b62429
from django.db import models
from django.urls import reverse
from dashboard.models import WidgetUser
......@@ -6,10 +7,13 @@ class Announcement(models.Model):
title = models.CharField(max_length = 50)
body = models.TextField(max_length = 700, null = True, blank = True)
author = models.ForeignKey(WidgetUser, on_delete = models.CASCADE)
pub_datetime = models.DateTimeField('publication date and time')
pub_datetime = models.DateTimeField('publication date and time', auto_now_add=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('announcementboard:announcement-details', kwargs={'pk': self.pk})
class Reaction(models.Model):
class Choices(models.TextChoices):
......
{% extends 'base.html' %}
{% block title %} Add Announcement {% endblock %}
{% block header %}
<h1> Add a new announcement: </h1>
{% endblock %}
{% block body %}
<form method='POST'>
{% csrf_token %}
{{ form }}
<input type="Submit" value="Add Announcement">
</form>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %} {{ announcement.title }} {% endblock %}
{% block header %}
<h1> {{ announcement.title }} </h1>
{% endblock %}
{% block body %}
<h4> by {{ announcement.author.first_name }} {{ announcement.author.last_name }}</h4>
<h4>{{ announcement.pub_datetime|date:'m/d/Y, H:i A'}}</h4>
<h4>{{ announcement.body }}</h4>
<ul>
<li>Like: {{ tallies.like|default:0 }}</li>
<li>Love: {{ tallies.love|default:0 }}</li>
<li>Angry: {{ tallies.angry|default:0 }}</li>
</ul>
<a href="{% url 'announcementboard:announcement-edit' announcement.id %}"> Edit Announcement </a>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %} Edit Announcement {% endblock %}
{% block header %}
<h1> Edit announcement: </h1>
{% endblock %}
{% block body %}
<form method='POST'>
{% csrf_token %}
{{ form }}
<input type="Submit" value="Save Changes to Announcement">
</form>
{% endblock %}
{% extends 'base.html' %}
{% block title %} Widget's Announcement Board {% endblock %}
{% block header %}
<h1> Welcome to Widget's Announcement Board! </h1>
{% endblock %}
{% block content %}
<h2> Announcements: </h2><br>
<div>
<ul>
{% for announcement in announcements %}
<li>
<a href="{{ announcement.get_absolute_url }}">
{{ announcement.title }} by {{ announcement.author.first_name }} {{ announcement.author.last_name }}
</a>
</li>
{% endfor %}
</ul>
</div>
<div>
<a href="/announcements/add/"> New Announcement </a>
</div>
<a href="/dashboard/"> Dashboard </a>
<a href="/forum/"> Forum </a>
<a href="/assignments/"> Assignments </a>
<a href="/calendar/"> Calendar </a>
{% endblock %}
\ No newline at end of file
from django.urls import path
from . import views
from .views import (AnnouncementsDetailView, AnnouncementsUpdateView, AnnouncementsCreateView
)
urlpatterns = [
path('', views.index, name='index'),
path('', views.announcement_view, name='announcements'),
path('<int:pk>/details/', AnnouncementsDetailView.as_view(), name='announcement-details'),
path('add/', AnnouncementsCreateView.as_view(), name='announcement-add'),
path('<int:pk>/edit/', AnnouncementsUpdateView.as_view(), name='announcement-edit'),
]
app_name = 'announcementboard'
\ No newline at end of file
from django.http import HttpResponse
from django.shortcuts import render
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from .models import Announcement, Reaction
def index(request):
all_announcements = Announcement.objects.all()
all_reactions = Reaction.objects.all()
welcomeMessage = 'Widget\'s Announcement Board<br><br>Announcements:<br>'
response = ''
for announcement in all_announcements:
reactions = ''
reactions_list = {'Like':0, 'Love':0, 'Angry':0}
for reaction in all_reactions:
if reaction.announcement.title == announcement.title:
reactions_list[reaction.name] = reaction.tally
keys = list(reactions_list.keys())
for key in keys:
reactions += key + ': ' + str(reactions_list[key]) + '<br>'
datetime = announcement.pub_datetime.strftime('%m/%d/%Y, %I:%M %p')
author = announcement.author.first_name + ' '+ announcement.author.last_name
response += announcement.title + ' by ' + author + ' published ' + datetime
response += ':' + '<br>' + announcement.body + '<br>' + reactions + '<br>'
return HttpResponse(welcomeMessage + response)
def announcement_view(request):
announcements = Announcement.objects.order_by('-pub_datetime')
context = {
'announcements': announcements
}
return render(request, 'announcementboard/announcements.html', context)
class AnnouncementsCreateView(CreateView):
model = Announcement
template_name = 'announcementboard/announcement-add.html'
fields = '__all__'
class AnnouncementsUpdateView(UpdateView):
model = Announcement
template_name = 'announcementboard/announcement-edit.html'
fields = '__all__'
class AnnouncementsDetailView(DetailView):
model = Announcement
template_name = 'announcementboard/announcement-details.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
reactions = Reaction.objects.filter(announcement=self.object)
tallies = {
'like': 0,
'love': 0,
'angry': 0
}
for reaction in reactions:
if reaction.name == 'Like':
tallies['like'] += reaction.tally
elif reaction.name == 'Love':
tallies['love'] += reaction.tally
elif reaction.name == 'Angry':
tallies['angry'] += reaction.tally
context['tallies'] = tallies
return context
\ 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