Commit d749b779 authored by justin's avatar justin

staging: merged forums

parents 0ebf8de9 f9a34572
# Generated by Django 3.2 on 2023-05-08 08:54
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('forum', '0002_rename_forum_reply_forum_post'),
]
operations = [
migrations.AlterField(
model_name='reply',
name='forum_post',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='replies', to='forum.forumpost'),
),
]
from django.db import models from django.db import models
from dashboard.models import WidgetUser from dashboard.models import WidgetUser
from django.urls import reverse
class ForumPost(models.Model): class ForumPost(models.Model):
...@@ -11,9 +12,13 @@ class ForumPost(models.Model): ...@@ -11,9 +12,13 @@ class ForumPost(models.Model):
def __str__(self): def __str__(self):
return self.title return self.title
def get_absolute_url(self):
return reverse('forum:forumpostdetails', kwargs={'pk': self.pk})
class Reply(models.Model): class Reply(models.Model):
forum_post = models.ForeignKey(ForumPost, on_delete=models.CASCADE) forum_post = models.ForeignKey(ForumPost, on_delete=models.CASCADE, related_name="replies")
body = models.CharField(max_length=1000) body = models.CharField(max_length=1000)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE) author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE)
pub_datetime = models.DateTimeField("Published Date and Time", auto_now_add=True) pub_datetime = models.DateTimeField("Published Date and Time", auto_now_add=True)
......
{% extends 'base.html' %}
{% load static %}
{% block title %} Widget's Forum {% endblock %}
{% block content %}
<h1>Welcome to Widget's Forum!</h1>
<h2>Forum posts:</h2>
<ul>
{% for post in forumpost %}
<li><a href="{{ post.get_absolute_url }}">{{ post.title }} by {{ post.author.first_name }} {{ post.author.last_name }}</a></li>
{% endfor %}
</ul>
<form action="./forumpost/add">
<button type="Submit">New Post</button>
</form>
<a href="../dashboard/">Dashboard</a> <br>
<a href="../announcements/">Announcements</a> <br>
<a href="../assignments/">Assignments</a> <br>
<a href="../calendar/">Calendar</a> <br>
{% endblock %}
{% extends 'base.html' %}
{% load static %}
{% block title %} Add Post {% endblock %}
{% block content %}
<h1> Add a new post: </h1>
<form method="POST">
{% csrf_token %}
{% for field in form%}
<b>{{ field.label }}:</b> {{ field }} <br>
<br>
{% endfor %}
<button type="submit">Save New Post</button>
</form>
{% endblock %}
{% extends 'base.html' %}
{% load static %}
{% block title %} {{ object.title }} {% endblock %}
{% block content %}
<h1> {{ object.title }} </h1>
<h2> {{ object.author.first_name }} {{ object.author.last_name }}</h2>
<h3> {{ object.pub_datetime|date:"m/d/Y, h:i A" }} </h3>
<p> {{ object.body }} </p> <br>
Post Replies:
<ul>
{% for reply in object.replies.all %}
<li>
<p>by {{ reply.author.first_name }} {{ reply.author.last_name }}</p>
<p>{{ reply.pub_datetime|date:"m/d/Y, h:i A" }}</p>
<p>{{ reply.body }}</p>
</li>
{% endfor %}
</ul>
<form action="../edit">
<button type="Submit">Edit Post</button>
</form>
{% endblock %}
{% extends 'base.html' %}
{% load static %}
{% block title %} Edit Post {% endblock %}
{% block content %}
<h1> Edit Post: </h1>
<form method="POST">
{% csrf_token %}
{% for field in form%}
<b>{{ field.label }}:</b> {{ field }} <br><br>
{% endfor %}
<button type="submit">Save Changes to Post</button>
</form>
{% endblock %}
...@@ -4,6 +4,10 @@ from . import views ...@@ -4,6 +4,10 @@ from . import views
urlpatterns = [ urlpatterns = [
path("", views.forum, name="index"), path("", views.forum, name="index"),
path("forumpost/<int:pk>/details/", views.ForumDetailView.as_view(), name="forumpostdetails"),
path("forumpost/add/", views.ForumCreateView.as_view(), name="forumpostadd"),
path("forumpost/<int:pk>/edit/", views.ForumUpdateView.as_view(), name="forumpostedit"),
] ]
......
from django.http import HttpResponse from django.http import HttpResponse
from .models import ForumPost from .models import ForumPost
from django.utils import timezone from django.utils import timezone
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from django.shortcuts import render
def forum(request): def forum(request):
context = {
"forumpost" : ForumPost.objects.all().order_by('pub_datetime'),
}
return render(request, 'forum/forum.html', context)
# fetch forum posts and replies class ForumDetailView(DetailView):
forum = ForumPost.objects.all() model = ForumPost
template_name = 'forum/forumpost-details.html'
forum_view = "Widget's Forum <br> <br> Forum Posts:<br>" class ForumCreateView(CreateView):
for post in forum: model = ForumPost
post_replies = post.reply_set.all() template_name = 'forum/forumpost-add.html'
localdatetime = timezone.localtime(post.pub_datetime) fields = ['title', 'body', 'author']
forum_view += """
{} by {} posted {}: <br>
{} <br>
""".format(
post.title,
post.author,
localdatetime.strftime("%m/%d/%Y, %I:%M %p"),
post.body,
)
for reply in post_replies: class ForumUpdateView(UpdateView):
localdatetime = timezone.localtime(reply.pub_datetime) model = ForumPost
if reply.forum_post == post: template_name = 'forum/forumpost-edit.html'
forum_view += """ fields = ['title', 'body', 'author']
Reply by {} posted {}: <br>
{} <br>
""".format(
reply.author,
localdatetime.strftime("%m/%d/%Y, %I:%M %p"),
reply.body,
)
forum_view += "<br>"
return HttpResponse(forum_view)
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