From 912a5dcb2502673af5bb54cc2febce8d49042e95 Mon Sep 17 00:00:00 2001 From: Zak Timson Date: Thu, 11 Jan 2018 00:58:07 -0500 Subject: [PATCH] Added forums --- OACPL/settings.base.py | 9 ++- OACPL/urls.py | 3 +- forum/admin.py | 8 +-- forum/forms.py | 29 ++++++++ forum/templates/post.html | 143 +++++++++++++++++++++++++++++--------- forum/templates/view.html | 60 ++++++++-------- forum/views.py | 64 ++++++++++------- requirements.txt | 3 +- 8 files changed, 223 insertions(+), 96 deletions(-) create mode 100644 forum/forms.py diff --git a/OACPL/settings.base.py b/OACPL/settings.base.py index abbbb27..9a36762 100644 --- a/OACPL/settings.base.py +++ b/OACPL/settings.base.py @@ -48,6 +48,7 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'django_forms_bootstrap', 'expert_witnesses', 'forum.apps.ForumConfig', 'main.apps.MainConfig', @@ -94,12 +95,14 @@ WSGI_APPLICATION = 'OACPL.wsgi.application' DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': 'postgres', + 'USER': 'postgres', + 'HOST': 'db', + 'PORT': 5432, } } - # Password validation # https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators diff --git a/OACPL/urls.py b/OACPL/urls.py index 45331d0..52dc719 100644 --- a/OACPL/urls.py +++ b/OACPL/urls.py @@ -31,8 +31,7 @@ urlpatterns = [ url(r'^contact/', main.views.contact, name='contact'), url(r'^experts/(?P\d+)', expert_witnesses.views.viewer, name='expert'), url(r'^experts/', expert_witnesses.views.browser, name='experts'), - url(r'^forum/post/(?P\d*)', forum.views.post, name='post'), - url(r'^forum/comment', forum.views.comment, name='comment'), + url(r'^forum/post/(?P\d*)', forum.views.viewPost, name='post'), url(r'^forum/(?P\d*)?', forum.views.view, name='forum'), url(r'^login/', main.views.login, name='login'), url(r'^logout/', main.views.logout, name='logout'), diff --git a/forum/admin.py b/forum/admin.py index 6db8ede..3a73d08 100644 --- a/forum/admin.py +++ b/forum/admin.py @@ -8,15 +8,15 @@ admin.site.register(Thread) @admin.register(Post) class PostAdmin(admin.ModelAdmin): - list_display = ['title', 'topic', 'creator', 'created'] - search_fields = ['title', 'topic', 'creator', 'created'] + list_display = ['title', 'resolved', 'topic', 'creator', 'created'] + search_fields = ['title', 'resolved', 'topic', 'creator', 'created'] def get_form(self, request, obj=None, **kwargs): if obj: - self.fields = ['creator', 'created', 'topic', 'title', 'question'] + self.fields = ['creator', 'created', 'resolved', 'title', 'topic', 'question'] self.readonly_fields = ['creator', 'created'] else: - self.fields = ['topic', 'title', 'question'] + self.fields = ['title', 'topic', 'question'] self.readonly_fields = [] return super(PostAdmin, self).get_form(request, obj, **kwargs) diff --git a/forum/forms.py b/forum/forms.py new file mode 100644 index 0000000..fef7e85 --- /dev/null +++ b/forum/forms.py @@ -0,0 +1,29 @@ +from django import forms + +from tinymce import TinyMCE + +from .models import Comment, Post, Thread + + +class CommentForm(forms.ModelForm): + reply = forms.CharField(widget=TinyMCE(mce_attrs={'height': 200})) + + class Meta: + model = Comment + fields = ['reply'] + + +class CreatePostForm(forms.ModelForm): + question = forms.CharField(widget=TinyMCE(mce_attrs={'height': 200})) + + class Meta: + model = Post + fields = ['title', 'topic', 'question'] + + +class EditPostForm(forms.ModelForm): + question = forms.CharField(widget=TinyMCE(mce_attrs={'height': 200})) + + class Meta: + model = Post + fields = ['question'] diff --git a/forum/templates/post.html b/forum/templates/post.html index 971fdc5..aecebb5 100644 --- a/forum/templates/post.html +++ b/forum/templates/post.html @@ -6,45 +6,126 @@
+ {% if post.creator == user and not post.resolved or perms.forum.change_post and not post.resolved %} +
+ + {% if post.creator == user or perms.forum.delete_post %} + + {% endif %} + +
+ {% endif %}

{{ post.title }}

