Commit e521c58b authored by Ysabella Panghulan's avatar Ysabella Panghulan

Merge branch 'announcementsv2'

parents fcc9f836 92b62429
from django.db import models from django.db import models
from django.urls import reverse
from dashboard.models import WidgetUser from dashboard.models import WidgetUser
...@@ -6,11 +7,14 @@ class Announcement(models.Model): ...@@ -6,11 +7,14 @@ class Announcement(models.Model):
title = models.CharField(max_length = 50) title = models.CharField(max_length = 50)
body = models.TextField(max_length = 700, null = True, blank = True) body = models.TextField(max_length = 700, null = True, blank = True)
author = models.ForeignKey(WidgetUser, on_delete = models.CASCADE) 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): def __str__(self):
return self.title return self.title
def get_absolute_url(self):
return reverse('announcementboard:announcement-details', kwargs={'pk': self.pk})
class Reaction(models.Model): class Reaction(models.Model):
class Choices(models.TextChoices): class Choices(models.TextChoices):
like = 'Like' like = 'Like'
......
{% 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 django.urls import path
from . import views from . import views
from .views import (AnnouncementsDetailView, AnnouncementsUpdateView, AnnouncementsCreateView
)
urlpatterns = [ 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.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 from .models import Announcement, Reaction
def index(request): def announcement_view(request):
all_announcements = Announcement.objects.all() announcements = Announcement.objects.order_by('-pub_datetime')
all_reactions = Reaction.objects.all() context = {
welcomeMessage = 'Widget\'s Announcement Board<br><br>Announcements:<br>' 'announcements': announcements
response = '' }
for announcement in all_announcements: return render(request, 'announcementboard/announcements.html', context)
reactions = ''
reactions_list = {'Like':0, 'Love':0, 'Angry':0} class AnnouncementsCreateView(CreateView):
for reaction in all_reactions: model = Announcement
if reaction.announcement.title == announcement.title: template_name = 'announcementboard/announcement-add.html'
reactions_list[reaction.name] = reaction.tally fields = '__all__'
keys = list(reactions_list.keys())
for key in keys: class AnnouncementsUpdateView(UpdateView):
reactions += key + ': ' + str(reactions_list[key]) + '<br>' model = Announcement
datetime = announcement.pub_datetime.strftime('%m/%d/%Y, %I:%M %p') template_name = 'announcementboard/announcement-edit.html'
author = announcement.author.first_name + ' '+ announcement.author.last_name fields = '__all__'
response += announcement.title + ' by ' + author + ' published ' + datetime
response += ':' + '<br>' + announcement.body + '<br>' + reactions + '<br>' class AnnouncementsDetailView(DetailView):
return HttpResponse(welcomeMessage + response) 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