Commit 1e4cf00e authored by justin's avatar justin

Merge branch 'announcements_wip'

parents a14fc9da 7f83c099
from django.contrib import admin from django.contrib import admin
from .models import Announcement, Reaction
# Register your models here. # Register your models here.
# show reactions in page for announcements
class ReactionInline(admin.TabularInline):
model = Reaction
# announcement admin panel
class AnnouncementAdmin(admin.ModelAdmin):
model = Announcement
# display title, body, datetime, author
list_display = ("title", "get_author", "pub_datetime", "body")
# search by title or body
search_fields = ("title", "body")
# filter by author
list_filter = ("get_author")
def get_author(self, obj):
return obj.author.displayName()
get_author.short_description = "Author Name"
get_author.admin_order_field = "author"
inlines = [ReactionInline]
class ReactionAdmin(admin.ModelAdmin):
# display reaction name, tally, announcement
list_display = ("reaction_name", "tally", "get_name")
# search by announcement name
search_fields = ("get_name")
# filter by announcement
list_filter = ("get_name")
def get_name(self, obj):
return obj.announcement.title
# title of column for get_name
get_name.short_description = "Announcement Title"
# sorting by announcement
get_name.admin_order_field = "announcement"
admin.site.register(Announcement, AnnouncementAdmin)
admin.site.register(Reaction, ReactionAdmin)
\ No newline at end of file
from django.db import models from django.db import models
from dashboard.models import WidgetUser
# Create your models here.
class Announcement(models.Model):
title = models.CharField(max_length=50)
body = models.CharField(max_length=500)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE)
pub_datetime = models.DateTimeField(
"Date and Time Published",
auto_now_add=True,
)
def __str__(self):
return self.title
class Reaction(models.Model):
name = models.CharField(max_length=50)
tally = models.IntegerField(default=0)
announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE)
def __str__(self):
return self.name
from django.urls import path
from . import views
urlpatterns = [
path('', views.announcements, name="announcements"),
]
\ No newline at end of file
from django.shortcuts import render from django.http import HttpResponse
from .models import Announcement, Reaction
from datetime import datetime
# Create your views here.
def announcements(request):
# retrieve all announcement entries in a list
announcementList = Announcement.objects.all()
# build response string
response = "Widget's Announcement Board <br><br>" + "Announcements:"
for announcement in announcementList:
# string formatting for date-time
datetime = announcement.pub_datetime.strftime("%m/%d/%Y, %I:%M %p")
# list of reactions for announcement
reactions = announcement.reaction_set.all()
# response proper
response = (
response
+ "<br>"
+ announcement.title
+ " by "
+ announcement.author.displayName()
+ " published "
+ datetime
+ ": <br>"
)
response = response + announcement.body + "<br>"
# for each reaction, add line with reaction and tally
for reaction in reactions:
response = response + reaction.name + ": " + str(reaction.tally) + "<br>"
return HttpResponse(response)
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