Commit 24e86a9b authored by Bianca Aguilar's avatar Bianca Aguilar

Created Announcements models + corresponding views for this

parent 9f2f14a0
from django.contrib import admin
from .models import Announcement
from .models import Announcement, Reaction
# Register your models here.
class AnnouncementAdmin(admin.ModelAdmin):
model = Announcement
admin.site.register(Announcement, AnnouncementAdmin)
\ No newline at end of file
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: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),
),
]
......@@ -11,14 +11,44 @@ class Announcement(models.Model):
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 = [
('Like', 'Like'),
('Love', 'Love'),
('Angry', 'Angry'),
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
)
\ No newline at end of file
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
No preview for this file type
......@@ -8,10 +8,11 @@ def index(request):
final_list = ''
for p in range(len(Post.objects.all())):
final_list += '{}'.format(Post.objects.get(pk=p+1).post_detail)
reply_list = Post.objects.get(pk=p+1).comments.all()
reply_list = list(Reply.objects.filter(post=Post.objects.get(pk=p+1)))
for r in range(len(reply_list)):
final_list += '{}'.format(Reply.objects.get(post__exact=Post.objects.get(pk=p+1)).reply_detail)
return final_list
html = f'''
......
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