From: Stephen Finucane Date: Tue, 7 Feb 2017 11:17:13 +0000 (+0000) Subject: utils: Move patch_to_mbox to utils module X-Git-Tag: v2.0.0-rc1~55 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7d96268a56c2d9a229c2d06d9147567956f3b806;p=thirdparty%2Fpatchwork.git utils: Move patch_to_mbox to utils module There's no reason to have this in '__init__.py'. Signed-off-by: Stephen Finucane Reviewed-by: Daniel Axtens --- diff --git a/patchwork/views/__init__.py b/patchwork/views/__init__.py index db53cdf4..c8845150 100644 --- a/patchwork/views/__init__.py +++ b/patchwork/views/__init__.py @@ -17,25 +17,13 @@ # along with Patchwork; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -from __future__ import absolute_import - -import datetime -from email.encoders import encode_7or8bit -from email.header import Header -from email.mime.nonmultipart import MIMENonMultipart -from email.parser import HeaderParser -import email.utils -import re - from django.contrib import messages from django.shortcuts import get_object_or_404 -from django.utils import six from patchwork.filters import Filters from patchwork.forms import MultiplePatchForm from patchwork.models import Bundle from patchwork.models import BundlePatch -from patchwork.models import Comment from patchwork.models import Patch from patchwork.models import Project from patchwork.paginator import Paginator @@ -341,70 +329,3 @@ def process_multiplepatch_form(request, form, action, patches, context): messages.warning(request, 'No patches updated') return errors - - -class PatchMbox(MIMENonMultipart): - patch_charset = 'utf-8' - - def __init__(self, _text): - MIMENonMultipart.__init__(self, 'text', 'plain', - **{'charset': self.patch_charset}) - self.set_payload(_text.encode(self.patch_charset)) - encode_7or8bit(self) - - -def patch_to_mbox(patch): - postscript_re = re.compile('\n-{2,3} ?\n') - body = '' - - if patch.content: - body = patch.content.strip() + "\n" - - parts = postscript_re.split(body, 1) - if len(parts) == 2: - (body, postscript) = parts - body = body.strip() + "\n" - postscript = postscript.rstrip() - else: - postscript = '' - - # TODO(stephenfin): Make this use the tags infrastructure - for comment in Comment.objects.filter(submission=patch): - body += comment.patch_responses - - if postscript: - body += '---\n' + postscript + '\n' - - if patch.diff: - body += '\n' + patch.diff - - delta = patch.date - datetime.datetime.utcfromtimestamp(0) - utc_timestamp = delta.seconds + delta.days * 24 * 3600 - - mail = PatchMbox(body) - mail['Subject'] = patch.name - mail['X-Patchwork-Submitter'] = email.utils.formataddr(( - str(Header(patch.submitter.name, mail.patch_charset)), - patch.submitter.email)) - mail['X-Patchwork-Id'] = str(patch.id) - if patch.delegate: - mail['X-Patchwork-Delegate'] = str(patch.delegate.email) - mail['Message-Id'] = patch.msgid - mail.set_unixfrom('From patchwork ' + patch.date.ctime()) - - copied_headers = ['To', 'Cc', 'Date', 'From', 'List-Id'] - orig_headers = HeaderParser().parsestr(str(patch.headers)) - for header in copied_headers: - if header in orig_headers: - mail[header] = orig_headers[header] - - if 'Date' not in mail: - mail['Date'] = email.utils.formatdate(utc_timestamp) - - # NOTE(stephenfin) http://stackoverflow.com/a/28584090/613428 - if six.PY3: - mail = mail.as_bytes(True).decode() - else: - mail = mail.as_string(True) - - return mail diff --git a/patchwork/views/bundle.py b/patchwork/views/bundle.py index 09798473..f752094f 100644 --- a/patchwork/views/bundle.py +++ b/patchwork/views/bundle.py @@ -29,7 +29,8 @@ from django.shortcuts import render, get_object_or_404 from patchwork.filters import DelegateFilter from patchwork.forms import BundleForm, DeleteBundleForm from patchwork.models import Bundle, BundlePatch, Project -from patchwork.views import generic_list, patch_to_mbox +from patchwork.views import generic_list +from patchwork.views.utils import patch_to_mbox if settings.ENABLE_REST_API: from rest_framework.authentication import BasicAuthentication # noqa diff --git a/patchwork/views/patch.py b/patchwork/views/patch.py index 705359f7..d1e81d11 100644 --- a/patchwork/views/patch.py +++ b/patchwork/views/patch.py @@ -29,7 +29,8 @@ from django.shortcuts import render, get_object_or_404 from patchwork.forms import PatchForm, CreateBundleForm from patchwork.models import Patch, Project, Bundle, Submission -from patchwork.views import generic_list, patch_to_mbox +from patchwork.views import generic_list +from patchwork.views.utils import patch_to_mbox def patch_list(request, project_id): diff --git a/patchwork/views/utils.py b/patchwork/views/utils.py new file mode 100644 index 00000000..c9986476 --- /dev/null +++ b/patchwork/views/utils.py @@ -0,0 +1,106 @@ +# Patchwork - automated patch tracking system +# Copyright (C) 2008 Jeremy Kerr +# Copyright (C) 2017 Stephen Finucane +# +# This file is part of the Patchwork package. +# +# Patchwork is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# Patchwork is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Patchwork; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +import datetime +from email.encoders import encode_7or8bit +from email.header import Header +from email.mime.nonmultipart import MIMENonMultipart +from email.parser import HeaderParser +import email.utils +import re + +from django.utils import six + +from patchwork.models import Comment + + +class PatchMbox(MIMENonMultipart): + patch_charset = 'utf-8' + + def __init__(self, _text): + MIMENonMultipart.__init__(self, 'text', 'plain', + **{'charset': self.patch_charset}) + self.set_payload(_text.encode(self.patch_charset)) + encode_7or8bit(self) + + +def patch_to_mbox(patch): + """Get an mbox representation of a single patch. + + Arguments: + patch: The Patch object to convert. + + Returns: + A string for the mbox file. + """ + postscript_re = re.compile('\n-{2,3} ?\n') + body = '' + + if patch.content: + body = patch.content.strip() + "\n" + + parts = postscript_re.split(body, 1) + if len(parts) == 2: + (body, postscript) = parts + body = body.strip() + "\n" + postscript = postscript.rstrip() + else: + postscript = '' + + # TODO(stephenfin): Make this use the tags infrastructure + for comment in Comment.objects.filter(submission=patch): + body += comment.patch_responses + + if postscript: + body += '---\n' + postscript + '\n' + + if patch.diff: + body += '\n' + patch.diff + + delta = patch.date - datetime.datetime.utcfromtimestamp(0) + utc_timestamp = delta.seconds + delta.days * 24 * 3600 + + mail = PatchMbox(body) + mail['Subject'] = patch.name + mail['X-Patchwork-Submitter'] = email.utils.formataddr(( + str(Header(patch.submitter.name, mail.patch_charset)), + patch.submitter.email)) + mail['X-Patchwork-Id'] = str(patch.id) + if patch.delegate: + mail['X-Patchwork-Delegate'] = str(patch.delegate.email) + mail['Message-Id'] = patch.msgid + mail.set_unixfrom('From patchwork ' + patch.date.ctime()) + + copied_headers = ['To', 'Cc', 'Date', 'From', 'List-Id'] + orig_headers = HeaderParser().parsestr(str(patch.headers)) + for header in copied_headers: + if header in orig_headers: + mail[header] = orig_headers[header] + + if 'Date' not in mail: + mail['Date'] = email.utils.formatdate(utc_timestamp) + + # NOTE(stephenfin) http://stackoverflow.com/a/28584090/613428 + if six.PY3: + mail = mail.as_bytes(True).decode() + else: + mail = mail.as_string(True) + + return mail diff --git a/patchwork/views/xmlrpc.py b/patchwork/views/xmlrpc.py index 80e96b2f..0d438ef4 100644 --- a/patchwork/views/xmlrpc.py +++ b/patchwork/views/xmlrpc.py @@ -45,7 +45,7 @@ from patchwork.models import Patch from patchwork.models import Person from patchwork.models import Project from patchwork.models import State -from patchwork.views import patch_to_mbox +from patchwork.views.utils import patch_to_mbox class PatchworkXMLRPCDispatcher(SimpleXMLRPCDispatcher,