Commit 1eafa94e authored by Alia Lawraine Olay's avatar Alia Lawraine Olay

Created Reaction model and added author field in Announcement model

parent e6b01d65
from django.contrib import admin
from .models import Announcement
from .models import Announcement, Reaction
class AnnouncementAdmin(admin.ModelAdmin):
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(Reaction, ReactionAdmin)
\ No newline at end of file
......@@ -4,6 +4,38 @@ class Announcement(models.Model):
announcement_title = models.CharField(max_length=150)
announcement_body = models.TextField()
pub_date = models.DateField(auto_now=True)
author = models.ForeignKey(
'homepage.WidgetUser',
on_delete=models.CASCADE,
related_name='announcements'
)
def __str__(self):
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
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