##// END OF EJS Templates
Added media directory cache size
neko259 -
r1424:78342ab8 default
parent child Browse files
Show More
@@ -1,138 +1,143
1 1 """
2 2 This module contains helper functions and helper classes.
3 3 """
4 4 import hashlib
5 5 from random import random
6 6 import time
7 7 import hmac
8 8
9 9 from django.core.cache import cache
10 10 from django.db.models import Model
11 11 from django import forms
12 12 from django.utils import timezone
13 13 from django.utils.translation import ugettext_lazy as _
14 14 import magic
15 15 from portage import os
16 16
17 17 import boards
18 18 from boards.settings import get_bool
19 19 from neboard import settings
20 20
21 21 CACHE_KEY_DELIMITER = '_'
22 22
23 23 HTTP_FORWARDED = 'HTTP_X_FORWARDED_FOR'
24 24 META_REMOTE_ADDR = 'REMOTE_ADDR'
25 25
26 26 SETTING_MESSAGES = 'Messages'
27 27 SETTING_ANON_MODE = 'AnonymousMode'
28 28
29 29 ANON_IP = '127.0.0.1'
30 30
31 31 UPLOAD_DIRS ={
32 32 'PostImage': 'images/',
33 33 'Attachment': 'files/',
34 34 }
35 35 FILE_EXTENSION_DELIMITER = '.'
36 36
37 37
38 38 def is_anonymous_mode():
39 39 return get_bool(SETTING_MESSAGES, SETTING_ANON_MODE)
40 40
41 41
42 42 def get_client_ip(request):
43 43 if is_anonymous_mode():
44 44 ip = ANON_IP
45 45 else:
46 46 x_forwarded_for = request.META.get(HTTP_FORWARDED)
47 47 if x_forwarded_for:
48 48 ip = x_forwarded_for.split(',')[-1].strip()
49 49 else:
50 50 ip = request.META.get(META_REMOTE_ADDR)
51 51 return ip
52 52
53 53
54 54 # TODO The output format is not epoch because it includes microseconds
55 55 def datetime_to_epoch(datetime):
56 56 return int(time.mktime(timezone.localtime(
57 57 datetime,timezone.get_current_timezone()).timetuple())
58 58 * 1000000 + datetime.microsecond)
59 59
60 60
61 61 def get_websocket_token(user_id='', timestamp=''):
62 62 """
63 63 Create token to validate information provided by new connection.
64 64 """
65 65
66 66 sign = hmac.new(settings.CENTRIFUGE_PROJECT_SECRET.encode())
67 67 sign.update(settings.CENTRIFUGE_PROJECT_ID.encode())
68 68 sign.update(user_id.encode())
69 69 sign.update(timestamp.encode())
70 70 token = sign.hexdigest()
71 71
72 72 return token
73 73
74 74
75 # TODO Test this carefully
75 76 def cached_result(key_method=None):
76 77 """
77 78 Caches method result in the Django's cache system, persisted by object name,
78 object name and model id if object is a Django model.
79 object name, model id if object is a Django model, args and kwargs if any.
79 80 """
80 81 def _cached_result(function):
81 82 def inner_func(obj, *args, **kwargs):
82 # TODO Include method arguments to the cache key
83 83 cache_key_params = [obj.__class__.__name__, function.__name__]
84
85 cache_key_params += args
86 for key, value in kwargs:
87 cache_key_params.append(key + ':' + value)
88
84 89 if isinstance(obj, Model):
85 90 cache_key_params.append(str(obj.id))
86 91
87 92 if key_method is not None:
88 93 cache_key_params += [str(arg) for arg in key_method(obj)]
89 94
90 95 cache_key = CACHE_KEY_DELIMITER.join(cache_key_params)
91 96
92 97 persisted_result = cache.get(cache_key)
93 98 if persisted_result is not None:
94 99 result = persisted_result
95 100 else:
96 101 result = function(obj, *args, **kwargs)
97 102 cache.set(cache_key, result)
98 103
99 104 return result
100 105
101 106 return inner_func
102 107 return _cached_result
103 108
104 109
105 110 def get_file_hash(file) -> str:
106 111 md5 = hashlib.md5()
107 112 for chunk in file.chunks():
108 113 md5.update(chunk)
109 114 return md5.hexdigest()
110 115
111 116
112 117 def validate_file_size(size: int):
113 118 max_size = boards.settings.get_int('Forms', 'MaxFileSize')
114 119 if size > max_size:
115 120 raise forms.ValidationError(
116 121 _('File must be less than %s bytes')
117 122 % str(max_size))
118 123
119 124
120 125 def get_extension(filename):
121 126 return filename.split(FILE_EXTENSION_DELIMITER)[-1:][0]
122 127
123 128
124 129 def get_upload_filename(model_instance, old_filename):
125 130 # TODO Use something other than random number in file name
126 131 extension = get_extension(old_filename)
127 132 new_name = '{}{}.{}'.format(
128 133 str(int(time.mktime(time.gmtime()))),
129 134 str(int(random() * 1000)),
130 135 extension)
131 136
132 137 directory = UPLOAD_DIRS[type(model_instance).__name__]
133 138
134 139 return os.path.join(directory, new_name)
135 140
136 141
137 142 def get_file_mimetype(file) -> str:
138 143 return magic.from_buffer(file.chunks().__next__(), mime=True).decode()
@@ -1,32 +1,34
1 1 import os
2 2
3 3 from django.shortcuts import render
4 4
5 5 import neboard
6 6 from boards.authors import authors
7 from boards.utils import cached_result
7 8 from boards.views.base import BaseBoardView
8 9 from boards.models import Post
9 10
10 11
11 12 PARAM_AUTHORS = 'authors'
12 13 PARAM_MEDIA_SIZE = 'media_size'
13 14 PARAM_POST_COUNT = 'post_count'
14 15
15 16
16 17 class AuthorsView(BaseBoardView):
17 18
18 19 def get(self, request):
19 20 params = dict()
20 21 params[PARAM_AUTHORS] = authors
21 22 params[PARAM_MEDIA_SIZE] = self._get_directory_size(neboard.settings.MEDIA_ROOT)
22 23 params[PARAM_POST_COUNT] = Post.objects.count()
23 24
24 25 return render(request, 'boards/authors.html', params)
25 26
27 @cached_result()
26 28 def _get_directory_size(self, directory):
27 29 total_size = 0
28 30 for dirpath, dirnames, filenames in os.walk(directory):
29 31 for f in filenames:
30 32 fp = os.path.join(dirpath, f)
31 33 total_size += os.path.getsize(fp)
32 34 return total_size
General Comments 0
You need to be logged in to leave comments. Login now