- Asked By: {{ post.creator }} + + {% if post.resolved %}Closed {% endif %} + Asked By: {{ post.creator }}, {{ post.created | date }} + +
-
- {{ post }} +
+ {{ post.question | safe }}
- {% for comment in comments %} -
-
- Answered By: {{ comment.creator }} -
- {% autoescape off %} - {{ comment }} - {% endautoescape %} -
-
+ {% if comments %} +
+

Answers

+
    + {% for comment in comments %} +
  • + {{ comment.creator }}, {{ comment.created | date }} + {% autoescape off %} + {{ comment }} + {% endautoescape %} +
  • + {% endfor %} +
- {% endfor %} + {% endif %}
-
-
-
Comment
-
- {% csrf_token %} - - - -
- {% if not request.user.is_authenticated %} - You must login to comment - {% endif %} - {% if request.user.is_authenticatednot and perms.form.add_comment %} - You do not have permission to comment - {% endif %} + {% if not post.resolved and perms.forum.add_comment %} +
+
+
Comment
+
+
+ {% csrf_token %} + + {{ form.reply }} + +
+
+
+ {% endif %} +
+
+ + + diff --git a/forum/templates/view.html b/forum/templates/view.html index 76950c8..cb59caa 100644 --- a/forum/templates/view.html +++ b/forum/templates/view.html @@ -1,14 +1,23 @@ {% extends 'base.html' %} +{% load bootstrap_tags %} {% block head %} {% endblock %} @@ -42,17 +51,22 @@
{% endif %} -
-
-

Posts

- + {% if posts %} +
+
+

{% if thread %}"{{ thread }}" {% endif %}Posts

+ +
-
+ {% endif %}
@@ -66,29 +80,13 @@ diff --git a/forum/views.py b/forum/views.py index af44d9d..2f641ea 100644 --- a/forum/views.py +++ b/forum/views.py @@ -1,40 +1,56 @@ from django.shortcuts import render, redirect +from .forms import CommentForm, CreatePostForm, EditPostForm from .models import Thread, Post, Comment def view(request, thread=None): - myPosts = None + if request.method == 'POST': + form = CreatePostForm(request.POST) + if form.is_valid(): + instance = form.save(commit=False) + instance.creator = request.user + instance.save() + return redirect('post', post=instance.id) + else: + form = CreatePostForm() + + my_posts = None if request.user.is_authenticated(): - myPosts = Post.objects.filter(creator=request.user) + my_posts = Post.objects.filter(creator=request.user, resolved=False) if not thread: threads = Thread.objects.all() - posts = Post.objects.order_by('created')[:10] + thread_name = None + posts = Post.objects.filter(resolved=False).order_by('-created')[:10] else: threads = None - posts = Post.objects.filter(topic=thread) - return render(request, 'view.html', {'threads': threads, 'posts': posts, 'myPosts': myPosts}) + thread_name = Thread.objects.get(id=thread).topic + posts = Post.objects.filter(topic=thread).order_by('-created') + return render(request, 'view.html', {'threads': threads, 'posts': posts, 'myPosts': my_posts, 'thread': thread_name, 'form': form}) -def post(request, post): +def viewPost(request, post): this_post = Post.objects.get(id=post) - comments = Comment.objects.filter(post=post) - return render(request, 'post.html', {'post': this_post, 'comments': comments}) + if request.method == 'POST': + if request.POST.get('request') == 'comment': + form = CommentForm(request.POST) + Comment.objects.create(post_id=post, + reply=form.data.get('reply'), + creator=request.user) + elif request.POST.get('request') == 'edit': + form = EditPostForm(request.POST, instance=this_post) + form.save() + elif request.POST.get('request') == 'resolve': + if this_post.creator == request.user or request.user.has_perm('forum.change_post'): + this_post.resolved = True + this_post.save() + elif request.POST.get('request') == 'delete': + if this_post.creator == request.user or request.user.has_perm('forum.delete_post'): + this_post.delete() + return redirect('forum') -def create(request): - pass - - -def comment(request): - success = False - post = Post.objects.get(id=request.POST.get('post')) - comments = Comment.objects.filter(post=post) - try: - - success = Comment.objects.create(post=post, - reply=request.POST.get('comment'), - creator=request.user) - success.save() - finally: - return redirect('forum', post.id) + form = CommentForm() + edit_form = EditPostForm(instance=this_post) + comments = Comment.objects.filter(post=this_post) + return render(request, 'post.html', {'post': this_post, 'comments': comments, 'form': form, 'editForm': edit_form}) diff --git a/requirements.txt b/requirements.txt index de69a84..d1d4be8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ bootstrap-admin==0.3.7.1 Django==1.11.5 +django-forms-bootstrap==3.1.0 +django-tinymce4-lite==1.7.0 Pillow==4.2.1 requests==2.11.1 -django-tinymce4-lite==1.7.0