Commit b131a0ad authored by Neal Berones's avatar Neal Berones

feat: added Reaction model to Announcements app

parent 54c90414
......@@ -2,6 +2,7 @@ from django.contrib import admin
# Register your models here.
from .models import Announcement
from .models import Announcement, Reaction
admin.site.register(Announcement)
\ No newline at end of file
admin.site.register(Announcement)
admin.site.register(Reaction)
# Generated by Django 4.0.3 on 2022-04-04 14:24
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('homepage', '0006_alter_widgetuser_department'),
('announcements', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='announcement',
name='author',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='homepage.widgetuser'),
),
migrations.CreateModel(
name='Reaction',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('reaction_name', models.CharField(choices=[('Like', 'Like'), ('Love', 'Love'), ('Angry', 'Angry')], default='Like', max_length=10)),
('tally', models.IntegerField(default=0)),
('announcement', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='announcements.announcement')),
],
),
]
from django.db import models
from homepage.models import WidgetUser
Like = 'Like'
Love = 'Love'
Angry = 'Angry'
REACTION_CHOICES = ((Like, 'Like'), (Love, 'Love'), (Angry, 'Angry'))
# Create your models here.
class Announcement(models.Model):
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null=True)
announcement_title = models.CharField(max_length=50)
announcement_body = models.CharField(max_length=500)
pub_date = models.DateTimeField("date published")
\ No newline at end of file
pub_date = models.DateTimeField("date published")
class Reaction(models.Model):
reaction_name = models.CharField(max_length=10, choices=REACTION_CHOICES, default=Like)
tally = models.IntegerField(default = 0)
announcement = models.ForeignKey(Announcement, on_delete = models.CASCADE)
from django.http import HttpResponse
from django.shortcuts import render
from .models import WidgetUser, Announcement, Reaction
# Create your views here.
def index(request):
return HttpResponse("This is the Announcement Board!")
\ No newline at end of file
return HttpResponse("This is the Announcement Board!")
def index(request):
announcement_view = 'ANNOUNCEMENTS: '
announcements = Announcement.objects.all()
reactions = Reaction.objects.all()
for a in announcements:
like_tally=0
love_tally=0
angry_tally=0
for r in reactions:
if r.reaction_name==Like and r.announcement==a:
like_tally+=r.tally
elif r.reaction_name==Love and r.announcement==a:
love_tally+=r.tally
elif r.reaction_name==Angry and r.announcement==a:
angry_tally+=r.tally
announcement_view += "<br/> {} by {} {} dated {}: <br/> {} <br/> {} <br/> {} <br/> {}".\
format(a.announcement_title,
a.author.first_name,
a.author.last_name,
a.pub_date,
a.announcement_body,
like_tally,
love_tally,
angry_tally)
return HttpResponse(announcement_view)
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