summaryrefslogtreecommitdiffstats
path: root/apps/patchwork/views/xmlrpc.py
blob: 283eb34be865fd129c5b1ddcd09328f3fa98e225 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# 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
#
# Patchwork XMLRPC interface
#

from SimpleXMLRPCServer import SimpleXMLRPCDispatcher
from django.http import HttpResponse, HttpResponseRedirect, \
     HttpResponseServerError
from django.core import urlresolvers
from django.contrib.auth import authenticate
from patchwork.models import Patch, Project, Person, State
from django.views.decorators.csrf import csrf_exempt

import sys
import base64
import xmlrpclib

class PatchworkXMLRPCDispatcher(SimpleXMLRPCDispatcher):
    def __init__(self):
        if sys.version_info[:3] >= (2,5,):
            SimpleXMLRPCDispatcher.__init__(self, allow_none=False,
                    encoding=None)
            def _dumps(obj, *args, **kwargs):
                kwargs['allow_none'] = self.allow_none
                kwargs['encoding'] = self.encoding
                return xmlrpclib.dumps(obj, *args, **kwargs)
        else:
            def _dumps(obj, *args, **kwargs):
                return xmlrpclib.dumps(obj, *args, **kwargs)
            SimpleXMLRPCDispatcher.__init__(self)

        self.dumps = _dumps

        # map of name => (auth, func)
        self.func_map = {}

    def register_function(self, fn, auth_required):
        self.func_map[fn.__name__] = (auth_required, fn)


    def _user_for_request(self, request):
        auth_header = None

        if 'HTTP_AUTHORIZATION' in request.META:
            auth_header = request.META.get('HTTP_AUTHORIZATION')
        elif 'Authorization' in request.META:
            auth_header = request.META.get('Authorization')

        if auth_header is None or auth_header == '':
            raise Exception("No authentication credentials given")

        str = auth_header.strip()

        if not str.startswith('Basic '):
            raise Exception("Authentication scheme not supported")

        str = str[len('Basic '):].strip()

        try:
            decoded = base64.decodestring(str)
            username, password = decoded.split(':', 1)
        except:
            raise Exception("Invalid authentication credentials")

        return authenticate(username = username, password = password)


    def _dispatch(self, request, method, params):
        if method not in self.func_map.keys():
            raise Exception('method "%s" is not supported' % method)

        auth_required, fn = self.func_map[method]

        if auth_required:
            user = self._user_for_request(request)
            if not user:
                raise Exception("Invalid username/password")

            params = (user,) + params

        return fn(*params)

    def _marshaled_dispatch(self, request):
        try:
            params, method = xmlrpclib.loads(request.raw_post_data)

            response = self._dispatch(request, method, params)
            # wrap response in a singleton tuple
            response = (response,)
            response = self.dumps(response, methodresponse=1)
        except xmlrpclib.Fault, fault:
            response = self.dumps(fault)
        except:
            # report exception back to server
            response = self.dumps(
                xmlrpclib.Fault(1, "%s:%s" % (sys.exc_type, sys.exc_value)),
                )

        return response

dispatcher = PatchworkXMLRPCDispatcher()

# XMLRPC view function
@csrf_exempt
def xmlrpc(request):
    if request.method != 'POST':
        return HttpResponseRedirect(
                urlresolvers.reverse('patchwork.views.help',
                    kwargs = {'path': 'pwclient/'}))

    response = HttpResponse()
    try:
        ret = dispatcher._marshaled_dispatch(request)
        response.write(ret)
    except Exception:
        return HttpResponseServerError()

    return response

# decorator for XMLRPC methods. Setting login_required to true will call
# the decorated function with a non-optional user as the first argument.
def xmlrpc_method(login_required = False):
    def wrap(f):
        dispatcher.register_function(f, login_required)
        return f

    return wrap



# We allow most of the Django field lookup types for remote queries
LOOKUP_TYPES = ["iexact", "contains", "icontains", "gt", "gte", "lt",
                "in", "startswith", "istartswith", "endswith",
                "iendswith", "range", "year", "month", "day", "isnull" ]

#######################################################################
# Helper functions
#######################################################################

def project_to_dict(obj):
    """Return a trimmed down dictionary representation of a Project
    object which is OK to send to the client."""
    return \
        {
         'id'           : obj.id,
         'linkname'     : obj.linkname,
         'name'         : obj.name,
        }

def person_to_dict(obj):
    """Return a trimmed down dictionary representation of a Person
    object which is OK to send to the client."""
    return \
        {
         'id'           : obj.id,
         'email'        : obj.email,
         'name'         : obj.name,
         'user'         : unicode(obj.user).encode("utf-8"),
        }

def patch_to_dict(obj):
    """Return a trimmed down dictionary representation of a Patch
    object which is OK to send to the client."""
    return \
        {
         'id'           : obj.id,
         'date'         : unicode(obj.date).encode("utf-8"),
         'filename'     : obj.filename(),
         'msgid'        : obj.msgid,
         'name'         : obj.name,
         'project'      : unicode(obj.project).encode("utf-8"),
         'project_id'   : obj.project_id,
         'state'        : unicode(obj.state).encode("utf-8"),
         'state_id'     : obj.state_id,
         'submitter'    : unicode(obj.submitter).encode("utf-8"),
         'submitter_id' : obj.submitter_id,
         'delegate'     : unicode(obj.delegate).encode("utf-8"),
         'delegate_id'  : max(obj.delegate_id, 0),
         'commit_ref'   : max(obj.commit_ref, ''),
        }

def bundle_to_dict(obj):
    """Return a trimmed down dictionary representation of a Bundle
    object which is OK to send to the client."""
    return \
        {
         'id'           : obj.id,
         'name'         : obj.name,
         'n_patches'    : obj.n_patches(),
         'public_url'   : obj.public_url(),
        }

