Commit 332712dc authored by nheoxoz's avatar nheoxoz

Merge branch 'origin/forumv2'

parents 7eba6986 b33ad66c
from django.db import models
from django.urls import reverse
class ForumPost(models.Model):
......@@ -9,7 +10,7 @@ class ForumPost(models.Model):
on_delete=models.CASCADE,
related_name='forumpost_author'
)
pub_datetime = models.DateTimeField()
pub_datetime = models.DateTimeField(auto_now=True)
def __str__(self):
return '{} by {} posted {}: {}'.format(
......@@ -19,6 +20,12 @@ class ForumPost(models.Model):
self.body
)
def get_absolute_url(self):
return reverse(
'forum:forumpost-details',
kwargs={'pk': self.pk}
)
class Reply(models.Model):
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 .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-create'),
path('forumposts/<int:pk>/edit/',
ForumPostUpdateView.as_view(),
name='forumpost-update'),
]
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
def index(request):
return_string = "<p>Widget's Forum</p>Forum Posts:"
for post in ForumPost.objects.all():
counter = 0
for reply in Reply.objects.all():
if counter == 0 and post.title == reply.forumpost.title:
return_string += '<br>{} by {} {} posted {}<br>{}'.format(
reply.forumpost.title,
reply.forumpost.author.first_name,
reply.forumpost.author.last_name,
reply.forumpost.pub_datetime.strftime('%m/%d/%Y, %H:%M %p:'),
reply.forumpost.body,
)
return_string += '<br>Reply by {} {} posted {}<br>{}'.format(
reply.author.first_name,
reply.author.last_name,
reply.pub_datetime.strftime('%m/%d/%Y, %H:%M %p:'),
reply.body,
)
counter += 1
continue
elif counter == 1 and post.title == reply.forumpost.title:
return_string += '<br>Reply by {} {} posted {}<br>{}'.format(
reply.author.first_name,
reply.author.last_name,
reply.pub_datetime.strftime('%m/%d/%Y, %H:%M %p:'),
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)
def forum(request):
posts = ForumPost.objects.all()
return render(request, 'forum/forum.html', {'posts': posts})
class ForumPostDetailView(DetailView):
model = ForumPost
template_name = 'forum/forumpost-details.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['replys'] = Reply.objects.all()
return context
class ForumPostCreateView(CreateView):
model = ForumPost
fields = '__all__'
template_name = 'forum/forumpost-add.html'
class ForumPostUpdateView(UpdateView):
model = ForumPost
fields = '__all__'
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