Commit 39490954 authored by Nicolas Reichert's avatar Nicolas Reichert

saving for later

parent ee928773
from django.contrib import admin
from .models import Announcement
from .models import Announcement, Reaction
# Register your models here.
class AnnouncementAdmin(admin.ModelAdmin):
model = Announcement
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 4.0.3 on 2022-04-05 06:41
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('homepage', '0003_widgetuser_department'),
('announcements', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='announcement',
name='author',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='homepage.widgetuser'),
preserve_default=False,
),
migrations.CreateModel(
name='Reaction',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('tally', models.IntegerField()),
('announcement', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='announcements.announcement')),
],
),
]
# Generated by Django 4.0.3 on 2022-04-05 06:49
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0002_announcement_author_reaction'),
]
operations = [
migrations.AddField(
model_name='reaction',
name='reaction_name',
field=models.CharField(choices=[('LK', 'Like'), ('LV', 'Love'), ('AG', 'Angry')], default='Like', max_length=2),
),
]
......@@ -6,3 +6,49 @@ class Announcement(models.Model):
announcement_title = models.CharField(max_length=50)
announcement_body = models.CharField(max_length=500)
pub_date = models.DateTimeField("Date Published", auto_now=True)
author = models.ForeignKey(
'homepage.WidgetUser',
on_delete=models.CASCADE
)
def __str__(self):
return '{}'.format(self.announcement_title)
@property
def announcement_detail(self):
announcement = '<br>{} by {} dated {}:'.format(self.announcement_title, self.author.forum_name, self.pub_date)
announcement += '<br>{}'.format(self.announcement_body)
return announcement
class Reaction(models.Model):
reaction_name_choices = [
('LK', 'Like'),
('LV', 'Love'),
('AG', 'Angry'),
]
reaction_name = models.CharField(
max_length=2,
choices=reaction_name_choices,
default='Like',
)
tally = models.IntegerField()
announcement = models.ForeignKey(
Announcement,
on_delete=models.CASCADE,
related_name="reacts",
)
def __str__(self):
return '{}'.format(self.reaction_name)
@property
def reaction_detail(self):
reaction = '<br>{}: {}'.format(self.reaction_name, self.tally)
return reaction
@property
def reaction_tally(self):
return self.tally
from django.shortcuts import render
from django.http import HttpResponse
from .models import Announcement, Reaction
# Create your views here.
def index(request):
return HttpResponse("This is the Announcement Board!")
\ No newline at end of file
def announcement_list():
final_list = ''
for p in range(len(Announcement.objects.all())):
final_list += '{}'.format(Announcement.objects.get(pk=p+1).announcement_detail)
reaction_list = list(Reaction.objects.filter(announcement=Announcement.objects.get(pk=p+1)))
lk_tally = 0
lv_tally = 0
ag_tally = 0
for r in range(len(reaction_list)):
if str(reaction_list[r]) == 'LK':
lk_tally += reaction_list[r].reaction_tally
elif str(reaction_list[r]) == 'LV':
lv_tally += reaction_list[r].reaction_tally
elif str(reaction_list[r]) == 'AG':
ag_tally += reaction_list[r].reaction_tally
final_list += f'<br>Like: {lk_tally}'
final_list += f'<br>Love: {lv_tally}'
final_list += f'<br>Angry: {ag_tally}<br>'
return final_list
html = f'''
<html>
<body>
<h1>ANNOUNCEMENTS:</h1>
<main>
<p>{announcement_list()}</p>
</main>
</body>
</html>
'''
return HttpResponse(html)
\ No newline at end of file
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