Commit b1973695 authored by Bryan Carlo Guanlao's avatar Bryan Carlo Guanlao

Updated announcement views

added listview and detailview along with templates
parent fe23067f
<h1>{{object.announcement_title}}</h1>
<h3>by {{announcement.author.first_name}} {{announcement.author.last_name}} dated
{{announcement.pub_date|date:"d/m/Y"}}</h3>
<body>{{object.announcement_body}}<br><br>
</body>
<body>{% if object.reaction.all %}
Reactions:
{% for reaction in object.reaction.all %}
{% if reaction.reaction_name == "Like" %}
<li>{{reaction}}: {{reaction.tally}}</li>
{% endif %}
{% endfor %}
{% for reaction in object.reaction.all %}
{% if reaction.reaction_name == "Love" %}
<li>{{reaction}}: {{reaction.tally}}</li>
{% endif %}
{% endfor %}
{% for reaction in object.reaction.all %}
{% if reaction.reaction_name == "Angry" %}
<li>{{reaction}}: {{reaction.tally}}</li>
{% endif %}
{% endfor %}
{% endif %}
{% load static %}
<img src="{% static 'announcements/tsukasa.png' %}" alt="image">
</body>
\ No newline at end of file
{% extends 'base.html' %}
{% block content %}
<h1>Announcements</h1>
<ul>
{% for announcement in object_list %}
<li>
<a href="{% url 'announcements:announcement-detail' announcement.id %}">
{{announcement.announcement_title}} by
{{announcement.author.first_name}} {{announcement.author.last_name}} dated
{{announcement.pub_date|date:"d/m/Y"}}</a>
</li>
{% endfor %}
</ul>
{% endblock content %}
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import announcements from .views import AnnouncementsDetailView, AnnouncementsListView
urlpatterns = [ urlpatterns = [
path('', announcements, name='announcements'), path('', AnnouncementsListView.as_view(), name='announcement-list'),
path('<int:pk>/details/', AnnouncementsDetailView.as_view(),
name='announcement-detail'),
] ]
app_name = 'announcements' app_name = 'announcements'
from django.shortcuts import render from django.shortcuts import render
from django.http import Http404, HttpRequest, HttpResponse from django.http import Http404, HttpRequest, HttpResponse
from .models import Announcement, Reaction, WidgetUser from .models import Announcement, Reaction, WidgetUser
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
def get_reactions(a): class AnnouncementsListView(ListView):
reaction = '' model = Announcement
like = Reaction.objects.filter(reaction_name='Like', announcement=a) ordering = ["-pub_date"]
love = Reaction.objects.filter(reaction_name='Love', announcement=a)
angry = Reaction.objects.filter(reaction_name='Angry', announcement=a)
for l in like:
reaction += '{}: '.format(l) + '{}'.format(l.tally) + '<br>'
for l in love:
reaction += '{}: '.format(l) + '{}'.format(l.tally) + '<br>'
for a in angry:
reaction += '{}: '.format(a) + '{}'.format(a.tally) + '<br>'
return reaction
class AnnouncementsDetailView(DetailView):
def announcements(request): model = Announcement
text = '<h1>ANNOUNCEMENTS:</h1>'
for announcement in Announcement.objects.all():
text += (
'{} '.format(announcement)
+ 'by {} {} '.format(announcement.author.first_name,
announcement.author.last_name)
+ 'dated {}').format(announcement.pub_date.strftime('%x')
+ ':<br>'
+ '{}<br>'.format(announcement.announcement_body)
+ get_reactions(announcement)
+ '<br>'
)
return HttpResponse(text)
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