Commit 358a16217264be2d0b52ce22b6e6986e12fee41c
1 parent
9d9caf57
Exists in
master
and in
13 other branches
Using CBV to handle get and post
Showing
2 changed files
with
74 additions
and
81 deletions
Show diff stats
src/super_archives/urls.py
| 1 | 1 | from django.conf.urls import patterns, include, url |
| 2 | 2 | |
| 3 | -from .views import EmailView, EmailValidationView | |
| 3 | +from .views import EmailView, EmailValidationView, ThreadView | |
| 4 | 4 | |
| 5 | 5 | |
| 6 | 6 | urlpatterns = patterns('super_archives.views', |
| 7 | 7 | # url(r'thread/(?P<thread>\d+)/$', 'thread', name='thread'), |
| 8 | - url(r'thread/(?P<mailinglist>[-\w]+)/(?P<thread_token>[-\w]+)$', 'thread', | |
| 9 | - name="thread_view"), | |
| 8 | + url(r'thread/(?P<mailinglist>[-\w]+)/(?P<thread_token>[-\w]+)$', | |
| 9 | + ThreadView.as_view(), name="thread_view"), | |
| 10 | 10 | url(r'thread/$', 'list_messages', name='thread_list'), |
| 11 | 11 | url(r'manage/email/validate/?$', EmailValidationView.as_view(), |
| 12 | 12 | name="archive_email_validation_view"), | ... | ... |
src/super_archives/views.py
| ... | ... | @@ -23,96 +23,89 @@ from .utils.email import send_verification_email |
| 23 | 23 | from .models import MailingList, Thread, EmailAddress, EmailAddressValidation |
| 24 | 24 | |
| 25 | 25 | |
| 26 | -def thread(request, mailinglist, thread_token): | |
| 27 | - if request.method == 'GET': | |
| 28 | - return thread_get(request, mailinglist, thread_token) | |
| 29 | - elif request.method == 'POST': | |
| 30 | - return thread_post(request, mailinglist, thread_token) | |
| 31 | - else: | |
| 32 | - return HttpResponseNotAllowed(['HEAD', 'GET', 'POST']) | |
| 33 | - | |
| 26 | +class ThreadView(View): | |
| 27 | + http_method_names = [u'get', u'post'] | |
| 34 | 28 | |
| 35 | -def thread_get(request, mailinglist, thread_token): | |
| 36 | - try: | |
| 37 | - first_message = queries.get_first_message_in_thread(mailinglist, | |
| 38 | - thread_token) | |
| 39 | - except ObjectDoesNotExist: | |
| 40 | - raise http.Http404 | |
| 29 | + def get(self, request, mailinglist, thread_token): | |
| 30 | + try: | |
| 31 | + first_message = queries.get_first_message_in_thread(mailinglist, | |
| 32 | + thread_token) | |
| 33 | + except ObjectDoesNotExist: | |
| 34 | + raise http.Http404 | |
| 41 | 35 | |
| 42 | - thread = Thread.objects.get(subject_token=thread_token, | |
| 43 | - mailinglist__name=mailinglist) | |
| 44 | - thread.hit(request) | |
| 36 | + thread = Thread.objects.get(subject_token=thread_token, | |
| 37 | + mailinglist__name=mailinglist) | |
| 38 | + thread.hit(request) | |
| 45 | 39 | |
| 46 | - order_by = request.GET.get('order') | |
| 47 | - if order_by == 'voted': | |
| 48 | - msgs_query = queries.get_messages_by_voted() | |
| 49 | - else: | |
| 50 | - msgs_query = queries.get_messages_by_date() | |
| 40 | + order_by = request.GET.get('order') | |
| 41 | + if order_by == 'voted': | |
| 42 | + msgs_query = queries.get_messages_by_voted() | |
| 43 | + else: | |
| 44 | + msgs_query = queries.get_messages_by_date() | |
| 51 | 45 | |
| 52 | - msgs_query = msgs_query.filter(thread__subject_token=thread_token) | |
| 53 | - msgs_query = msgs_query.filter(thread__mailinglist__name=mailinglist) | |
| 54 | - emails = msgs_query.exclude(id=first_message.id) | |
| 46 | + msgs_query = msgs_query.filter(thread__subject_token=thread_token) | |
| 47 | + msgs_query = msgs_query.filter(thread__mailinglist__name=mailinglist) | |
| 48 | + emails = msgs_query.exclude(id=first_message.id) | |
| 55 | 49 | |
| 56 | - total_votes = first_message.votes_count() | |
| 57 | - for email in emails: | |
| 58 | - total_votes += email.votes_count() | |
| 50 | + total_votes = first_message.votes_count() | |
| 51 | + for email in emails: | |
| 52 | + total_votes += email.votes_count() | |
| 59 | 53 | |
| 60 | - # Update relevance score | |
| 61 | - thread.update_score() | |
| 54 | + # Update relevance score | |
| 55 | + thread.update_score() | |
| 62 | 56 | |
| 63 | - context = { | |
| 64 | - 'first_msg': first_message, | |
| 65 | - 'emails': [first_message] + list(emails), | |
| 66 | - 'pagehits': thread.hits, | |
| 67 | - 'total_votes': total_votes, | |
| 68 | - 'thread': thread, | |
| 69 | - } | |
| 57 | + context = { | |
| 58 | + 'first_msg': first_message, | |
| 59 | + 'emails': [first_message] + list(emails), | |
| 60 | + 'pagehits': thread.hits, | |
| 61 | + 'total_votes': total_votes, | |
| 62 | + 'thread': thread, | |
| 63 | + } | |
| 70 | 64 | |
| 71 | - return render(request, 'message-thread.html', context) | |
| 65 | + return render(request, 'message-thread.html', context) | |
| 72 | 66 | |
| 67 | + def post(self, request, mailinglist, thread_token): | |
| 68 | + try: | |
| 69 | + thread = Thread.objects.get(subject_token=thread_token, | |
| 70 | + mailinglist__name=mailinglist) | |
| 71 | + except Thread.DoesNotExist: | |
| 72 | + raise http.Http404 | |
| 73 | 73 | |
| 74 | -def thread_post(request, mailinglist, thread_token): | |
| 75 | - try: | |
| 76 | - thread = Thread.objects.get(subject_token=thread_token, | |
| 77 | - mailinglist__name=mailinglist) | |
| 78 | - except Thread.DoesNotExist: | |
| 79 | - raise http.Http404 | |
| 80 | - | |
| 81 | - data = {} | |
| 82 | - data['from'] = '{} <{}>'.format(request.user.get_full_name(), | |
| 83 | - request.user.email) | |
| 84 | - data['subject'] = thread.message_set.first().subject_clean | |
| 85 | - data['body'] = request.POST.get('emailbody', '').strip() | |
| 74 | + data = {} | |
| 75 | + data['from'] = '{} <{}>'.format(request.user.get_full_name(), | |
| 76 | + request.user.email) | |
| 77 | + data['subject'] = thread.message_set.first().subject_clean | |
| 78 | + data['body'] = request.POST.get('emailbody', '').strip() | |
| 86 | 79 | |
| 87 | - url = urlparse.urljoin(settings.MAILMAN_API_URL, mailinglist + '/sendmail') | |
| 80 | + url = urlparse.urljoin(settings.MAILMAN_API_URL, mailinglist + '/sendmail') | |
| 88 | 81 | |
| 89 | - error_msg = None | |
| 90 | - try: | |
| 91 | - resp = requests.post(url, data=data, timeout=2) | |
| 92 | - except requests.exceptions.ConnectionError: | |
| 93 | - resp = None | |
| 94 | - error_msg = _('Error trying to connect to Mailman API') | |
| 95 | - except requests.exceptions.Timeout: | |
| 96 | - resp = None | |
| 97 | - error_msg = _('Timout trying to connect to Mailman API') | |
| 98 | - | |
| 99 | - if resp and resp.status_code == 200: | |
| 100 | - messages.success(request, _("Your message was sent. It may take " | |
| 101 | - "some minutes before it's delivered. " | |
| 102 | - "Why don't you breath some fresh air " | |
| 103 | - "in the meanwhile.")) | |
| 104 | - else: | |
| 105 | - if not error_msg: | |
| 106 | - if resp is not None: | |
| 107 | - if resp.status_code == 400: | |
| 108 | - error_msg = _('You cannot send an empty email') | |
| 109 | - elif resp.status_code == 404: | |
| 110 | - error_msg = _('Mailing list does not exist') | |
| 111 | - else: | |
| 112 | - error_msg = _('Unkown error trying to connect to Mailman API') | |
| 113 | - messages.error(request, error_msg) | |
| 114 | - | |
| 115 | - return thread_get(request, mailinglist, thread_token) | |
| 82 | + error_msg = None | |
| 83 | + try: | |
| 84 | + resp = requests.post(url, data=data, timeout=2) | |
| 85 | + except requests.exceptions.ConnectionError: | |
| 86 | + resp = None | |
| 87 | + error_msg = _('Error trying to connect to Mailman API') | |
| 88 | + except requests.exceptions.Timeout: | |
| 89 | + resp = None | |
| 90 | + error_msg = _('Timout trying to connect to Mailman API') | |
| 91 | + | |
| 92 | + if resp and resp.status_code == 200: | |
| 93 | + messages.success(request, _("Your message was sent. It may take " | |
| 94 | + "some minutes before it's delivered. " | |
| 95 | + "Why don't you breath some fresh air " | |
| 96 | + "in the meanwhile.")) | |
| 97 | + else: | |
| 98 | + if not error_msg: | |
| 99 | + if resp is not None: | |
| 100 | + if resp.status_code == 400: | |
| 101 | + error_msg = _('You cannot send an empty email') | |
| 102 | + elif resp.status_code == 404: | |
| 103 | + error_msg = _('Mailing list does not exist') | |
| 104 | + else: | |
| 105 | + error_msg = _('Unkown error trying to connect to Mailman API') | |
| 106 | + messages.error(request, error_msg) | |
| 107 | + | |
| 108 | + return self.get(request, mailinglist, thread_token) | |
| 116 | 109 | |
| 117 | 110 | |
| 118 | 111 | def list_messages(request): | ... | ... |