Add commenting form

This commit is contained in:
Jan Tuomi
2017-10-11 21:19:25 +03:00
parent 70ccc88c86
commit 438ae7e8f7
7 changed files with 104 additions and 12 deletions
+9 -1
View File
@@ -4,7 +4,8 @@ from django import forms
from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ValidationError
from webapp.models import OhlhafvChallenge, KaehmyForm
from webapp.models import OhlhafvChallenge, KaehmyForm, KaehmyMessage
class KaehmyForm_Form(forms.ModelForm):
@@ -27,6 +28,13 @@ class KaehmyForm_Form(forms.ModelForm):
raise ValidationError(_('Invalid value'))
class KaehmyCommentForm(forms.ModelForm):
class Meta:
model = KaehmyMessage
fields = ['name', 'email', 'message', 'parent']
class OhlhafvForm(forms.ModelForm):
"""Class representing Ohlhafv form."""
+10
View File
@@ -0,0 +1,10 @@
{% extends "base.html" %}
{% block navigation %}
{% endblock navigation %}
{% block content %}
<div class="alert alert-danger" role="alert">
{{ errors|safe }}
</div>
{% endblock content %}
+56 -7
View File
@@ -8,6 +8,17 @@
{% endblock %}
{% block content %}
<script>
function commentOn(id, op) {
setTimeout(function() {
document.getElementById("commentNameField").focus();
}, 50);
document.getElementById("collapse_add_comment").scrollIntoView();
document.getElementById("commentOP").innerHTML = op;
document.getElementById("commentId").value = id;
}
</script>
<div>
<div>
<h2>{% trans "All kaehmys" %}</h2>
@@ -17,6 +28,34 @@
<h6>{% trans "Total kaehmys:" %} {{ application_count }}</h6>
</div>
<div class="collapse" id="collapse_add_comment">
<div class="card">
<div class="card-block">
<form method="POST" action="/kaehmy/add_comment" class="form">{% csrf_token %}
<div class="form-group">
{% trans "Commenting on post by " %} <span id="commentOP"></span>
<input type="hidden" name="parent" id="commentId">
</div>
<div class="form-group">
<label>{% trans "Name" %}</label>
<input id="commentNameField" name="name" type="text" class="form-control" placeholder="Teemu Teekkari">
</div>
<div class="form-group">
<label>{% trans "Email" %}</label>
<input name="email" type="email" class="form-control" placeholder="teemu@teekka.ri">
</div>
<div class="form-group">
<label>{% trans "Comment" %}</label>
<textarea name="message" class="form-control" rows=3 placeholder="Hei!"></textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary">
</div>
</form>
</div>
</div>
</div>
{% for application in applications %}
<div class="card">
<div class="card-block">
@@ -31,16 +70,26 @@
<p>{{ application.text }}</p>
<a type="button" style="cursor: pointer" data-toggle="collapse" data-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
{% trans "Show comments" %} ({{ application.comment_count }})
</a>
<div class="collapse" id="collapseExample">
{% for message in application.messages.all %}
{% include "kaehmy_message.html" with messages=message.messages.all %}
{% endfor %}
{% if application.comment_count > 0 %}
<a type="button" style="cursor: pointer" data-toggle="collapse" data-target="#collapse_{{ application.id }}" aria-expanded="false" aria-controls="collapse_{{ application.id }}">
{% trans "Show comments" %} ({{ application.comment_count }})
</a>
{% endif %}
<div>
<a type="button" onclick="commentOn({{ application.id }}, '{{ application.name }}')" style="cursor: pointer" data-toggle="collapse" data-target="#collapse_add_comment" aria-expanded="false" aria-controls="collapse_add_comment">
{% trans "Add comment" %}
</a>
</div>
<div class="collapse" id="collapse_{{ application.id }}">
{% for message in application.messages.all %}
{% include "kaehmy_message.html" with messages=message.messages.all %}
{% endfor %}
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock content %}
+8 -2
View File
@@ -1,10 +1,16 @@
<div class="card" style="margin-top: 0.5rem">
{% load i18n %}
<div class="card" style="margin-top: 0.5rem; margin-bottom: 0">
<div class="card-block">
<h4>{{ message.name }}</h4>
<p>{{ message.message }}</p>
<h6 class="card-subtitle mb-2 text-muted">{{ message.timestamp }}</h6>
<div>
<a type="button" onclick="commentOn({{ message.id }}, '{{ message.name }}')" style="cursor: pointer" data-toggle="collapse" data-target="#collapse_add_comment" aria-expanded="false" aria-controls="collapse_add_comment">
{% trans "Reply" %}
</a>
</div>
<div>
{% for message in messages %}
{% include "kaehmy_message.html" with messages=message.messages.all %}
+1 -1
View File
@@ -3,6 +3,6 @@
<div class="header-content">
<div class="logo">
<a href="/"><img src="/static/img/logo_header.png" alt="Shiit ei kuvaa"></a>
<a href="/"><img src="/static/img/logo_header.png" alt="Aalto-yliopiston Sähköinsinöörikilta ry"></a>
</div>
</div>
+2
View File
@@ -21,6 +21,7 @@ from webapp.views import contact_view
from webapp.views import kaehmy_view
from webapp.views import kaehmy_list_view
from webapp.views import kaehmy_submit
from webapp.views import kaehmy_comment
urlpatterns = [
# main
@@ -49,6 +50,7 @@ urlpatterns = [
url(r'^kaehmy$', kaehmy_list_view),
url(r'^kaehmy/new', kaehmy_view),
url(r'^kaehmy/submit', kaehmy_submit),
url(r'^kaehmy/add_comment', kaehmy_comment),
# ohlhafv
url(r'^ohlhafv$', ohlhafv_view),
+18 -1
View File
@@ -10,7 +10,7 @@ from django.conf import settings
import logging
from webapp.models import CustomKaehmyRole
from webapp.models import OhlhafvChallenge, KaehmyForm
from webapp.forms import OhlhafvForm, KaehmyForm_Form
from webapp.forms import OhlhafvForm, KaehmyForm_Form, KaehmyCommentForm
from webapp.tables import OhlhafvTable
@@ -187,3 +187,20 @@ def kaehmy_list_view(request, *args, **kwargs):
'application_count': len(applications)
}
return render(request, 'kaehmy_list.html', context)
@ensure_csrf_cookie
@require_http_methods(["POST"])
def kaehmy_comment(request, *args, **kwargs):
"""POST endpoint for commenting"""
form = KaehmyCommentForm(request.POST)
if form.is_valid():
form.save()
return redirect('/kaehmy')
else:
print(form)
context = {
'errors': form.errors
}
return render(request, 'error.html', context)