Commit cf8d3ce5 authored by Bryan Carlo Guanlao's avatar Bryan Carlo Guanlao

Merge branch 'guanlao/announcements'

parents fc7602a2 0108d3cb
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):
...@@ -8,4 +8,10 @@ class AnnouncementAdmin(admin.ModelAdmin): ...@@ -8,4 +8,10 @@ class AnnouncementAdmin(admin.ModelAdmin):
list_display = ('announcement_title', 'pub_date') list_display = ('announcement_title', 'pub_date')
class ReactionAdmin(admin.ModelAdmin):
model = Reaction
list_display = ('reaction_name', 'announcement', 'tally')
admin.site.register(Announcement, AnnouncementAdmin) admin.site.register(Announcement, AnnouncementAdmin)
admin.site.register(Reaction, ReactionAdmin)
# Generated by Django 3.2.12 on 2022-03-22 18:58 # Generated by Django 3.2.12 on 2022-04-03 02:52
from django.db import migrations, models from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration): class Migration(migrations.Migration):
...@@ -8,6 +9,7 @@ class Migration(migrations.Migration): ...@@ -8,6 +9,7 @@ class Migration(migrations.Migration):
initial = True initial = True
dependencies = [ dependencies = [
('homepage', '0001_initial'),
] ]
operations = [ operations = [
...@@ -18,6 +20,16 @@ class Migration(migrations.Migration): ...@@ -18,6 +20,16 @@ class Migration(migrations.Migration):
('announcement_title', models.CharField(max_length=50)), ('announcement_title', models.CharField(max_length=50)),
('announcement_body', models.TextField(max_length=500)), ('announcement_body', models.TextField(max_length=500)),
('pub_date', models.DateField(auto_now_add=True)), ('pub_date', models.DateField(auto_now_add=True)),
('author', models.ForeignKey(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'), ('Hate', 'Hate')], default='Like', max_length=10)),
('tally', models.IntegerField()),
('announcement_name', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='announcements.announcement')),
], ],
), ),
] ]
# Generated by Django 3.2.12 on 2022-04-03 02:55
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('announcements', '0001_initial'),
]
operations = [
migrations.RenameField(
model_name='reaction',
old_name='announcement_name',
new_name='announcement',
),
]
from django.db import models from django.db import models
from homepage.models import WidgetUser
REACTION_CHOICES = [('Like', 'Like'), ('Love', 'Love'), ('Hate', 'Hate')]
class Announcement(models.Model): class Announcement(models.Model):
author = models.ForeignKey(
WidgetUser,
on_delete=models.CASCADE,
)
announcement_title = models.CharField(max_length=50) announcement_title = models.CharField(max_length=50)
announcement_body = models.TextField(max_length=500) announcement_body = models.TextField(max_length=500)
pub_date = models.DateField(auto_now_add=True) pub_date = models.DateField(auto_now_add=True)
def __str__(self): def __str__(self):
return '{}'.format(self.announcement_title) return '{}'.format(self.announcement_title)
class Reaction(models.Model):
announcement = models.ForeignKey(
Announcement,
on_delete=models.CASCADE,
)
reaction_name = models.CharField(
max_length=10,
choices=REACTION_CHOICES,
default='Like'
)
tally = models.IntegerField()
def __str__(self):
return '{}'.format(self.reaction_name)
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse from django.http import Http404, HttpRequest, HttpResponse
from .models import Announcement, Reaction, WidgetUser
def get_reactions(a):
reaction = ''
like = Reaction.objects.filter(reaction_name='Like', announcement=a)
love = Reaction.objects.filter(reaction_name='Love', announcement=a)
hate = Reaction.objects.filter(reaction_name='Hate', announcement=a)
for l in like:
reaction += '{}: '.format(l) + '{}'.format(l.tally) + '<br>'
for l in love:
reaction += '{}: '.format(l) + '{}'.format(l.tally) + '<br>'
for h in hate:
reaction += '{}: '.format(h) + '{}'.format(h.tally) + '<br>'
return reaction
def announcements(request): def announcements(request):
return HttpResponse("This is the Announcement Board!") text = 'ANNOUNCEMENTS:<br>'
for announcement in Announcement.objects.all():
text += (
'{} '.format(announcement)
+ 'by {} {} '.format(announcement.author.first_name,
announcement.author.last_name)
+ 'dated {}').format(announcement.pub_date.strftime('%x')
+ ':<br>'
+ '{}<br>'.format(announcement.announcement_body)
+ get_reactions(announcement)
+ '<br>'
)
return HttpResponse(text)
# Generated by Django 4.0.3 on 2022-03-22 03:09 # Generated by Django 3.2.12 on 2022-04-03 02:44
from django.db import migrations, models from django.db import migrations, models
......
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