2016-09-19 07:47:58 +00:00
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
from django.db import models
|
2016-10-01 12:26:43 +00:00
|
|
|
from django.http import JsonResponse
|
2017-12-26 17:29:23 +00:00
|
|
|
from django.utils import timezone
|
2019-05-21 08:24:01 +00:00
|
|
|
from django.core.cache import cache
|
2016-09-19 07:47:58 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2019-04-25 02:11:50 +00:00
|
|
|
from rest_framework.utils import html
|
|
|
|
from rest_framework.settings import api_settings
|
|
|
|
from rest_framework.exceptions import ValidationError
|
|
|
|
from rest_framework.fields import SkipField
|
2018-07-20 10:42:01 +00:00
|
|
|
|
2019-05-21 08:24:01 +00:00
|
|
|
from .const import KEY_CACHE_RESOURCES_ID
|
|
|
|
|
2016-09-19 07:47:58 +00:00
|
|
|
|
|
|
|
class NoDeleteQuerySet(models.query.QuerySet):
|
|
|
|
|
|
|
|
def delete(self):
|
2017-12-26 17:29:23 +00:00
|
|
|
return self.update(is_discard=True, discard_time=timezone.now())
|
2016-09-19 07:47:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NoDeleteManager(models.Manager):
|
|
|
|
|
|
|
|
def get_all(self):
|
|
|
|
return NoDeleteQuerySet(self.model, using=self._db)
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return NoDeleteQuerySet(self.model, using=self._db).filter(is_discard=False)
|
|
|
|
|
|
|
|
def get_deleted(self):
|
|
|
|
return NoDeleteQuerySet(self.model, using=self._db).filter(is_discard=True)
|
|
|
|
|
|
|
|
|
|
|
|
class NoDeleteModelMixin(models.Model):
|
|
|
|
is_discard = models.BooleanField(verbose_name=_("is discard"), default=False)
|
|
|
|
discard_time = models.DateTimeField(verbose_name=_("discard time"), null=True, blank=True)
|
|
|
|
|
|
|
|
objects = NoDeleteManager()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
self.is_discard = True
|
2017-12-26 17:29:23 +00:00
|
|
|
self.discard_time = timezone.now()
|
2016-09-19 07:47:58 +00:00
|
|
|
return self.save()
|
2016-10-01 12:26:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class JSONResponseMixin(object):
|
|
|
|
"""JSON mixin"""
|
2016-11-24 09:08:20 +00:00
|
|
|
@staticmethod
|
|
|
|
def render_json_response(context):
|
2016-10-01 12:26:43 +00:00
|
|
|
return JsonResponse(context)
|
2016-10-02 03:09:27 +00:00
|
|
|
|
|
|
|
|
2018-02-02 09:06:08 +00:00
|
|
|
class IDInFilterMixin(object):
|
2016-10-02 03:09:27 +00:00
|
|
|
def filter_queryset(self, queryset):
|
2018-02-02 09:06:08 +00:00
|
|
|
queryset = super(IDInFilterMixin, self).filter_queryset(queryset)
|
2016-10-02 03:09:27 +00:00
|
|
|
id_list = self.request.query_params.get('id__in')
|
|
|
|
if id_list:
|
|
|
|
import json
|
|
|
|
try:
|
|
|
|
ids = json.loads(id_list)
|
|
|
|
except Exception as e:
|
|
|
|
return queryset
|
|
|
|
if isinstance(ids, list):
|
|
|
|
queryset = queryset.filter(id__in=ids)
|
|
|
|
return queryset
|
2017-12-12 04:19:45 +00:00
|
|
|
|
|
|
|
|
2019-05-21 08:24:01 +00:00
|
|
|
class IDInCacheFilterMixin(object):
|
|
|
|
|
|
|
|
def filter_queryset(self, queryset):
|
|
|
|
queryset = super(IDInCacheFilterMixin, self).filter_queryset(queryset)
|
|
|
|
spm = self.request.query_params.get('spm')
|
|
|
|
cache_key = KEY_CACHE_RESOURCES_ID.format(spm)
|
|
|
|
resources_id = cache.get(cache_key)
|
|
|
|
if resources_id and isinstance(resources_id, list):
|
|
|
|
queryset = queryset.filter(id__in=resources_id)
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
|
class IDExportFilterMixin(object):
|
|
|
|
def filter_queryset(self, queryset):
|
|
|
|
# 下载导入模版
|
|
|
|
if self.request.query_params.get('template') == 'import':
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
return super(IDExportFilterMixin, self).filter_queryset(queryset)
|
|
|
|
|
|
|
|
|
2017-12-12 04:19:45 +00:00
|
|
|
class BulkSerializerMixin(object):
|
|
|
|
"""
|
|
|
|
Become rest_framework_bulk not support uuid as a primary key
|
|
|
|
so rewrite it. https://github.com/miki725/django-rest-framework-bulk/issues/66
|
|
|
|
"""
|
|
|
|
def to_internal_value(self, data):
|
|
|
|
from rest_framework_bulk import BulkListSerializer
|
|
|
|
ret = super(BulkSerializerMixin, self).to_internal_value(data)
|
|
|
|
|
|
|
|
id_attr = getattr(self.Meta, 'update_lookup_field', 'id')
|
2018-09-07 04:40:26 +00:00
|
|
|
if self.context.get('view'):
|
|
|
|
request_method = getattr(getattr(self.context.get('view'), 'request'), 'method', '')
|
|
|
|
# add update_lookup_field field back to validated data
|
|
|
|
# since super by default strips out read-only fields
|
|
|
|
# hence id will no longer be present in validated_data
|
|
|
|
if all((isinstance(self.root, BulkListSerializer),
|
|
|
|
id_attr,
|
|
|
|
request_method in ('PUT', 'PATCH'))):
|
|
|
|
id_field = self.fields[id_attr]
|
|
|
|
if data.get("id"):
|
|
|
|
id_value = id_field.to_internal_value(data.get("id"))
|
|
|
|
else:
|
|
|
|
id_value = id_field.to_internal_value(data.get("pk"))
|
|
|
|
ret[id_attr] = id_value
|
2017-12-12 04:19:45 +00:00
|
|
|
|
|
|
|
return ret
|
2019-04-25 02:11:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BulkListSerializerMixin(object):
|
|
|
|
"""
|
|
|
|
Become rest_framework_bulk doing bulk update raise Exception:
|
|
|
|
'QuerySet' object has no attribute 'pk' when doing bulk update
|
|
|
|
so rewrite it .
|
|
|
|
https://github.com/miki725/django-rest-framework-bulk/issues/68
|
|
|
|
"""
|
|
|
|
|
|
|
|
def to_internal_value(self, data):
|
|
|
|
"""
|
|
|
|
List of dicts of native values <- List of dicts of primitive datatypes.
|
|
|
|
"""
|
|
|
|
if html.is_html_input(data):
|
|
|
|
data = html.parse_html_list(data)
|
|
|
|
|
|
|
|
if not isinstance(data, list):
|
|
|
|
message = self.error_messages['not_a_list'].format(
|
|
|
|
input_type=type(data).__name__
|
|
|
|
)
|
|
|
|
raise ValidationError({
|
|
|
|
api_settings.NON_FIELD_ERRORS_KEY: [message]
|
|
|
|
}, code='not_a_list')
|
|
|
|
|
|
|
|
if not self.allow_empty and len(data) == 0:
|
|
|
|
if self.parent and self.partial:
|
|
|
|
raise SkipField()
|
|
|
|
|
|
|
|
message = self.error_messages['empty']
|
|
|
|
raise ValidationError({
|
|
|
|
api_settings.NON_FIELD_ERRORS_KEY: [message]
|
|
|
|
}, code='empty')
|
|
|
|
|
|
|
|
ret = []
|
|
|
|
errors = []
|
|
|
|
|
|
|
|
for item in data:
|
|
|
|
try:
|
|
|
|
# prepare child serializer to only handle one instance
|
2019-05-21 08:24:01 +00:00
|
|
|
if 'id' in item.keys():
|
|
|
|
self.child.instance = self.instance.get(id=item['id']) if self.instance else None
|
|
|
|
if 'pk' in item.keys():
|
|
|
|
self.child.instance = self.instance.get(id=item['pk']) if self.instance else None
|
|
|
|
|
2019-04-25 02:11:50 +00:00
|
|
|
self.child.initial_data = item
|
|
|
|
# raw
|
|
|
|
validated = self.child.run_validation(item)
|
|
|
|
except ValidationError as exc:
|
|
|
|
errors.append(exc.detail)
|
|
|
|
else:
|
|
|
|
ret.append(validated)
|
|
|
|
errors.append({})
|
|
|
|
|
|
|
|
if any(errors):
|
|
|
|
raise ValidationError(errors)
|
|
|
|
|
|
|
|
return ret
|
2017-12-12 04:19:45 +00:00
|
|
|
|
2017-12-26 17:29:23 +00:00
|
|
|
|
|
|
|
class DatetimeSearchMixin:
|
2018-01-01 07:08:33 +00:00
|
|
|
date_format = '%Y-%m-%d'
|
2017-12-26 17:29:23 +00:00
|
|
|
date_from = date_to = None
|
|
|
|
|
2018-07-19 10:36:52 +00:00
|
|
|
def get_date_range(self):
|
2017-12-26 17:29:23 +00:00
|
|
|
date_from_s = self.request.GET.get('date_from')
|
|
|
|
date_to_s = self.request.GET.get('date_to')
|
|
|
|
|
|
|
|
if date_from_s:
|
2018-01-01 07:08:33 +00:00
|
|
|
date_from = timezone.datetime.strptime(date_from_s, self.date_format)
|
2018-03-02 07:39:01 +00:00
|
|
|
tz = timezone.get_current_timezone()
|
|
|
|
self.date_from = tz.localize(date_from)
|
2017-12-26 17:29:23 +00:00
|
|
|
else:
|
|
|
|
self.date_from = timezone.now() - timezone.timedelta(7)
|
|
|
|
|
|
|
|
if date_to_s:
|
|
|
|
date_to = timezone.datetime.strptime(
|
2018-01-01 07:08:33 +00:00
|
|
|
date_to_s + ' 23:59:59', self.date_format + ' %H:%M:%S'
|
2017-12-26 17:29:23 +00:00
|
|
|
)
|
|
|
|
self.date_to = date_to.replace(
|
|
|
|
tzinfo=timezone.get_current_timezone()
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.date_to = timezone.now()
|
2018-07-19 10:36:52 +00:00
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.get_date_range()
|
2018-01-11 12:10:27 +00:00
|
|
|
return super().get(request, *args, **kwargs)
|