Commit 9eb03f98 authored by Mavrick Jordan Lee's avatar Mavrick Jordan Lee

Merged forumv2 to dashboardv2 and announcementsv2.

parents 96536f33 f9e6a556
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
from django.utils import timezone
# Create your models here. # Create your models here.
...@@ -12,6 +14,16 @@ class ForumPost(models.Model): ...@@ -12,6 +14,16 @@ class ForumPost(models.Model):
def __str__(self): def __str__(self):
return self.title return self.title
def save(self, *args, **kwargs):
if not self.pk:
self.pub_datetime = timezone.now()
return super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse('forum:forumpost-details', kwargs={'pk': self.pk})
class Reply(models.Model): class Reply(models.Model):
body = models.TextField(default = "", null = True,) body = models.TextField(default = "", null = True,)
author = models.ForeignKey(WidgetUser, on_delete = models.CASCADE, default = "", null = True,) author = models.ForeignKey(WidgetUser, on_delete = models.CASCADE, default = "", null = True,)
......
{% extends 'base.html' %}
{% block title %} Widget's Forum {% endblock %}
{% block content %}
<h1>Welcome to Widget's Forum!</h1>
<h3>Forum Posts:</h3>
{% for forum in forums %}
<a href="{% url 'forum:forumpost-details' forum.id %}"> {{ forum.title }}
by {{ forum.author.first_name }} {{ forum.author.last_name }}
</a> <br>
{% endfor %} <br>
<a href="{% url 'forum:forumpost-add' %}">
<button>New Post</button>
</a> <br><br>
<a href="">Dashboard</a> <br>
<a href="">Announcements</a>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %} Add Post {% endblock %}
{% block content %}
<h1> Add a new post</h1>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="Submit" value="Add Post">
</form>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %}{{ forumpost.title }}{% endblock %}
{% block content %}
<h1>{{ forumpost.title }} <br>
by {{ forumpost.author.first_name }}
{{ forumpost.author.last_name }}
</h1>
{{ forumpost.pub_datetime }} <br>
{{ forumpost.body }} <br> <br>
Post Replies: <br>
{% for reply in replies %}
<br> by {{ reply.author.first_name }}
{{ reply.author.last_name }}
{{ reply.pub_datetime }} <br>
{{ reply.body }} <br>
{% endfor %} <br>
<a href="{% url 'forum:forumpost-edit' object.id %}">
<button>Edit Post</button>
</a>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %} Edit Post {% endblock %}
{% block content %}
<h1> Edit post:</h1>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="Submit" value="Save Changes">
</form>
{% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from.import views from.views import Forum, ForumDetailViews, ForumCreateView, ForumEditView
urlpatterns = [ urlpatterns = [
path('', views.forum, name = "forum"), path('', Forum, name="forum"),
path('<int:pk>/details/', ForumDetailViews.as_view(), name="forumpost-details"),
path('add/', ForumCreateView.as_view(), name="forumpost-add"),
path('<int:pk>/edit/', ForumEditView.as_view(), name="forumpost-edit"),
] ]
app_name = 'forum'
\ No newline at end of file
from .models import ForumPost, Reply from .models import ForumPost, Reply
from django.http import HttpResponse from django.http import HttpResponse
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from django.shortcuts import render
# Create your views here. # Create your views here.
def forum(request): def Forum(request):
posts = ForumPost.objects.all() forums = ForumPost.objects.order_by("-pub_datetime")
replies = Reply.objects.all() context = {
response = "Widget's Forums <br> <br>" + "Forum Posts: <br>" "forums": forums,
for post in posts: }
post_name = post.author.first_name + " " + post.author.last_name
response += "<br>" + post.title + " by " + post_name + " posted " + post.pub_datetime.strftime("%a, %b %d, %Y %I:%M %p") + ": <br>" + post.body + "<br>" return render(request, 'forum/forum.html', context)
for reply in replies:
if(post.title == reply.forum_post.title):
reply_name = reply.author.first_name + " " + reply.author.last_name class ForumDetailViews(DetailView):
response += "Reply by " + reply_name + " posted " + reply.pub_datetime.strftime("%a, %b %d, %Y %I:%M %p") + ": <br>" + reply.body + "<br>" model = ForumPost
template_name = 'forum/forumpost-details.html'
return HttpResponse(response)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['replies'] = Reply.objects.filter(forum_post=self.object)
return context
class ForumCreateView(CreateView):
model = ForumPost
fields = 'title', 'body', 'author'
template_name = 'forum/forumpost-add.html'
class ForumEditView(UpdateView):
model = ForumPost
fields = 'title', 'body', 'author'
template_name = 'forum/forumpost-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