##// END OF EJS Templates
Extracted some magic strings
neko259 -
r2002:a3d63355 default
parent child Browse files
Show More
@@ -1,33 +1,41 b''
1 1 import configparser
2 2
3 3
4 4 CONFIG_DEFAULT_SETTINGS = 'boards/config/default_settings.ini'
5 5 CONFIG_SETTINGS = 'boards/config/settings.ini'
6 6
7 7
8 SECTION_FORMS = 'Forms'
9
10 VALUE_TRUE = 'true'
11
12 LIST_DELIMITER = ','
13 DICT_DELIMITER = ':'
14
15
8 16 config = configparser.ConfigParser()
9 17 config.read(CONFIG_DEFAULT_SETTINGS)
10 18 config.read(CONFIG_SETTINGS)
11 19
12 20
13 21 def get(section, name):
14 22 return config[section][name]
15 23
16 24
17 25 def get_int(section, name):
18 26 return int(get(section, name))
19 27
20 28
21 29 def get_bool(section, name):
22 return get(section, name) == 'true'
30 return get(section, name) == VALUE_TRUE
23 31
24 32
25 33 def get_list_dict(section, name):
26 34 str_dict = get(section, name)
27 return [item.split(':') for item in str_dict.split(',')]
35 return [item.split(DICT_DELIMITER) for item in str_dict.split(LIST_DELIMITER)]
28 36
29 37
30 38 def get_list(section, name):
31 39 str_list = get(section, name)
32 return str_list.split(',')
40 return str_list.split(LIST_DELIMITER)
33 41
@@ -1,157 +1,161 b''
1 1 """
2 2 This module contains helper functions and helper classes.
3 3 """
4 4 import time
5 5 import uuid
6 6
7 7 import hashlib
8 8 import magic
9 9 import os
10 10 from django import forms
11 11 from django.core.cache import cache
12 12 from django.db.models import Model
13 13 from django.template.defaultfilters import filesizeformat
14 14 from django.utils import timezone
15 15 from django.utils.translation import ugettext_lazy as _
16 16
17 17 import boards
18 18 from neboard import settings
19 19 from boards.abstracts.constants import FILE_DIRECTORY
20 20 from boards.settings import get_bool
21 21
22 22 CACHE_KEY_DELIMITER = '_'
23 23
24 24 HTTP_FORWARDED = 'HTTP_X_FORWARDED_FOR'
25 25 META_REMOTE_ADDR = 'REMOTE_ADDR'
26 26
27 27 SETTING_MESSAGES = 'Messages'
28 28 SETTING_ANON_MODE = 'AnonymousMode'
29 29
30 30 ANON_IP = '127.0.0.1'
31 31
32 32 FILE_EXTENSION_DELIMITER = '.'
33 URL_DELIMITER = '/'
34 CACHE_KEY_DELIMITER = ':'
35
36 DEFAULT_MIMETYPE = 'application/octet-stream'
33 37
34 38
35 39 def is_anonymous_mode():
36 40 return get_bool(SETTING_MESSAGES, SETTING_ANON_MODE)
37 41
38 42
39 43 def get_client_ip(request):
40 44 if is_anonymous_mode():
41 45 ip = ANON_IP
42 46 else:
43 47 x_forwarded_for = request.META.get(HTTP_FORWARDED)
44 48 if x_forwarded_for:
45 49 ip = x_forwarded_for.split(',')[-1].strip()
46 50 else:
47 51 ip = request.META.get(META_REMOTE_ADDR)
48 52 return ip
49 53
50 54
51 55 # TODO The output format is not epoch because it includes microseconds
52 56 def datetime_to_epoch(datetime):
53 57 return int(time.mktime(timezone.localtime(
54 58 datetime,timezone.get_current_timezone()).timetuple())
55 59 * 1000000 + datetime.microsecond)
56 60
57 61
58 62 # TODO Test this carefully
59 63 def cached_result(key_method=None):
60 64 """
61 65 Caches method result in the Django's cache system, persisted by object name,
62 66 object name, model id if object is a Django model, args and kwargs if any.
63 67 """
64 68 def _cached_result(function):
65 69 def inner_func(obj, *args, **kwargs):
66 70 cache_key_params = [obj.__class__.__name__, function.__name__]
67 71
68 72 cache_key_params += args
69 73 for key, value in kwargs:
70 cache_key_params.append(key + ':' + value)
74 cache_key_params.append(key + CACHE_KEY_DELIMITER + value)
71 75
72 76 if isinstance(obj, Model):
73 77 cache_key_params.append(str(obj.id))
74 78
75 79 if key_method is not None:
76 80 cache_key_params += [str(arg) for arg in key_method(obj)]
77 81
78 82 cache_key = CACHE_KEY_DELIMITER.join(cache_key_params)
79 83
80 84 persisted_result = cache.get(cache_key)
81 85 if persisted_result is not None:
82 86 result = persisted_result
83 87 else:
84 88 result = function(obj, *args, **kwargs)
85 89 if result is not None:
86 90 cache.set(cache_key, result)
87 91
88 92 return result
89 93
90 94 return inner_func
91 95 return _cached_result
92 96
93 97
94 98 def get_file_hash(file) -> str:
95 99 md5 = hashlib.md5()
96 100 for chunk in file.chunks():
97 101 md5.update(chunk)
98 102 return md5.hexdigest()
99 103
100 104
101 105 def validate_file_size(size: int):
102 106 max_size = boards.settings.get_int('Forms', 'MaxFileSize')
103 107 if 0 < max_size < size:
104 108 raise forms.ValidationError(
105 109 _('Total file size must be less than %s but is %s.')
106 110 % (filesizeformat(max_size), filesizeformat(size)))
107 111
108 112
109 113 def get_extension(filename):
110 114 return filename.split(FILE_EXTENSION_DELIMITER)[-1:][0]
111 115
112 116
113 117 def get_upload_filename(model_instance, old_filename):
114 118 extension = get_extension(old_filename)
115 119 new_name = '{}.{}'.format(uuid.uuid4(), extension)
116 120
117 121 # Create 2 directories to split the files because holding many files in
118 122 # one directory may impact performance
119 123 dir1 = new_name[0]
120 124 dir2 = new_name[1]
121 125
122 126 return os.path.join(FILE_DIRECTORY, dir1, dir2, new_name)
123 127
124 128
125 129 def get_file_mimetype(file) -> str:
126 130 buf = b''
127 131 for chunk in file.chunks():
128 132 buf += chunk
129 133
130 134 file_type = magic.from_buffer(buf, mime=True)
131 135 if file_type is None:
132 file_type = 'application/octet-stream'
136 file_type = DEFAULT_MIMETYPE
133 137 elif type(file_type) == bytes:
134 138 file_type = file_type.decode()
135 139 return file_type
136 140
137 141
138 142 def get_domain(url: str) -> str:
139 143 """
140 144 Gets domain from an URL with random number of domain levels.
141 145 """
142 domain_parts = url.split('/')
146 domain_parts = url.split(URL_DELIMITER)
143 147 if len(domain_parts) >= 2:
144 148 full_domain = domain_parts[2]
145 149 else:
146 150 full_domain = ''
147 151
148 152 return full_domain
149 153
150 154
151 155 def get_tripcode_from_text(text: str) -> str:
152 156 tripcode = ''
153 157 if text:
154 158 code = text + settings.SECRET_KEY
155 159 tripcode = hashlib.md5(code.encode()).hexdigest()
156 160 return tripcode
157 161
General Comments 0
You need to be logged in to leave comments. Login now