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