Commit 946c0fd4 authored by Lay Villanueva's avatar Lay Villanueva

Updated models and views of announcementboard

Added related_name for foreign keys, changed pub_datetime to be an automatic timestamp. Updated views of announcement board to match the necessary format.
parent 6efaccdb
# Generated by Django 4.1.7 on 2023-03-04 14:43
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
('dashboard', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Announcement',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=255)),
('body', models.TextField()),
('pub_datetime', models.DateTimeField()),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='dashboard.widgetuser')),
],
),
migrations.CreateModel(
name='Reaction',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255)),
('tally', models.IntegerField()),
('announcement', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='announcementboard.announcement')),
],
),
]
# Generated by Django 4.1.7 on 2023-03-04 16:14
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('dashboard', '0001_initial'),
('announcementboard', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='author',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='announcements', to='dashboard.widgetuser'),
),
migrations.AlterField(
model_name='announcement',
name='pub_datetime',
field=models.DateTimeField(verbose_name='Posted On'),
),
migrations.AlterField(
model_name='reaction',
name='announcement',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='reacts', to='announcementboard.announcement'),
),
migrations.AlterField(
model_name='reaction',
name='name',
field=models.CharField(choices=[('Like', 'Like'), ('Love', 'Love'), ('Angry', 'Angry')], max_length=255),
),
migrations.AlterField(
model_name='reaction',
name='tally',
field=models.IntegerField(default=0),
),
]
# Generated by Django 4.1.7 on 2023-03-04 17:07
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcementboard', '0002_alter_announcement_author_and_more'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='pub_datetime',
field=models.DateTimeField(auto_now_add=True, verbose_name='Posted On'),
),
migrations.AlterField(
model_name='reaction',
name='tally',
field=models.IntegerField(blank=True, default=0, null=True),
),
]
......@@ -8,8 +8,8 @@ from dashboard.models import WidgetUser
class Announcement(models.Model):
title = models.CharField(max_length=255)
body = models.TextField()
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE)
pub_datetime = models.DateTimeField()
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, related_name="announcements")
pub_datetime = models.DateTimeField(verbose_name="Posted On", auto_now_add=True)
def __str__(self):
return '{}'.format(self.title)
......@@ -19,9 +19,10 @@ class Announcement(models.Model):
class Reaction(models.Model):
name = models.CharField(max_length=255)
tally = models.IntegerField()
announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE)
reaction_names = [('Like','Like'),('Love','Love'),('Angry','Angry')]
name = models.CharField(choices=reaction_names, max_length=255)
tally = models.IntegerField(default=0, blank=True, null=True)
announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE, related_name="reacts")
def __str__(self):
return '{}'.format(self.name)
......
from django.shortcuts import render
from django.http import HttpResponse
from .models import Announcement, Reaction
# Create your views here.
def index(request):
return HttpResponse("Widget's Announcement Board <br> Announcements: <br>")
output = "<h1><b> Widget's Announcement Board </b></h1> <br> <h3>Announcements:</h3>"
for announcement in Announcement.objects.all():
output += '{} by {} {} published {}:<br> {} <br>'.format(
announcement.title, announcement.author.first_name, announcement.author.last_name, announcement.pub_datetime.strftime("%m/%d/%Y %I:%M %p"), announcement.body)
reactList = announcement.reacts.all()
for react in reactList:
output += '{}: {} <br>'.format(react.name, react.tally)
output += '<br>'
return HttpResponse(output)
......@@ -107,6 +107,7 @@ AUTH_PASSWORD_VALIDATORS = [
]
# Internationalization
# https://docs.djangoproject.com/en/4.1/topics/i18n/
......@@ -118,7 +119,6 @@ USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/
......
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