-
Notifications
You must be signed in to change notification settings - Fork 53
/
mixins.py
127 lines (96 loc) · 3.85 KB
/
mixins.py
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
125
126
127
import datetime
import random
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.db.models import Sum
from django.utils.cache import patch_response_headers
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page, never_cache
from django.views.decorators.csrf import csrf_exempt
from django.views.generic.base import ContextMixin
from conf.utils import likes_enable
from digest.models import Item
def get_feed_items(count=10):
return (
Item.objects.filter(
status="active",
activated_at__lte=datetime.datetime.now(),
activated_at__gte=datetime.datetime.now() - datetime.timedelta(days=90),
)
.exclude(section=None)
.prefetch_related("issue", "section", "tags")
.order_by("-created_at", "-related_to_date")[:count]
)
class FeedItemsMixin(ContextMixin):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["feed_items"] = get_feed_items(15)
return context
class FavoriteItemsMixin(ContextMixin):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# вариант1
# Получить все голоса
# Сгруппировать по Item
# Из всех объектов голосов выгрузить значение
# Определить total
# Выбрать лучшие
# вариант2
# Получить все голоса
# Получить по ним сет из Item (без дублей)
# пройтись по всем и сформировать лист
if likes_enable():
pass
date = datetime.datetime.now() - datetime.timedelta(days=12)
items = (
Item.objects.filter(
status="active",
related_to_date__gt=date,
)
.exclude(section=None)
.annotate(
q_vote_total=Sum(
"votes__vote",
)
)
.filter(q_vote_total__gte=0)
.prefetch_related("tags", "votes")
)
items_score = [(item, item.q_vote_total) for item in items if item.q_vote_total >= 0]
items_score = sorted(items_score, key=lambda item: item[1], reverse=True)
context["favorite_items"] = [x[0] for x in items_score[:10]]
return context
class NeverCacheMixin:
@method_decorator(never_cache)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
class LoginRequiredMixin:
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
class CSRFExemptMixin:
@method_decorator(csrf_exempt)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
class CacheMixin:
cache_timeout = 60
def get_cache_timeout(self):
return self.cache_timeout
def dispatch(self, *args, **kwargs):
if not settings.CACHE_PAGE_ENABLED:
return super().dispatch(*args, **kwargs)
return cache_page(self.get_cache_timeout())(super().dispatch)(*args, **kwargs)
class CacheControlMixin:
cache_timeout = 60
def get_cache_timeout(self):
return self.cache_timeout
def dispatch(self, *args, **kwargs):
response = super().dispatch(*args, **kwargs)
patch_response_headers(response, self.get_cache_timeout())
return response
class JitterCacheMixin(CacheControlMixin):
cache_range = [40, 80]
def get_cache_range(self):
return self.cache_range
def get_cache_timeout(self):
return random.randint(*self.get_cache_range())