views.py 4.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from django import forms
  2. from django.core.urlresolvers import reverse
  3. from django.db.models import F
  4. from django.shortcuts import redirect
  5. from django.template import RequestContext
  6. from django.utils.translation import ugettext as _
  7. from misago.authn.decorators import block_guest
  8. from misago.forms import Form, FormLayout, FormFields
  9. from misago.messages import Message
  10. from misago.watcher.models import ThreadWatch
  11. from misago.utils import make_pagination
  12. @block_guest
  13. def watched_threads(request, page=0, new=False):
  14. # Find mode and fetch threads
  15. queryset = ThreadWatch.objects.filter(user=request.user).filter(forum_id__in=request.acl.threads.get_readable_forums(request.acl)).select_related('thread').filter(thread__moderated=False).filter(thread__deleted=False)
  16. if new:
  17. queryset = queryset.filter(last_read__lt=F('thread__last'))
  18. count = queryset.count()
  19. pagination = make_pagination(page, count, request.settings.threads_per_page)
  20. queryset = queryset.order_by('-thread__last')
  21. if request.settings.threads_per_page < count:
  22. queryset = queryset[pagination['start']:pagination['stop']]
  23. queryset.prefetch_related('thread__forum', 'thread__last_poster')
  24. threads = []
  25. for thread in queryset:
  26. thread.thread.send_email = thread.email
  27. thread.thread.is_read = thread.thread.last <= thread.last_read
  28. threads.append(thread.thread)
  29. # Build form and handle submit
  30. form = None
  31. message = request.messages.get_message('watcher')
  32. if threads:
  33. form_fields = {}
  34. form_fields['list_action'] = forms.ChoiceField(choices=(
  35. ('mails', _("Send me e-mails")),
  36. ('nomails', _("Don't send me e-mails")),
  37. ('delete', _("Remove from watched threads")),
  38. ))
  39. list_choices = []
  40. for item in threads:
  41. list_choices.append((item.pk, None))
  42. form_fields['list_items'] = forms.MultipleChoiceField(choices=list_choices, widget=forms.CheckboxSelectMultiple)
  43. form = type('ThreadsWatchForm', (Form,), form_fields)
  44. if request.method == 'POST':
  45. form = form(request.POST, request=request)
  46. if form.is_valid():
  47. checked_items = []
  48. for thread in threads:
  49. if str(thread.pk) in form.cleaned_data['list_items']:
  50. checked_items.append(thread.pk)
  51. if checked_items:
  52. queryset = ThreadWatch.objects.filter(user=request.user).filter(thread_id__in = checked_items)
  53. if form.cleaned_data['list_action'] == 'mails':
  54. queryset.update(email=True)
  55. request.messages.set_flash(Message(_('Selected threads will now send e-mails with notifications when somebody replies to them.')), 'success', 'watcher')
  56. if form.cleaned_data['list_action'] == 'nomails':
  57. queryset.update(email=False)
  58. request.messages.set_flash(Message(_('Selected threads will no longer send e-mails with notifications when somebody replies to them.')), 'success', 'watcher')
  59. if form.cleaned_data['list_action'] == 'delete':
  60. queryset.delete()
  61. request.messages.set_flash(Message(_('Selected threads have been removed from watched threads list.')), 'success', 'watcher')
  62. return redirect(reverse('watched_threads_new' if new else 'watched_threads'))
  63. else:
  64. message = Message(_("You have to select at least one thread."), 'error')
  65. else:
  66. if 'list_action' in form.errors:
  67. message = Message(_("Action requested is incorrect."), 'error')
  68. else:
  69. message = Message(form.non_field_errors()[0], 'error')
  70. else:
  71. form = form(request=request)
  72. # Display page
  73. return request.theme.render_to_response('watched.html',
  74. {
  75. 'total_items': count,
  76. 'pagination': pagination,
  77. 'new': new,
  78. 'list_form': FormFields(form).fields if form else None,
  79. 'threads': threads,
  80. 'message': message,
  81. },
  82. context_instance=RequestContext(request))