Commit f623ad47 authored by Michael Lopez's avatar Michael Lopez

added announcement-details url and html file, created absolute url function...

added announcement-details url and html file, created absolute url function for each announcement post, and created announcement list and detail view classes
parent 6fe20255
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):
title = models.CharField(max_length=50) title = models.CharField(max_length=50)
...@@ -9,20 +10,23 @@ class Announcement(models.Model): ...@@ -9,20 +10,23 @@ class Announcement(models.Model):
def __str__(self): def __str__(self):
return self.title return self.title
def get_absolute_url(self):
return reverse("announcements:announcement_details", kwargs={"pk": self.pk})
class Reaction(models.Model): class Reaction(models.Model):
LIKE = 'Like' LIKE = 'Like'
LOVE = 'Love' LOVE = 'Love'
ANGR = 'Angry' ANGR = 'Angry'
namechoices = [ name_choices = [
(LIKE, 'Like'), (LIKE, 'Like'),
(LOVE, 'Love'), (LOVE, 'Love'),
(ANGR, 'Angry') (ANGR, 'Angry')
] ]
name = models.CharField( name = models.CharField(
max_length=5, max_length=5,
choices=namechoices, choices=name_choices,
) )
tally = models.IntegerField() tally = models.IntegerField()
......
{% extends "base.html" %}
{% block title %}
{{object.title}}
{% endblock %}
{% block content %}
<h1>{{object.title}}</h1> <br>
<h2> by {{object.author.first_name}} {{object.author.last_name}}</h2> <br>
{{pubpost.datetime}} <br> <br> <br>
<li>{{object.body}}</li>
<br><br>
Like: {{like_count}} <br>
Love: {{love_count}} <br>
Angry: {{angry_count}}
<form action='edit'>
<input type="submit" value="Edit Announcement"/>
</form>
{% endblock %}
from django.urls import path from django.urls import path
from .views import AnnouncementDetailView
from .views import pageview from .views import pageview
urlpatterns = [ urlpatterns = [
path('', pageview, name='pageview') path('', pageview, name='pageview'),
path('<int:pk>/details/', AnnouncementDetailView.as_view(), name="announcement_details"),
] ]
app_name = "announcements" app_name = "announcements"
\ No newline at end of file
import pytz
from .models import Announcement, Reaction from .models import Announcement, Reaction
from django.http import HttpResponse from django.utils import timezone
from django.views.generic import ListView, DetailView
from django.db.models import Sum
def pageview(request): def UTC_to_PHTZ(utctime):
announcements = Announcement.objects.all() ph_tz = "%m/%d/%Y, %I:%M %p"
reactions = Reaction.objects.all() utc = utctime.replace(tzinfo=pytz.UTC)
local_tz = utc.astimezone(timezone.get_current_timezone)
return local_tz.strftime(ph_tz)
string_builder = "Widget's Announcement Board <br> <br> Announcements: <br> " class AnnouncementListView(ListView):
model = Announcement
template_name = "announcements.html"
for announcement in announcements: class AnnouncementDetailView(DetailView):
model = Announcement
template_name = "announcement-detail.html"
string_builder = "{} <br> {} by {} published {} <br> {}".format( def get_announcement_detail(self, **kwargs):
string_builder, announcement = self.get_object()
announcement.title, context = super().get_announcement_detail(**kwargs)
announcement.author, reactions = Reaction.objects.filter(announcement=announcement)
announcement.pub_datetime.strftime("%m/%d/%Y, %I:%M %p:"),
announcement.body
)
for reaction in reactions: context['like_count'] = reactions.filter(name='Like').aggregate(total=Sum('tally'))['total'] or 0
if reaction.announcement == announcement: context['love_count'] = reactions.filter(name='Love').aggregate(total=Sum('tally'))['total'] or 0
string_builder = "{} <br> {}: {}".format( context['angry_count'] = reactions.filter(name='Angry').aggregate(total=Sum('tally'))['total'] or 0
string_builder, context['pubpost_datetime'] = UTC_to_PHTZ(announcement.pub_datetime)
reaction.name, return context
reaction.tally
)
string_builder += "<br>"
return HttpResponse(string_builder)
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