source: mauRepo/dj_security/tags/v_0_0_7/dj_security/encoder.py @ 8704

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/dj_security/tags/v_0_0_7/dj_security/encoder.py@8704
Revision 8704, 3.2 KB checked in by mnagni, 7 years ago (diff)

Encodes base64 the GET's parameters

  • Property svn:mime-type set to text/plain
Line 
1'''
2BSD Licence
3Copyright (c) 2012, Science & Technology Facilities Council (STFC)
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without modification,
7are permitted provided that the following conditions are met:
8
9    * Redistributions of source code must retain the above copyright notice,
10        this list of conditions and the following disclaimer.
11    * Redistributions in binary form must reproduce the above copyright notice,
12        this list of conditions and the following disclaimer in the documentation
13        and/or other materials provided with the distribution.
14    * Neither the name of the Science & Technology Facilities Council (STFC)
15        nor the names of its contributors may be used to endorse or promote
16        products derived from this software without specific prior written permission.
17
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29Created on 14 Nov 2012
30
31@author: mnagni
32'''
33import logging
34import datetime
35from json.encoder import JSONEncoder
36from decimal import Decimal
37from django.conf import settings
38
39class SecurityEncoder(JSONEncoder):
40    log = logging.getLogger('SecurityEncoder')
41
42    def __init__(self):
43        super(SecurityEncoder, self).__init__()
44
45    def default(self, obj):
46        mydict = {}
47       
48        #if isinstance(obj, User):
49        #    mydict.update({'permissions': ','.join(obj.get_all_permissions())})
50       
51        if isinstance(obj, datetime.datetime):
52            # wanted a simple yield str(o) in the next line,
53            # but that would mean a yield on the line with super(...),
54            # which wouldn't work (see my comment below), so...
55            return str(obj)
56       
57        if isinstance(obj, Decimal):
58            # wanted a simple yield str(o) in the next line,
59            # but that would mean a yield on the line with super(...),
60            # which wouldn't work (see my comment below), so...
61            return str(obj)         
62       
63        if not getattr(obj, '__dict__', None):
64            return str(obj)
65       
66        for key in obj.__dict__.keys():
67            if not key.startswith("_") \
68                and key not in getattr(settings, 'NOT_ENCODE', []):
69                mydict.update({key: getattr(obj, key)})             
70
71        return mydict
72
73def is_primitive(obj):
74    # Is a simple primitive?
75    return obj is None \
76            or isinstance(obj, str) \
77            or isinstance(obj, int) \
78            or isinstance(obj, unicode) \
79            or isinstance(obj, Decimal)     
Note: See TracBrowser for help on using the repository browser.