summaryrefslogtreecommitdiffstats
path: root/patchwork/views/__init__.py
blob: b7916f0b7d7b7d3c26e58d47ae1819b8b3291adc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# Patchwork - automated patch tracking system
# Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
#
# 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


from base import *
from patchwork.utils import Order, get_patch_ids, bundle_actions, set_bundle
from patchwork.paginator import Paginator
from patchwork.forms import MultiplePatchForm
from patchwork.models import Comment
import re
import datetime

try:
    from email.mime.nonmultipart import MIMENonMultipart
    from email.encoders import encode_7or8bit
    from email.parser import HeaderParser
    from email.header import Header
    import email.utils
except ImportError:
    # Python 2.4 compatibility
    from email.MIMENonMultipart import MIMENonMultipart
    from email.Encoders import encode_7or8bit
    from email.Parser import HeaderParser
    from email.Header import Header
    import email.Utils
    email.utils = email.Utils

def generic_list(request, project, view,
        view_args = {}, filter_settings = [], patches = None,
        editable_order = False):

    context = PatchworkRequestContext(request,
            list_view = view,
            list_view_params = view_args)

    context.project = project
    order = Order(request.REQUEST.get('order'), editable = editable_order)

    # Explicitly set data to None because request.POST will be an empty dict
    # when the form is not submitted, but passing a non-None data argument to
    # a forms.Form will make it bound and we don't want that to happen unless
    # there's been a form submission.
    data = None
    if request.method == 'POST':
        data = request.POST
    user = request.user
    properties_form = None
    if project.is_editable(user):

        # we only pass the post data to the MultiplePatchForm if that was
        # the actual form submitted
        data_tmp = None
        if data and data.get('form', '') == 'patchlistform':
            data_tmp = data

        properties_form = MultiplePatchForm(project, data = data_tmp)

    if request.method == 'POST' and data.get('form') == 'patchlistform':
        action = data.get('action', '').lower()

        # special case: the user may have hit enter in the 'create bundle'
        # text field, so if non-empty, assume the create action:
        if data.get('bundle_name', False):
            action = 'create'

        ps = Patch.objects.filter(id__in = get_patch_ids(data))

        if action in bundle_actions:
            errors = set_bundle(user, project, action, data, ps, context)

        elif properties_form and action == properties_form.action:
            errors = process_multiplepatch_form(properties_form, user,
                                                action, ps, context)
        else:
            errors = []

        if errors:
            context['errors'] = errors

    for (filterclass, setting) in filter_settings:
        if isinstance(setting, dict):
            context.filters.set_status(filterclass, **setting)
        elif isinstance(setting, list):
            context.filters.set_status(filterclass, *setting)
        else:
            context.filters.set_status(filterclass, setting)

    if patches is None:
        patches = Patch.objects.filter(project=project)

    # annotate with tag counts
    patches = patches.with_tag_counts(project)

    patches = context.filters.apply(patches)
    if not editable_order:
        patches = order.apply(patches)

    # we don't need the content or headers for a list; they're text fields
    # that can potentially contain a lot of data
    patches = patches.defer('content', 'headers')

    # but we will need to follow the state and submitter relations for
    # rendering the list template
    patches = patches.select_related('state', 'submitter')

    paginator = Paginator(request, patches)

    context.update({
            'page':             paginator.current_page,
            'patchform':        properties_form,
            'project':          project,
            'order':            order,
            })

    return context


def process_multiplepatch_form(form, user, action, patches, context):
    errors = []
    if not form.is_valid() or action != form.action:
        return ['The submitted form data was invalid']

    if len(patches) == 0:
        context.add_message("No patches selected; nothing updated")
        return errors

    changed_patches = 0
    for patch in patches:
        if not patch.is_editable(user):
            errors.append("You don't have permissions to edit patch '%s'"
                            % patch.name)
            continue

        changed_patches += 1
        form.save(patch)

    if changed_patches == 1:
        context.add_message("1 patch updated")
    elif changed_patches > 1:
        context.add_message("%d patches updated" % changed_patches)
    else:
        context.add_message("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')

    comment = None
    try:
        comment = Comment.objects.get(patch = patch, msgid = patch.msgid)
    except Exception:
        pass

    body = ''
    if comment:
        body = comment.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 = ''

    for comment in Comment.objects.filter(patch = patch) \
            .exclude(msgid = patch.msgid):
        body += comment.patch_responses()

    if postscript:
        body += '---\n' + postscript + '\n'

    if patch.content:
        body += '\n' + patch.content

    delta = patch.date - datetime.datetime.utcfromtimestamp(0)
    utc_timestamp = delta.seconds + delta.days*24*3600

    mail = PatchMbox(body)
    mail['Subject'] = patch.name
    mail['From'] = email.utils.formataddr((
                    str(Header(patch.submitter.name, mail.patch_charset)),
                    patch.submitter.email))
    mail['X-Patchwork-Id'] = str(patch.id)
    mail['Message-Id'] = patch.msgid
    mail.set_unixfrom('From patchwork ' + patch.date.ctime())


    copied_headers = ['To', 'Cc', 'Date']
    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)

    return mail