Commit 92de728a479266b988b8ab4f46d1b64352114dbe
1 parent
fc50841e
Exists in
master
and in
5 other branches
Adding form to reply post [Issue: #73]
Showing
8 changed files
with
104 additions
and
12 deletions
Show diff stats
forum/forms.py
1 | 1 | from django import forms |
2 | 2 | from django.utils.translation import ugettext_lazy as _ |
3 | -from .models import Forum | |
3 | +from .models import Forum, PostAnswer | |
4 | 4 | |
5 | 5 | class ForumForm(forms.ModelForm): |
6 | 6 | |
... | ... | @@ -17,4 +17,16 @@ class ForumForm(forms.ModelForm): |
17 | 17 | } |
18 | 18 | widgets = { |
19 | 19 | 'description': forms.Textarea(attrs={'cols': 80, 'rows': 5}), |
20 | + } | |
21 | + | |
22 | +class PostAnswerForm(forms.ModelForm): | |
23 | + | |
24 | + class Meta: | |
25 | + model = PostAnswer | |
26 | + fields = ('message', ) | |
27 | + labels = { | |
28 | + 'message': _('Message') | |
29 | + } | |
30 | + widgets = { | |
31 | + 'message': forms.Textarea(attrs={'cols': 80, 'rows': 3}), | |
20 | 32 | } |
21 | 33 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,30 @@ |
1 | +# -*- coding: utf-8 -*- | |
2 | +# Generated by Django 1.10 on 2016-09-28 18:58 | |
3 | +from __future__ import unicode_literals | |
4 | + | |
5 | +from django.db import migrations, models | |
6 | + | |
7 | + | |
8 | +class Migration(migrations.Migration): | |
9 | + | |
10 | + dependencies = [ | |
11 | + ('forum', '0003_forum_create_date'), | |
12 | + ] | |
13 | + | |
14 | + operations = [ | |
15 | + migrations.AddField( | |
16 | + model_name='forum', | |
17 | + name='modification_date', | |
18 | + field=models.DateTimeField(auto_now=True, verbose_name='Modification Date'), | |
19 | + ), | |
20 | + migrations.AddField( | |
21 | + model_name='post', | |
22 | + name='modification_date', | |
23 | + field=models.DateTimeField(auto_now=True, verbose_name='Modification Date'), | |
24 | + ), | |
25 | + migrations.AddField( | |
26 | + model_name='postanswer', | |
27 | + name='modification_date', | |
28 | + field=models.DateTimeField(auto_now=True, verbose_name='Modification Date'), | |
29 | + ), | |
30 | + ] | ... | ... |
forum/models.py
... | ... | @@ -12,6 +12,7 @@ It works like a 'topic' of forum, which users can post to it and answer posts of |
12 | 12 | """ |
13 | 13 | class Forum(Activity): |
14 | 14 | description = models.TextField(_('Description'), blank = True) |
15 | + modification_date = models.DateTimeField(_('Modification Date'), auto_now = True) | |
15 | 16 | create_date = models.DateTimeField(_('Create Date'), auto_now_add = True) |
16 | 17 | |
17 | 18 | class Meta: |
... | ... | @@ -28,6 +29,7 @@ It represents a post made in a forum (topic) |
28 | 29 | class Post(models.Model): |
29 | 30 | user = models.ForeignKey(User, verbose_name = _('Autor')) |
30 | 31 | message = models.TextField(_('Post message'), blank = False) |
32 | + modification_date = models.DateTimeField(_('Modification Date'), auto_now = True) | |
31 | 33 | post_date = models.DateTimeField(_('Post Date'), auto_now_add = True) |
32 | 34 | forum = models.ForeignKey(Forum, verbose_name = _('Forum')) |
33 | 35 | |
... | ... | @@ -46,6 +48,7 @@ class PostAnswer(models.Model): |
46 | 48 | user = models.ForeignKey(User, verbose_name = _('Autor')) |
47 | 49 | post = models.ForeignKey(Post, verbose_name = _('Post')) |
48 | 50 | message = models.TextField(_('Answer message'), blank = False) |
51 | + modification_date = models.DateTimeField(_('Modification Date'), auto_now = True) | |
49 | 52 | answer_date = models.DateTimeField(_('Answer Date'), auto_now_add = True) |
50 | 53 | |
51 | 54 | class Meta: | ... | ... |
forum/static/js/forum.js
... | ... | @@ -15,15 +15,15 @@ function showForum(url, forum_id) { |
15 | 15 | $('#forumModal').modal(); |
16 | 16 | } |
17 | 17 | |
18 | -function getForm(url) { | |
18 | +function answer(id, url) { | |
19 | 19 | $.ajax({ |
20 | 20 | url: url, |
21 | 21 | success: function(data) { |
22 | - $(".forum_form").html(data); | |
22 | + $("#post_"+id).find(".answer_post").html(data); | |
23 | 23 | } |
24 | 24 | }); |
25 | 25 | |
26 | - $(".forum_form").show(); | |
26 | + $("#post_"+id).find(".answer_post").show(); | |
27 | 27 | } |
28 | 28 | |
29 | 29 | function showPosts(url, forum) { | ... | ... |
forum/templates/post/post_list.html
... | ... | @@ -2,12 +2,15 @@ |
2 | 2 | |
3 | 3 | {% if posts|length > 0 %} |
4 | 4 | {% for post in posts %} |
5 | - <div class="row"> | |
6 | - <div class="col-sm-12 col-xs-12"> | |
5 | + <div class="row"> | |
6 | + <div id="post_{{ post.id }}" class="col-sm-12 col-xs-12"> | |
7 | 7 | <h3 class="user-name"> |
8 | 8 | {{ post.user }} |
9 | - {% if request.user|has_role:'system_admin' or request.user|has_role:'professor' and request.user == post.user %} | |
10 | - <div class="pull-right"> | |
9 | + <div class="pull-right"> | |
10 | + <a href="javascript:answer('{{ post.id }}', '{% url 'forum:reply_post' %}');"> | |
11 | + <i class="material-icons">reply</i> | |
12 | + </a> | |
13 | + {% if request.user|has_role:'system_admin' or request.user|has_role:'professor' and request.user == post.user %} | |
11 | 14 | <div class="btn-group icon-more-horiz"> |
12 | 15 | <a class="btn btn-default btn-xs dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> |
13 | 16 | <i class="material-icons">more_horiz</i> |
... | ... | @@ -17,13 +20,14 @@ |
17 | 20 | <li><a href="javascript:void(0)"><i class="material-icons">delete_sweep</i> {% trans 'Remove' %}</a></li> |
18 | 21 | </ul> |
19 | 22 | </div> |
20 | - </div> | |
21 | - {% endif %} | |
23 | + {% endif %} | |
24 | + </div> | |
22 | 25 | </h3> |
23 | 26 | <div class="card-data"> |
24 | 27 | <p class="comment-date"><i class="fa fa-clock-o"></i> {{ post.post_date }}</p> |
25 | 28 | </div> |
26 | 29 | <p class="comment-text">{{ post.message|linebreaks }}</p> |
30 | + <div class="answer_post"></div> | |
27 | 31 | </div> |
28 | 32 | </div> |
29 | 33 | {% list_post_answer request post %} | ... | ... |
... | ... | @@ -0,0 +1,36 @@ |
1 | +{% load static i18n %} | |
2 | +{% load widget_tweaks %} | |
3 | + | |
4 | +<form method="post" action="#" enctype="multipart/form-data"> | |
5 | + {% csrf_token %} | |
6 | + {% for field in form %} | |
7 | + <div class="form-group {% if form.has_error %} has-error {% endif %} is-fileinput"> | |
8 | + <div class="input-group"> | |
9 | + <label for="{{ field.auto_id }}">{{ field.label }}</label> | |
10 | + {% render_field field class='form-control' %} | |
11 | + <span class="help-block">{{ field.help_text }}</span> | |
12 | + {% if field.errors %} | |
13 | + <div class="row"> | |
14 | + <br /> | |
15 | + <div class="alert alert-danger alert-dismissible" role="alert"> | |
16 | + <button type="button" class="close" data-dismiss="alert" aria-label="Close"> | |
17 | + <span aria-hidden="true">×</span> | |
18 | + </button> | |
19 | + <ul> | |
20 | + {% for error in field.errors %} | |
21 | + <li>{{ error }}</li> | |
22 | + {% endfor %} | |
23 | + </ul> | |
24 | + </div> | |
25 | + </div> | |
26 | + {% endif %} | |
27 | + <span class="input-group-btn"> | |
28 | + <button type="button" class="btn btn-fab btn-fab-mini"> | |
29 | + <i class="material-icons">send</i> | |
30 | + </button> | |
31 | + </span> | |
32 | + </div> | |
33 | + </div> | |
34 | + {% endfor %} | |
35 | + | |
36 | +</form> | |
0 | 37 | \ No newline at end of file | ... | ... |
forum/urls.py
... | ... | @@ -8,4 +8,5 @@ urlpatterns = [ |
8 | 8 | url(r'^create$', views.CreateForumView.as_view(), name='create'), |
9 | 9 | url(r'^posts$', views.PostIndex.as_view(), name='posts'), |
10 | 10 | url(r'^post_answers$', views.PostAnswerIndex.as_view(), name='post_answers'), |
11 | + url(r'^reply_post$', views.CreatePostAnswerView.as_view(), name='reply_post'), | |
11 | 12 | ] | ... | ... |
forum/views.py
... | ... | @@ -7,7 +7,7 @@ from django.contrib.auth.mixins import LoginRequiredMixin |
7 | 7 | from .models import Forum, Post, PostAnswer |
8 | 8 | from courses.models import Topic |
9 | 9 | |
10 | -from .forms import ForumForm | |
10 | +from .forms import ForumForm, PostAnswerForm | |
11 | 11 | |
12 | 12 | class ForumIndex(LoginRequiredMixin, generic.ListView): |
13 | 13 | login_url = reverse_lazy("core:home") |
... | ... | @@ -55,4 +55,10 @@ class PostAnswerIndex(LoginRequiredMixin, generic.ListView): |
55 | 55 | |
56 | 56 | context = PostAnswer.objects.filter(post = post) |
57 | 57 | |
58 | - return context | |
59 | 58 | \ No newline at end of file |
59 | + return context | |
60 | + | |
61 | +class CreatePostAnswerView(LoginRequiredMixin, generic.edit.CreateView): | |
62 | + | |
63 | + template_name = 'post_answers/post_answer_form.html' | |
64 | + form_class = PostAnswerForm | |
65 | + success_url = reverse_lazy('forum:index') | |
60 | 66 | \ No newline at end of file | ... | ... |