Commit a1716a06 authored by Michael Lopez's avatar Michael Lopez

fixed on how to display the number of reactions on views.py, and added announcement-add.html

parent b096622d
......@@ -16,16 +16,13 @@ class Announcement(models.Model):
class Reaction(models.Model):
LIKE = 'Like'
LOVE = 'Love'
ANGR = 'Angry'
name_choices = [
(LIKE, 'Like'),
(LOVE, 'Love'),
(ANGR, 'Angry')
("Like", 'Like'),
("Love", 'Love'),
("Angry", 'Angry')
]
name = models.CharField(
max_length=5,
max_length=10,
choices=name_choices,
)
......
......@@ -5,16 +5,16 @@
{% endblock %}
{% block content %}
<h2>{{object.title}}</h2> <br>
<h3> by {{object.author.first_name}} {{object.author.last_name}}</h3> <br>
<p>{{pubpost.datetime}}</p> <br> <br> <br>
<li>{{object.body}}</li>
<br><br>
Like: {{like_count}} <br>
Love: {{love_count}} <br>
Angry: {{angry_count}}
<h2>{{object.title}}</h2>
<h3> by {{object.author.first_name}} {{object.author.last_name}}</h3>
<p>{{object.pub_datetime}}</p>
<br>
<p>{{object.body}}</p>
<br>
<p>Like: {{like_count}} <br><br>
Love: {{love_count}} <br><br>
Angry: {{angry_count}}</p>
<br>
<form action='edit'>
<input type="submit" value="Edit Announcement"/>
</form>
......
......@@ -6,17 +6,17 @@ Widget's Announcement Board
{% block content %}
<h2>Welcome to Widget's Announcement Board!</h2>
<br>
<h3>Announcements:</h3>
<ul>
{% for object in announcement_list %}
<a href="{{object.get_absolute_url}}"><li>{{object.title}} by {{object.author.first_name}}, {{object.author.last_name}}</li></a>
<p>
<a href="{{object.get_absolute_url}}">{{object.title}} by {{object.author.first_name}} {{object.author.last_name}}</a>
</p>
{% endfor %}
</ul>
<br><br>
<br>
<form action={% url 'announcements:announcement-add' %}>
<form action={% url 'announcements:announcement_add' %}>
<input type="submit" value="New Announcement">
</form>
......
import pytz # need to do "pip install pytz" for this to be activated
from .forms import *
from .models import Announcement, Reaction
from django.utils import timezone
from django.views.generic import ListView, DetailView, CreateView, UpdateView
from django.db.models import Sum
from django.shortcuts import render
......@@ -11,12 +9,6 @@ def pageview(request):
model_object = Announcement.objects.order_by("-pub_datetime")
return render(request, "announcements/announcements.html", context={"announcement_list":model_object})
def UTC_to_PHTZ(utctime):
ph_tz = "%m/%d/%Y, %I:%M %p"
utc = utctime.replace(tzinfo=pytz.UTC)
local_tz = utc.astimezone(timezone.get_current_timezone)
return local_tz.strftime(ph_tz)
class AnnouncementListView(ListView):
model = Announcement
......@@ -26,15 +18,13 @@ class AnnouncementDetailView(DetailView):
model = Announcement
template_name = "announcements/announcement-detail.html"
def get_announcement_detail(self, **kwargs):
announcement = self.get_object()
context = super().get_announcement_detail(**kwargs)
reactions = Reaction.objects.filter(announcement=announcement)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
reactions = Reaction.objects.filter(announcement=self.object)
context['like_count'] = reactions.filter(name='Like').aggregate(total=Sum('tally'))['total'] or 0
context['love_count'] = reactions.filter(name='Love').aggregate(total=Sum('tally'))['total'] or 0
context['angry_count'] = reactions.filter(name='Angry').aggregate(total=Sum('tally'))['total'] or 0
context['pubpost_datetime'] = UTC_to_PHTZ(announcement.pub_datetime)
return context
class AnnouncementCreateView(CreateView):
......@@ -46,7 +36,7 @@ class AnnouncementCreateView(CreateView):
form.save()
return response
def get_success_url(self):
return reverse("announcement_board:announcement_details", args=(self.object.pk,))
return reverse("announcements:announcement_details", args=(self.object.pk,))
class AnnouncementEditView(UpdateView):
model = Announcement
......@@ -57,5 +47,5 @@ class AnnouncementEditView(UpdateView):
form.save()
return response
def get_success_url(self):
return reverse("announcement_board:announcement_details", args=(self.object.pk,))
return reverse("announcements:announcement_details", args=(self.object.pk,))
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