Compare commits

...

19 Commits

Author SHA1 Message Date
2171d7fda9 Fix string index 2023-06-27 01:19:51 +01:00
3c4ccfb103 Fix url 2023-06-27 01:14:23 +01:00
04c7e4b518 Fix ommited json parsing wotsit 2023-06-27 01:06:34 +01:00
beb0ba915d Fix import, again 2023-06-27 01:01:11 +01:00
ac15ab3729 Different header access method 2023-06-27 00:55:04 +01:00
15230cb361 Okay, put that back where it was because I inavertently overloaded my import
Flashbacks to my java days...
2023-06-27 00:48:25 +01:00
b5b8dc104c Add debug print 2023-06-27 00:42:45 +01:00
55aa41acfd More fiddling with auth 2023-06-27 00:42:22 +01:00
63eb3bebef What if I gave it the right arguments. That might be a good start. 2023-06-27 00:14:36 +01:00
f76222763d Try again at signing 2023-06-27 00:07:00 +01:00
32c8573c71 Third shot 2023-06-26 23:33:25 +01:00
146dbb3be7 >.< 2023-06-26 23:23:34 +01:00
c2cb73f73d Oops 2023-06-26 23:17:25 +01:00
e440ef88d4 Second shot at webhook reciever 2023-06-26 23:06:27 +01:00
13fec124c6 That was also dumb, fix that too 2023-06-26 22:36:20 +01:00
e842471b9e Use f-strings correctly, not like a big dumb 2023-06-26 22:33:50 +01:00
039f9f68d3 Correct method of CRSF exemption for webhook reciever 2023-06-26 22:27:20 +01:00
2c808d0adf Mockup webhook recieving view 2023-06-26 19:30:29 +01:00
76cd5459fc Add button for creating forum thread draft from event detail
TODO: Allow RIGS to ingest POST requests sent from the forum on new posts in RIG info to link up the forum thread

RE https://forum.nottinghamtec.co.uk/t/rigs-discourse-integration/15592/21
2023-06-26 19:08:28 +01:00
5 changed files with 76 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
# Generated by Django 3.2.18 on 2023-06-26 17:46
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('RIGS', '0049_auto_20230529_1123'),
]
operations = [
migrations.AddField(
model_name='event',
name='forum_url',
field=models.URLField(blank=True, null=True),
),
]

View File

@@ -357,6 +357,8 @@ class Event(models.Model, RevisionMixin):
auth_request_at = models.DateTimeField(null=True, blank=True)
auth_request_to = models.EmailField(blank=True, default='')
forum_url = models.URLField(null=True, blank=True)
@property
def display_id(self):
if self.pk:

View File

@@ -77,6 +77,15 @@
<dt class="col-sm-6">PO</dt>
<dd class="col-sm-6">{{ object.purchase_order }}</dd>
{% endif %}
<dt class="col-6">Forum Thread</dt>
{% if event.forum_thread %}
<dd class="col-6"><a href="{{event.forum_thread}}">{{event.forum_thread}}</a></dd>
{% else %}
<a href="{% url 'event_thread' event.pk %}" class="btn btn-primary" title="Create Forum Thread"><span
class="fas fa-plus"></span> <span
class="hidden-xs">Create Forum Thread</span></a>
{% endif %}
</dl>
</div>
</div>

View File

@@ -110,6 +110,9 @@ urlpatterns = [
path('event/<int:pk>/checkin/add/', login_required(views.EventCheckInOverride.as_view()),
name='event_checkin_override'),
path('event/<int:pk>/thread/', permission_required_with_403('RIGS.change_event')(views.CreateForumThread.as_view()), name='event_thread'),
path('event/webhook/', views.RecieveForumWebhook.as_view(), name='webhook_recieve'),
# Finance
path('invoice/', permission_required_with_403('RIGS.view_invoice')(views.InvoiceIndex.as_view()),
name='invoice_list'),

View File

@@ -3,6 +3,12 @@ import datetime
import re
import premailer
import simplejson
import urllib
import hmac
import hashlib
from envparse import env
from bs4 import BeautifulSoup
from django.conf import settings
from django.contrib import messages
@@ -19,6 +25,7 @@ from django.urls import reverse_lazy
from django.utils import timezone
from django.utils.decorators import method_decorator
from django.views import generic
from django.views.decorators.csrf import csrf_exempt
from PyRIGS import decorators
from PyRIGS.views import OEmbedView, is_ajax, ModalURLMixin, PrintView, get_related
@@ -377,3 +384,40 @@ class EventAuthoriseRequestEmailPreview(generic.DetailView):
context['to_name'] = self.request.GET.get('to_name', None)
context['target'] = 'event_authorise_form_preview'
return context
class CreateForumThread(generic.base.RedirectView):
permanent = False
def get_redirect_url(self, *args, **kwargs):
event = get_object_or_404(models.Event, pk=kwargs['pk'])
if event.forum_url:
return event.forum_url
params = {
'title': str(event),
'body': f'https://rigs.nottinghamtec.co.uk/event/{event.pk}',
'category': 'rig-info'
}
return f'https://forum.nottinghamtec.co.uk/new-topic?{urllib.parse.urlencode(params)}'
class RecieveForumWebhook(generic.View):
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
computed = f"sha256={hmac.new(env('FORUM_WEBHOOK_SECRET').encode(), request.body, hashlib.sha256).hexdigest()}"
print(computed)
if not hmac.compare_digest(request.headers.get('X-Discourse-Event-Signature'), computed):
return HttpResponseForbidden('Invalid signature header')
body = simplejson.loads(request.body.decode('utf-8'))
event_id = int(body['topic']['title'][1:6]) # find the ID, force convert it to an int to eliminate leading zeros
event = models.Event.objects.filter(pk=event_id).first()
if event:
event.forum_url = f"https://forum.nottinghamtec.co.uk/t/{body['topic']['slug']}"
event.save()
return HttpResponse(status=202)
return HttpResponse(status=204)