Project

General

Profile

0004-Add-middleware-to-load-settings-from-a-JSON-file-bas.patch

Benjamin Dauvergne, 11 September 2014 12:57 PM

Download (3.89 KB)

View differences:

Subject: [PATCH 04/11] Add middleware to load settings from a JSON file based
 on the tenant

* Loaded settings are cached based on the mtime of the setting file
* JSON file path is <settings.TENANT_BASE>/<schema_name>/settings.json
 entrouvert/djommon/multitenant/middleware.py |   63 ++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
entrouvert/djommon/multitenant/middleware.py
1
import os
2
import json
3
import glob
4
import hashlib
5

  
1 6
from django.conf import settings, UserSettingsHolder
2 7

  
3 8
from tenant_schemas.middleware import TenantMiddleware
......
18 23
    def process_response(self, request, response):
19 24
        settings._wrapped = self.wrapped
20 25
        return response
26

  
27
class TenantSettingBaseMiddleware(object):
28
    '''Base middleware classe for loading settings based on tenants
29

  
30
       Child classes MUST override the load_tenant_settings() method.
31
    '''
32
    def __init__(self, *args, **kwargs):
33
        self.tenants_settings = {}
34

  
35
    def get_tenant_settings(self, wrapped, tenant):
36
        '''Get last loaded settings for tenant, try to update it by loading
37
           settings again is last loading time is less recent thant settings data
38
           store. Compare with last modification time is done in the
39
           load_tenant_settings() method.
40
        '''
41
        tenant_settings, last_time = self.tenants_settings.get(tenant.schema_name, (None,None))
42
        if tenant_settings is None:
43
            tenant_settings = UserSettingsHolder(wrapped)
44
        tenant_settings, last_time = self.load_tenant_settings(wrapped, tenant, tenant_settings, last_time)
45
        self.tenants_settings[tenant.schema_name] = tenant_settings, last_time
46
        return tenant_settings
47

  
48
    def load_tenant_settings(self, wrapped, tenant, tenant_settings, last_time):
49
        '''Load tenant settings into tenant_settings object, eventually skip if
50
           last_time is more recent than last update time for settings and return
51
           the new value for tenant_settings and last_time'''
52
        raise NotImplemented
53

  
54
    def process_request(self, request):
55
        if not hasattr(request, '_old_settings_wrapped'):
56
            request._old_settings_wrapped = []
57
        request._old_settings_wrapped.append(settings._wrapped)
58
        settings._wrapped = self.get_tenant_settings(settings._wrapped, request.tenant)
59

  
60
    def process_response(self, request, response):
61
        if hasattr(request, '_old_settings_wrapped') and request._old_settings_wrapped:
62
            settings._wrapped = request._old_settings_wrapped.pop()
63
        return response
64

  
65
class JSONSettingsMiddleware(TenantSettingBaseMiddleware):
66
    '''Load settings from a JSON file whose path is given by:
67

  
68
            os.path.join(settings.TENANT_BASE % schema_name, 'settings.json')
69
    '''
70
    def load_tenant_settings(self, wrapped, tenant, tenant_settings, last_time):
71
        path = os.path.join(settings.TENANT_BASE, tenant.schema_name, 'settings.json')
72
        if not os.path.exists(path):
73
            if last_time is not None:
74
                return UserSettingsHolder(wrapped), None
75
        else:
76
            new_time = os.stat(path).st_mtime
77
            if new_time >= last_time:
78
                tenant_settings = UserSettingsHolder(wrapped)
79
                json_settings = json.load(file(path))
80
                for key in json_settings:
81
                    setattr(tenant_settings, key, json_settings[key])
82
                return tenant_settings, new_time
83
        return tenant_settings, last_time
21
-