Commit 7312d9c6 authored by Alia Lawraine Olay's avatar Alia Lawraine Olay

Merge branch 'olay/announcementboard'

parents 73531f72 3c614762
from django.contrib import admin from django.contrib import admin
from .models import Announcement from .models import Announcement, Reaction
class AnnouncementAdmin(admin.ModelAdmin): class AnnouncementAdmin(admin.ModelAdmin):
model = Announcement model = Announcement
list_display = ('announcement_title', 'announcement_body', 'pub_date') list_display = ('announcement_title', 'announcement_body', 'pub_date', 'author')
class ReactionAdmin(admin.ModelAdmin):
model = Reaction
list_display = ('announcement', 'reaction_name', 'tally')
admin.site.register(Announcement, AnnouncementAdmin) admin.site.register(Announcement, AnnouncementAdmin)
admin.site.register(Reaction, ReactionAdmin)
\ No newline at end of file
...@@ -4,6 +4,38 @@ class Announcement(models.Model): ...@@ -4,6 +4,38 @@ class Announcement(models.Model):
announcement_title = models.CharField(max_length=150) announcement_title = models.CharField(max_length=150)
announcement_body = models.TextField() announcement_body = models.TextField()
pub_date = models.DateField(auto_now=True) pub_date = models.DateField(auto_now=True)
author = models.ForeignKey(
'homepage.WidgetUser',
on_delete=models.CASCADE,
related_name='announcements'
)
def __str__(self): def __str__(self):
return '"{}" published {}'.format(self.announcement_title, self.pub_date) return '"{}" published {}'.format(self.announcement_title, self.pub_date)
class Reaction(models.Model):
announcement = models.ForeignKey(
Announcement,
on_delete=models.CASCADE,
related_name='reactions'
)
REACTION_CHOICES = [
('Like', 'Like'),
('Love', 'Love'),
('Angry', 'Angry')
]
reaction_name = models.CharField(
max_length=5,
choices=REACTION_CHOICES,
default='Like'
)
tally = models.IntegerField(default=1, editable=False)
def save(self):
if self.reaction_name=='Like':
self.tally = self.announcement.reactions.filter(reaction_name='Like').count()+1
elif self.reaction_name=='Love':
self.tally = self.announcement.reactions.filter(reaction_name='Love').count()+1
elif self.reaction_name=='Angry':
self.tally = self.announcement.reactions.filter(reaction_name='Angry').count()+1
return super().save()
\ No newline at end of file
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse from django.http import HttpResponse
from .models import Announcement
def index(request): def index(request):
return HttpResponse("This is the Announcement Board!") heading = 'ANNOUNCEMENTS:<br>'
all_announcements = Announcement.objects.all()
body = ''
for announcement in all_announcements:
count_like = announcement.reactions.filter(reaction_name='Like').count()
count_love = announcement.reactions.filter(reaction_name='Love').count()
count_angry = announcement.reactions.filter(reaction_name='Angry').count()
body += '{} by {} {} dated {}:<br>{}<br>Like: {}<br>Love: {}<br>Angry: {}<br><br>'.format(
announcement.announcement_title, announcement.author.first_name,
announcement.author.last_name, announcement.pub_date,
announcement.announcement_body, count_like,
count_love, count_angry
)
return HttpResponse(heading + body)
\ No newline at end of file
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