Add support for testing twitter integration
authorMagnus Hagander <magnus@hagander.net>
Mon, 15 Oct 2018 16:58:17 +0000 (18:58 +0200)
committerMagnus Hagander <magnus@hagander.net>
Mon, 15 Oct 2018 16:58:17 +0000 (18:58 +0200)
We test it by sending a direct message from the configured account. That
way we can make sure all keys are correctly setup and still working
without having to post anything in public.

postgresqleu/confreg/backendforms.py
postgresqleu/confreg/backendviews.py
template/confreg/admin_integ_twitter.html

index 33779512ce078477b32ceff7b6fc55cc13293c4c..6e7385ba0121b6684eb9188dad04ad6350932a33 100644 (file)
@@ -776,3 +776,7 @@ class TwitterForm(ConcurrentProtectedModelForm):
        class Meta:
                model = Conference
                fields = ['twittersync_active', ]
+
+class TwitterTestForm(django.forms.Form):
+       recipient = django.forms.CharField(max_length=64)
+       message = django.forms.CharField(max_length=200)
index f7be4b44321521c4a703acadfc392c2f6258bc00..4eba24d1a68a24e3fb07a868b7e87cb7a71b96d3 100644 (file)
@@ -18,7 +18,7 @@ from postgresqleu.util.middleware import RedirectException
 from postgresqleu.util.db import exec_to_list, exec_to_dict, exec_no_result
 from postgresqleu.util.lists import flatten_list
 from postgresqleu.util.decorators import superuser_required
-from postgresqleu.util.messaging.twitter import TwitterSetup
+from postgresqleu.util.messaging.twitter import Twitter, TwitterSetup
 
 from models import Conference, ConferenceSeries
 from models import AccessToken
@@ -38,7 +38,7 @@ from backendforms import BackendAccessTokenForm
 from backendforms import BackendConferenceSeriesForm
 from backendforms import BackendTshirtSizeForm
 from backendforms import BackendNewsForm
-from backendforms import TwitterForm
+from backendforms import TwitterForm, TwitterTestForm
 
 def get_authenticated_conference(request, urlname):
        if not request.user.is_authenticated:
@@ -634,6 +634,20 @@ def twitter_integration(request, urlname):
                        conference.save()
                        messages.info(request, 'Twitter integration disabled')
                        return HttpResponseRedirect('.')
+               elif request.POST.get('test_twitter', '') == '1':
+                       testform = TwitterTestForm(data=request.POST)
+                       if testform.is_valid():
+                               tw = Twitter(conference)
+                               recipient = testform.cleaned_data['recipient']
+                               message = testform.cleaned_data['message']
+
+                               ok, msg = tw.send_message(recipient, message)
+                               if ok:
+                                       messages.info(request, 'Message successfully sent to {0}'.format(recipient))
+                               else:
+                                       messages.error(request, 'Failed to send to {0}: {1}'.format(recipient, msg))
+                               return HttpResponseRedirect('.')
+                       form = TwitterForm(instance=conference)
                else:
                        form = TwitterForm(instance=conference, data=request.POST)
                        if form.is_valid():
@@ -641,11 +655,13 @@ def twitter_integration(request, urlname):
                                return HttpResponseRedirect('.')
        else:
                form = TwitterForm(instance=conference)
+               testform = TwitterTestForm()
 
 
        return render(request, 'confreg/admin_integ_twitter.html', {
                'conference': conference,
                'form': form,
+               'testform': testform,
                'helplink': 'integrations#twitter',
        })
 
index 7a2f3400f3a75fe8d51cecc88571f8e5bfd5e85b..a0bb79aa52aa0007bb0c6e67503cd23c2beec108 100644 (file)
@@ -8,6 +8,17 @@
 <form class="form-horizontal" method="POST" action="." enctype="multipart/form-data">{%csrf_token%}
 {%include "confreg/admin_backend_form_content.html" %}
 </form>
+
+<h3>Test twitter integration</h3>
+<p>
+The twitter integration can be tested by sending a direct message to a user,
+for example yourself.
+</p>
+<form method="post" action="." class="form-horizontal">{%csrf_token%}
+<input type="hidden" name="test_twitter" value="1">
+{%include "confreg/admin_backend_form_content.html" with form=testform%}
+</form>
+
 <h3>Disable twitter integration</h3>
 <p>
   Disabling twitter integration will remove all OAuth keys.