Fix #5382 - Add migration and fix so tombstones are removed from collections
[larjonas-mediagoblin.git] / mediagoblin / tools / processing.py
blob26d7bb9b466b844721b7531b0068929daf997f95
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 import logging
18 import json
19 import traceback
21 from six.moves.urllib import request, parse
23 _log = logging.getLogger(__name__)
25 TESTS_CALLBACKS = {}
28 def create_post_request(url, data, **kw):
29 '''
30 Issue a HTTP POST request.
32 Args:
33 url: The URL to which the POST request should be issued
34 data: The data to be send in the body of the request
35 **kw:
36 data_parser: The parser function that is used to parse the `data`
37 argument
38 '''
39 data_parser = kw.get('data_parser', parse.urlencode)
40 headers = kw.get('headers', {})
42 return request.Request(url, data_parser(data), headers=headers)
45 def json_processing_callback(entry):
46 '''
47 Send an HTTP post to the registered callback url, if any.
48 '''
49 if not entry.processing_metadata:
50 _log.debug('No processing callback URL for {0}'.format(entry))
51 return
53 url = entry.processing_metadata[0].callback_url
55 _log.debug('Sending processing callback for {0} to {1}'.format(
56 entry,
57 url))
59 headers = {
60 'Content-Type': 'application/json'}
62 data = {
63 'id': entry.id,
64 'state': entry.state}
66 # Trigger testing mode, no callback will be sent
67 if url.endswith('secrettestmediagoblinparam'):
68 TESTS_CALLBACKS.update({url: data})
69 return True
71 request = create_post_request(
72 url,
73 data,
74 headers=headers,
75 data_parser=json.dumps)
77 try:
78 request.urlopen(request)
79 _log.debug('Processing callback for {0} sent'.format(entry))
81 return True
82 except request.HTTPError:
83 _log.error('Failed to send callback: {0}'.format(
84 traceback.format_exc()))
86 return False