Added support for http callbacks on processing
[mediagoblin.git] / mediagoblin / db / sql / extratypes.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3 #
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.
8 #
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.
13 #
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/>.
16
17
18 from sqlalchemy.types import TypeDecorator, Unicode, TEXT
19 import json
20
21
22 class PathTupleWithSlashes(TypeDecorator):
23 "Represents a Tuple of strings as a slash separated string."
24
25 impl = Unicode
26
27 def process_bind_param(self, value, dialect):
28 if value is not None:
29 if len(value) == 0:
30 value = None
31 else:
32 value = '/'.join(value)
33 return value
34
35 def process_result_value(self, value, dialect):
36 if value is not None:
37 value = tuple(value.split('/'))
38 return value
39
40
41 # The following class and only this one class is in very
42 # large parts based on example code from sqlalchemy.
43 #
44 # The original copyright notice and license follows:
45 # Copyright (C) 2005-2011 the SQLAlchemy authors and contributors <see AUTHORS file>
46 #
47 # This module is part of SQLAlchemy and is released under
48 # the MIT License: http://www.opensource.org/licenses/mit-license.php
49 #
50 class JSONEncoded(TypeDecorator):
51 "Represents an immutable structure as a json-encoded string."
52
53 impl = TEXT
54
55 def process_bind_param(self, value, dialect):
56 if value is not None:
57 value = json.dumps(value)
58 return value
59
60 def process_result_value(self, value, dialect):
61 if value is not None:
62 value = json.loads(value)
63 return value