greatly improved announcement functionality and display. Added new Reaction...

greatly improved announcement functionality and display. Added new Reaction model as well as added a new view to Announcements.
parent 57130a46
...@@ -2,5 +2,21 @@ from django.contrib import admin ...@@ -2,5 +2,21 @@ from django.contrib import admin
# Register your models here. # Register your models here.
from .models import Announcement from .models import Announcement
from .models import Reaction
admin.site.register(Announcement) class AnnouncementAdmin(admin.ModelAdmin):
\ No newline at end of file model = Announcement
search_fields = ('announcement_title',)
list_display = ('announcement_title', 'pub_date', 'author')
list_filter = ('announcement_title', 'pub_date',)
class ReactionAdmin(admin.ModelAdmin):
model = Reaction
search_fields = ('announcement',)
list_display = ('announcement', 'reaction_name', 'tally')
list_filter = ('announcement',)
admin.site.register(Announcement, AnnouncementAdmin)
admin.site.register(Reaction, ReactionAdmin)
\ No newline at end of file
# Generated by Django 3.2.12 on 2022-04-06 17:32
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('homepage', '0002_widgetuser_email_widgetuser_id_num_department'),
('announcements', '0002_alter_announcement_announcement_body'),
]
operations = [
migrations.AddField(
model_name='announcement',
name='author',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='homepage.widgetuser'),
),
migrations.AlterField(
model_name='announcement',
name='announcement_title',
field=models.CharField(max_length=100),
),
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=5)),
('tally', models.IntegerField(default=0)),
('announcement', models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='announcements.announcement')),
],
),
]
from django.db import models from django.db import models
from homepage.models import WidgetUser
# Create your models here. # Create your models here.
class Announcement(models.Model): class Announcement(models.Model):
announcement_title = models.CharField(max_length=50) announcement_title = models.CharField(max_length=100)
announcement_body = models.CharField(max_length=400) announcement_body = models.CharField(max_length=400)
pub_date = models.DateTimeField("date published") pub_date = models.DateTimeField("date published")
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, default=1)
def __str__(self):
return self.announcement_title
class Reaction(models.Model):
announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE, default=1)
LIKE = 'LIKE'
LOVE = 'LOVE'
ANGRY = 'ANGRY'
REACTION_CHOICES = [(LIKE, 'Like'), (LOVE, 'Love'), (ANGRY, 'Angry')]
reaction_name = models.CharField(max_length=5, choices=REACTION_CHOICES, default=LIKE)
tally = models.IntegerField(default=0)
def possible_choices(self):
return self.reaction_name in {self.LIKE, self.LOVE, self.ANGRY}
from django.http import HttpResponse from django.http import HttpResponse
from . models import Announcement, Reaction
from homepage.models import WidgetUser
from django.shortcuts import render
# Create your views here. # Create your views here.
def index(request): def index(request):
return HttpResponse("This is the Announcement Board!")
announcement_objects = Announcement.objects.all()
response = "<h1>~~~Announcements~~~</h1> <br><br>"
for announcement in announcement_objects:
like = announcement.reaction_set.get(reaction_name="LIKE").tally
love = announcement.reaction_set.get(reaction_name="LOVE").tally
angry = announcement.reaction_set.get(reaction_name="ANGRY").tally
response = (response + f"{announcement.announcement_title} by "
+ f"{announcement.author.first_name} {announcement.author.last_name}<br>"
+ f"dated {announcement.pub_date}: <br><br> {announcement.announcement_body}<br><br>"
+ f"Like: {like} <br> Love: {love} <br> Angry: {angry} <br><br><br>")
return HttpResponse(response)
\ No newline at end of file
No preview for this file type
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