Commit 7cc0ac9c9f8b70973071901240002e1cd0542fc3
1 parent
c7feb7db
Exists in
master
and in
3 other branches
Adding category post crud
Showing
9 changed files
with
307 additions
and
80 deletions
Show diff stats
amadeus/static/js/mural.js
1 | var new_posts = []; | 1 | var new_posts = []; |
2 | var new_comments = {}; | 2 | var new_comments = {}; |
3 | -// loadOnScroll handler | ||
4 | -var loadOnScroll = function() { | ||
5 | - // If the current scroll position is past out cutoff point... | ||
6 | - if ($(window).scrollTop() >= $(document).height() - $(window).height() - 10) { | ||
7 | - // temporarily unhook the scroll event watcher so we don't call a bunch of times in a row | ||
8 | - $(window).unbind(); | ||
9 | - // execute the load function below that will visit the view and return the content | ||
10 | - loadPosts(); | ||
11 | - } | ||
12 | -}; | ||
13 | - | ||
14 | -var loadPosts = function() { | ||
15 | - var loadUrl = $('.mural').data('url'), | ||
16 | - pageNum = $('.mural').data('page'), | ||
17 | - numberPages = $('.mural').data('pages'), | ||
18 | - favorites = $('.mural').data('fav'), | ||
19 | - mine = $('.mural').data('mine'), | ||
20 | - showing = new_posts.join(','); | ||
21 | - // Check if page is equal to the number of pages | ||
22 | - if (pageNum == numberPages) { | ||
23 | - return false | ||
24 | - } | ||
25 | - // Update the page number | ||
26 | - pageNum = pageNum + 1; | ||
27 | - | ||
28 | - $("#loading_posts").show(); | ||
29 | - // Configure the url we're about to hit | ||
30 | - setTimeout(function (){ | ||
31 | - $.ajax({ | ||
32 | - url: loadUrl, | ||
33 | - data: {'page': pageNum, "favorite": favorites, "mine": mine, "showing": showing}, | ||
34 | - success: function(data) { | ||
35 | - $("#loading_posts").hide(); | ||
36 | - | ||
37 | - $(".posts").append(data); | ||
38 | - | ||
39 | - $('.mural').data('page', pageNum); | ||
40 | - | ||
41 | - setTimeout(function () { postHeightLimits() }, 10); | ||
42 | - }, | ||
43 | - complete: function(data, textStatus){ | ||
44 | - // Turn the scroll monitor back on | ||
45 | - $(window).bind('scroll', loadOnScroll); | ||
46 | - } | ||
47 | - }); | ||
48 | - }, 1000) | ||
49 | -}; | ||
50 | 3 | ||
51 | $(function () { | 4 | $(function () { |
52 | - $(window).bind('scroll', loadOnScroll); | ||
53 | - | ||
54 | $(".post-field").click(function () { | 5 | $(".post-field").click(function () { |
55 | var url = $(this).find('h4').data('url'); | 6 | var url = $(this).find('h4').data('url'); |
56 | 7 | ||
@@ -66,15 +17,7 @@ $(function () { | @@ -66,15 +17,7 @@ $(function () { | ||
66 | }); | 17 | }); |
67 | }); | 18 | }); |
68 | 19 | ||
69 | - $("#clear_filter").click(function () { | ||
70 | - var frm = $(this).parent(); | ||
71 | - | ||
72 | - frm.find("input[type='checkbox']").prop('checked', false); | ||
73 | - | ||
74 | - frm.submit(); | ||
75 | - }); | ||
76 | - | ||
77 | - $(".comment-section").each(function () { | 20 | + $(".comment-section:visible").each(function () { |
78 | var height = $(this)[0].scrollHeight; | 21 | var height = $(this)[0].scrollHeight; |
79 | 22 | ||
80 | $(this).animate({scrollTop: height}, 0); | 23 | $(this).animate({scrollTop: height}, 0); |
@@ -127,11 +70,11 @@ function setPostFormSubmit(post = "") { | @@ -127,11 +70,11 @@ function setPostFormSubmit(post = "") { | ||
127 | 70 | ||
128 | old.remove(); | 71 | old.remove(); |
129 | } else { | 72 | } else { |
130 | - $('.posts').prepend(data.view); | 73 | + $('.posts:visible').prepend(data.view); |
131 | 74 | ||
132 | new_posts.push(data.new_id); | 75 | new_posts.push(data.new_id); |
133 | 76 | ||
134 | - $('.no-subjects').attr('style', 'display:none'); | 77 | + $('.no-subjects:visible').attr('style', 'display:none'); |
135 | } | 78 | } |
136 | 79 | ||
137 | setTimeout(function () { postHeightLimits() }, 100); | 80 | setTimeout(function () { postHeightLimits() }, 100); |
@@ -0,0 +1,50 @@ | @@ -0,0 +1,50 @@ | ||
1 | +$('.mural-category').on('shown.bs.collapse', function(e) { | ||
2 | + if($(this).is(e.target)){ | ||
3 | + var li = $(".breadcrumb").find('li:last-child'); | ||
4 | + var li_text = $(li).html(); | ||
5 | + var url = $(".mural_url").val(); | ||
6 | + var new_li = $(li).clone(); | ||
7 | + | ||
8 | + new_li.html($(this).parent().find('.panel-title span').text()); | ||
9 | + | ||
10 | + $(li).html("<a href='" + url + "'>" + li_text + "</a>"); | ||
11 | + $(li).append("<span class='divider'>/</span>"); | ||
12 | + | ||
13 | + new_li.appendTo('.breadcrumb'); | ||
14 | + | ||
15 | + var post_section = $(this).find('.posts'), | ||
16 | + without = $(this).find('.no-subjects'), | ||
17 | + loading = $(this).find('.loading-posts'); | ||
18 | + | ||
19 | + if (post_section.children().length == 0) { | ||
20 | + var url = $(this).find('.mural').data('url'); | ||
21 | + | ||
22 | + $.ajax({ | ||
23 | + url: url, | ||
24 | + dataType: 'json', | ||
25 | + success: function (data) { | ||
26 | + loading.hide(); | ||
27 | + | ||
28 | + if (data.count > 0) { | ||
29 | + post_section.append(data.posts); | ||
30 | + | ||
31 | + without.hide(); | ||
32 | + } else { | ||
33 | + without.show(); | ||
34 | + } | ||
35 | + } | ||
36 | + }); | ||
37 | + } | ||
38 | + } | ||
39 | +}); | ||
40 | + | ||
41 | +$('.mural-category').on('hidden.bs.collapse', function(e) { | ||
42 | + if($(this).is(e.target)){ | ||
43 | + $(".breadcrumb").find('li:last-child').remove(); | ||
44 | + | ||
45 | + var li = $(".breadcrumb").find('li:last-child'); | ||
46 | + var text = $(li).find('a').text(); | ||
47 | + | ||
48 | + $(li).html(text); | ||
49 | + } | ||
50 | +}); | ||
0 | \ No newline at end of file | 51 | \ No newline at end of file |
@@ -0,0 +1,59 @@ | @@ -0,0 +1,59 @@ | ||
1 | +// loadOnScroll handler | ||
2 | +var loadOnScroll = function() { | ||
3 | + // If the current scroll position is past out cutoff point... | ||
4 | + if ($(window).scrollTop() >= $(document).height() - $(window).height() - 10) { | ||
5 | + // temporarily unhook the scroll event watcher so we don't call a bunch of times in a row | ||
6 | + $(window).unbind(); | ||
7 | + // execute the load function below that will visit the view and return the content | ||
8 | + loadPosts(); | ||
9 | + } | ||
10 | +}; | ||
11 | + | ||
12 | +var loadPosts = function() { | ||
13 | + var loadUrl = $('.mural').data('url'), | ||
14 | + pageNum = $('.mural').data('page'), | ||
15 | + numberPages = $('.mural').data('pages'), | ||
16 | + favorites = $('.mural').data('fav'), | ||
17 | + mine = $('.mural').data('mine'), | ||
18 | + showing = new_posts.join(','); | ||
19 | + // Check if page is equal to the number of pages | ||
20 | + if (pageNum == numberPages) { | ||
21 | + return false | ||
22 | + } | ||
23 | + // Update the page number | ||
24 | + pageNum = pageNum + 1; | ||
25 | + | ||
26 | + $("#loading_posts").show(); | ||
27 | + // Configure the url we're about to hit | ||
28 | + setTimeout(function (){ | ||
29 | + $.ajax({ | ||
30 | + url: loadUrl, | ||
31 | + data: {'page': pageNum, "favorite": favorites, "mine": mine, "showing": showing}, | ||
32 | + success: function(data) { | ||
33 | + $("#loading_posts").hide(); | ||
34 | + | ||
35 | + $(".posts").append(data); | ||
36 | + | ||
37 | + $('.mural').data('page', pageNum); | ||
38 | + | ||
39 | + setTimeout(function () { postHeightLimits() }, 10); | ||
40 | + }, | ||
41 | + complete: function(data, textStatus){ | ||
42 | + // Turn the scroll monitor back on | ||
43 | + $(window).bind('scroll', loadOnScroll); | ||
44 | + } | ||
45 | + }); | ||
46 | + }, 1000) | ||
47 | +}; | ||
48 | + | ||
49 | +$(function () { | ||
50 | + $(window).bind('scroll', loadOnScroll); | ||
51 | + | ||
52 | + $("#clear_filter").click(function () { | ||
53 | + var frm = $(this).parent(); | ||
54 | + | ||
55 | + frm.find("input[type='checkbox']").prop('checked', false); | ||
56 | + | ||
57 | + frm.submit(); | ||
58 | + }); | ||
59 | +}); | ||
0 | \ No newline at end of file | 60 | \ No newline at end of file |
mural/forms.py
@@ -3,7 +3,7 @@ from django import forms | @@ -3,7 +3,7 @@ from django import forms | ||
3 | from django.utils.translation import ugettext_lazy as _ | 3 | from django.utils.translation import ugettext_lazy as _ |
4 | from django.utils.html import strip_tags | 4 | from django.utils.html import strip_tags |
5 | 5 | ||
6 | -from .models import GeneralPost, Comment | 6 | +from .models import GeneralPost, CategoryPost, Comment |
7 | 7 | ||
8 | class Validation(forms.ModelForm): | 8 | class Validation(forms.ModelForm): |
9 | MAX_UPLOAD_SIZE = 5*1024*1024 | 9 | MAX_UPLOAD_SIZE = 5*1024*1024 |
@@ -40,6 +40,15 @@ class GeneralPostForm(Validation): | @@ -40,6 +40,15 @@ class GeneralPostForm(Validation): | ||
40 | 'post': forms.Textarea | 40 | 'post': forms.Textarea |
41 | } | 41 | } |
42 | 42 | ||
43 | +class CategoryPostForm(Validation): | ||
44 | + class Meta: | ||
45 | + model = CategoryPost | ||
46 | + fields = ['action', 'post', 'image'] | ||
47 | + widgets = { | ||
48 | + 'action': forms.RadioSelect, | ||
49 | + 'post': forms.Textarea | ||
50 | + } | ||
51 | + | ||
43 | class CommentForm(forms.ModelForm): | 52 | class CommentForm(forms.ModelForm): |
44 | MAX_UPLOAD_SIZE = 5*1024*1024 | 53 | MAX_UPLOAD_SIZE = 5*1024*1024 |
45 | 54 |
mural/models.py
@@ -51,7 +51,7 @@ class GeneralPost(Mural): | @@ -51,7 +51,7 @@ class GeneralPost(Mural): | ||
51 | return "mural:delete_general" | 51 | return "mural:delete_general" |
52 | 52 | ||
53 | class CategoryPost(Mural): | 53 | class CategoryPost(Mural): |
54 | - space = models.ForeignKey(Category, verbose_name = ('Category'), related_name = 'post_category') | 54 | + space = models.ForeignKey(Category, verbose_name = ('Category'), related_name = 'post_category', null = True) |
55 | 55 | ||
56 | def get_id(self): | 56 | def get_id(self): |
57 | return self.id | 57 | return self.id |
@@ -60,10 +60,10 @@ class CategoryPost(Mural): | @@ -60,10 +60,10 @@ class CategoryPost(Mural): | ||
60 | return self.id | 60 | return self.id |
61 | 61 | ||
62 | def update_link(self): | 62 | def update_link(self): |
63 | - return "" | 63 | + return "mural:update_category" |
64 | 64 | ||
65 | def delete_link(self): | 65 | def delete_link(self): |
66 | - return "" | 66 | + return "mural:delete_category" |
67 | 67 | ||
68 | class SubjectPost(Mural): | 68 | class SubjectPost(Mural): |
69 | space = models.ForeignKey(Subject, verbose_name = _('Subject'), related_name = 'post_subject') | 69 | space = models.ForeignKey(Subject, verbose_name = _('Subject'), related_name = 'post_subject') |
mural/templates/mural/list.html
@@ -65,7 +65,7 @@ | @@ -65,7 +65,7 @@ | ||
65 | </label> | 65 | </label> |
66 | </div> | 66 | </div> |
67 | <button type="submit" class="btn btn-success btn-raised btn-block">{% trans 'Filter' %}</button> | 67 | <button type="submit" class="btn btn-success btn-raised btn-block">{% trans 'Filter' %}</button> |
68 | - <button type="button" id="clear_filter" class="btn btn-default btn-raised btn-block">{% trans 'Clean Filters' %}</button> | 68 | + <button type="button" id="clear_filter" class="btn btn-default btn-raised btn-block clear_filter">{% trans 'Clean Filters' %}</button> |
69 | </form> | 69 | </form> |
70 | </div> | 70 | </div> |
71 | </div> | 71 | </div> |
@@ -73,4 +73,5 @@ | @@ -73,4 +73,5 @@ | ||
73 | <div class="modal fade" id="post-modal-form" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"></div> | 73 | <div class="modal fade" id="post-modal-form" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"></div> |
74 | 74 | ||
75 | <script type="text/javascript" src="{% static 'js/mural.js' %}"></script> | 75 | <script type="text/javascript" src="{% static 'js/mural.js' %}"></script> |
76 | + <script type="text/javascript" src="{% static 'js/mural_general.js' %}"></script> | ||
76 | {% endblock %} | 77 | {% endblock %} |
77 | \ No newline at end of file | 78 | \ No newline at end of file |
mural/templates/mural/list_category.html
@@ -12,6 +12,8 @@ | @@ -12,6 +12,8 @@ | ||
12 | {% endblock %} | 12 | {% endblock %} |
13 | 13 | ||
14 | {% block content %} | 14 | {% block content %} |
15 | + <input type="hidden" value="{% url 'mural:manage_category' %}" class="mural_url" /> | ||
16 | + | ||
15 | <div id="core-subjects-options-div"> | 17 | <div id="core-subjects-options-div"> |
16 | <ul class="core-subjects-options"> | 18 | <ul class="core-subjects-options"> |
17 | <a href="{% url 'mural:manage_general' %}"><li>{% trans "General" %} ({{ totals.general }})</li></a> | 19 | <a href="{% url 'mural:manage_general' %}"><li>{% trans "General" %} ({{ totals.general }})</li></a> |
@@ -38,7 +40,7 @@ | @@ -38,7 +40,7 @@ | ||
38 | <div class="col-md-12 category-header"> | 40 | <div class="col-md-12 category-header"> |
39 | <h4 class="panel-title"> | 41 | <h4 class="panel-title"> |
40 | <a class="category-course-link pull-left" data-parent="#accordion" data-toggle="collapse" href="#{{category.slug}}"> | 42 | <a class="category-course-link pull-left" data-parent="#accordion" data-toggle="collapse" href="#{{category.slug}}"> |
41 | - <button class="btn btn-default btn-xs text-center cat-selector"><i class="fa fa-angle-right fa-2x" aria-hidden="true"></i></button> {{ category.name }} | 43 | + <button class="btn btn-default btn-xs text-center cat-selector"><i class="fa fa-angle-right fa-2x" aria-hidden="true"></i></button> <span>{{ category.name }}</span> |
42 | 44 | ||
43 | ({{ category|unviewed:request.user }}) | 45 | ({{ category|unviewed:request.user }}) |
44 | </a> | 46 | </a> |
@@ -63,7 +65,7 @@ | @@ -63,7 +65,7 @@ | ||
63 | </div> | 65 | </div> |
64 | </div> | 66 | </div> |
65 | <div id="{{category.slug}}" class="panel-collapse panel-body collapse mural-category"> | 67 | <div id="{{category.slug}}" class="panel-collapse panel-body collapse mural-category"> |
66 | - <div class="col-md-12 cards-content mural" data-url="{% url 'mural:manage_general' %}"> | 68 | + <div class="col-md-12 cards-content mural" data-url="{% url 'mural:load_category' category.id %}"> |
67 | <div class="col-md-9 col-sm-9 col-xs-9 mural-list"> | 69 | <div class="col-md-9 col-sm-9 col-xs-9 mural-list"> |
68 | <div class="post_make panel panel-default"> | 70 | <div class="post_make panel panel-default"> |
69 | <div class="panel-body"> | 71 | <div class="panel-body"> |
@@ -72,14 +74,19 @@ | @@ -72,14 +74,19 @@ | ||
72 | </div> | 74 | </div> |
73 | <div class="col-lg-11 col-md-11 col-sm-11 col-xs-11 post-field"> | 75 | <div class="col-lg-11 col-md-11 col-sm-11 col-xs-11 post-field"> |
74 | <div> | 76 | <div> |
75 | - <h4 data-url="{% url 'mural:create_general' %}">{% trans 'Wish to make a new post?' %}</h4> | 77 | + <h4 data-url="{% url 'mural:create_category' category.slug %}">{% trans 'Wish to make a new post?' %}</h4> |
76 | </div> | 78 | </div> |
77 | </div> | 79 | </div> |
78 | </div> | 80 | </div> |
79 | </div> | 81 | </div> |
80 | 82 | ||
83 | + <button type="button" class="btn btn-block btn-default btn-sm loading-posts"><span class="fa fa-spin fa-circle-o-notch"></span></button> | ||
81 | <div class="posts"> | 84 | <div class="posts"> |
82 | </div> | 85 | </div> |
86 | + <div class="text-center no-subjects" style="display:none"> | ||
87 | + <i class="fa fa-list"></i> | ||
88 | + <h4>{% trans 'There are no posts in this mural yet.' %}</h4> | ||
89 | + </div> | ||
83 | </div> | 90 | </div> |
84 | <div class="col-md-3 col-sm-3 col-xs-3 post-filter"> | 91 | <div class="col-md-3 col-sm-3 col-xs-3 post-filter"> |
85 | <h4>{% trans 'Filter' %}</h4> | 92 | <h4>{% trans 'Filter' %}</h4> |
@@ -109,5 +116,9 @@ | @@ -109,5 +116,9 @@ | ||
109 | {% endif %} | 116 | {% endif %} |
110 | </div> | 117 | </div> |
111 | 118 | ||
119 | + <div class="modal fade" id="post-modal-form" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"></div> | ||
120 | + | ||
112 | <script type="text/javascript" src="{% static 'js/category.js' %}"></script> | 121 | <script type="text/javascript" src="{% static 'js/category.js' %}"></script> |
122 | + <script type="text/javascript" src="{% static 'js/mural.js' %}"></script> | ||
123 | + <script type="text/javascript" src="{% static 'js/mural_category.js' %}"></script> | ||
113 | {% endblock %} | 124 | {% endblock %} |
114 | \ No newline at end of file | 125 | \ No newline at end of file |
mural/urls.py
@@ -5,8 +5,12 @@ urlpatterns = [ | @@ -5,8 +5,12 @@ urlpatterns = [ | ||
5 | url(r'^$', views.GeneralIndex.as_view(), name='manage_general'), | 5 | url(r'^$', views.GeneralIndex.as_view(), name='manage_general'), |
6 | url(r'^categories/$', views.CategoryIndex.as_view(), name='manage_category'), | 6 | url(r'^categories/$', views.CategoryIndex.as_view(), name='manage_category'), |
7 | url(r'^create_gen/$', views.GeneralCreate.as_view(), name='create_general'), | 7 | url(r'^create_gen/$', views.GeneralCreate.as_view(), name='create_general'), |
8 | + url(r'^create_cat/(?P<slug>[\w_-]+)/$', views.CategoryCreate.as_view(), name='create_category'), | ||
8 | url(r'^update_gen/(?P<pk>[\w_-]+)/$', views.GeneralUpdate.as_view(), name='update_general'), | 9 | url(r'^update_gen/(?P<pk>[\w_-]+)/$', views.GeneralUpdate.as_view(), name='update_general'), |
10 | + url(r'^update_cat/(?P<pk>[\w_-]+)/$', views.CategoryUpdate.as_view(), name='update_category'), | ||
9 | url(r'^delete_gen/(?P<pk>[\w_-]+)/$', views.GeneralDelete.as_view(), name='delete_general'), | 11 | url(r'^delete_gen/(?P<pk>[\w_-]+)/$', views.GeneralDelete.as_view(), name='delete_general'), |
12 | + url(r'^delete_cat/(?P<pk>[\w_-]+)/$', views.CategoryDelete.as_view(), name='delete_category'), | ||
13 | + url(r'^load_category/([\w_-]+)/$', views.load_category_posts, name='load_category'), | ||
10 | url(r'^favorite/([\w_-]+)/$', views.favorite, name='favorite'), | 14 | url(r'^favorite/([\w_-]+)/$', views.favorite, name='favorite'), |
11 | url(r'^deleted/$', views.deleted_post, name='deleted_post'), | 15 | url(r'^deleted/$', views.deleted_post, name='deleted_post'), |
12 | url(r'^comment/(?P<post>[\w_-]+)/$', views.CommentCreate.as_view(), name='create_comment'), | 16 | url(r'^comment/(?P<post>[\w_-]+)/$', views.CommentCreate.as_view(), name='create_comment'), |
@@ -14,7 +18,7 @@ urlpatterns = [ | @@ -14,7 +18,7 @@ urlpatterns = [ | ||
14 | url(r'^delete_comment/(?P<pk>[\w_-]+)/$', views.CommentDelete.as_view(), name='delete_comment'), | 18 | url(r'^delete_comment/(?P<pk>[\w_-]+)/$', views.CommentDelete.as_view(), name='delete_comment'), |
15 | url(r'^deleted_comment/$', views.deleted_comment, name='deleted_comment'), | 19 | url(r'^deleted_comment/$', views.deleted_comment, name='deleted_comment'), |
16 | url(r'^render_comment/([\w_-]+)/([\w_-]+)/$', views.render_comment, name='render_comment'), | 20 | url(r'^render_comment/([\w_-]+)/([\w_-]+)/$', views.render_comment, name='render_comment'), |
17 | - url(r'^render_post/([\w_-]+)/([\w_-]+)/$', views.render_gen_post, name='render_post_general'), | 21 | + url(r'^render_post/([\w_-]+)/([\w_-]+)/([\w_-]+)/$', views.render_post, name='render_post'), |
18 | url(r'^load_comments/([\w_-]+)/([\w_-]+)/$', views.load_comments, name='load_comments'), | 22 | url(r'^load_comments/([\w_-]+)/([\w_-]+)/$', views.load_comments, name='load_comments'), |
19 | url(r'^suggest_users/$', views.suggest_users, name='suggest_users'), | 23 | url(r'^suggest_users/$', views.suggest_users, name='suggest_users'), |
20 | ] | 24 | ] |
21 | \ No newline at end of file | 25 | \ No newline at end of file |
mural/views.py
@@ -19,7 +19,7 @@ from subjects.models import Subject | @@ -19,7 +19,7 @@ from subjects.models import Subject | ||
19 | from users.models import User | 19 | from users.models import User |
20 | 20 | ||
21 | from .models import Mural, GeneralPost, CategoryPost, SubjectPost, MuralVisualizations, MuralFavorites, Comment | 21 | from .models import Mural, GeneralPost, CategoryPost, SubjectPost, MuralVisualizations, MuralFavorites, Comment |
22 | -from .forms import GeneralPostForm, CommentForm | 22 | +from .forms import GeneralPostForm, CategoryPostForm, CommentForm |
23 | 23 | ||
24 | """ | 24 | """ |
25 | Section for GeneralPost classes | 25 | Section for GeneralPost classes |
@@ -140,7 +140,7 @@ class GeneralCreate(LoginRequiredMixin, generic.edit.CreateView): | @@ -140,7 +140,7 @@ class GeneralCreate(LoginRequiredMixin, generic.edit.CreateView): | ||
140 | return context | 140 | return context |
141 | 141 | ||
142 | def get_success_url(self): | 142 | def get_success_url(self): |
143 | - return reverse_lazy('mural:render_post_general', args = (self.object.id, 'create', )) | 143 | + return reverse_lazy('mural:render_post', args = (self.object.id, 'create', 'gen', )) |
144 | 144 | ||
145 | class GeneralUpdate(LoginRequiredMixin, generic.UpdateView): | 145 | class GeneralUpdate(LoginRequiredMixin, generic.UpdateView): |
146 | login_url = reverse_lazy("users:login") | 146 | login_url = reverse_lazy("users:login") |
@@ -182,7 +182,7 @@ class GeneralUpdate(LoginRequiredMixin, generic.UpdateView): | @@ -182,7 +182,7 @@ class GeneralUpdate(LoginRequiredMixin, generic.UpdateView): | ||
182 | return context | 182 | return context |
183 | 183 | ||
184 | def get_success_url(self): | 184 | def get_success_url(self): |
185 | - return reverse_lazy('mural:render_post_general', args = (self.object.id, 'update', )) | 185 | + return reverse_lazy('mural:render_post', args = (self.object.id, 'update', 'gen', )) |
186 | 186 | ||
187 | class GeneralDelete(LoginRequiredMixin, generic.DeleteView): | 187 | class GeneralDelete(LoginRequiredMixin, generic.DeleteView): |
188 | login_url = reverse_lazy("users:login") | 188 | login_url = reverse_lazy("users:login") |
@@ -213,6 +213,33 @@ class GeneralDelete(LoginRequiredMixin, generic.DeleteView): | @@ -213,6 +213,33 @@ class GeneralDelete(LoginRequiredMixin, generic.DeleteView): | ||
213 | """ | 213 | """ |
214 | Section for CategoryPost classes | 214 | Section for CategoryPost classes |
215 | """ | 215 | """ |
216 | +def load_category_posts(request, category): | ||
217 | + context = { | ||
218 | + 'request': request, | ||
219 | + } | ||
220 | + | ||
221 | + showing = request.GET.get('showing', '') | ||
222 | + | ||
223 | + posts = CategoryPost.objects.extra(select = {"most_recent": "greatest(last_update, (select max(mural_comment.last_update) from mural_comment where mural_comment.post_id = mural_categorypost.mural_ptr_id))"}).filter(space__id = category).order_by("-most_recent") | ||
224 | + | ||
225 | + paginator = Paginator(posts, 10) | ||
226 | + | ||
227 | + try: | ||
228 | + page_number = int(request.GET.get('page', 1)) | ||
229 | + except ValueError: | ||
230 | + raise Http404 | ||
231 | + | ||
232 | + try: | ||
233 | + page_obj = paginator.page(page_number) | ||
234 | + except EmptyPage: | ||
235 | + raise Http404 | ||
236 | + | ||
237 | + context['posts'] = page_obj.object_list | ||
238 | + | ||
239 | + response = render_to_string("mural/_list_view.html", context, request) | ||
240 | + | ||
241 | + return JsonResponse({"posts": response, "count": posts.count(), "num_pages": paginator.num_pages, "num_page": page_obj.number}) | ||
242 | + | ||
216 | class CategoryIndex(LoginRequiredMixin, generic.ListView): | 243 | class CategoryIndex(LoginRequiredMixin, generic.ListView): |
217 | login_url = reverse_lazy("users:login") | 244 | login_url = reverse_lazy("users:login") |
218 | redirect_field_name = 'next' | 245 | redirect_field_name = 'next' |
@@ -235,8 +262,6 @@ class CategoryIndex(LoginRequiredMixin, generic.ListView): | @@ -235,8 +262,6 @@ class CategoryIndex(LoginRequiredMixin, generic.ListView): | ||
235 | self.totals['category'] = MuralVisualizations.objects.filter(Q(user = user) & Q(viewed = False) & (Q(post__categorypost__space__coordinators = user) | Q(comment__post__categorypost__space__coordinators = user) | Q(post__categorypost__space__subject_category__professor = user) | Q(post__categorypost__space__subject_category__students = user) | Q(comment__post__categorypost__space__subject_category__professor = user) | Q(comment__post__categorypost__space__subject_category__students = user))).distinct().count() | 262 | self.totals['category'] = MuralVisualizations.objects.filter(Q(user = user) & Q(viewed = False) & (Q(post__categorypost__space__coordinators = user) | Q(comment__post__categorypost__space__coordinators = user) | Q(post__categorypost__space__subject_category__professor = user) | Q(post__categorypost__space__subject_category__students = user) | Q(comment__post__categorypost__space__subject_category__professor = user) | Q(comment__post__categorypost__space__subject_category__students = user))).distinct().count() |
236 | self.totals['subject'] = MuralVisualizations.objects.filter(Q(user = user) & Q(viewed = False) & (Q(post__subjectpost__space__professor = user) | Q(comment__post__subjectpost__space__professor = user) | Q(post__subjectpost__space__students = user) | Q(comment__post__subjectpost__space__students = user))).distinct().count() | 263 | self.totals['subject'] = MuralVisualizations.objects.filter(Q(user = user) & Q(viewed = False) & (Q(post__subjectpost__space__professor = user) | Q(comment__post__subjectpost__space__professor = user) | Q(post__subjectpost__space__students = user) | Q(comment__post__subjectpost__space__students = user))).distinct().count() |
237 | 264 | ||
238 | - print(categories) | ||
239 | - | ||
240 | return categories | 265 | return categories |
241 | 266 | ||
242 | def get_context_data(self, **kwargs): | 267 | def get_context_data(self, **kwargs): |
@@ -248,22 +273,144 @@ class CategoryIndex(LoginRequiredMixin, generic.ListView): | @@ -248,22 +273,144 @@ class CategoryIndex(LoginRequiredMixin, generic.ListView): | ||
248 | 273 | ||
249 | return context | 274 | return context |
250 | 275 | ||
251 | -def render_gen_post(request, post, msg): | ||
252 | - post = get_object_or_404(GeneralPost, id = post) | 276 | +class CategoryCreate(LoginRequiredMixin, generic.edit.CreateView): |
277 | + login_url = reverse_lazy("users:login") | ||
278 | + redirect_field_name = 'next' | ||
279 | + | ||
280 | + template_name = 'mural/_form.html' | ||
281 | + form_class = CategoryPostForm | ||
282 | + | ||
283 | + def form_invalid(self, form): | ||
284 | + context = super(CategoryCreate, self).form_invalid(form) | ||
285 | + context.status_code = 400 | ||
286 | + | ||
287 | + return context | ||
288 | + | ||
289 | + def form_valid(self, form): | ||
290 | + self.object = form.save(commit = False) | ||
291 | + | ||
292 | + slug = self.kwargs.get('slug', None) | ||
293 | + cat = get_object_or_404(Category, slug = slug) | ||
294 | + | ||
295 | + self.object.space = cat | ||
296 | + self.object.user = self.request.user | ||
297 | + | ||
298 | + self.object.save() | ||
299 | + | ||
300 | + users = User.objects.all().exclude(id = self.request.user.id) | ||
301 | + entries = [] | ||
302 | + | ||
303 | + notify_type = "mural" | ||
304 | + user_icon = self.object.user.image_url | ||
305 | + #_view = render_to_string("mural/_view.html", {"post": self.object}, self.request) | ||
306 | + simple_notify = _("%s has made a post in General")%(str(self.object.user)) | ||
307 | + pathname = reverse("mural:manage_general") | ||
308 | + | ||
309 | + #for user in users: | ||
310 | + # entries.append(MuralVisualizations(viewed = False, user = user, post = self.object)) | ||
311 | + # Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "create", "user_icon": user_icon, "pathname": pathname, "simple": simple_notify, "complete": _view})}) | ||
312 | + | ||
313 | + #MuralVisualizations.objects.bulk_create(entries) | ||
314 | + | ||
315 | + return super(CategoryCreate, self).form_valid(form) | ||
316 | + | ||
317 | + def get_context_data(self, *args, **kwargs): | ||
318 | + context = super(CategoryCreate, self).get_context_data(*args, **kwargs) | ||
319 | + | ||
320 | + context['form_url'] = reverse_lazy("mural:create_category", args = (), kwargs = {'slug': self.kwargs.get('slug', None)}) | ||
321 | + | ||
322 | + return context | ||
323 | + | ||
324 | + def get_success_url(self): | ||
325 | + return reverse_lazy('mural:render_post', args = (self.object.id, 'create', 'cat', )) | ||
326 | + | ||
327 | +class CategoryUpdate(LoginRequiredMixin, generic.UpdateView): | ||
328 | + login_url = reverse_lazy("users:login") | ||
329 | + redirect_field_name = 'next' | ||
330 | + | ||
331 | + template_name = 'mural/_form.html' | ||
332 | + model = CategoryPost | ||
333 | + form_class = CategoryPostForm | ||
334 | + | ||
335 | + def form_invalid(self, form): | ||
336 | + context = super(CategoryUpdate, self).form_invalid(form) | ||
337 | + context.status_code = 400 | ||
338 | + | ||
339 | + return context | ||
340 | + | ||
341 | + def form_valid(self, form): | ||
342 | + self.object = form.save(commit = False) | ||
343 | + | ||
344 | + self.object.edited = True | ||
345 | + | ||
346 | + self.object.save() | ||
347 | + | ||
348 | + users = User.objects.all().exclude(id = self.request.user.id) | ||
349 | + | ||
350 | + notify_type = "mural" | ||
351 | + #_view = render_to_string("mural/_view.html", {"post": self.object}, self.request) | ||
352 | + pathname = reverse("mural:manage_general") | ||
353 | + | ||
354 | + #for user in users: | ||
355 | + # Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "update", "pathname": pathname, "complete": _view, "post_id": self.object.id})}) | ||
356 | + | ||
357 | + return super(CategoryUpdate, self).form_valid(form) | ||
358 | + | ||
359 | + def get_context_data(self, *args, **kwargs): | ||
360 | + context = super(CategoryUpdate, self).get_context_data(*args, **kwargs) | ||
361 | + | ||
362 | + context['form_url'] = reverse_lazy("mural:update_category", args = (), kwargs = {'pk': self.object.id}) | ||
363 | + | ||
364 | + return context | ||
365 | + | ||
366 | + def get_success_url(self): | ||
367 | + return reverse_lazy('mural:render_post', args = (self.object.id, 'update', 'cat', )) | ||
368 | + | ||
369 | +class CategoryDelete(LoginRequiredMixin, generic.DeleteView): | ||
370 | + login_url = reverse_lazy("users:login") | ||
371 | + redirect_field_name = 'next' | ||
372 | + | ||
373 | + template_name = 'mural/delete.html' | ||
374 | + model = CategoryPost | ||
375 | + | ||
376 | + def get_context_data(self, *args, **kwargs): | ||
377 | + context = super(CategoryDelete, self).get_context_data(*args, **kwargs) | ||
378 | + | ||
379 | + context['form_url'] = reverse_lazy("mural:delete_category", args = (), kwargs = {'pk': self.object.id}) | ||
380 | + context['message'] = _('Are you sure you want to delete this post?') | ||
381 | + | ||
382 | + return context | ||
383 | + | ||
384 | + def get_success_url(self): | ||
385 | + users = User.objects.all().exclude(id = self.request.user.id) | ||
386 | + | ||
387 | + notify_type = "mural" | ||
388 | + pathname = reverse("mural:manage_general") | ||
389 | + | ||
390 | + #for user in users: | ||
391 | + # Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "delete", "pathname": pathname, "post_id": self.object.id})}) | ||
392 | + | ||
393 | + return reverse_lazy('mural:deleted_post') | ||
394 | + | ||
395 | +def render_post(request, post, msg, ptype): | ||
396 | + if ptype == 'gen': | ||
397 | + post = get_object_or_404(GeneralPost, id = post) | ||
398 | + elif ptype == 'cat': | ||
399 | + post = get_object_or_404(CategoryPost, id = post) | ||
253 | 400 | ||
254 | context = {} | 401 | context = {} |
255 | context['post'] = post | 402 | context['post'] = post |
256 | 403 | ||
257 | - msg = "" | 404 | + message = "" |
258 | 405 | ||
259 | if msg == 'create': | 406 | if msg == 'create': |
260 | - msg = _('Your post was published successfully!') | 407 | + message = _('Your post was published successfully!') |
261 | else: | 408 | else: |
262 | - msg = _('Your post was edited successfully!') | 409 | + message = _('Your post was edited successfully!') |
263 | 410 | ||
264 | html = render_to_string("mural/_view.html", context, request) | 411 | html = render_to_string("mural/_view.html", context, request) |
265 | 412 | ||
266 | - return JsonResponse({'message': msg, 'view': html, 'new_id': post.id}) | 413 | + return JsonResponse({'message': message, 'view': html, 'new_id': post.id}) |
267 | 414 | ||
268 | def deleted_post(request): | 415 | def deleted_post(request): |
269 | return JsonResponse({'msg': _('Post deleted successfully!')}) | 416 | return JsonResponse({'msg': _('Post deleted successfully!')}) |
@@ -282,6 +429,9 @@ def favorite(request, post): | @@ -282,6 +429,9 @@ def favorite(request, post): | ||
282 | 429 | ||
283 | return JsonResponse({'label': _('Favorite')}) | 430 | return JsonResponse({'label': _('Favorite')}) |
284 | 431 | ||
432 | +""" | ||
433 | + Section for comment functions | ||
434 | +""" | ||
285 | class CommentCreate(LoginRequiredMixin, generic.edit.CreateView): | 435 | class CommentCreate(LoginRequiredMixin, generic.edit.CreateView): |
286 | login_url = reverse_lazy("users:login") | 436 | login_url = reverse_lazy("users:login") |
287 | redirect_field_name = 'next' | 437 | redirect_field_name = 'next' |