Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

from django.conf import settings 

from django.core.exceptions import ImproperlyConfigured 

from rosetta.conf import settings as rosetta_settings 

import hashlib 

import time 

import six 

import django 

 

 

try: 

from django.core.cache import caches 

cache = caches[rosetta_settings.ROSETTA_CACHE_NAME] 

except ImportError: 

from django.core.cache import get_cache 

cache = get_cache(rosetta_settings.ROSETTA_CACHE_NAME) 

 

try: 

import importlib 

except ImportError: 

from django.utils import importlib 

 

 

class BaseRosettaStorage(object): 

def __init__(self, request): 

self.request = request 

 

def get(self, key, default=None): 

raise NotImplementedError 

 

def set(self, key, val): 

raise NotImplementedError 

 

def has(self, key): 

raise NotImplementedError 

 

def delete(self, key): 

raise NotImplementedError 

 

 

class DummyRosettaStorage(BaseRosettaStorage): 

def get(self, key, default=None): 

return default 

 

def set(self, key, val): 

pass 

 

def has(self, key): 

return False 

 

def delete(self, key): 

pass 

 

 

class SessionRosettaStorage(BaseRosettaStorage): 

def __init__(self, request): 

super(SessionRosettaStorage, self).__init__(request) 

 

if 'signed_cookies' in settings.SESSION_ENGINE and django.VERSION[1] >= 6 and 'pickle' not in settings.SESSION_SERIALIZER.lower(): 

raise ImproperlyConfigured("Sorry, but django-rosetta doesn't support the `signed_cookies` SESSION_ENGINE in Django >= 1.6, because rosetta specific session files cannot be serialized.") 

 

def get(self, key, default=None): 

if key in self.request.session: 

return self.request.session[key] 

return default 

 

def set(self, key, val): 

self.request.session[key] = val 

 

def has(self, key): 

return key in self.request.session 

 

def delete(self, key): 

del(self.request.session[key]) 

 

 

class CacheRosettaStorage(BaseRosettaStorage): 

# unlike the session storage backend, cache is shared among all users 

# so we need to per-user key prefix, which we store in the session 

def __init__(self, request): 

super(CacheRosettaStorage, self).__init__(request) 

 

if 'rosetta_cache_storage_key_prefix' in self.request.session: 

self._key_prefix = self.request.session['rosetta_cache_storage_key_prefix'] 

else: 

self._key_prefix = hashlib.new('sha1', six.text_type(time.time()).encode('utf8')).hexdigest() 

self.request.session['rosetta_cache_storage_key_prefix'] = self._key_prefix 

 

88 ↛ 89line 88 didn't jump to line 89, because the condition on line 88 was never true if self.request.session['rosetta_cache_storage_key_prefix'] != self._key_prefix: 

raise ImproperlyConfigured("You can't use the CacheRosettaStorage because your Django Session storage doesn't seem to be working. The CacheRosettaStorage relies on the Django Session storage to avoid conflicts.") 

 

# Make sure we're not using DummyCache 

92 ↛ 93line 92 didn't jump to line 93, because the condition on line 92 was never true if 'dummycache' in settings.CACHES[rosetta_settings.ROSETTA_CACHE_NAME]['BACKEND'].lower(): 

raise ImproperlyConfigured("You can't use the CacheRosettaStorage if your cache isn't correctly set up (you are using the DummyCache cache backend).") 

 

# Make sure the cache actually works 

try: 

self.set('rosetta_cache_test', 'rosetta') 

98 ↛ 99line 98 didn't jump to line 99, because the condition on line 98 was never true if not self.get('rosetta_cache_test') == 'rosetta': 

raise ImproperlyConfigured("You can't use the CacheRosettaStorage if your cache isn't correctly set up, please double check your Django DATABASES setting and that the cache server is responding.") 

finally: 

self.delete('rosetta_cache_test') 

 

def get(self, key, default=None): 

# print ('get', self._key_prefix + key) 

return cache.get(self._key_prefix + key, default) 

 

def set(self, key, val): 

# print ('set', self._key_prefix + key) 

cache.set(self._key_prefix + key, val, 86400) 

 

def has(self, key): 

# print ('has', self._key_prefix + key) 

return (self._key_prefix + key) in cache 

 

def delete(self, key): 

# print ('del', self._key_prefix + key) 

cache.delete(self._key_prefix + key) 

 

 

def get_storage(request): 

from rosetta.conf import settings 

storage_module, storage_class = settings.STORAGE_CLASS.rsplit('.', 1) 

storage_module = importlib.import_module(storage_module) 

return getattr(storage_module, storage_class)(request)