Added 'add announcement' feature

parent 2d945d2d
from django import forms
from django.forms import ModelForm
from django import forms
from .models import *
class AnnouncementForm(ModelForm):
class Meta:
model = Announcement
fields ='__all__'
\ No newline at end of file
# Generated by Django 3.2.12 on 2022-05-21 15:33
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0022_alter_announcement_pub_date'),
]
operations = [
migrations.AddField(
model_name='announcement',
name='related_image',
field=models.ImageField(blank=True, null=True, upload_to='announcements/'),
),
]
# Generated by Django 3.2.12 on 2022-05-21 15:35
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0023_announcement_related_image'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='announcement_body',
field=models.CharField(max_length=500, null=True),
),
migrations.AlterField(
model_name='announcement',
name='announcement_title',
field=models.CharField(max_length=50, null=True),
),
]
# Generated by Django 3.2.12 on 2022-05-21 17:17
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0024_auto_20220521_2335'),
]
operations = [
migrations.AddField(
model_name='reaction',
name='related_image',
field=models.ImageField(blank=True, null=True, upload_to='announcementPictures/'),
),
migrations.AlterField(
model_name='announcement',
name='related_image',
field=models.ImageField(blank=True, null=True, upload_to='announcementPictures/'),
),
]
# Generated by Django 3.2.12 on 2022-05-21 17:17
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('announcements', '0025_auto_20220522_0117'),
]
operations = [
migrations.RemoveField(
model_name='reaction',
name='related_image',
),
]
...@@ -3,11 +3,11 @@ from homepage.models import WidgetUser ...@@ -3,11 +3,11 @@ from homepage.models import WidgetUser
# Create your models here. # Create your models here.
class Announcement(models.Model): class Announcement(models.Model):
announcement_title = models.CharField(max_length=50) announcement_title = models.CharField(max_length=50, null=True)
announcement_body = models.CharField(max_length=500) announcement_body = models.CharField(max_length=500, null=True)
pub_date = models.DateField("date published", null=True, auto_now_add=True) pub_date = models.DateField("date published", null=True, auto_now_add=True)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null=True, related_name='author') author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null=True, related_name='author')
related_image = models.ImageField(upload_to="announcementPictures/", height_field=None, width_field=None, max_length=100, null=True, blank=True)
def __str__(self): def __str__(self):
return self.announcement_title return self.announcement_title
......
/* body body
{ {
background-color: black; }
} */
h1 h1
{ {
...@@ -93,7 +92,7 @@ li ...@@ -93,7 +92,7 @@ li
width: 455px; width: 455px;
height: 530px; height: 530px;
border-radius: 20px; border-radius: 20px;
position: relative; position: absolute;
top: -380px; top: 200px ;
left: 50% ; left: 50% ;
} }
\ No newline at end of file
...@@ -2,11 +2,10 @@ ...@@ -2,11 +2,10 @@
{% block content %} {% block content %}
<div class="indexRectangularShadow"></div> <div class="indexRectangularShadow"></div>
<h1 style="text-align: center">New Announcement</h1> <h1 style="text-align: center">New Announcement</h1>
<form action=""> <form method="POST" enctype="multipart/form-data">
<p>Enter Title: <input type="text" name="title"></p> {% csrf_token %}
<p>Enter Body: <input type="text" name="body"></p> {{form.as_p}}
<p>Choose author: <input type="text" name="author"></p> <input type="submit" value="Submit"></input>
<p>Image (optional): <input type="image" name="image"></p>
<input type="Submit">
</form> </form>
<h2><a href="/announcements/">Announcement Board</a></h2>
{% endblock %} {% endblock %}
\ No newline at end of file
...@@ -5,13 +5,15 @@ ...@@ -5,13 +5,15 @@
<div class="detailsTitleRectangularShadow"></div> <div class="detailsTitleRectangularShadow"></div>
<h1>{{Announcement.announcement_title}}</h1> <h1>{{Announcement.announcement_title}}</h1>
<h2 style="position: relative; left: 70px;">by {{Announcement.author.first_name}} {{Announcement.author.last_name}}, {{Announcement.pub_date}}</h2> <h2 style="position: relative; left: 70px;">by {{Announcement.author.first_name}} {{Announcement.author.last_name}}, {{Announcement.pub_date}}</h2>
<img src="/static/announcements/{{Announcement.author.last_name}}.jpg" class="detailsProfilePicture"> <img src="{{ user.picture.url }}" class="detailsProfilePicture">
<p style="margin-right: 50%; position: relative; top: -60px;">{{Announcement.announcement_body}}</p> <p style="margin-right: 50%; position: relative; top: -60px;">{{Announcement.announcement_body}}</p>
{% for reaction in reactions %} {% for reaction in reactions %}
<img src="/static/announcements/{{reaction.reaction_name}}.jpg" class="detailsReactions"> <img src="/images/announcementPictures/{{reaction.reaction_name}}.jpg" class="detailsReactions">
<p style="position: relative; top: -110px; left: 50px;">{{reaction.reaction_name}}: {{reaction.tally}}</p> <p style="position: relative; top: -110px; left: 50px;">{{reaction.reaction_name}}: {{reaction.tally}}</p>
{% endfor %} {% endfor %}
<img src="/static/announcements/{{Announcement.announcement_title}}.jpg" class="detailsRelatedPicture"> {% if Announcement.related_image %}
<img src="{{ Announcement.related_image.url }}" class="detailsRelatedPicture">
{% endif %}
{% else %} {% else %}
<p>Announcement not found.</p> <p>Announcement not found.</p>
{% endif %} {% endif %}
......
...@@ -7,10 +7,10 @@ ...@@ -7,10 +7,10 @@
<ul> <ul>
{% for Announcement in announcement_list %} {% for Announcement in announcement_list %}
<li><a href="/announcements/{{Announcement.id}}/details">{{Announcement.announcement_title}}, by {{Announcement.author.last_name}}, {{Announcement.author.first_name}}, dated {{Announcement.pub_date}}</a></li> <li><a href="/announcements/{{Announcement.id}}/details">{{Announcement.announcement_title}}, by {{Announcement.author.last_name}}, {{Announcement.author.first_name}}, dated {{Announcement.pub_date}}</a></li>
<img src="/static/announcements/indexMegaphoneIcon.jpg" class="indexMegaphoneIcon"> <img src="/images/announcementPictures/indexMegaphoneIcon.jpg" class="indexMegaphoneIcon">
{% endfor %} {% endfor %}
</ul> </ul>
<h2><a href="/announcements/add">New Announcement</a></h2> <h2><a href="/announcements/add/">New Announcement</a></h2>
{% else %} {% else %}
<p>No Announcements are available</p> <p>No Announcements are available</p>
{% endif %} {% endif %}
......
from django.urls import path from django.urls import path
from . import views from .views import AnnouncementsPageView, AnnouncementAddView, AnnouncementDetailsView
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [ urlpatterns = [
path('', views.index, name="announcements"), path('', AnnouncementsPageView.as_view(), name="announcements"),
path('add/', views.add, name="add"), path('add/', AnnouncementAddView.as_view(), name="add"),
path('<int:announcement_id>/details/', views.details, name="details"), path('<int:announcement_id>/details/', AnnouncementDetailsView.as_view(), name="details"),
] ]
from array import array from array import array
from distutils.command.upload import upload
from tkinter import Widget
from django.http import HttpResponse, Http404 from django.http import HttpResponse, Http404
from announcements.models import Announcement, Reaction, WidgetUser from announcements.models import Announcement, Reaction, WidgetUser
from django.shortcuts import render from django.shortcuts import render
from django.template import loader from django.template import ContextPopException, loader
from django.views import View
from .forms import AnnouncementForm
# Create your views here. # Create your views here.
def index(request): class AnnouncementsPageView(View):
def get(self, request):
announcement_list = Announcement.objects.order_by("pub_date") announcement_list = Announcement.objects.order_by("pub_date")
user_list = WidgetUser.objects.order_by("last_name")
context = { context = {
"announcement_list": announcement_list, "announcement_list": announcement_list,
"user_list": user_list,
} }
return render(request, "announcements/index.html", context) return render(request, "announcements/index.html", context)
def add(request): class AnnouncementAddView(View):
return render(request, "announcements/announcements.html") def get(self, request):
form = AnnouncementForm()
context ={
"form": form,
}
return render(request, "announcements/announcements.html", context)
def post(self, request):
context = {}
if request.method =="POST":
form = AnnouncementForm(request.POST, request.FILES)
if form.is_valid():
form.save()
context ={
"form": form,
}
return render(request, "announcements/announcements.html", context)
# helper function that iterates through every reaction of an announcement, adds it to an array, then returns it class AnnouncementDetailsView(View):
def getReactions(pk): # helper function that iterates through every reaction of an announcement, adds it to an array, then returns it
def getReactions(self, pk):
announcementReactions = [] announcementReactions = []
for reaction in Announcement.objects.get(pk=pk).announcements.all(): announcement = Announcement.objects.get(pk=pk)
if len(announcement.announcements.all()) == 0:
announcement.announcements.create(reaction_name="Like", tally=0)
announcement.announcements.create(reaction_name="Love", tally=0)
announcement.announcements.create(reaction_name="Angry", tally=0)
for reaction in announcement.announcements.all():
announcementReactions.append(reaction) announcementReactions.append(reaction)
return announcementReactions return announcementReactions
def details(request, announcement_id): #helper function to get the context
def getContext(self, request, announcement_id):
try: try:
announcement = Announcement.objects.get(pk=announcement_id) announcement = Announcement.objects.get(pk=announcement_id)
except Announcement.DoesNotExist: except Announcement.DoesNotExist:
raise Http404("Announcement does not exist!") raise Http404("Announcement does not exist!")
try: try:
reactions = getReactions(pk=announcement_id) reactions = self.getReactions(pk=announcement_id)
except Reaction.DoesNotExist: except Reaction.DoesNotExist:
raise Http404("Reaction does not exist!") raise Http404("Reaction does not exist!")
try:
user = WidgetUser.objects.get(pk=announcement.author.id)
except WidgetUser.DoesNotExist:
raise Http404("User does not exist!")
context = { context = {
"Announcement": announcement, "Announcement": announcement,
"reactions": reactions "reactions": reactions,
"user": user,
} }
return context
def get(self, request, announcement_id):
context = self.getContext(request, announcement_id)
return render(request, "announcements/details.html", context)
def post(self, request, announcement_id):
context = self.getContext(request, announcement_id)
return render(request, "announcements/details.html", context) return render(request, "announcements/details.html", context)
\ No newline at end of file
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset = "UTF-8" <meta charset = "UTF-8">
<meta http-equiv="X-UA-COMPATIBLE" content="IE=edge"> <meta http-equiv="X-UA-COMPATIBLE" content="IE=edge">
<meta name="viewport" content ="width=device-width, inital-scale=1.0"> <meta name="viewport" content ="width=device-width, inital-scale=1.0">
{% block head %} {% block head %}
......
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