2019-07-02 09:51:50 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# coding: utf-8
|
2021-04-08 06:59:14 +00:00
|
|
|
from django.contrib.auth.mixins import UserPassesTestMixin
|
2019-07-02 09:51:50 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
|
|
|
|
|
2021-04-29 09:15:46 +00:00
|
|
|
__all__ = ["DatetimeSearchMixin", "PermissionsMixin"]
|
2019-07-02 09:51:50 +00:00
|
|
|
|
2021-04-08 06:59:14 +00:00
|
|
|
from rest_framework import permissions
|
|
|
|
|
2019-07-02 09:51:50 +00:00
|
|
|
|
|
|
|
class DatetimeSearchMixin:
|
|
|
|
date_format = '%Y-%m-%d'
|
|
|
|
date_from = date_to = None
|
|
|
|
|
|
|
|
def get_date_range(self):
|
|
|
|
date_from_s = self.request.GET.get('date_from')
|
|
|
|
date_to_s = self.request.GET.get('date_to')
|
|
|
|
|
|
|
|
if date_from_s:
|
|
|
|
date_from = timezone.datetime.strptime(date_from_s, self.date_format)
|
|
|
|
tz = timezone.get_current_timezone()
|
|
|
|
self.date_from = tz.localize(date_from)
|
|
|
|
else:
|
|
|
|
self.date_from = timezone.now() - timezone.timedelta(7)
|
|
|
|
|
|
|
|
if date_to_s:
|
|
|
|
date_to = timezone.datetime.strptime(
|
|
|
|
date_to_s + ' 23:59:59', self.date_format + ' %H:%M:%S'
|
|
|
|
)
|
|
|
|
self.date_to = date_to.replace(
|
|
|
|
tzinfo=timezone.get_current_timezone()
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.date_to = timezone.now()
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.get_date_range()
|
|
|
|
return super().get(request, *args, **kwargs)
|
2021-04-08 06:59:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PermissionsMixin(UserPassesTestMixin):
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
|
|
|
|
def get_permissions(self):
|
|
|
|
return self.permission_classes
|
|
|
|
|
|
|
|
def test_func(self):
|
|
|
|
permission_classes = self.get_permissions()
|
|
|
|
for permission_class in permission_classes:
|
|
|
|
if not permission_class().has_permission(self.request, self):
|
|
|
|
return False
|
2021-04-29 09:15:46 +00:00
|
|
|
return True
|