Commit b7a646e6 authored by Junho Park's avatar Junho Park

feat: update views and model and add templates to views in announcement app

parent d489fdef
No preview for this file type
No preview for this file type
# Generated by Django 4.0.4 on 2022-05-26 07:04
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('announcements', '0006_reaction_tally'),
]
operations = [
migrations.AlterField(
model_name='reaction',
name='announcement',
field=models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='reactions', to='announcements.announcement'),
),
]
...@@ -33,8 +33,10 @@ class Reaction(models.Model): ...@@ -33,8 +33,10 @@ class Reaction(models.Model):
) )
announcement = models.ForeignKey( announcement = models.ForeignKey(
'Announcement', Announcement,
related_name="reactions",
on_delete=models.CASCADE, on_delete=models.CASCADE,
default=None,
null=True, null=True,
) )
......
h1 {
color: #15133C;
font-weight: bold;
font-size: 50px;
font-family: 'Courier New', Courier, monospace;
margin-bottom: 0px;
text-align: center;
}
h2 {
font-family: 'Courier New', Courier, monospace;
color: #73777B;
text-align: center;
}
li {
font-family: 'Courier New', Courier, monospace;
color: #73777B;
text-align: center;
display: inline-block;
}
h3 {
color: #EC994B;
font-family: 'Courier New', Courier, monospace;
text-align: center;
}
ol {
text-align: center;
}
p {
color:#15133C;
font-weight: bold;
font-family: 'Courier New', Courier, monospace;
text-align: center;
}
body {
background-color: #F1EEE9;
padding-left: 50px;
padding-right: 50px;
text-align: center;
}
a {
color: #EC994B;
font-size: 20px;
font-family: 'Courier New', Courier, monospace;
}
img {
display: block;
width: 20%;
margin-left: auto;
margin-right: auto;
}
\ No newline at end of file
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="{% static "announcements/style.css" %}">
<title>Widget's Announcements</title>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
{% extends "announcements/base.html"%}
{% load static %}
{% block content %}
<h1>{{ announcement.announcement_title }}</h1>
<p>by {{ announcement.author.first_name }} {{ announcement.author.last_name }}, {{ announcement.pub_date }}</p>
<br/>
<h3>{{ announcement.announcement_body }}</h3>
<br/>
<div>
{% if announcement.id == 1 %}
<img src = "{% static 'announcements/images/headache.jpeg' %}" alt = ":o">
{% elif announcement.id == 2 %}
<img src = "{% static 'announcements/images/backpain.jpeg' %}" alt = "hehe>">
{% elif announcement.id == 3 %}
<img src = "{% static 'announcements/images/fear.jpeg' %}" alt = "hehe>">
{% elif announcement.id == 4 %}
<img src = "{% static 'announcements/images/sore_eyes.jpeg' %}" alt = "profile image">
{% endif %}
</div>
<br/>
<br/>
<h2>Reactions:</h2>
{% if not like and love and angry %}
No Reaction
{% else %}
<li>
Like: {{like}}<br/>
Love: {{love}}<br/>
Angry: {{angry}}
</li>
{% endif %}
{% endblock %}
{% extends "announcements/base.html" %}
{% block content %}
<h1>Announcement Board</h1>
<br/>
<h2>Important announcements:</h2>
{% if announcement_list %}
{% for announcement in announcement_list %}
<ol><a href="/announcements/{{ announcement.id }}/details/">{{ announcement.announcement_title }} by {{ announcement.author.first_name }} {{ announcement.author.last_name }} dated {{ announcement.pub_date }}</a></ol>
{% endfor %}
{% else %}
<p>No announcements are available.</p>
{% endif %}
{% endblock %}
from django.urls import path from django.urls import path
from . import views
from .views import index
urlpatterns = [ urlpatterns = [
path('', index, name='index'), path("", views.index, name='Announcements'),
path("<int:announcement_id>/details/", views.details, name="details")
] ]
app_name = 'announcements' app_name = 'announcements'
from django.http import HttpResponse from django.http import HttpResponse, Http404
from .models import Announcement, Reaction from .models import Announcement, Reaction
from django.shortcuts import render
# Create your views here. # Create your views here.
def index(request):
announcement = Announcement.objects.all()
html = '<html><body><h3>ANNOUNCEMENT POSTS:</h3>'
for a in announcement:
html += "{} by {} {} dated {}:<br>{}<br>".format(
a.announcement_title,
a.author.first_name,
a.author.last_name,
a.pub_date,
a.announcement_body
)
reaction = Reaction.objects.filter(announcement=a.id) # announcements/
html += "Like: {}<br>Love: {}<br>Angry: {}".format( def index(request):
announcement_list = Announcement.objects.order_by("pub_date")
reaction.filter(reaction_name__exact="Like").count(), context = {
reaction.filter(reaction_name__exact="Love").count(), "announcement_list": announcement_list,
reaction.filter(reaction_name__exact="Angry").count() }
) return render(request, "announcements/index.html", context)
# announcements/<announncements_id>/details
def details(request, announcement_id):
try:
announcement = Announcement.objects.get(pk=announcement_id)
#use .tally instead of .count() if preset tally number is needed
#.count() returns number of reaction model not the preset integer
like = announcement.reactions.filter(reaction_name__exact="Like").count()
love = announcement.reactions.filter(reaction_name__exact="Love").count()
angry = announcement.reactions.filter(reaction_name__exact="Angry").count()
except Announcement.DoesNotExist:
raise Http404("Announcement does not exist.")
return render(request, "Announcements/detail.html", {"announcement": announcement, "angry": angry, "love": love, "like":like})
html += '<br>'
html += '</body></html>'
return HttpResponse(html)
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