Commit 0a6e74f3 authored by Jonathan Talbot's avatar Jonathan Talbot

final project

parent 5b7d8393
...@@ -7,17 +7,16 @@ class ReactionInline(admin.TabularInline): ...@@ -7,17 +7,16 @@ class ReactionInline(admin.TabularInline):
class AnnouncementAdmin(admin.ModelAdmin): class AnnouncementAdmin(admin.ModelAdmin):
model = Announcement model = Announcement
search_fields = ('announcement_title', 'announcement_body', 'authors_First_Name', 'authors_Last_Name') search_fields = ('announcement_title', 'announcement_body', 'author')
list_display = ('announcement_title', 'announcement_body', 'authors_First_Name', 'authors_Last_Name') list_display = ('announcement_title', 'announcement_body', 'author')
list_filter = ('announcement_title', 'announcement_body', 'authors_First_Name', 'authors_Last_Name') list_filter = ('announcement_title', 'announcement_body', 'author')
fieldsets = [ fieldsets = [
('Announcement Data', { ('Announcement Data', {
'fields': [ 'fields': [
'announcement_title', 'announcement_title',
'announcement_body', 'announcement_body',
'authors_First_Name', 'author'
'authors_Last_Name'
] ]
}) })
] ]
......
from django.forms import ModelForm
from .models import Announcement
class AnnouncementForm(ModelForm):
class Meta:
model = Announcement
fields = ['announcement_title', 'announcement_body', 'author'] # Selected fields for the form
\ No newline at end of file
# Generated by Django 4.0.3 on 2022-05-23 04:10
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('homepage', '__first__'),
('announcements', '0002_remove_announcement_reaction_reaction_announcement'),
]
operations = [
migrations.RemoveField(
model_name='announcement',
name='authors_First_Name',
),
migrations.RemoveField(
model_name='announcement',
name='authors_Last_Name',
),
migrations.AddField(
model_name='announcement',
name='author',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='homepage.widgetuser'),
),
]
...@@ -2,12 +2,14 @@ from django.db import models ...@@ -2,12 +2,14 @@ from django.db import models
from django.urls import reverse from django.urls import reverse
from django.core.validators import RegexValidator from django.core.validators import RegexValidator
# Using WidgetUser from homepage
from homepage.models import WidgetUser
class Announcement(models.Model): class Announcement(models.Model):
announcement_title= models.CharField(max_length=50) announcement_title= models.CharField(max_length=50)
announcement_body = models.CharField(max_length=200) announcement_body = models.CharField(max_length=200)
authors_First_Name = models.CharField(max_length=20) author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, default=1)
authors_Last_Name = models.CharField(max_length=20)
pub_date = models.DateTimeField(auto_now_add=True, editable=False) pub_date = models.DateTimeField(auto_now_add=True, editable=False)
def get_absolute_url(self): def get_absolute_url(self):
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
</head> </head>
<body> <body>
<h2>{{ announcement.announcement_title }}</h2> <h2>{{ announcement.announcement_title }}</h2>
<h3>by {{ announcement.First_Name }} {{ announcement.Last_Name }}, {{ announcement.pub_date|date:"d/m/yy" }}</h3> <h3>by {{ announcement.author.first_name }} {{ announcement.author.last_name }}, {{ announcement.pub_date|date:"d/m/yy" }}</h3>
<p>{{ announcement.announcement_body }}</p> <p>{{ announcement.announcement_body }}</p>
<hr> <hr>
{% for reaction in reactions %} {% for reaction in reactions %}
...@@ -16,5 +16,7 @@ ...@@ -16,5 +16,7 @@
<p>{{ reaction.reaction_name }} : {{ reaction.tally }}</p> <p>{{ reaction.reaction_name }} : {{ reaction.tally }}</p>
{%endif%} {%endif%}
{% endfor %} {% endfor %}
<hr>
<a href="{% url 'announcement-list' %}">Back to Announcements</a>
</body> </body>
</html> </html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Announcement</title>
</head>
<body>
<h1>Add Announcement</h1>
<hr>
<form action="{% url 'announcement-add' %}" method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save Announcement">
</form>
<hr>
<a href="{% url 'announcement-list' %}">Back to Announcements</a>
</body>
</html>
\ No newline at end of file
...@@ -8,9 +8,17 @@ ...@@ -8,9 +8,17 @@
</head> </head>
<body> <body>
<h1>Important Announcements:</h1> <h1>Important Announcements:</h1>
<hr>
{% if announcements %}
<ul>
{% for announcement in announcements %} {% for announcement in announcements %}
<a href="{{ announcement.get_absolute_url }}">{{ announcement.announcement_title }} by {{ announcement.authors_First_Name }} {{ announcement.authors_Last_Name }} dated {{ announcement.pub_date }}</a> <li><a href="{{ announcement.get_absolute_url }}">{{ announcement.announcement_title }} by {{ announcement.author.first_name }} {{ announcement.author.last_name }} dated {{ announcement.pub_date }}</a></li>
<br>
{% endfor %} {% endfor %}
</ul>
{% else %}
<p>There are no announcements.</p>
{% endif %}
<hr>
<a href="{% url 'announcement-add' %}"><button>Add Announcement</button></a>
</body> </body>
</html> </html>
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import AnnouncementListView, AnnouncementDetailView from .views import AnnouncementListView, AnnouncementDetailView, addAnnouncement
urlpatterns = [ urlpatterns = [
path('', AnnouncementListView.as_view(), name='announcement-list'), path('', AnnouncementListView.as_view(), name='announcement-list'),
path('<int:pk>/details', AnnouncementDetailView, name='announcement-detail'), path('<int:pk>/details', AnnouncementDetailView, name='announcement-detail'),
path('add/', addAnnouncement, name='announcement-add'),
] ]
\ No newline at end of file
from django.http import HttpResponse from django.shortcuts import render, get_object_or_404, redirect
from django.shortcuts import render, get_object_or_404
from django.views import View from django.views import View
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from django.views.generic.list import ListView from django.views.generic.list import ListView
from .models import Announcement, Reaction from .models import Announcement, Reaction
from .forms import AnnouncementForm
# def announcements(request):
# announcements = Announcement.objects.all()
# reactions = Reaction.objects.all()
# output = "ANNOUNCEMENTS:\n" + "\n".join(
# ['{} by {} {} dated by {}: \n {} \n'.format(
# str(announcement.announcement_title),
# str(announcement.authors_First_Name),
# str(announcement.authors_Last_Name),
# str(announcement.pub_date),
# str(announcement.announcement_body),
# )
# for announcement in announcements]
# ) + "REACTIONS:\n" + "\n".join(
# ['{}: {} \n {}: {} \n {}: {}'.format(
# str(reaction.reaction_name1),
# str(reaction.tally1),
# str(reaction.reaction_name2),
# str(reaction.tally2),
# str(reaction.reaction_name3),
# str(reaction.tally3),
# )
# for reaction in reactions]
# )
# return HttpResponse(output, content_type="text/plain")
class AnnouncementListView(View): class AnnouncementListView(View):
def get(self, request): def get(self, request):
announcements = Announcement.objects.order_by('pub_date').all() announcements = Announcement.objects.order_by('-pub_date').all()
context = { context = {
'announcements': announcements, 'announcements': announcements,
} }
...@@ -47,3 +20,15 @@ def AnnouncementDetailView(request, pk): ...@@ -47,3 +20,15 @@ def AnnouncementDetailView(request, pk):
'announcement': announcement, 'announcement': announcement,
'reactions': reactions 'reactions': reactions
}) })
def addAnnouncement(request):
form = AnnouncementForm()
if request.method == 'POST':
form = AnnouncementForm(request.POST)
if form.is_valid():
new_announcement = form.save() # Creates new widget user
return redirect('announcement-detail', pk=new_announcement.pk) # Redirects to detailed view of new widget user
else:
form = AnnouncementForm()
context = {'form':form}
return render(request,'announcements/announcements_form.html', context)
No preview for this file type
# Generated by Django 4.0.3 on 2022-05-23 04:10
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('forum', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='post',
name='post_body',
field=models.CharField(max_length=1000, verbose_name='Body'),
),
migrations.AlterField(
model_name='post',
name='post_title',
field=models.CharField(max_length=100, verbose_name='Title'),
),
migrations.AlterField(
model_name='reply',
name='replied_post',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='replies', to='forum.post'),
),
migrations.AlterField(
model_name='reply',
name='reply_body',
field=models.CharField(max_length=500),
),
]
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