From 240d21bf3d6c6c0d34e0785ce3395f5102fe8816 Mon Sep 17 00:00:00 2001 From: Magnus Hagander Date: Thu, 17 Jan 2019 10:24:55 +0100 Subject: [PATCH] Create and track a mapping between bug ids and messageids Not used yet (though a prototype redirect view is present) since we need to populate it with data from the past, but with this we start collecting the mapping for future bugs. --- pgweb/misc/migrations/0001_bugidmap.py | 23 ++++++++++ pgweb/misc/migrations/__init__.py | 0 pgweb/misc/models.py | 8 +++- pgweb/misc/views.py | 58 ++++++++++++++++---------- pgweb/urls.py | 1 + 5 files changed, 65 insertions(+), 25 deletions(-) create mode 100644 pgweb/misc/migrations/0001_bugidmap.py create mode 100644 pgweb/misc/migrations/__init__.py diff --git a/pgweb/misc/migrations/0001_bugidmap.py b/pgweb/misc/migrations/0001_bugidmap.py new file mode 100644 index 00000000..5bcb8e26 --- /dev/null +++ b/pgweb/misc/migrations/0001_bugidmap.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.10 on 2019-01-17 08:41 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='BugIdMap', + fields=[ + ('id', models.IntegerField(primary_key=True, serialize=False)), + ('messageid', models.CharField(max_length=500)), + ], + ), + ] diff --git a/pgweb/misc/migrations/__init__.py b/pgweb/misc/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pgweb/misc/models.py b/pgweb/misc/models.py index 54f60fe2..90c7e240 100644 --- a/pgweb/misc/models.py +++ b/pgweb/misc/models.py @@ -1,3 +1,7 @@ -#from django.db import models +from django.db import models -# Create your models here. +class BugIdMap(models.Model): + # Explicit id field because we don't want a SERIAL here, since we generate + # the actual bug IDs externally. + id = models.IntegerField(null=False, blank=False, primary_key=True) + messageid = models.CharField(max_length=500, null=False, blank=False) diff --git a/pgweb/misc/views.py b/pgweb/misc/views.py index 0cd7c861..3cf8e3bb 100644 --- a/pgweb/misc/views.py +++ b/pgweb/misc/views.py @@ -1,6 +1,7 @@ from pgweb.util.decorators import login_required -from django.http import HttpResponse -from django.db import connection +from django.http import HttpResponse, HttpResponseRedirect +from django.db import connection, transaction +from django.shortcuts import get_object_or_404 from django.conf import settings import os @@ -12,6 +13,7 @@ from pgweb.util.helpers import template_to_string from pgweb.util.misc import send_template_mail from pgweb.core.models import Version +from pgweb.misc.models import BugIdMap from forms import SubmitBugForm @@ -26,29 +28,34 @@ def submitbug(request): if request.method == 'POST': form = SubmitBugForm(request.POST) if form.is_valid(): - c = connection.cursor() - c.execute("SELECT nextval('bug_id_seq')") - bugid = c.fetchall()[0][0] + with transaction.atomic(): + c = connection.cursor() + c.execute("SELECT nextval('bug_id_seq')") + bugid = c.fetchall()[0][0] - send_template_mail( - settings.BUGREPORT_NOREPLY_EMAIL, - settings.BUGREPORT_EMAIL, - 'BUG #%s: %s' % (bugid, form.cleaned_data['shortdesc']), - 'misc/bugmail.txt', - { + messageid = _make_bugs_messageid(bugid) + + BugIdMap(id=bugid, messageid=messageid.strip('<>')).save() + + send_template_mail( + settings.BUGREPORT_NOREPLY_EMAIL, + settings.BUGREPORT_EMAIL, + 'BUG #%s: %s' % (bugid, form.cleaned_data['shortdesc']), + 'misc/bugmail.txt', + { + 'bugid': bugid, + 'bug': form.cleaned_data, + }, + usergenerated=True, + cc=form.cleaned_data['email'], + replyto='%s, %s' % (form.cleaned_data['email'], settings.BUGREPORT_EMAIL), + sendername="PG Bug reporting form", + messageid=messageid, + ) + + return render_pgweb(request, 'support', 'misc/bug_completed.html', { 'bugid': bugid, - 'bug': form.cleaned_data, - }, - usergenerated=True, - cc=form.cleaned_data['email'], - replyto='%s, %s' % (form.cleaned_data['email'], settings.BUGREPORT_EMAIL), - sendername="PG Bug reporting form", - messageid=_make_bugs_messageid(bugid), - ) - - return render_pgweb(request, 'support', 'misc/bug_completed.html', { - 'bugid': bugid, - }) + }) else: form = SubmitBugForm(initial={ 'name': '%s %s' % (request.user.first_name, request.user.last_name), @@ -69,6 +76,11 @@ def submitbug(request): }) +def bugs_redir(request, bugid): + r = get_object_or_404(BugIdMap, id=bugid) + + return HttpResponseRedirect("{0}/message-id/{1}".format(settings.SITE_ROOT, r.messageid)) + # A crash testing URL. If the file /tmp/crashtest exists, raise a http 500 # error. Otherwise, just return a fixed text response def crashtest(request): diff --git a/pgweb/urls.py b/pgweb/urls.py index 56f6a3fe..615a6a85 100644 --- a/pgweb/urls.py +++ b/pgweb/urls.py @@ -83,6 +83,7 @@ urlpatterns = [ url(r'^account/submitbug/$', pgweb.misc.views.submitbug), url(r'^support/submitbug/$', RedirectView.as_view(url='/account/submitbug/', permanent=True)), url(r'^support/versioning/$', pgweb.core.views.versions), + url(r'^bugs_redir/(\d+)/$', pgweb.misc.views.bugs_redir), url(r'^about/sponsors/$', pgweb.sponsors.views.sponsors), url(r'^about/servers/$', pgweb.sponsors.views.servers),