Commit 50c5d084 authored by Nics De Vega's avatar Nics De Vega

Merge branch 'forumv2'

parents a124dc97 78a4466b
# Generated by Django 4.1.7 on 2023-05-10 11:51
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('forum', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='forumpost',
name='pub_datetime',
field=models.DateTimeField(default=django.utils.timezone.now),
),
migrations.AlterField(
model_name='reply',
name='forum_post',
field=models.ForeignKey(default='', on_delete=django.db.models.deletion.CASCADE, related_name='forum_reply', to='forum.forumpost'),
),
]
from django.db import models
from dashboard.models import WidgetUser
from django.urls import reverse
from django.utils import timezone
class ForumPost(models.Model):
title = models.CharField(max_length=255,default="")
body = models.TextField(default="")
author = models.ForeignKey(WidgetUser,on_delete=models.CASCADE)
pub_datetime = models.DateTimeField()
pub_datetime = models.DateTimeField(default=timezone.now)
def __str__(self):
return '{}'.format(self.title)
def get_absolute_url(self):
return reverse("forum:forumpost-details",kwargs={'pk':self.pk})
class Reply(models.Model):
body = models.TextField(default="")
author = models.ForeignKey(WidgetUser,on_delete=models.CASCADE)
pub_datetime = models.DateTimeField()
forum_post = models.ForeignKey(ForumPost,on_delete=models.CASCADE,default="")
forum_post = models.ForeignKey(ForumPost,on_delete=models.CASCADE,default="",related_name='forum_reply')
def __str__(self):
return '{}'.format(self.body)
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}Widget's Forum{% endblock %}
{% block content %}
<h1>Welcome to Widget's Forum!</h1>
<h2>Forum Posts:</h2>
<h3>
{% for object in forum %}
<a href="{{ object.get_absolute_url }}">{{object.title}} by {{object.author.first_name}} {{object.author.last_name}}</a>
<br>
{% endfor %}
</h3>
<br>
<form action="forumposts/add">
<input type="submit" value="Add Post">
</form>
<h3>
<a href="../dashboard/">Dashboard</a> <br>
<a href="../announcements/">Announcements</a> <br>
<a href="../assignments/">Assignments</a> <br>
<a href="../calendar/">Calendar</a>
</h3>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% 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="Save New Post">
</form>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}{{object.title}}{% endblock %}
{% block content %}
<h1>{{object.title}}</h1>
<h3>by {{object.author.first_name}} {{object.author.last_name}}</h3>
<h3>{{object.pub_datetime|date:'m/d/Y, h:i A'}}</h3>
<h3>{{object.body}}</h3>
<br>
<h2>POST REPLIES:</h2>
{% for reply in object.forum_reply.all %}
<h3>by {{reply.author.first_name}} {{reply.author.last_name}}</h3>
<h3>{{reply.pub_datetime|date:'m/d/Y, h:i A'}}</h3>
<h3>{{reply.body}}</h3>
<br>
{% endfor %}
<form action="../edit/">
<input type="submit" value="Edit Post">
</form>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% 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 to Post">
</form>
{% endblock %}
\ No newline at end of file
from django.urls import path
from .views import index
from .views import (forum, ForumPostDetailView, ForumPostCreateView, ForumPostUpdateView)
urlpatterns = [
path('', index, name='index'),
path('',forum,name='forum'),
path('forumposts/<int:pk>/details/',ForumPostDetailView.as_view(),name='forumpost-details'),
path('forumposts/add/',ForumPostCreateView.as_view(),name='forumpost-add'),
path('forumposts/<int:pk>/edit/',ForumPostUpdateView.as_view(),name='forumpost-edit'),
]
app_name = "forum"
\ No newline at end of file
from django.shortcuts import render
from django.http import HttpResponse
from .models import ForumPost, Reply
from dashboard.models import WidgetUser
def index(request):
return_string = '<body>'
for post in ForumPost.objects.all():
post_heading = '{} by {} {} posted {}:<br>'.format(
post.title,
post.author.first_name,
post.author.last_name,
post.pub_datetime.strftime("%m/%d/%Y, %I:%M %p")
)
post_body = '{}<br>'.format(post.body)
post_replies = ''
for reply in Reply.objects.filter(forum_post=post):
post_replies+= 'Reply by {} {} posted {}:<br>'.format(
reply.author.first_name,
reply.author.last_name,
reply.pub_datetime.strftime("%m/%d/%Y, %I:%M %p")
)
post_replies += '{}<br>'.format(reply.body)
return_string += post_heading + post_body + post_replies + '<br>'
html_string ='<html>{}</html>'.format(return_string)
return HttpResponse('Widget’s Forum<br><br>Forum Posts:<br>' + html_string)
\ No newline at end of file
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
def forum(request):
return render(request, 'forum/forum.html', {'forum':ForumPost.objects.order_by('-pub_datetime')
})
class ForumPostDetailView(DetailView):
model = ForumPost
template_name = "forum/forumpost-details.html"
class ForumPostCreateView(CreateView):
model = ForumPost
fields = ['title','body','author']
template_name = 'forum/forumpost-add.html'
class ForumPostUpdateView(UpdateView):
model = ForumPost
fields = ['title','body','author']
template_name = 'forum/forumpost-edit.html'
\ 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