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 **/.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 django.db import models
from dashboard import models as dashboard_models from dashboard import models as dashboard_models
from django.urls import reverse
class ForumPost(models.Model): class ForumPost(models.Model):
title = models.CharField(max_length=255, default='') title = models.CharField(max_length=255, default='')
body = models.TextField(default='') body = models.TextField(default='')
author = models.ForeignKey(dashboard_models.WidgetUser, on_delete=models.CASCADE) 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): def __str__(self):
return '{} by {} on {}'.format(self.title, self.author, self.pub_datetime.strftime("%m/%d/%Y %I:%M %p")) 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): class Reply(models.Model):
body = models.TextField(default='') body = models.TextField(default='')
author = models.ForeignKey(dashboard_models.WidgetUser, on_delete=models.CASCADE) author = models.ForeignKey(dashboard_models.WidgetUser, on_delete=models.CASCADE)
...@@ -18,3 +28,12 @@ class Reply(models.Model): ...@@ -18,3 +28,12 @@ class Reply(models.Model):
def __str__(self): def __str__(self):
return 'Reply by {} on {}'.format(self.author, self.pub_datetime.strftime("%m/%d/%Y %I:%M %p")) 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 django.urls import path
from .views import ForumPostList, ForumPostDetailView, ForumPostAddView, ForumPostUpdateView
from .views import index
urlpatterns = [ 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" app_name="forum"
\ No newline at end of file
from typing import Any, Dict
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse from .models import ForumPost, Reply
from .models import ForumPost from django.views.generic import DetailView, CreateView, UpdateView
from .models import Reply
def index(request): def ForumPostList(request):
return_string = "<p>Widget's Forum</p> <p>Forum Posts:<br>" posts = ForumPost.objects.all()
context = {
'posts': posts
}
return render(request, "forum/forum.html", context)
for post in ForumPost.objects.all(): class ForumPostDetailView(DetailView):
post_string = '{} by {} {} posted {}:<br>'.format( model = ForumPost
post.title, post.author.first_name, post.author.last_name, template_name = 'forum/forumpost-details.html'
post.pub_datetime.strftime("%m/%d/%Y, %I:%M %p") fields = '__all__'
)
post_string += '{}<br>'.format(post.body)
return_string += post_string
for reply in Reply.objects.all(): def get_context_data(self, *args, **kwargs):
if post == reply.forum_post: context = super(ForumPostDetailView, self).get_context_data(*args, **kwargs)
reply_string = 'Reply by {} {} posted {}:<br>'.format( context['reply_list'] = Reply.objects.all()
reply.author.first_name, reply.author.last_name, return context
reply.pub_datetime.strftime("%m/%d/%Y, %I:%M %p")
)
reply_string += '{}<br>'.format(reply.body)
return_string += reply_string
return_string += '<br>' class ForumPostAddView(CreateView):
model = ForumPost
template_name = 'forum/forumpost-add.html'
fields = '__all__'
return_string += '</p>' class ForumPostUpdateView(UpdateView):
html_string = '<html><body>{}</body></html>'.format(return_string) model = ForumPost
template_name = 'forum/forumpost-edit.html'
return HttpResponse(return_string) fields = '__all__'
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
{% load django_bootstrap5 %} {% load django_bootstrap5 %}
{% bootstrap_css %} {% bootstrap_css %}
{% bootstrap_javascript %} {% bootstrap_javascript %}
<link rel="stylesheet" href="style.css"> <!-- <link rel="stylesheet" href="style.css"> -->
<title>{% block title %}{% endblock %}</title> <title>{% block title %}{% endblock %}</title>
{% block styles %}{% endblock %} {% block styles %}{% endblock %}
</head> </head>
......
...@@ -64,7 +64,7 @@ ROOT_URLCONF = 'widget_jenicaesports.urls' ...@@ -64,7 +64,7 @@ ROOT_URLCONF = 'widget_jenicaesports.urls'
TEMPLATES = [ TEMPLATES = [
{ {
'BACKEND': 'django.template.backends.django.DjangoTemplates', 'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [], 'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True, 'APP_DIRS': True,
'OPTIONS': { 'OPTIONS': {
'context_processors': [ '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