Commit cf47e2da authored by iyamiks's avatar iyamiks

Updated models, urls, and views

parent 1a47773a
...@@ -2,6 +2,8 @@ from django.db import models ...@@ -2,6 +2,8 @@ from django.db import models
from django.urls import reverse from django.urls import reverse
from django.utils import timezone
from dashboard.models import WidgetUser from dashboard.models import WidgetUser
...@@ -12,10 +14,40 @@ class Announcement(models.Model): ...@@ -12,10 +14,40 @@ class Announcement(models.Model):
pub_datetime = models.DateTimeField(verbose_name="Posted On", auto_now_add=True) pub_datetime = models.DateTimeField(verbose_name="Posted On", auto_now_add=True)
def __str__(self): def __str__(self):
return '{}'.format(self.title) return '{} by {} {} on {}'.format(
self.title, self.author.first_name, self.author.last_name,
self.pub_datetime.strftime("%m/%d/%Y %I:%M %p"), self.body
)
def getLikeReacts(self):
try:
return '{}'.format(Reaction.objects.get(name="Like", announcement=self).tally)
except:
return '0'
def getLoveReacts(self):
try:
return '{}'.format(Reaction.objects.get(name="Love", announcement=self).tally)
except:
return '0'
def getAngryReacts(self):
try:
return '{}'.format(Reaction.objects.get(name="Angry", announcement=self).tally)
except:
return '0'
def getFormattedDateTime(self):
return '{}'.format(self.pub_datetime.strftime("%m/%d/%Y %I:%M %p"))
def get_absolute_url(self): def get_absolute_url(self):
return reverse('announcement_detail', args=[str(self.title)]) return reverse('announcements:announcement-details', kwargs={'pk':self.pk})
class Reaction(models.Model): class Reaction(models.Model):
......
from django.shortcuts import render
from django.urls import path from django.urls import path
from .views import index from .views import announcements, AnnouncementDetailView, AnnouncementCreateView, AnnouncementUpdateView
# Create your views here. # Create your views here.
urlpatterns = [ urlpatterns = [
path('', index, name='index'), path('', announcements, name='announcements'),
path('<int:pk>/details/', AnnouncementDetailView.as_view(), name='announcement-details'),
path('add/', AnnouncementCreateView.as_view(), name='announcement-add'),
path('<int:pk>/edit/', AnnouncementUpdateView.as_view(), name='announcement-edit'),
] ]
app_name = "announcementboard" app_name = "announcementboard"
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from .models import Announcement, Reaction
from .models import Announcement
# Create your views here.
def index(request):
output = "<h1><b> Widget's Announcement Board </b></h1><h3>Announcements:</h3>"
for announcement in Announcement.objects.order_by('-pub_datetime'):
output += '<b>{}</b> by {} {} published {}:<br> {} <br>'.format(
announcement.title, announcement.author.first_name, announcement.author.last_name, announcement.pub_datetime.strftime("%m/%d/%Y %I:%M %p"), announcement.body)
reactList = announcement.reacts.all() # Create your views here.
sortedReactList = sortReacts(reactList) def announcements(request):
return render(request, 'announcementboard/announcements.html',
for react in sortedReactList: {'announcements':Announcement.objects.order_by('-pub_datetime')})
output += '{}: {} <br>'.format(react.name, react.tally)
output += '<br>' class AnnouncementDetailView(DetailView):
model = Announcement
template_name = 'announcementboard/announcement-details.html'
return HttpResponse(output) class AnnouncementCreateView(CreateView):
model = Announcement
fields = 'title', 'body', 'author'
template_name = 'announcementboard/announcement-add.html'
class AnnouncementUpdateView(UpdateView):
model = Announcement
fields = 'title', 'body', 'author'
template_name = 'announcementboard/announcement-edit.html'
# Sorts reactions in the following order: Like, Love, Angry
def sortReacts(list):
sortedReactList = ['']*3
for react in list:
if react.name == "Like":
sortedReactList[0] = react
elif react.name == "Love":
sortedReactList[1] = react
elif react.name == "Angry":
sortedReactList[2] = react
return sortedReactList
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