Commit f8695e46f09f536152077000ec6d180a9ea232c4
1 parent
4ebba8c9
Exists in
master
and in
39 other branches
Calling mailman api to send emails using requests
Showing
5 changed files
with
76 additions
and
9 deletions
Show diff stats
TODO.rst
src/colab/custom_settings.py
| @@ -259,6 +259,10 @@ FEEDZILLA_SITE_TITLE = gettext(u'Planet Colab') | @@ -259,6 +259,10 @@ FEEDZILLA_SITE_TITLE = gettext(u'Planet Colab') | ||
| 259 | FEEDZILLA_SITE_DESCRIPTION = gettext(u'Colab blog aggregator') | 259 | FEEDZILLA_SITE_DESCRIPTION = gettext(u'Colab blog aggregator') |
| 260 | 260 | ||
| 261 | 261 | ||
| 262 | +### Mailman API settings | ||
| 263 | +MAILMAN_API_URL = 'http://listas.interlegis.gov.br:8000' | ||
| 264 | + | ||
| 265 | + | ||
| 262 | ### BrowserID / Persona | 266 | ### BrowserID / Persona |
| 263 | SITE_URL = 'https://colab.interlegis.leg.br' | 267 | SITE_URL = 'https://colab.interlegis.leg.br' |
| 264 | BROWSERID_AUDIENCES = [SITE_URL] | 268 | BROWSERID_AUDIENCES = [SITE_URL] |
src/super_archives/templates/message-thread.html
| @@ -74,10 +74,12 @@ | @@ -74,10 +74,12 @@ | ||
| 74 | $('html, body').animate({scrollTop: aTag.offset().top}, 800); | 74 | $('html, body').animate({scrollTop: aTag.offset().top}, 800); |
| 75 | } | 75 | } |
| 76 | 76 | ||
| 77 | + {% if user.is_active %} | ||
| 77 | function focus_reply(event) { | 78 | function focus_reply(event) { |
| 78 | scrollToAnchor('#msg-reply'); | 79 | scrollToAnchor('#msg-reply'); |
| 79 | $('textarea', '#msg-reply').focus(); | 80 | $('textarea', '#msg-reply').focus(); |
| 80 | } | 81 | } |
| 82 | + {% endif %} | ||
| 81 | 83 | ||
| 82 | // Binding functions | 84 | // Binding functions |
| 83 | $(function() { | 85 | $(function() { |
src/super_archives/templates/superarchives/includes/message.html
| @@ -35,9 +35,11 @@ | @@ -35,9 +35,11 @@ | ||
| 35 | </button> | 35 | </button> |
| 36 | </div> | 36 | </div> |
| 37 | 37 | ||
| 38 | + {% if user.is_active %} | ||
| 38 | <button class="btn btn-default reply" title="{% trans 'Reply' %}"> | 39 | <button class="btn btn-default reply" title="{% trans 'Reply' %}"> |
| 39 | <span class="glyphicon glyphicon-share"></span> | 40 | <span class="glyphicon glyphicon-share"></span> |
| 40 | </button> | 41 | </button> |
| 42 | + {% endif %} | ||
| 41 | </div> | 43 | </div> |
| 42 | </div> | 44 | </div> |
| 43 | {% endif %} | 45 | {% endif %} |
| @@ -48,15 +50,18 @@ | @@ -48,15 +50,18 @@ | ||
| 48 | {% if not reply %} | 50 | {% if not reply %} |
| 49 | {% display_message email %} | 51 | {% display_message email %} |
| 50 | {% else %} | 52 | {% else %} |
| 51 | - <p> | ||
| 52 | - <textarea placeholder="{% trans 'Send a message' %}" rows="5" class="form-control"></textarea> | ||
| 53 | - </p> | ||
| 54 | - <div class="col-lg-9 col-md-8 col-sm-8 col-xs-7"> | ||
| 55 | - <p class="quiet">{% trans "After sending a message it will take few minutes before it shows up in here. Why don't you grab a coffee?" %}</p> | ||
| 56 | - </div> | ||
| 57 | - <div class="col-lg-3 col-md-4 col-sm-4 col-xs-5 text-right"> | ||
| 58 | - <button class="btn btn-success btn-lg">{% trans 'Send' %}</button> | ||
| 59 | - </div> | 53 | + <form method="POST"> |
| 54 | + {% csrf_token %} | ||
| 55 | + <p> | ||
| 56 | + <textarea placeholder="{% trans 'Send a message' %}" rows="5" class="form-control"></textarea> | ||
| 57 | + </p> | ||
| 58 | + <div class="col-lg-9 col-md-8 col-sm-8 col-xs-7"> | ||
| 59 | + <p class="quiet">{% trans "After sending a message it will take few minutes before it shows up in here. Why don't you grab a coffee?" %}</p> | ||
| 60 | + </div> | ||
| 61 | + <div class="col-lg-3 col-md-4 col-sm-4 col-xs-5 text-right"> | ||
| 62 | + <button class="btn btn-success" type="submit">{% trans 'Send' %}</button> | ||
| 63 | + </div> | ||
| 64 | + </form> | ||
| 60 | {% endif %} | 65 | {% endif %} |
| 61 | </div> | 66 | </div> |
| 62 | </div> | 67 | </div> |
src/super_archives/views.py
| @@ -2,6 +2,9 @@ | @@ -2,6 +2,9 @@ | ||
| 2 | 2 | ||
| 3 | import smtplib | 3 | import smtplib |
| 4 | import logging | 4 | import logging |
| 5 | +import urlparse | ||
| 6 | + | ||
| 7 | +import requests | ||
| 5 | 8 | ||
| 6 | from django import http | 9 | from django import http |
| 7 | from django.conf import settings | 10 | from django.conf import settings |
| @@ -21,7 +24,15 @@ from .models import MailingList, Thread, EmailAddress, EmailAddressValidation | @@ -21,7 +24,15 @@ from .models import MailingList, Thread, EmailAddress, EmailAddressValidation | ||
| 21 | 24 | ||
| 22 | 25 | ||
| 23 | def thread(request, mailinglist, thread_token): | 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 | + | ||
| 24 | 34 | ||
| 35 | +def thread_get(request, mailinglist, thread_token): | ||
| 25 | try: | 36 | try: |
| 26 | first_message = queries.get_first_message_in_thread(mailinglist, | 37 | first_message = queries.get_first_message_in_thread(mailinglist, |
| 27 | thread_token) | 38 | thread_token) |
| @@ -60,6 +71,47 @@ def thread(request, mailinglist, thread_token): | @@ -60,6 +71,47 @@ def thread(request, mailinglist, thread_token): | ||
| 60 | return render(request, 'message-thread.html', context) | 71 | return render(request, 'message-thread.html', context) |
| 61 | 72 | ||
| 62 | 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['email_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('body', '').strip() | ||
| 86 | + | ||
| 87 | + url = urlparse.urljoin(settings.MAILMAN_API_URL, mailinglist + '/sendmail') | ||
| 88 | + | ||
| 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 and resp.status_code == 400: | ||
| 107 | + error_msg = _('You cannot send an empty email') | ||
| 108 | + else: | ||
| 109 | + error_msg = _('Unkown error trying to connect to Mailman API') | ||
| 110 | + messages.error(request, error_msg) | ||
| 111 | + | ||
| 112 | + return thread_get(request, mailinglist, thread_token) | ||
| 113 | + | ||
| 114 | + | ||
| 63 | def list_messages(request): | 115 | def list_messages(request): |
| 64 | selected_lists = request.GET.get('list', []) | 116 | selected_lists = request.GET.get('list', []) |
| 65 | if selected_lists: | 117 | if selected_lists: |