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 dashboard.models import WidgetUser
from django.urls import reverse
class Announcement(models.Model):
title = models.CharField(max_length=50)
......@@ -9,20 +10,23 @@ class Announcement(models.Model):
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("announcements:announcement_details", kwargs={"pk": self.pk})
class Reaction(models.Model):
LIKE = 'Like'
LOVE = 'Love'
ANGR = 'Angry'
namechoices = [
name_choices = [
(LIKE, 'Like'),
(LOVE, 'Love'),
(ANGR, 'Angry')
]
name = models.CharField(
max_length=5,
choices=namechoices,
choices=name_choices,
)
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 .views import AnnouncementDetailView
from .views import pageview
urlpatterns = [
path('', pageview, name='pageview')
path('', pageview, name='pageview'),
path('<int:pk>/details/', AnnouncementDetailView.as_view(), name="announcement_details"),
]
app_name = "announcements"
\ No newline at end of file
import pytz
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):
announcements = Announcement.objects.all()
reactions = Reaction.objects.all()
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)
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(
string_builder,
announcement.title,
announcement.author,
announcement.pub_datetime.strftime("%m/%d/%Y, %I:%M %p:"),
announcement.body
)
def get_announcement_detail(self, **kwargs):
announcement = self.get_object()
context = super().get_announcement_detail(**kwargs)
reactions = Reaction.objects.filter(announcement=announcement)
for reaction in reactions:
if reaction.announcement == announcement:
string_builder = "{} <br> {}: {}".format(
string_builder,
reaction.name,
reaction.tally
)
string_builder += "<br>"
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
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