def state_to_dict(obj):
    """Return a trimmed down dictionary representation of a State
    object which is OK to send to the client."""
    return \
        {
         'id'           : obj.id,
         'name'         : obj.name,
        }

#######################################################################
# Public XML-RPC methods
#######################################################################

@xmlrpc_method(False)
def pw_rpc_version():
    """Return Patchwork XML-RPC interface version."""
    return 1

@xmlrpc_method(False)
def project_list(search_str="", max_count=0):
    """Get a list of projects matching the given filters."""
    try:
        if len(search_str) > 0:
            projects = Project.objects.filter(linkname__icontains = search_str)
        else:
            projects = Project.objects.all()

        if max_count > 0:
            return map(project_to_dict, projects)[:max_count]
        else:
            return map(project_to_dict, projects)
    except:
        return []

@xmlrpc_method(False)
def project_get(project_id):
    """Return structure for the given project ID."""
    try:
        project = Project.objects.filter(id = project_id)[0]
        return project_to_dict(project)
    except:
        return {}

@xmlrpc_method(False)
def person_list(search_str="", max_count=0):
    """Get a list of Person objects matching the given filters."""
    try:
        if len(search_str) > 0:
            people = (Person.objects.filter(name__icontains = search_str) |
                Person.objects.filter(email__icontains = search_str))
        else:
            people = Person.objects.all()

        if max_count > 0:
            return map(person_to_dict, people)[:max_count]
        else:
            return map(person_to_dict, people)

    except:
        return []

@xmlrpc_method(False)
def person_get(person_id):
    """Return structure for the given person ID."""
    try:
        person = Person.objects.filter(id = person_id)[0]
        return person_to_dict(person)
    except:
        return {}

@xmlrpc_method(False)
def patch_list(filter={}):
    """Get a list of patches matching the given filters."""
    try:
        # We allow access to many of the fields.  But, some fields are
        # filtered by raw object so we must lookup by ID instead over
        # XML-RPC.
        ok_fields = [
            "id",
            "name",
            "project_id",
            "submitter_id",
            "delegate_id",
            "state_id",
            "date",
            "commit_ref",
            "hash",
            "msgid",
            "max_count",
            ]

        dfilter = {}
        max_count = 0

        for key in filter:
            parts = key.split("__")
            if parts[0] not in ok_fields:
                # Invalid field given
                return []
            if len(parts) > 1:
                if LOOKUP_TYPES.count(parts[1]) == 0:
                    # Invalid lookup type given
                    return []

            if parts[0] == 'project_id':
                dfilter['project'] = Project.objects.filter(id =
                                        filter[key])[0]
            elif parts[0] == 'submitter_id':
                dfilter['submitter'] = Person.objects.filter(id =
                                        filter[key])[0]
            elif parts[0] == 'state_id':
                dfilter['state'] = State.objects.filter(id =
                                        filter[key])[0]
            elif parts[0] == 'max_count':
                max_count = filter[key]
            else:
                dfilter[key] = filter[key]

        patches = Patch.objects.filter(**dfilter)

        if max_count > 0:
            return map(patch_to_dict, patches[:max_count])
        else:
            return map(patch_to_dict, patches)

    except:
        return []

@xmlrpc_method(False)
def patch_get(patch_id):
    """Return structure for the given patch ID."""
    try:
        patch = Patch.objects.filter(id = patch_id)[0]
        return patch_to_dict(patch)
    except:
        return {}

@xmlrpc_method(False)
def patch_get_by_hash(hash):
    """Return structure for the given patch hash."""
    try:
        patch = Patch.objects.filter(hash = hash)[0]
        return patch_to_dict(patch)
    except:
        return {}

@xmlrpc_method(False)
def patch_get_by_project_hash(project, hash):
    """Return structure for the given patch hash."""
    try:
        patch = Patch.objects.filter(project__linkname = project,
                                     hash = hash)[0]
        return patch_to_dict(patch)
    except:
        return {}

@xmlrpc_method(False)
def patch_get_mbox(patch_id):
    """Return mbox string for the given patch ID."""
    try:
        patch = Patch.objects.filter(id = patch_id)[0]
        return patch.mbox().as_string()
    except:
        return ""

@xmlrpc_method(False)
def patch_get_diff(patch_id):
    """Return diff for the given patch ID."""
    try:
        patch = Patch.objects.filter(id = patch_id)[0]
        return patch.content
    except:
        return ""

@xmlrpc_method(True)
def patch_set(user, patch_id, params):
    """Update a patch with the key,value pairs in params. Only some parameters
       can be set"""
    try:
        ok_params = ['state', 'commit_ref', 'archived']

        patch = Patch.objects.get(id = patch_id)

        if not patch.is_editable(user):
            raise Exception('No permissions to edit this patch')

        for (k, v) in params.iteritems():
            if k not in ok_params:
                continue

            if k == 'state':
                patch.state = State.objects.get(id = v)

            else:
                setattr(patch, k, v)

        patch.save()

        return True

    except:
        raise

@xmlrpc_method(False)
def state_list(search_str="", max_count=0):
    """Get a list of state structures matching the given search string."""
    try:
        if len(search_str) > 0:
            states = State.objects.filter(name__icontains = search_str)
        else:
            states = State.objects.all()

        if max_count > 0:
            return map(state_to_dict, states)[:max_count]
        else:
            return map(state_to_dict, states)
    except:
        return []

@xmlrpc_method(False)
def state_get(state_id):
    """Return structure for the given state ID."""
    try:
        state = State.objects.filter(id = state_id)[0]
        return state_to_dict(state)
    except:
        return {}