Commit 1f43cf10 authored by Jonathan Talbot's avatar Jonathan Talbot

Merge branch 'flores/forum' into 'main'

Flores/forum

See merge request !14
parents 79f420d2 3da5dd23
from django.forms import ModelForm
from .models import Post
class PostForm(ModelForm):
class Meta:
model = Post
fields = ['post_title', 'post_body', 'author'] # Selected fields for the form
\ No newline at end of file
from django.db import models from django.db import models
from django.urls import reverse from django.urls import reverse
from django.core.validators import RegexValidator
# Using WidgetUser from homepage # Using WidgetUser from homepage
from homepage.models import WidgetUser from homepage.models import WidgetUser
class Post(models.Model): class Post(models.Model):
post_title = models.CharField(max_length=50) post_title = models.CharField('Title', max_length=100)
post_body = models.CharField(max_length=100) post_body = models.CharField('Body', max_length=1000)
pub_date = models.DateTimeField(auto_now_add=True, editable=False) pub_date = models.DateTimeField(auto_now_add=True, editable=False)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE) author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE)
# author_name = WidgetUser.objects.get(fk=author).full_name()
def __str__(self):
return '{} by {} dated {}:\n {}'.format(self.post_title, self.author.full_name(), self.pub_date, self.post_body)
# Takes URL to ForumDetailView
def get_absolute_url(self): def get_absolute_url(self):
return reverse('forum', args=[(self.post_title)]) return reverse('Details', args=[(self.pk)])
def get_author_name(self):
return str(self.author.first_name + " " + self.author.last_name)
author_name = get_author_name
class Reply(models.Model): class Reply(models.Model):
reply_body = models.CharField(max_length=100) reply_body = models.CharField(max_length=500)
pub_date = models.DateTimeField(auto_now_add=True, editable=False) pub_date = models.DateTimeField(auto_now_add=True, editable=False)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE) author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE)
replied_post = models.ForeignKey(Post, on_delete=models.CASCADE) replied_post = models.ForeignKey(Post, related_name = 'replies', on_delete=models.CASCADE)
def get_author_name(self):
return str(self.author.first_name + " " + self.author.last_name)
def __str__(self): author_name = get_author_name
return '\nReply by {} dated {}:\n {}'.format(self.author.full_name(), self.pub_date, self.reply_body) \ No newline at end of file
\ No newline at end of file
{% block content %}
<h3>{{post.post_title}}</h3>
<p>by {{post.author_name}}, {{post.pub_date|date:"d/m/Y"}}<p>
{{post.post_body}}
<ul>
{% for reply in post.replies.all %}
<li>
{{reply.author_name}}, {{reply.pub_date|date:"d/m/Y"}}:<br>
{{reply.reply_body}}
</li>
{% endfor %}
</ul>
<a href="{% url 'Forum' %}">Back to Forum</a>
{% endblock %}
\ No newline at end of file
{% block content %}
<h3>New Forum Post</h3>
<form action="{% url 'Add' %}" method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save Post">
</form>
<a href="{% url 'Forum' %}">Back to Forum</a>
{% endblock %}
\ No newline at end of file
{% block content %}
{% block title %}Welcome to Widget’s Forum!{% endblock %}
<p>Forum posts:</p>
{% if post_list %}
<ul>
{% for post in post_list %}
<li>
<a href="{{ post.get_absolute_url }}">{{ post.post_title }}</a> by {{post.author_name}} dated {{post.pub_date|date:"d/m/Y"}}<br>
</li>
{% endfor %}
</ul>
{% else %}
<p>There are no posts.</p>
{% endif %}
<p><a href="{% url 'Add' %}">New Forum Post</a></p>
{% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import forum from . import views
from .views import ForumListView, ForumDetailView
urlpatterns = [ urlpatterns = [
path('', forum, name='forum') path('', ForumListView.as_view(), name='Forum'),
path('<int:pk>/details/', ForumDetailView.as_view(), name='Details'),
path('add/', views.addPost, name='Add'),
] ]
\ No newline at end of file
from django.http import HttpResponse from django.views.generic import ListView, DetailView
from .models import Post, Reply from django.shortcuts import render, redirect
from django.db import models
from .models import Post
from .forms import PostForm
def forum(request): class ForumListView(ListView):
post = Post.objects.all() queryset = Post.objects.order_by('-pub_date') # Orders publication by most recent
reply = Reply.objects.all() context_object_name = "post_list"
output = "FORUM POSTS: \n" + "\n".join([str(x) for x in post]) + "\n".join([str(z) for z in reply]) template_name = "post_list.html"
return HttpResponse(output, content_type="text/plain")
class ForumDetailView(DetailView):
model = Post
template_name = "forum/post_detail.html"
def addPost(request):
form = PostForm()
if request.method == 'POST':
form = PostForm(request.POST)
if form.is_valid():
form.pub_date = models.DateTimeField(auto_now_add=True, editable=False) # Sets current time as pub_date
new_post = form.save() # Creates new post
return redirect('Details', pk=new_post.pk) # Redirects to detailed view of new post
else:
form = PostForm()
context = {'form':form}
return render(request,'forum/post_form.html', context) # Takes post_form template and displays form and post button
\ No newline at end of file
"""widget_group3 URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin from django.contrib import admin
from django.urls import path, include from django.urls import path, include
...@@ -23,6 +8,7 @@ urlpatterns = [ ...@@ -23,6 +8,7 @@ urlpatterns = [
path('homepage/', include('homepage.urls'), name='Homepage'), path('homepage/', include('homepage.urls'), name='Homepage'),
path('users/', include('homepage.urls'), name='Homepage'), path('users/', include('homepage.urls'), name='Homepage'),
path('forum/', include('forum.urls'), name='Forum'), path('forum/', include('forum.urls'), name='Forum'),
path('posts/', include('forum.urls'), name='Forum'),
path('assignments/', include('assignments.urls'), name='Assignments'), path('assignments/', include('assignments.urls'), name='Assignments'),
path('announcements/', include('announcements.urls'), name='Announcement Board'), path('announcements/', include('announcements.urls'), name='Announcement Board'),
] ]
\ No newline at end of file
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