Commit 41907f71 authored by Katrina Bernice Tan's avatar Katrina Bernice Tan

Merge branch 'martin/announcements' into 'master'

Add Reactions Model, Author field in Announcement, and update view

See merge request !9
parents 6628f3ec 9ee771ce
......@@ -2,6 +2,17 @@ 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
class AnnouncementAdmin(admin.ModelAdmin):
model = Announcement
search_fields = ('announcement_title', 'announcement_body', 'author')
list_display = ('announcement_title',)
list_filter = ('author', 'announcement_title')
class ReactionAdmin(admin.ModelAdmin):
model = Reaction
list_display = ('tally',)
admin.site.register(Announcement, AnnouncementAdmin)
admin.site.register(Reaction, ReactionAdmin)
\ No newline at end of file
# Generated by Django 4.0.3 on 2022-03-27 04:16
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('Homepage', '0001_initial'),
('Announcements', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='announcement',
name='author',
field=models.ForeignKey(default=1, 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=5)),
('tally', models.IntegerField()),
('announcement', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='Announcements.announcement')),
],
),
]
from django.db import models
from Homepage.models import WidgetUser
class Announcement(models.Model):
announcement_title = models.CharField(max_length=100)
announcement_body = models.CharField(max_length=500)
pub_date = models.DateTimeField("Date published", auto_now_add=True)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, default=1)
def __str__(self):
return self.announcement_title
class Reaction(models.Model):
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()
announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE)
def __str__(self):
return self.reaction_name
\ No newline at end of file
from django.shortcuts import render
from django.http import HttpResponse
from .models import Announcement, Reaction
def date_month_year(timestamp):
return timestamp.strftime("%d/%m/%Y")
def index(request):
return HttpResponse("This is the Announcement Board!")
\ No newline at end of file
content = "ANNOUNCEMENTS:<br>"
announcements = Announcement.objects.all()
for announcements in announcements:
content += f'{announcements.announcement_title} by {announcements.author.last_name}, {announcements.author.first_name} dated {date_month_year(announcements.pub_date)}: <br>'
content += f'{announcements.announcement_body} <br>'
reactions = announcements.reaction_set.all()
for reactions in reactions:
content += f'{reactions.reaction_name}: {reactions.tally} <br>'
content +='<br>'
return HttpResponse(content)
\ 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