Commit 5113bb1b authored by Junho Park's avatar Junho Park

Merge branch 'park/announcement'

parents a4e83ea8 5bd8ffcd
from django.contrib import admin from django.contrib import admin
from .models import Announcement from .models import Announcement, Reaction
# Register your models here. # Register your models here.
class AnnouncementAdmin(admin.ModelAdmin):
model = Announcement
admin.site.register(Announcement) admin.site.register(Announcement)
class ReactionAdmin(admin.ModelAdmin):
model = Reaction
admin.site.register(Reaction)
# Generated by Django 3.2.12 on 2022-04-07 16:34
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('announcements', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Reaction',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('reaction_name', models.CharField(choices=[('LV', 'Love'), ('LK', 'Like'), ('AG', 'Angry')], default=None, max_length=2)),
('tally', models.IntegerField()),
],
),
migrations.AddField(
model_name='announcement',
name='reaction',
field=models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.CASCADE, to='announcements.reaction'),
),
]
# Generated by Django 3.2.12 on 2022-04-07 17:07
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('homepage', '0002_auto_20220407_0505'),
('announcements', '0002_auto_20220407_1634'),
]
operations = [
migrations.AddField(
model_name='announcement',
name='author',
field=models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.CASCADE, to='homepage.widgetuser'),
),
migrations.AlterField(
model_name='reaction',
name='reaction_name',
field=models.CharField(choices=[('Love', 'Love'), ('Like', 'Like'), ('Angry', 'Angry')], default=None, max_length=5),
),
]
# Generated by Django 3.2.12 on 2022-04-07 19:42
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('announcements', '0003_auto_20220407_1707'),
]
operations = [
migrations.RemoveField(
model_name='announcement',
name='reaction',
),
migrations.AddField(
model_name='reaction',
name='announcement',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='announcements.announcement'),
),
migrations.AlterField(
model_name='reaction',
name='tally',
field=models.IntegerField(default=0),
),
]
# Generated by Django 3.2.12 on 2022-04-08 02:38
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('announcements', '0004_auto_20220407_1942'),
]
operations = [
migrations.RemoveField(
model_name='reaction',
name='tally',
),
]
# Generated by Django 3.2.12 on 2022-04-08 03:56
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0005_remove_reaction_tally'),
]
operations = [
migrations.AddField(
model_name='reaction',
name='tally',
field=models.IntegerField(default=0),
),
]
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=100) announcement_title = models.CharField(max_length=100)
announcement_body = models.CharField(max_length=1000) announcement_body = models.CharField(max_length=1000)
pub_date = models.DateTimeField('date published') pub_date = models.DateTimeField('date published')
author = models.ForeignKey(
'homepage.WidgetUser',
on_delete=models.CASCADE,
default=None,
null=True
)
def __str__(self): def __str__(self):
return self.announcement_title return self.announcement_title
class Reaction(models.Model):
LOVE = 'Love'
LIKE = 'Like'
ANGRY = 'Angry'
REACTION_CHOICES = [
(LOVE, 'Love'),
(LIKE, 'Like'),
(ANGRY, 'Angry'),
]
reaction_name = models.CharField(
max_length=5,
choices=REACTION_CHOICES,
default=None,
)
announcement = models.ForeignKey(
'Announcement',
on_delete=models.CASCADE,
null=True,
)
tally = models.IntegerField(default=0)
def __str__(self):
return self.reaction_name
def count(self):
return self.tally
from django.shortcuts import render
from django.http import HttpResponse from django.http import HttpResponse
from .models import Announcement, Reaction
# Create your views here.
def index(request): def index(request):
return HttpResponse('This is the Announcement Board!') announcement = Announcement.objects.all()
html = '<html><body><h3>ANNOUNCEMENT POSTS:</h3>'
for a in announcement:
html += "{} by {} {} dated {}:<br>{}<br>".format(
a.announcement_title,
a.author.first_name,
a.author.last_name,
a.pub_date,
a.announcement_body,
)
reaction = Reaction.objects.filter(announcement=a.id)
html += "Like: {}<br>Love: {}<br>Angry: {}".format(
reaction.filter(reaction_name__exact="Like").count,
reaction.filter(reaction_name__exact="Love").count,
reaction.filter(reaction_name__exact="Angry").count,
)
html += '<br>'
html += '</body></html>'
return HttpResponse(html)
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