Commit f6700b35 authored by nheoxoz's avatar nheoxoz

finished admin.py that updates views.py

parent add322b7
from django.contrib import admin
from .models import Announcement, Reaction
# Register your models here.
class AnnouncementAdmin(admin.ModelAdmin):
model = Announcement
list_display = ('title', 'body', 'author', 'pub_datetime',)
search_fields = ('title', 'body', 'author', 'pub_datetime',)
list_filter = ('title', 'body', 'author', 'pub_datetime',)
class ReactionAdmin(admin.ModelAdmin):
model = Reaction
list_display = ('name', 'tally', 'announcement',)
search_fields = ('name', 'tally', 'announcement',)
list_filter = ('name', 'tally', 'announcement',)
admin.site.register(Announcement, AnnouncementAdmin)
admin.site.register(Reaction, ReactionAdmin)
# Generated by Django 4.1.7 on 2023-03-04 07:57
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('dashboard', '0001_initial'),
('announcements', '0002_alter_announcement_pub_datetime_alter_reaction_name'),
]
operations = [
migrations.AddField(
model_name='announcement',
name='first_name',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='first_names', to='dashboard.widgetuser'),
),
migrations.AddField(
model_name='announcement',
name='last_name',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='last_names', to='dashboard.widgetuser'),
),
]
# Generated by Django 4.1.7 on 2023-03-04 08:27
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('dashboard', '0001_initial'),
('announcements', '0003_announcement_first_name_announcement_last_name'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='author',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='dashboard.widgetuser'),
),
]
# Generated by Django 4.1.7 on 2023-03-04 10:03
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('dashboard', '0001_initial'),
('announcements', '0004_alter_announcement_author'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='author',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='announcements_author', to='dashboard.widgetuser'),
),
migrations.AlterField(
model_name='reaction',
name='name',
field=models.CharField(choices=[('LIKE', 'Like'), ('LOVE', 'Love'), ('ANGRY', 'Angry')], default='LIKE', max_length=5),
),
]
# Generated by Django 4.1.7 on 2023-03-04 10:42
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('announcements', '0005_alter_announcement_author_alter_reaction_name'),
]
operations = [
migrations.RemoveField(
model_name='announcement',
name='first_name',
),
migrations.RemoveField(
model_name='announcement',
name='last_name',
),
]
from django.db import models
from dashboard.models import WidgetUser
class Announcement(models.Model):
title = models.CharField(max_length=100)
body = models.TextField()
author = models.CharField(max_length=100)
author = models.ForeignKey(
'dashboard.WidgetUser',
on_delete=models.CASCADE,
related_name='announcements_author'
)
pub_datetime = models.DateTimeField()
first_name = models.ForeignKey(WidgetUser, on_delete=models.CASCADE)
last_name = models.ForeignKey(WidgetUser, on_delete=models.CASCADE)
def __str__(self):
return '''{} by {} {} published {}\n{}'''.format(
return '''{} by {} published {}\n{}'''.format(
self.title,
self.first_name,
self.last_name,
self.author,
self.pub_datetime,
self.body,
)
......@@ -22,13 +22,13 @@ class Announcement(models.Model):
class Reaction(models.Model):
REACTION_LIKE = "Like"
REACTION_LOVE = "Love"
REACTION_ANGRY = "Angry"
REACTION_LIKE = "LIKE"
REACTION_LOVE = "LOVE"
REACTION_ANGRY = "ANGRY"
REACTION_CHOICES = [
(REACTION_LIKE, "LIKE"),
(REACTION_LOVE, "LOVE"),
(REACTION_ANGRY, "ANGRY"),
(REACTION_LIKE, "Like"),
(REACTION_LOVE, "Love"),
(REACTION_ANGRY, "Angry"),
]
name = models.CharField(
......@@ -40,8 +40,4 @@ class Reaction(models.Model):
announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE)
def __str__(self):
return '''Like: {}\nLove: {}\nAngry: {}'''.format(
self.tally,
self.tally,
self.tally,
)
return self.name
from django.http import HttpResponse
from .models import Announcement, Reaction
def index(request):
return HttpResponse('This came from the announcements view')
return_string = "<p>Widget's Announcement Board</p>Announcements:<ul style='list-style: none; padding: 0; margin: 0;'>"
for a in Announcement.objects.all():
return_string += '''<li>{} by {} {} published {}</li>{}<br>'''.format(
a.title,
a.author.first_name,
a.author.last_name,
a.pub_datetime.strftime('%m/%d/%Y, %H:%M %p:'),
a.body,
)
likeTally = 0
loveTally = 0
angryTally = 0
for r in Reaction.objects.all():
if r.name == "LIKE" and r.announcement == a:
likeTally += r.tally
if r.name == "LOVE" and r.announcement == a:
loveTally += r.tally
if r.name == "ANGRY" and r.announcement == a:
angryTally += r.tally
return_string += '''Like: {}<br>Love: {}<br>Angry: {}<br><br>'''.format(
likeTally,
loveTally,
angryTally,
)
return_string += '</ul>'
html_string = '<html><body>{}</body></html>'.format(return_string)
return HttpResponse(html_string)
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