Commit 332712dc authored by nheoxoz's avatar nheoxoz

Merge branch 'origin/forumv2'

parents 7eba6986 b33ad66c
from django.db import models from django.db import models
from django.urls import reverse
class ForumPost(models.Model): class ForumPost(models.Model):
...@@ -9,7 +10,7 @@ class ForumPost(models.Model): ...@@ -9,7 +10,7 @@ class ForumPost(models.Model):
on_delete=models.CASCADE, on_delete=models.CASCADE,
related_name='forumpost_author' related_name='forumpost_author'
) )
pub_datetime = models.DateTimeField() pub_datetime = models.DateTimeField(auto_now=True)
def __str__(self): def __str__(self):
return '{} by {} posted {}: {}'.format( return '{} by {} posted {}: {}'.format(
...@@ -19,6 +20,12 @@ class ForumPost(models.Model): ...@@ -19,6 +20,12 @@ class ForumPost(models.Model):
self.body self.body
) )
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() body = models.TextField()
......
{% extends 'base.html' %}
{% load static %}
{% block title %}Widget's Forum{% endblock %}
{% block heading %}Welcome to Widget's Forum!{% endblock %}
{% block content %}
<p>
Forum Posts:<br>
{% for post in posts|slice:"::-1" %}
<a href="{{ post.get_absolute_url }}">
{{ post.title }} by {{ post.author.first_name }} {{ post.author.last_name }}
</a><br>
{% endfor %}
</p>
{% endblock %}
{% block footing %}
<a href="{% url 'forum:forumpost-create' %}">
<button class="btn add">New Post</button>
</a><br><br>
<a href="/dashboard/" class="link">Dashboard</a><br>
<a href="/announcements/" class="link">Announcements</a><br>
<a href="/assignments/" class="link">Assignments</a><br>
<a href="/calendar/" class="link">Calendar</a>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}Add Post{% endblock %}
{% block heading %}<h1>Add a new post:</h1>{% endblock %}
{% block content %}
<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 heading %}
<h1>
{{ object.title }}
</h1>
{% endblock %}
{% block content %}
<p>
by {{ object.author.first_name }} {{ object.author.last_name }}<br>
{{ object.pub_datetime|date:"m/d/Y" }}, {{ object.pub_datetime|time:"g:iA" }}<br>
{{ object.body }}<br>
</p>
POST REPLIES:<br>
{% for reply in replys %}
{% if reply.forumpost == object %}
by {{ reply.author.first_name }} {{ reply.author.last_name }}<br>
{{ reply.pub_datetime|date:"m/d/Y" }}, {{ reply.pub_datetime|time:"g:iA" }}<br>
{{ reply.body }}<br><br>
{% endif %}
{% endfor %}
{% block footing %}
<a href="{% url 'forum:forumpost-update' object.pk %}">
<button class="btn edit">Edit Post</button>
</a>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}Edit Post{% endblock %}
{% block heading %}Edit Post:{% endblock %}
{% block content %}
<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 django.urls import path
from .views import index from .views import (forum, ForumPostDetailView,
ForumPostCreateView, ForumPostUpdateView)
urlpatterns = [ 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-create'),
path('forumposts/<int:pk>/edit/',
ForumPostUpdateView.as_view(),
name='forumpost-update'),
] ]
app_name = "forum" app_name = "forum"
\ No newline at end of file
from django.http import HttpResponse from django.shortcuts import render
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from .models import ForumPost, Reply from .models import ForumPost, Reply
def index(request): def forum(request):
return_string = "<p>Widget's Forum</p>Forum Posts:" posts = ForumPost.objects.all()
for post in ForumPost.objects.all(): return render(request, 'forum/forum.html', {'posts': posts})
counter = 0
for reply in Reply.objects.all():
if counter == 0 and post.title == reply.forumpost.title: class ForumPostDetailView(DetailView):
return_string += '<br>{} by {} {} posted {}<br>{}'.format( model = ForumPost
reply.forumpost.title, template_name = 'forum/forumpost-details.html'
reply.forumpost.author.first_name,
reply.forumpost.author.last_name, def get_context_data(self, **kwargs):
reply.forumpost.pub_datetime.strftime('%m/%d/%Y, %H:%M %p:'), context = super().get_context_data(**kwargs)
reply.forumpost.body, context['replys'] = Reply.objects.all()
) return context
return_string += '<br>Reply by {} {} posted {}<br>{}'.format(
reply.author.first_name,
reply.author.last_name, class ForumPostCreateView(CreateView):
reply.pub_datetime.strftime('%m/%d/%Y, %H:%M %p:'), model = ForumPost
reply.body, fields = '__all__'
) template_name = 'forum/forumpost-add.html'
counter += 1
continue
elif counter == 1 and post.title == reply.forumpost.title: class ForumPostUpdateView(UpdateView):
return_string += '<br>Reply by {} {} posted {}<br>{}'.format( model = ForumPost
reply.author.first_name, fields = '__all__'
reply.author.last_name, template_name = 'forum/forumpost-edit.html'
reply.pub_datetime.strftime('%m/%d/%Y, %H:%M %p:'), \ No newline at end of file
reply.body,
)
continue
elif counter == 1 and post.title != reply.forumpost.title:
return_string += '<br>'
break
else:
continue
html_string = '<html><body>{}</body></html>'.format(return_string)
return HttpResponse(html_string)
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