Mercurial > public > sg101
comparison forums/views/main.py @ 1031:e1c03da72818
Get rid of some warnings in Django 1.8.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 20 Dec 2015 22:18:59 -0600 |
parents | 4619290d171d |
children | a60aabced346 |
comparison
equal
deleted
inserted
replaced
1030:d9610b1e2a3d | 1031:e1c03da72818 |
---|---|
13 from django.http import HttpResponseForbidden | 13 from django.http import HttpResponseForbidden |
14 from django.http import HttpResponseRedirect | 14 from django.http import HttpResponseRedirect |
15 from django.core.urlresolvers import reverse | 15 from django.core.urlresolvers import reverse |
16 from django.core.paginator import InvalidPage | 16 from django.core.paginator import InvalidPage |
17 from django.shortcuts import get_object_or_404 | 17 from django.shortcuts import get_object_or_404 |
18 from django.shortcuts import render_to_response | 18 from django.shortcuts import render |
19 from django.template.loader import render_to_string | 19 from django.template.loader import render_to_string |
20 from django.template import RequestContext | |
21 from django.views.decorators.http import require_POST | 20 from django.views.decorators.http import require_POST |
22 from django.db.models import F | 21 from django.db.models import F |
23 | 22 |
24 import antispam | 23 import antispam |
25 import antispam.utils | 24 import antispam.utils |
106 cat['forums'].append(forum) | 105 cat['forums'].append(forum) |
107 | 106 |
108 cmpdef = lambda a, b: cmp(a['cat'].position, b['cat'].position) | 107 cmpdef = lambda a, b: cmp(a['cat'].position, b['cat'].position) |
109 cats = sorted(cats.values(), cmpdef) | 108 cats = sorted(cats.values(), cmpdef) |
110 | 109 |
111 return render_to_response('forums/index.html', { | 110 return render(request, 'forums/index.html', { |
112 'cats': cats, | 111 'cats': cats, |
113 'feeds': feeds, | 112 'feeds': feeds, |
114 }, | 113 }) |
115 context_instance=RequestContext(request)) | |
116 | 114 |
117 | 115 |
118 def forum_index(request, slug): | 116 def forum_index(request, slug): |
119 """ | 117 """ |
120 Displays all the topics in a forum. | 118 Displays all the topics in a forum. |
145 # we do this for the template since it is rendered twice | 143 # we do this for the template since it is rendered twice |
146 page_nav = render_to_string('forums/pagination.html', {'page': page}) | 144 page_nav = render_to_string('forums/pagination.html', {'page': page}) |
147 | 145 |
148 can_moderate = perms.can_moderate(forum, request.user) | 146 can_moderate = perms.can_moderate(forum, request.user) |
149 | 147 |
150 return render_to_response('forums/forum_index.html', { | 148 return render(request, 'forums/forum_index.html', { |
151 'forum': forum, | 149 'forum': forum, |
152 'feed': feed, | 150 'feed': feed, |
153 'page': page, | 151 'page': page, |
154 'page_nav': page_nav, | 152 'page_nav': page_nav, |
155 'can_moderate': can_moderate, | 153 'can_moderate': can_moderate, |
156 }, | 154 }) |
157 context_instance=RequestContext(request)) | |
158 | 155 |
159 | 156 |
160 def topic_index(request, id): | 157 def topic_index(request, id): |
161 """ | 158 """ |
162 Displays all the posts in a topic. | 159 Displays all the posts in a topic. |
221 topic in request.user.favorite_topics.all()) | 218 topic in request.user.favorite_topics.all()) |
222 | 219 |
223 is_subscribed = request.user.is_authenticated() and ( | 220 is_subscribed = request.user.is_authenticated() and ( |
224 topic in request.user.subscriptions.all()) | 221 topic in request.user.subscriptions.all()) |
225 | 222 |
226 return render_to_response('forums/topic.html', { | 223 return render(request, 'forums/topic.html', { |
227 'forum': topic.forum, | 224 'forum': topic.forum, |
228 'topic': topic, | 225 'topic': topic, |
229 'page': page, | 226 'page': page, |
230 'page_nav': page_nav, | 227 'page_nav': page_nav, |
231 'last_page': last_page, | 228 'last_page': last_page, |
232 'can_moderate': can_moderate, | 229 'can_moderate': can_moderate, |
233 'can_reply': can_reply, | 230 'can_reply': can_reply, |
234 'form': NewPostForm(initial={'topic_id': topic.id}), | 231 'form': NewPostForm(initial={'topic_id': topic.id}), |
235 'is_favorite': is_favorite, | 232 'is_favorite': is_favorite, |
236 'is_subscribed': is_subscribed, | 233 'is_subscribed': is_subscribed, |
237 }, | 234 }) |
238 context_instance=RequestContext(request)) | |
239 | 235 |
240 | 236 |
241 def topic_unread(request, id): | 237 def topic_unread(request, id): |
242 """ | 238 """ |
243 This view redirects to the first post the user hasn't read, if we can | 239 This view redirects to the first post the user hasn't read, if we can |
306 return HttpResponseRedirect(reverse('forums-new_topic_thanks', | 302 return HttpResponseRedirect(reverse('forums-new_topic_thanks', |
307 kwargs={'tid': topic.pk})) | 303 kwargs={'tid': topic.pk})) |
308 else: | 304 else: |
309 form = NewTopicForm(request.user, forum) | 305 form = NewTopicForm(request.user, forum) |
310 | 306 |
311 return render_to_response('forums/new_topic.html', { | 307 return render(request, 'forums/new_topic.html', { |
312 'forum': forum, | 308 'forum': forum, |
313 'form': form, | 309 'form': form, |
314 }, | 310 }) |
315 context_instance=RequestContext(request)) | |
316 | 311 |
317 | 312 |
318 @login_required | 313 @login_required |
319 def new_topic_thanks(request, tid): | 314 def new_topic_thanks(request, tid): |
320 """ | 315 """ |
321 This view displays the success page for a newly created topic. | 316 This view displays the success page for a newly created topic. |
322 """ | 317 """ |
323 topic = get_object_or_404(Topic.objects.select_related(), pk=tid) | 318 topic = get_object_or_404(Topic.objects.select_related(), pk=tid) |
324 return render_to_response('forums/new_topic_thanks.html', { | 319 return render(request, 'forums/new_topic_thanks.html', { |
325 'forum': topic.forum, | 320 'forum': topic.forum, |
326 'topic': topic, | 321 'topic': topic, |
327 }, | 322 }) |
328 context_instance=RequestContext(request)) | |
329 | 323 |
330 | 324 |
331 @require_POST | 325 @require_POST |
332 def quick_reply_ajax(request): | 326 def quick_reply_ajax(request): |
333 """ | 327 """ |
350 post.unread = True | 344 post.unread = True |
351 post.attach_list = post.attachments.all() | 345 post.attach_list = post.attachments.all() |
352 _bump_post_count(request.user) | 346 _bump_post_count(request.user) |
353 _update_last_visit(request.user, form.topic, datetime.datetime.now()) | 347 _update_last_visit(request.user, form.topic, datetime.datetime.now()) |
354 | 348 |
355 return render_to_response('forums/display_post.html', { | 349 return render(request, 'forums/display_post.html', { |
356 'post': post, | 350 'post': post, |
357 'can_moderate': perms.can_moderate(form.topic.forum, request.user), | 351 'can_moderate': perms.can_moderate(form.topic.forum, request.user), |
358 'can_reply': True, | 352 'can_reply': True, |
359 }, | 353 }) |
360 context_instance=RequestContext(request)) | |
361 | 354 |
362 # The client side javascript is pretty simplistic right now and we don't | 355 # The client side javascript is pretty simplistic right now and we don't |
363 # want to change it yet. It is expecting a single error string. Just grab | 356 # want to change it yet. It is expecting a single error string. Just grab |
364 # the first error message and use that. | 357 # the first error message and use that. |
365 errors = form.errors.as_data() | 358 errors = form.errors.as_data() |
457 | 450 |
458 return HttpResponseRedirect(post.get_absolute_url()) | 451 return HttpResponseRedirect(post.get_absolute_url()) |
459 else: | 452 else: |
460 form = PostForm(instance=post, topic_name=topic_name) | 453 form = PostForm(instance=post, topic_name=topic_name) |
461 | 454 |
462 return render_to_response('forums/edit_post.html', { | 455 return render(request, 'forums/edit_post.html', { |
463 'forum': post.topic.forum, | 456 'forum': post.topic.forum, |
464 'topic': post.topic, | 457 'topic': post.topic, |
465 'post': post, | 458 'post': post, |
466 'form': form, | 459 'form': form, |
467 'can_moderate': can_moderate, | 460 'can_moderate': can_moderate, |
468 }, | 461 }) |
469 context_instance=RequestContext(request)) | |
470 | 462 |
471 | 463 |
472 @require_POST | 464 @require_POST |
473 def delete_post(request): | 465 def delete_post(request): |
474 """ | 466 """ |
613 else: | 605 else: |
614 form = PostForm() | 606 form = PostForm() |
615 else: | 607 else: |
616 form = None | 608 form = None |
617 | 609 |
618 return render_to_response('forums/new_post.html', { | 610 return render(request, 'forums/new_post.html', { |
619 'forum': topic.forum, | 611 'forum': topic.forum, |
620 'topic': topic, | 612 'topic': topic, |
621 'form': form, | 613 'form': form, |
622 'can_post': can_post, | 614 'can_post': can_post, |
623 }, | 615 }) |
624 context_instance=RequestContext(request)) | |
625 | 616 |
626 | 617 |
627 @login_required | 618 @login_required |
628 def mod_topic_stick(request, id): | 619 def mod_topic_stick(request, id): |
629 """ | 620 """ |
683 _move_topic(topic, old_forum, new_forum) | 674 _move_topic(topic, old_forum, new_forum) |
684 return HttpResponseRedirect(topic.get_absolute_url()) | 675 return HttpResponseRedirect(topic.get_absolute_url()) |
685 else: | 676 else: |
686 form = MoveTopicForm(request.user) | 677 form = MoveTopicForm(request.user) |
687 | 678 |
688 return render_to_response('forums/move_topic.html', { | 679 return render(request, 'forums/move_topic.html', { |
689 'forum': topic.forum, | 680 'forum': topic.forum, |
690 'topic': topic, | 681 'topic': topic, |
691 'form': form, | 682 'form': form, |
692 }, | 683 }) |
693 context_instance=RequestContext(request)) | |
694 | 684 |
695 | 685 |
696 @login_required | 686 @login_required |
697 def mod_forum(request, slug): | 687 def mod_forum(request, slug): |
698 """ | 688 """ |
738 return HttpResponseRedirect(url) | 728 return HttpResponseRedirect(url) |
739 | 729 |
740 if form is None: | 730 if form is None: |
741 form = MoveTopicForm(request.user, hide_label=True) | 731 form = MoveTopicForm(request.user, hide_label=True) |
742 | 732 |
743 return render_to_response('forums/mod_forum.html', { | 733 return render(request, 'forums/mod_forum.html', { |
744 'forum': forum, | 734 'forum': forum, |
745 'page': page, | 735 'page': page, |
746 'page_nav': page_nav, | 736 'page_nav': page_nav, |
747 'form': form, | 737 'form': form, |
748 }, | 738 }) |
749 context_instance=RequestContext(request)) | |
750 | 739 |
751 | 740 |
752 @login_required | 741 @login_required |
753 @require_POST | 742 @require_POST |
754 def catchup_all(request): | 743 def catchup_all(request): |
755 """ | 744 """ |
756 This view marks all forums as being read. | 745 This view marks all forums as being read. |
757 """ | 746 """ |
758 forum_ids = Forum.objects.forum_ids_for_user(request.user) | 747 forum_ids = Forum.objects.forum_ids_for_user(request.user) |
759 | 748 |
760 tlvs = TopicLastVisit.objects.filter(user=request.user, | 749 TopicLastVisit.objects.filter( |
750 user=request.user, | |
761 topic__forum__id__in=forum_ids).delete() | 751 topic__forum__id__in=forum_ids).delete() |
762 | 752 |
763 now = datetime.datetime.now() | 753 now = datetime.datetime.now() |
764 ForumLastVisit.objects.filter(user=request.user, | 754 ForumLastVisit.objects.filter(user=request.user, |
765 forum__in=forum_ids).update(begin_date=now, end_date=now) | 755 forum__in=forum_ids).update(begin_date=now, end_date=now) |
807 else: | 797 else: |
808 form = SplitTopicForm(request.user) | 798 form = SplitTopicForm(request.user) |
809 | 799 |
810 posts = topic.posts.select_related() | 800 posts = topic.posts.select_related() |
811 | 801 |
812 return render_to_response('forums/mod_split_topic.html', { | 802 return render(request, 'forums/mod_split_topic.html', { |
813 'forum': topic.forum, | 803 'forum': topic.forum, |
814 'topic': topic, | 804 'topic': topic, |
815 'posts': posts, | 805 'posts': posts, |
816 'form': form, | 806 'form': form, |
817 }, | 807 }) |
818 context_instance=RequestContext(request)) | |
819 | 808 |
820 | 809 |
821 @login_required | 810 @login_required |
822 def unread_topics(request): | 811 def unread_topics(request): |
823 """Displays the topics with unread posts for a given user.""" | 812 """Displays the topics with unread posts for a given user.""" |
834 attach_topic_page_ranges(page.object_list) | 823 attach_topic_page_ranges(page.object_list) |
835 | 824 |
836 # we do this for the template since it is rendered twice | 825 # we do this for the template since it is rendered twice |
837 page_nav = render_to_string('forums/pagination.html', {'page': page}) | 826 page_nav = render_to_string('forums/pagination.html', {'page': page}) |
838 | 827 |
839 return render_to_response('forums/topic_list.html', { | 828 return render(request, 'forums/topic_list.html', { |
840 'title': 'Topics With Unread Posts', | 829 'title': 'Topics With Unread Posts', |
841 'page': page, | 830 'page': page, |
842 'page_nav': page_nav, | 831 'page_nav': page_nav, |
843 'unread': True, | 832 'unread': True, |
844 }, | 833 }) |
845 context_instance=RequestContext(request)) | |
846 | 834 |
847 | 835 |
848 def unanswered_topics(request): | 836 def unanswered_topics(request): |
849 """Displays the topics with no replies.""" | 837 """Displays the topics with no replies.""" |
850 | 838 |
863 attach_topic_page_ranges(page.object_list) | 851 attach_topic_page_ranges(page.object_list) |
864 | 852 |
865 # we do this for the template since it is rendered twice | 853 # we do this for the template since it is rendered twice |
866 page_nav = render_to_string('forums/pagination.html', {'page': page}) | 854 page_nav = render_to_string('forums/pagination.html', {'page': page}) |
867 | 855 |
868 return render_to_response('forums/topic_list.html', { | 856 return render(request, 'forums/topic_list.html', { |
869 'title': 'Unanswered Topics', | 857 'title': 'Unanswered Topics', |
870 'page': page, | 858 'page': page, |
871 'page_nav': page_nav, | 859 'page_nav': page_nav, |
872 'unread': False, | 860 'unread': False, |
873 }, | 861 }) |
874 context_instance=RequestContext(request)) | |
875 | 862 |
876 | 863 |
877 def active_topics(request, num): | 864 def active_topics(request, num): |
878 """Displays the last num topics that have been posted to.""" | 865 """Displays the last num topics that have been posted to.""" |
879 | 866 |
908 # we do this for the template since it is rendered twice | 895 # we do this for the template since it is rendered twice |
909 page_nav = render_to_string('forums/pagination.html', {'page': page}) | 896 page_nav = render_to_string('forums/pagination.html', {'page': page}) |
910 | 897 |
911 title = 'Last %d Active Topics' % num | 898 title = 'Last %d Active Topics' % num |
912 | 899 |
913 return render_to_response('forums/topic_list.html', { | 900 return render(request, 'forums/topic_list.html', { |
914 'title': title, | 901 'title': title, |
915 'page': page, | 902 'page': page, |
916 'page_nav': page_nav, | 903 'page_nav': page_nav, |
917 'unread': False, | 904 'unread': False, |
918 }, | 905 }) |
919 context_instance=RequestContext(request)) | |
920 | 906 |
921 | 907 |
922 @login_required | 908 @login_required |
923 def my_posts(request): | 909 def my_posts(request): |
924 """Displays a list of posts the requesting user made.""" | 910 """Displays a list of posts the requesting user made.""" |
943 return HttpResponseForbidden("You don't have permission for this post.") | 929 return HttpResponseForbidden("You don't have permission for this post.") |
944 | 930 |
945 ip_users = sorted(set(Post.objects.filter( | 931 ip_users = sorted(set(Post.objects.filter( |
946 user_ip=post.user_ip).values_list('user__username', flat=True))) | 932 user_ip=post.user_ip).values_list('user__username', flat=True))) |
947 | 933 |
948 return render_to_response('forums/post_ip.html', { | 934 return render(request, 'forums/post_ip.html', { |
949 'post': post, | 935 'post': post, |
950 'ip_users': ip_users, | 936 'ip_users': ip_users, |
951 }, | 937 }) |
952 context_instance=RequestContext(request)) | |
953 | 938 |
954 | 939 |
955 def _user_posts(request, target_user, req_user, page_title): | 940 def _user_posts(request, target_user, req_user, page_title): |
956 """Displays a list of posts made by the target user. | 941 """Displays a list of posts made by the target user. |
957 req_user is the user trying to view the posts. Only the forums | 942 req_user is the user trying to view the posts. Only the forums |
970 raise Http404 | 955 raise Http404 |
971 | 956 |
972 # we do this for the template since it is rendered twice | 957 # we do this for the template since it is rendered twice |
973 page_nav = render_to_string('forums/pagination.html', {'page': page}) | 958 page_nav = render_to_string('forums/pagination.html', {'page': page}) |
974 | 959 |
975 return render_to_response('forums/post_list.html', { | 960 return render(request, 'forums/post_list.html', { |
976 'title': page_title, | 961 'title': page_title, |
977 'page': page, | 962 'page': page, |
978 'page_nav': page_nav, | 963 'page_nav': page_nav, |
979 }, | 964 }) |
980 context_instance=RequestContext(request)) | |
981 | 965 |
982 | 966 |
983 def _bump_post_count(user): | 967 def _bump_post_count(user): |
984 """ | 968 """ |
985 Increments the forum_post_count for the given user. | 969 Increments the forum_post_count for the given user. |