Commit e05c0b182a58a2ed90f97913ba998b7f7075ce33
1 parent
5f472d6b
Exists in
master
and in
3 other branches
Adding general post edit
Showing
7 changed files
with
128 additions
and
12 deletions
Show diff stats
... | ... | @@ -0,0 +1,20 @@ |
1 | +# -*- coding: utf-8 -*- | |
2 | +# Generated by Django 1.10 on 2017-02-06 21:12 | |
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 | + ('mural', '0004_auto_20170206_1348'), | |
12 | + ] | |
13 | + | |
14 | + operations = [ | |
15 | + migrations.AddField( | |
16 | + model_name='mural', | |
17 | + name='edited', | |
18 | + field=models.BooleanField(default=False, verbose_name='Edited'), | |
19 | + ), | |
20 | + ] | ... | ... |
mural/models.py
... | ... | @@ -3,6 +3,8 @@ from django.core import validators |
3 | 3 | from django.core.exceptions import ValidationError |
4 | 4 | from django.utils.translation import ugettext_lazy as _ |
5 | 5 | |
6 | +from topics.decorators import always_as_child | |
7 | + | |
6 | 8 | from categories.models import Category |
7 | 9 | from subjects.models import Subject |
8 | 10 | from topics.models import KnowsChild, Resource |
... | ... | @@ -22,17 +24,31 @@ class Mural(KnowsChild): |
22 | 24 | user = models.ForeignKey(User, verbose_name = _('User'), related_name = "post_user", null = True) |
23 | 25 | create_date = models.DateTimeField(_('Create Date'), auto_now_add = True) |
24 | 26 | last_update = models.DateTimeField(_('Last Update'), auto_now = True) |
27 | + edited = models.BooleanField(_('Edited'), default = False) | |
28 | + | |
29 | + @always_as_child | |
30 | + def update_link(self): | |
31 | + pass | |
25 | 32 | |
26 | 33 | class GeneralPost(Mural): |
27 | 34 | space = models.IntegerField(_('Space'), default = 0, blank = True) |
28 | 35 | |
36 | + def update_link(self): | |
37 | + return "mural:update_general" | |
38 | + | |
29 | 39 | class CategoryPost(Mural): |
30 | 40 | space = models.ForeignKey(Category, verbose_name = ('Category'), related_name = 'post_category') |
31 | 41 | |
42 | + def update_link(self): | |
43 | + return "" | |
44 | + | |
32 | 45 | class SubjectPost(Mural): |
33 | 46 | space = models.ForeignKey(Subject, verbose_name = _('Subject'), related_name = 'post_subject') |
34 | 47 | resource = models.ForeignKey(Resource, verbose_name = _('Resource'), related_name = 'post_resource', null = True) |
35 | 48 | |
49 | + def update_link(self): | |
50 | + return "" | |
51 | + | |
36 | 52 | class Comment(models.Model): |
37 | 53 | comment = models.TextField(_('Comment'), blank = True) |
38 | 54 | image = models.ImageField(verbose_name = _('Image'), null=True, blank = True, upload_to = 'posts/comments/', validators = [validate_img_extension]) |
... | ... | @@ -41,12 +57,18 @@ class Comment(models.Model): |
41 | 57 | create_date = models.DateTimeField(_('Create Date'), auto_now_add = True) |
42 | 58 | last_update = models.DateTimeField(_('Last Update'), auto_now = True) |
43 | 59 | |
60 | +""" | |
61 | + Model to handle posts visualizations | |
62 | +""" | |
44 | 63 | class MuralVisualizations(models.Model): |
45 | 64 | viewed = models.BooleanField(_('Viewed'), default = False) |
46 | 65 | post = models.ForeignKey(Mural, verbose_name = _('Post'), related_name = 'visualization_post', null = True) |
47 | 66 | comment = models.ForeignKey(Comment, verbose_name = _('Comment'), related_name = 'visualization_comment', null = True) |
48 | 67 | user = models.ForeignKey(User, verbose_name = _('User'), related_name = "visualization_user", null = True) |
49 | 68 | |
69 | +""" | |
70 | + Model to handle users favorite posts | |
71 | +""" | |
50 | 72 | class MuralFavorites(models.Model): |
51 | 73 | post = models.ForeignKey(Mural, verbose_name = _('Post'), related_name = 'favorites_post', null = True) |
52 | 74 | user = models.ForeignKey(User, verbose_name = _('User'), related_name = "favorites_user", null = True) |
53 | 75 | \ No newline at end of file | ... | ... |
mural/templates/mural/_view.html
1 | 1 | {% load i18n mural_filters %} |
2 | 2 | |
3 | -<div class="row panel panel-default"> | |
3 | +<div id="post-{{ post.id }}" class="row panel panel-default"> | |
4 | 4 | <div class="panel-body post"> |
5 | 5 | <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 post-img"> |
6 | 6 | <img src="{{ post.user.image_url }}" class="img-responsive" /> |
... | ... | @@ -18,7 +18,7 @@ |
18 | 18 | <i class="fa fa-ellipsis-v" aria-hidden="true"></i> |
19 | 19 | </button> |
20 | 20 | <ul class="dropdown-menu pull-right" aria-labelledby="moreActions"> |
21 | - <li><a href=""><i class="fa fa-pencil fa-fw" aria-hidden="true"></i> {% trans 'Edit' %}</a></li> | |
21 | + <li><a onclick="editPost($(this));" data-url="{% url post.update_link post.id %}" data-post="{{ post.id }}"><i class="fa fa-pencil fa-fw" aria-hidden="true"></i> {% trans 'Edit' %}</a></li> | |
22 | 22 | <li> |
23 | 23 | <a href="" aria-hidden="true"><i class="fa fa-trash fa-fw" aria-hidden="true"></i> {% trans 'Remove' %}</a></li> |
24 | 24 | </ul> |
... | ... | @@ -32,7 +32,7 @@ |
32 | 32 | </h4> |
33 | 33 | <p class="time"> |
34 | 34 | <i class="fa fa-clock-o"></i> |
35 | - {% trans 'In' %} {{ post.last_update }} | |
35 | + {% trans 'In' %} {{ post.last_update }} {{ post|is_edited }} | |
36 | 36 | </p> |
37 | 37 | |
38 | 38 | {% autoescape off %} | ... | ... |
mural/templates/mural/list.html
... | ... | @@ -65,11 +65,11 @@ |
65 | 65 | |
66 | 66 | $('#post-modal-form').modal('show'); |
67 | 67 | } |
68 | - }) | |
68 | + }); | |
69 | 69 | }); |
70 | 70 | }); |
71 | 71 | |
72 | - function setPostFormSubmit() { | |
72 | + function setPostFormSubmit(post = "") { | |
73 | 73 | var frm = $('#post-form'); |
74 | 74 | |
75 | 75 | frm.submit(function () { |
... | ... | @@ -82,9 +82,17 @@ |
82 | 82 | dataType: "json", |
83 | 83 | async: false, |
84 | 84 | success: function (data) { |
85 | - $('.posts').prepend(data.view); | |
85 | + if (post != "") { | |
86 | + var old = $("#post-" + post); | |
87 | + | |
88 | + old.before(data.view); | |
89 | + | |
90 | + old.remove(); | |
91 | + } else { | |
92 | + $('.posts').prepend(data.view); | |
86 | 93 | |
87 | - $('.no-subjects').attr('style', 'display:none'); | |
94 | + $('.no-subjects').attr('style', 'display:none'); | |
95 | + } | |
88 | 96 | |
89 | 97 | $('#post-modal-form').modal('hide'); |
90 | 98 | |
... | ... | @@ -92,7 +100,7 @@ |
92 | 100 | }, |
93 | 101 | error: function(data) { |
94 | 102 | $("#post-modal-form").html(data.responseText); |
95 | - setPostFormSubmit(); | |
103 | + setPostFormSubmit(post); | |
96 | 104 | }, |
97 | 105 | cache: false, |
98 | 106 | contentType: false, |
... | ... | @@ -123,7 +131,25 @@ |
123 | 131 | btn.attr('data-original-title', response.label); |
124 | 132 | } |
125 | 133 | }); |
134 | + } | |
135 | + | |
136 | + function editPost(btn) { | |
137 | + var url = btn.data('url'); | |
138 | + var post = btn.data('post'); | |
139 | + | |
140 | + console.log(url); | |
141 | + console.log(post); | |
126 | 142 | |
143 | + $.ajax({ | |
144 | + url: url, | |
145 | + success: function (data) { | |
146 | + $('#post-modal-form').html(data); | |
147 | + | |
148 | + setPostFormSubmit(post); | |
149 | + | |
150 | + $('#post-modal-form').modal('show'); | |
151 | + } | |
152 | + }); | |
127 | 153 | } |
128 | 154 | </script> |
129 | 155 | {% endblock %} |
130 | 156 | \ No newline at end of file | ... | ... |
mural/templatetags/mural_filters.py
... | ... | @@ -5,6 +5,13 @@ from mural.models import MuralFavorites |
5 | 5 | |
6 | 6 | register = template.Library() |
7 | 7 | |
8 | +@register.filter(name = 'is_edited') | |
9 | +def is_edited(post): | |
10 | + if post.edited: | |
11 | + return _("(Edited)") | |
12 | + | |
13 | + return "" | |
14 | + | |
8 | 15 | @register.filter(name = 'action_icon') |
9 | 16 | def action_icon(action): |
10 | 17 | if action == "comment": | ... | ... |
mural/urls.py
... | ... | @@ -4,6 +4,7 @@ from . import views |
4 | 4 | urlpatterns = [ |
5 | 5 | url(r'^$', views.GeneralIndex.as_view(), name='manage_general'), |
6 | 6 | url(r'^create_gen/$', views.GeneralCreate.as_view(), name='create_general'), |
7 | - url(r'^render_post/([\w_-]+)/$', views.render_gen_post, name='render_post_general'), | |
7 | + url(r'^update_gen/(?P<pk>[\w_-]+)/$', views.GeneralUpdate.as_view(), name='update_general'), | |
8 | + url(r'^render_post/([\w_-]+)/([\w_-]+)/$', views.render_gen_post, name='render_post_general'), | |
8 | 9 | url(r'^favorite/([\w_-]+)/$', views.favorite, name='favorite'), |
9 | 10 | ] |
10 | 11 | \ No newline at end of file | ... | ... |
mural/views.py
... | ... | @@ -97,17 +97,57 @@ class GeneralCreate(LoginRequiredMixin, generic.edit.CreateView): |
97 | 97 | return context |
98 | 98 | |
99 | 99 | def get_success_url(self): |
100 | - return reverse_lazy('mural:render_post_general', args = (self.object.id, )) | |
100 | + return reverse_lazy('mural:render_post_general', args = (self.object.id, 'create', )) | |
101 | 101 | |
102 | -def render_gen_post(request, post): | |
102 | +class GeneralUpdate(LoginRequiredMixin, generic.UpdateView): | |
103 | + login_url = reverse_lazy("users:login") | |
104 | + redirect_field_name = 'next' | |
105 | + | |
106 | + template_name = 'mural/_form.html' | |
107 | + model = GeneralPost | |
108 | + form_class = GeneralPostForm | |
109 | + | |
110 | + def form_invalid(self, form): | |
111 | + context = super(GeneralUpdate, self).form_invalid(form) | |
112 | + context.status_code = 400 | |
113 | + | |
114 | + return context | |
115 | + | |
116 | + def form_valid(self, form): | |
117 | + self.object = form.save(commit = False) | |
118 | + | |
119 | + self.object.edited = True | |
120 | + | |
121 | + self.object.save() | |
122 | + | |
123 | + return super(GeneralUpdate, self).form_valid(form) | |
124 | + | |
125 | + def get_context_data(self, *args, **kwargs): | |
126 | + context = super(GeneralUpdate, self).get_context_data(*args, **kwargs) | |
127 | + | |
128 | + context['form_url'] = reverse_lazy("mural:update_general", args = (), kwargs = {'pk': self.object.id}) | |
129 | + | |
130 | + return context | |
131 | + | |
132 | + def get_success_url(self): | |
133 | + return reverse_lazy('mural:render_post_general', args = (self.object.id, 'update', )) | |
134 | + | |
135 | +def render_gen_post(request, post, msg): | |
103 | 136 | post = get_object_or_404(GeneralPost, id = post) |
104 | 137 | |
105 | 138 | context = {} |
106 | 139 | context['post'] = post |
107 | 140 | |
141 | + msg = "" | |
142 | + | |
143 | + if msg == 'create': | |
144 | + msg = _('Your post was published successfully!') | |
145 | + else: | |
146 | + msg = _('Your post was edited successfully!') | |
147 | + | |
108 | 148 | html = render_to_string("mural/_view.html", context, request) |
109 | 149 | |
110 | - return JsonResponse({'message': _('Your post was published successfully!'), 'view': html}) | |
150 | + return JsonResponse({'message': msg, 'view': html}) | |
111 | 151 | |
112 | 152 | @login_required |
113 | 153 | def favorite(request, post): | ... | ... |