Commit 96536f33 authored by Mavrick Jordan Lee's avatar Mavrick Jordan Lee

Merged deashboardv2 and announcementsv2.

parents 3e3d6946 54970ec4
from django.db import models
from dashboard.models import WidgetUser
from django.urls import reverse
from django.utils import timezone
# Create your models here.
# please insert foreign key from dashboard application on announcement_author
class Announcement(models.Model):
announcement_title = models.CharField(max_length=50, unique=True, null=True, )
announcement_body = models.CharField(max_length=250, unique=True, null=True, )
announcement_author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null=True)
announcement_title = models.CharField(max_length=50, unique=True, null=True, verbose_name="Title")
announcement_body = models.TextField(max_length=250, unique=True, null=True, verbose_name="Body")
announcement_author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null=True, verbose_name="Author")
announcement_pub_datetime = models.DateTimeField(null=True, )
def __str__(self):
return self.announcement_title
def save(self, *args, **kwargs):
if not self.pk:
self.announcement_pub_datetime = timezone.now()
return super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse('announcements:announcement-details', kwargs={'pk': self.pk})
reaction_choices = (
('Like', 'Like'),
......@@ -22,10 +33,10 @@ reaction_choices = (
class Reaction(models.Model):
reaction_name = models.CharField(max_length=50, blank=True, choices=reaction_choices, null=True,)
reaction_name = models.CharField(max_length=50, blank=True, choices=reaction_choices, null=True, )
reaction_tally = models.IntegerField(null=True, default=0, )
reaction_announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE, null=True, )
def __str__(self):
return self.reaction_name
{% extends 'base.html' %}
{% block title %} Add Announcement {% endblock %}
{% block content %}
<h1> Add a new announcement</h1>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="Submit" value="Add Announcement">
</form>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %}{{ announcement.announcement_title }}{% endblock %}
{% block content %}
<h1>{{ announcement.announcement_title }} <br>
by {{ announcement.announcement_author.first_name }}
{{ announcement.announcement_author.last_name }}
</h1>
{{ announcement.announcement_pub_datetime }} <br>
{{ announcement.announcement_body }} <br>
{% for reaction in reactions %}
<p>{{ reaction.reaction_name }}: {{ reaction.reaction_tally }}</p>
{% endfor %} <br>
<a href="{% url 'announcements:announcement-edit' pk=announcement.pk %}">
<button>Edit Announcement</button>
</a>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %} Edit Announcement {% endblock %}
{% block content %}
<h1> Edit announcement:</h1>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="Submit" value="Save Changes">
</form>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %} Widget's Announcement Board {% endblock %}
{% block content %}
<h1>Welcome to Widget's Announcement Board!</h1>
<h3>Announcements:</h3>
{% for announcement in announcements %}
<a href="{% url 'announcements:announcement-details' announcement.id %}"> {{ announcement.announcement_title }}
by {{ announcement.announcement_author.first_name }} {{ announcement.announcement_author.last_name }}
</a> <br>
{% endfor %} <br>
<a href="{% url 'announcements:announcement-add' %}">
<button>New Announcement</button>
</a> <br><br>
<a href="">Dashboard</a> <br>
<a href="">Forum</a>
{% endblock %}
from django.urls import path
from . import views
from .views import Announcements, AnnouncementsDetailsView, AnnouncementsCreateView, AnnouncementsEditView
urlpatterns = [
path('', views.announcements, name="announcements")
path('', Announcements, name="announcements"),
path('<int:pk>/details/', AnnouncementsDetailsView.as_view(), name="announcement-details"),
path('add/', AnnouncementsCreateView.as_view(), name="announcement-add"),
path('<int:pk>/edit/', AnnouncementsEditView.as_view(), name="announcement-edit"),
]
app_name = 'announcements'
\ No newline at end of file
from django.http import HttpResponse
from django.shortcuts import render
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from .models import Announcement, Reaction
# Create your views here.
def Announcements(request):
announcements = Announcement.objects.order_by("-announcement_pub_datetime")
context = {
"announcements": announcements,
}
return render(request, 'announcements/announcements.html', context)
def announcements(request):
announcement = Announcement.objects.all()
response = "<h3>Widget's Announcement Board</h3>"
for every in announcement:
datetime = every.announcement_pub_datetime.strftime("%a, %b %d, %Y %I:%M %p")
response += "<p>{}".format(every.announcement_title) +\
" By {}".format(every.announcement_author.first_name) +\
" {}".format(every.announcement_author.last_name) +\
" published {}".format(datetime) +\
"<br>{}".format(every.announcement_body)
class AnnouncementsDetailsView(DetailView):
model = Announcement
template_name = 'announcements/announcement-details.html'\
like_on_post = 0
love_on_post = 0
angry_on_post = 0
reactions = Reaction.objects.filter(reaction_announcement=every)
for every in reactions:
if every.reaction_name == "Like":
like_on_post = every.reaction_tally
elif every.reaction_name == "Love":
love_on_post = every.reaction_tally
elif every.reaction_name == "Angry":
angry_on_post = every.reaction_tally
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['reactions'] = Reaction.objects.filter(reaction_announcement=self.object)
return context
response += "<br>like: {}".format(like_on_post) +\
"<br>love: {}".format(love_on_post) +\
"<br>angry: {}<br>".format(angry_on_post)
return HttpResponse(response)
class AnnouncementsCreateView(CreateView):
model = Announcement
fields = 'announcement_title', 'announcement_body', 'announcement_author'
template_name = 'announcements/announcement-add.html'
class AnnouncementsEditView(UpdateView):
model = Announcement
fields = 'announcement_title', 'announcement_body', 'announcement_author'
template_name = 'announcements/announcement-edit.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