Announcements now has Reactions and Authors. View is also updated to show necessary information

parent e77afe34
from django.contrib import admin
from .models import Announcement
from django.db import models
from .models import Announcement, Reaction
# Register your models here.
admin.site.register(Announcement)
\ No newline at end of file
class ReactionAdminInline(admin.TabularInline):
model = Reaction
class AnnouncementAdmin(admin.ModelAdmin):
model = Announcement
inlines = [ReactionAdminInline]
class ReactionAdmin(admin.ModelAdmin):
model = Reaction
admin.site.register(Announcement, AnnouncementAdmin)
admin.site.register(Reaction, ReactionAdmin)
\ No newline at end of file
# Generated by Django 3.2.12 on 2022-03-31 07:44
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', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='announcement',
name='author',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='author', 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(max_length=50)),
('tally', models.IntegerField()),
('Announcement', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='announcements', to='announcements.announcement')),
],
),
]
# Generated by Django 3.2.12 on 2022-03-31 08:02
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('announcements', '0002_auto_20220331_1544'),
]
operations = [
migrations.RemoveField(
model_name='reaction',
name='Announcement',
),
migrations.AddField(
model_name='announcement',
name='reaction',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='announcements', to='announcements.reaction'),
),
]
# Generated by Django 3.2.12 on 2022-03-31 08:04
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('announcements', '0003_auto_20220331_1602'),
]
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, related_name='announcements', to='announcements.announcement'),
),
]
# Generated by Django 3.2.12 on 2022-03-31 08:10
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('announcements', '0004_auto_20220331_1604'),
]
operations = [
migrations.RemoveField(
model_name='reaction',
name='Announcement',
),
migrations.AddField(
model_name='announcement',
name='Announcement',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='announcements', to='announcements.reaction'),
),
]
# Generated by Django 3.2.12 on 2022-03-31 08:18
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('announcements', '0005_auto_20220331_1610'),
]
operations = [
migrations.RemoveField(
model_name='announcement',
name='Announcement',
),
migrations.AddField(
model_name='announcement',
name='reaction',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='reactions', to='announcements.reaction'),
),
]
# Generated by Django 3.2.12 on 2022-03-31 08:22
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('announcements', '0006_auto_20220331_1618'),
]
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, related_name='announcements', to='announcements.announcement'),
),
]
# Generated by Django 3.2.12 on 2022-03-31 14:38
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0007_auto_20220331_1622'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='pub_date',
field=models.DateField(verbose_name='date published'),
),
]
# Generated by Django 3.2.12 on 2022-03-31 14:39
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0008_alter_announcement_pub_date'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='pub_date',
field=models.DateTimeField(verbose_name='date published'),
),
]
# Generated by Django 3.2.12 on 2022-03-31 14:57
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0009_alter_announcement_pub_date'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='pub_date',
field=models.DateField(default=['%m/%d/%y'], verbose_name='date published'),
),
]
# Generated by Django 3.2.12 on 2022-03-31 15:01
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0010_alter_announcement_pub_date'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='pub_date',
field=models.DateTimeField(verbose_name='date published'),
),
]
# Generated by Django 3.2.12 on 2022-03-31 15:05
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0011_alter_announcement_pub_date'),
]
operations = [
migrations.AddField(
model_name='announcement',
name='date_only',
field=models.DateField(null=True, verbose_name='Date only'),
),
]
# Generated by Django 3.2.12 on 2022-03-31 15:15
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0012_announcement_date_only'),
]
operations = [
migrations.RemoveField(
model_name='announcement',
name='date_only',
),
migrations.AlterField(
model_name='announcement',
name='pub_date',
field=models.DateField(null=True, verbose_name='Date only'),
),
]
# Generated by Django 3.2.12 on 2022-03-31 15:15
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0013_auto_20220331_2315'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='pub_date',
field=models.DateField(auto_now_add=True, null=True, verbose_name='Date only'),
),
]
# Generated by Django 3.2.12 on 2022-03-31 15:16
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0014_alter_announcement_pub_date'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='pub_date',
field=models.DateTimeField(null=True, verbose_name='date published'),
),
]
# Generated by Django 3.2.12 on 2022-03-31 15:17
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0015_alter_announcement_pub_date'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='pub_date',
field=models.DateField(null=True, verbose_name='date published'),
),
]
# Generated by Django 3.2.12 on 2022-03-31 15:21
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0016_alter_announcement_pub_date'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='pub_date',
field=models.DateTimeField(null=True, verbose_name='date published'),
),
]
from django.db import models
from homepage.models import WidgetUser
# Create your models here.
class Announcement(models.Model):
announcement_title = models.CharField(max_length=50)
announcement_body = models.CharField(max_length=500)
pub_date = models.DateTimeField("date published")
pub_date = models.DateTimeField("date published", null=True)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null=True, related_name='author')
def __str__(self):
return self.announcement_title
class Reaction(models.Model):
Announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE, null=True, related_name='announcements')
reaction_name = models.CharField(max_length=50)
tally = models.IntegerField()
def __str__(self):
return self.announcement_title
\ No newline at end of file
return self.reaction_name
from django.http import HttpResponse
from announcements.models import Announcement, Reaction, WidgetUser
# Create your views here.
def index(request):
return HttpResponse("This is the Announcements Board!")
announcementEntries = Announcement.objects.all().count()
n = 1
announcement = "ANNOUNCEMENTS:<br/>"
while(n<=announcementEntries):
announcement += getAnnouncementHeader(n)
announcement += getReactions(n)
n += 1
return HttpResponse(announcement)
# helper function that iterates through every reaction of an announcement and returns it
def getReactions(pk):
announcementReactions = ""
for reaction in Announcement.objects.get(pk=pk).announcements.all():
announcementReactions += "{}: {}<br/>" .format(reaction.reaction_name, reaction.tally)
announcementReactions += "<br/>"
return announcementReactions
# helper function that takes the data in an announcement and creates the header of the announcement
# (First and Second lines)
def getAnnouncementHeader(pk):
announcementFirstLine = ""
announcementSecondLine = ""
announcementHeader = ""
announcementTitle = Announcement.objects.get(pk=pk).announcement_title
authorFirstName = Announcement.objects.get(pk=pk).author.first_name
authorLastName = Announcement.objects.get(pk=pk).author.last_name
publicationDate = Announcement.objects.get(pk=pk).pub_date
announcementBody = Announcement.objects.get(pk=pk).announcement_body
announcementFirstLine = "{} by {} {} dated {}<br/>" .format(announcementTitle, authorFirstName, authorLastName, publicationDate)
announcementSecondLine = "{}<br/>" .format(announcementBody)
announcementHeader += announcementFirstLine + announcementSecondLine
return announcementHeader
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