Commit bbab1cb3 authored by Jayson Lim's avatar Jayson Lim

Updated model class and its attributes, also updated the implementation of view

parent 7cc4c2cc
from django.db import models from django.db import models
from dashboard.models import WidgetUser from dashboard.models import WidgetUser
class Announcement(models.Model): class Announcement(models.Model):
title = models.CharField(max_length=250, default="") title = models.CharField(max_length=250, default="")
body = models.TextField(null=True, blank=True) body = models.TextField(null=True, blank=True)
...@@ -8,9 +9,9 @@ class Announcement(models.Model): ...@@ -8,9 +9,9 @@ class Announcement(models.Model):
WidgetUser, WidgetUser,
null=True, null=True,
default=True, default=True,
on_delete=models.SET_DEFAULT, on_delete=models.CASCADE,
related_name='announcements',
) )
pub_datetime = models.DateTimeField(auto_now_add=True) pub_datetime = models.DateTimeField(auto_now_add=True)
def __str__(self): def __str__(self):
...@@ -20,14 +21,18 @@ class Announcement(models.Model): ...@@ -20,14 +21,18 @@ class Announcement(models.Model):
return self.pub_datetime.strftime('%m/%d/%Y %I:%M %p') return self.pub_datetime.strftime('%m/%d/%Y %I:%M %p')
class Reaction(models.Model): class Reaction(models.Model):
name = models.CharField(max_length=5, default='') name = models.CharField(max_length=5, choices=[('Like', 'Like'),
('Love', 'Love'),
('Angry', 'Angry')], default='Like')
tally = models.PositiveIntegerField(default=0) tally = models.PositiveIntegerField(default=0)
annoucement = models.ForeignKey( annoucement = models.ForeignKey(
Announcement, Announcement,
null=True, null=True,
default=True, default=True,
on_delete=models.SET_DEFAULT on_delete=models.CASCADE,
related_name='reactions'
) )
def __str__(self): def __str__(self):
return self.name return self.name
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse from django.http import HttpResponse
from .models import Announcement from .models import Announcement
from .models import Reaction from dashboard.models import WidgetUser
def index(request): def index(request):
...@@ -13,26 +13,19 @@ def index(request): ...@@ -13,26 +13,19 @@ def index(request):
body = "<h2>Announcements:</h2>" body = "<h2>Announcements:</h2>"
announcements = Announcement.objects.all() for x in Announcement.objects.all():
reactions = Reaction.objects.all() reaction = x.reactions.all()
for i in range(0, len(announcements), 1):
announcement = announcements[i]
body += "<p style='border: 2px solid gray;\ body += "<p style='border: 2px solid gray;\
border-radius:5px;\ border-radius:5px;\
padding:20px 30px;'>\ padding:20px 30px;'>\
{} by {} published {}:\ {} by {} published {}:\
<br>\ <br>\
{}\ {}\
</p>".format(announcement.title, announcement.author, </p>".format(x.title, x.author,
announcement.format_pub_datetime(), announcement.body) x.format_pub_datetime(), x.body)
for y in reaction:
for j in range(i * 3, (i + 1) * 3):
if j >= len(reactions):
break
reaction = reactions[j]
body += "<p>{}: {}\ body += "<p>{}: {}\
</p>".format(reaction.name, reaction.tally) </p>".format(y.name, y.tally)
body += '<p>&nbsp;</p>' body += '<p>&nbsp;</p>'
......
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