Commit 4b0970c6 authored by Eldon Dagdag's avatar Eldon Dagdag

Updated Forum and added add/edit/details features

parent 3505d8fc
No preview for this file type
.DS_Store
**/.env
**/db.sqlite3
**/**/*.pyc
\ No newline at end of file
from django import forms
from .models import ForumPost
class ForumPostForm(forms.ModelForm):
class Meta:
model = ForumPost
fields = '__all__'
\ No newline at end of file
# Generated by Django 4.1.7 on 2023-05-10 04:07
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('forum', '0006_rename_reply_to_reply_forum_post'),
]
operations = [
migrations.AlterField(
model_name='forumpost',
name='pub_datetime',
field=models.DateTimeField(auto_now_add=True),
),
]
from django.db import models
from dashboard import models as dashboard_models
from django.urls import reverse
class ForumPost(models.Model):
title = models.CharField(max_length=255, default='')
body = models.TextField(default='')
author = models.ForeignKey(dashboard_models.WidgetUser, on_delete=models.CASCADE)
pub_datetime = models.DateTimeField(auto_now=False, auto_now_add=False)
pub_datetime = models.DateTimeField(auto_now=False, auto_now_add=True)
def __str__(self):
return '{} by {} on {}'.format(self.title, self.author, self.pub_datetime.strftime("%m/%d/%Y %I:%M %p"))
def get_absolute_url(self):
return reverse('forum:forumpost-details', kwargs={'pk':self.pk})
def formatted_date(self):
return '{}'.format(self.pub_datetime.strftime("%m/%d/%y"))
def formatted_time(self):
return '{}'.format(self.pub_datetime.strftime("%I:%M %p"))
class Reply(models.Model):
body = models.TextField(default='')
author = models.ForeignKey(dashboard_models.WidgetUser, on_delete=models.CASCADE)
......@@ -18,3 +28,12 @@ class Reply(models.Model):
def __str__(self):
return 'Reply by {} on {}'.format(self.author, self.pub_datetime.strftime("%m/%d/%Y %I:%M %p"))
def get_absolute_url(self):
return reverse('forum:reply-details', kwargs={'pk':self.pk})
def formatted_date(self):
return '{}'.format(self.pub_datetime.strftime("%m/%d/%y"))
def formatted_time(self):
return '{}'.format(self.pub_datetime.strftime("%I:%M %p"))
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}Widget's Forum{% endblock %}
{% block content %}
<html>
<h1>Welcome to Widget's Forum!</h1>
<body>
<h3>Forum posts:</h3>
{% for post in posts %}
<ul>
<a href="{{ post.get_absolute_url }}">{{ post.title }} by {{ post.author.first_name }} {{post.author.last_name }}</a>
</ul>
{% endfor %}
<button class="button"><a href="/forum/forumposts/add/">New Post</a></button>
<br>
<a href="/dashboard/">Dashboard</a>
<a href="/announcements/">Announcements</a>
<a href="/assignments/">Assignments</a>
<a href="/calendar/">Calendar</a>
</body>
</html>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}Add Post{% endblock %}
{% block content %}
<h3>Add a new post.</h3>
<form method="post">
{% csrf_token %}
{% for field in form %}
{{ field.label }}: {{ field }}<br><br>
{% endfor %}
<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 %}
<h3>{{ object.title }}</h4>
<h4>by {{ object.author.first_name }} {{ object.author.last_name }}</h4>
<h4>{{ object.formatted_date }}, {{object.formatted_time }}</h4>
<h4>{{ object.body }}</h4>
<br><br>
<h3>POST REPLIES:</h3>
{% for reply in reply_list %}
{% if reply.forum_post.title == object.title %}
<ul>
<h4>by {{ reply.author.first_name }} {{ reply.author.last_name }}</h4>
<h4>{{ reply.formatted_date }}, {{ reply.formatted_time }}</h4>
<h4>{{ reply.body }}</h4><br>
</ul>
{% endif %}
{% endfor %}
<h3><button class="button"><a href="/forum/forumposts/{{ object.pk }}/edit/">Edit Post</a></button></h3>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}Edit Post{% endblock %}
{% block content %}
<h3>Edit Post:</h3>
<form method="post">
{% csrf_token %}
{% for field in form %}
{{ field.label }}: {{ field }}<br>
{% endfor %}
<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 ForumPostList, ForumPostDetailView, ForumPostAddView, ForumPostUpdateView
urlpatterns = [
path('', index, name='index')
path('', ForumPostList, name='forum'),
path('forumposts/add/', ForumPostAddView.as_view(), name='forumpost-add'),
path('forumposts/<int:pk>/edit/', ForumPostUpdateView.as_view(), name='forumpost-edit'),
path('forumposts/<int:pk>/details/', ForumPostDetailView.as_view(), name='forumpost-details'),
]
app_name="forum"
\ No newline at end of file
from typing import Any, Dict
from django.shortcuts import render
from django.http import HttpResponse
from .models import ForumPost
from .models import Reply
from .models import ForumPost, Reply
from django.views.generic import DetailView, CreateView, UpdateView
def index(request):
return_string = "<p>Widget's Forum</p> <p>Forum Posts:<br>"
def ForumPostList(request):
posts = ForumPost.objects.all()
context = {
'posts': posts
}
return render(request, "forum/forum.html", context)
for post in ForumPost.objects.all():
post_string = '{} 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_string += '{}<br>'.format(post.body)
return_string += post_string
class ForumPostDetailView(DetailView):
model = ForumPost
template_name = 'forum/forumpost-details.html'
fields = '__all__'
for reply in Reply.objects.all():
if post == reply.forum_post:
reply_string = 'Reply by {} {} posted {}:<br>'.format(
reply.author.first_name, reply.author.last_name,
reply.pub_datetime.strftime("%m/%d/%Y, %I:%M %p")
)
reply_string += '{}<br>'.format(reply.body)
return_string += reply_string
def get_context_data(self, *args, **kwargs):
context = super(ForumPostDetailView, self).get_context_data(*args, **kwargs)
context['reply_list'] = Reply.objects.all()
return context
return_string += '<br>'
class ForumPostAddView(CreateView):
model = ForumPost
template_name = 'forum/forumpost-add.html'
fields = '__all__'
return_string += '</p>'
html_string = '<html><body>{}</body></html>'.format(return_string)
return HttpResponse(return_string)
class ForumPostUpdateView(UpdateView):
model = ForumPost
template_name = 'forum/forumpost-edit.html'
fields = '__all__'
......@@ -3,7 +3,7 @@
{% load django_bootstrap5 %}
{% bootstrap_css %}
{% bootstrap_javascript %}
<link rel="stylesheet" href="style.css">
<!-- <link rel="stylesheet" href="style.css"> -->
<title>{% block title %}{% endblock %}</title>
{% block styles %}{% endblock %}
</head>
......
......@@ -64,7 +64,7 @@ ROOT_URLCONF = 'widget_jenicaesports.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
......
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