Commit b357a962 authored by nheoxoz's avatar nheoxoz

Merge branch 'announcementsv2'

parents 3e0eca39 d7110c12
# Generated by Django 4.1.7 on 2023-05-12 04:52
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('announcements', '0006_remove_announcement_first_name_and_more'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='body',
field=models.TextField(default=''),
),
migrations.AlterField(
model_name='announcement',
name='pub_datetime',
field=models.DateTimeField(default=django.utils.timezone.now),
),
migrations.AlterField(
model_name='announcement',
name='title',
field=models.CharField(default='', max_length=100),
),
]
# Generated by Django 4.1.7 on 2023-05-12 06:23
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0007_alter_announcement_body_and_more'),
]
operations = [
migrations.AlterField(
model_name='reaction',
name='tally',
field=models.IntegerField(default=0),
),
]
from django.db import models from django.db import models
from django.urls import reverse
from django.utils import timezone
class Announcement(models.Model): class Announcement(models.Model):
title = models.CharField(max_length=100) title = models.CharField(max_length=100, default="")
body = models.TextField() body = models.TextField(default="")
author = models.ForeignKey( author = models.ForeignKey(
'dashboard.WidgetUser', 'dashboard.WidgetUser',
on_delete=models.CASCADE, on_delete=models.CASCADE,
related_name='announcements_author' related_name='announcements_author'
) )
pub_datetime = models.DateTimeField() pub_datetime = models.DateTimeField(default=timezone.now)
def __str__(self): def __str__(self):
return '''{} by {} published {}\n{}'''.format( return '{} by {} {} published {}, {}: {}'.format(
self.title, self.title,
self.author, self.author.first_name,
self.pub_datetime, self.author.last_name,
self.pub_datetime.strftime("%m/%d/%Y"),
self.pub_datetime.strftime("%H:%M %p"),
self.body, self.body,
) )
def format_date(self):
return '{}' .format(self.pub_datetime.strftime("%m/%d/%Y"))
def format_time(self):
return '{}' .format(self.pub_datetime.strftime("%H:%M %p"))
def getLike(self):
try:
return '{}'.format(Reaction.objects.get(name="Like", announcement=self).tally)
except:
return '0'
def getLove(self):
try:
return '{}'.format(Reaction.objects.get(name="Love", announcement=self).tally)
except:
return '0'
def getAngry(self):
try:
return '{}'.format(Reaction.objects.get(name="Angry", announcement=self).tally)
except:
return '0'
def get_absolute_url(self):
return reverse(
'announcements:announcement-details',
kwargs={'pk': self.pk},
)
class Reaction(models.Model): class Reaction(models.Model):
...@@ -36,8 +73,8 @@ class Reaction(models.Model): ...@@ -36,8 +73,8 @@ class Reaction(models.Model):
choices=REACTION_CHOICES, choices=REACTION_CHOICES,
default=REACTION_LIKE, default=REACTION_LIKE,
) )
tally = models.IntegerField() tally = models.IntegerField(default=0)
announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE) announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE)
def __str__(self): def __str__(self):
return self.name return '{} reactions for {}' .format(self.name, self.announcement)
{% extends 'base.html' %}
{% load static %}
{% block title %}Add Announcement:% {% endblock %}
{% block heading %}<h1>Add a new announcement:</h1>{% endblock %}
{% block content %}
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Add Announcement">
</form>
<a href="/announcements/">
<button class="btn">Back to Announcement Board</button>
</a>
{% endblock %}
{% extends 'base.html' %}
{% load static %}
{% block title %}{{ object.title }}{% endblock %}
{% block content %}
<h1>
{{ object.title }}
</h1>
<p>
by {{ object.author.first_name }} {{ object.author.last_name }}<br>
</p>
<p>
{{ object.format_date}}, {{ object.format_time }}<br><br>
{{ object.body }}<br>
Like: {{ object.getLike }}<br>
Love: {{ object.getLove }}<br>
Angry: {{ object.getAngry }}
</p>
<a href="/announcements/{{object.pk}}/edit">
<button class="btn">Edit Announcement</button>
</a><br>
<a href="/announcements/">
<button class="btn">Back to Announcement Board</button>
</a>
{% endblock %}
{% extends 'base.html' %}
{% load static %}
{% block title %}Edit Announcement{% endblock %}
{% block heading %}Edit announcement:{% endblock %}
{% block content %}
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save Changes to Announcement">
</form>
<a href="/announcements/">
<button class="btn">Back to Announcement Board</button>
</a>
{% endblock %}
{% extends 'base.html' %}
{% load static %}
{% block title %}Widget's Announcement Board{% endblock %}
{% block heading %}Welcome to Widget's Announcement Board!{% endblock %}
{% block content %}
<p>
Announcements:<br>
{% for a in announcements|slice:"::-1" %}
<a href="{{ a.get_absolute_url }}">
{{a.title}} by {{ a.author.first_name }} {{ a.author.last_name }}
</a><br>
{% endfor %}
</p>
{% endblock %}
{% block footing %}
<p>
<a href="/announcements/add">
<button class="btn add">New Announcement</button>
</a>
</p>
<button type="button" class="btn">
<a href="/dashboard/" class="link">Dashboard</a>
</button><br>
<button type="button" class="btn">
<a href="/forum/" class="link">Forum</a>
</button><br>
<button type="button" class="btn">
<a href="/assignments/" class="link">Assignments</a>
</button><br>
<button type="button" class="btn">
<a href="/widget_calendar/" class="link">Calendar</a>
</button>
{% endblock %}
from django.urls import path from django.urls import path
from .views import index from .views import (
index,
AnnouncementsDetailView,
AnnouncementsCreateView,
AnnouncementsUpdateView,
)
urlpatterns = [ urlpatterns = [
path('', index, name='index'), path('', index, name='index'),
path('<int:pk>/details/',
AnnouncementsDetailView.as_view(),
name='announcement-details'),
path('add/',
AnnouncementsCreateView.as_view(),
name='announcement-add'),
path('<int:pk>/edit/',
AnnouncementsUpdateView.as_view(),
name='announcement-edit'),
] ]
app_name = "announcements" app_name = "announcements"
from django.http import HttpResponse from django.shortcuts import render
from .models import Announcement, Reaction from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from .models import Announcement
def index(request): def index(request):
return_string = "<p>Widget's Announcement Board</p>Announcements:<br>" announcements = Announcement.objects.all()
for a in Announcement.objects.all(): return render(
return_string += '''{} by {} {} published {}{}<br>'''.format( request,
a.title, 'announcements/announcements.html',
a.author.first_name, {'announcements': announcements},
a.author.last_name, )
a.pub_datetime.strftime('%m/%d/%Y, %H:%M %p:'),
a.body,
) class AnnouncementsDetailView(DetailView):
likeTally = 0 model = Announcement
loveTally = 0 template_name = 'announcements/announcement-details.html'
angryTally = 0
for r in Reaction.objects.all():
if r.name == "LIKE" and r.announcement == a: class AnnouncementsCreateView(CreateView):
likeTally += r.tally model = Announcement
if r.name == "LOVE" and r.announcement == a: fields = 'title', 'body', 'author'
loveTally += r.tally template_name = 'announcements/announcement-add.html'
if r.name == "ANGRY" and r.announcement == a:
angryTally += r.tally
return_string += '''Like: {}<br>Love: {} class AnnouncementsUpdateView(UpdateView):
<br>Angry: {}<br><br>'''.format( model = Announcement
likeTally, fields = 'title', 'body', 'author'
loveTally, template_name = 'announcements/announcement-edit.html'
angryTally,
)
html_string = '<html><body>{}</body></html>'.format(return_string)
return HttpResponse(html_string)
...@@ -124,7 +124,7 @@ USE_TZ = True ...@@ -124,7 +124,7 @@ USE_TZ = True
# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/ # https://docs.djangoproject.com/en/4.1/howto/static-files/
STATIC_URL = '/static/' STATIC_URL = 'static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')] STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
# Default primary key field type # Default primary key field type
......
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