forked from getpatchwork/patchwork
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompat.py
More file actions
105 lines (86 loc) · 3.49 KB
/
Copy pathcompat.py
File metadata and controls
105 lines (86 loc) · 3.49 KB
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
# Patchwork - automated patch tracking system
# Copyright (C) 2016 Intel Corporation
#
# This file is part of the Patchwork package.
#
# Patchwork is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Patchwork is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Patchwork; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""Compatibility wrappers for various Django versions."""
import django
from django.conf import settings
# render_to_string
#
# The render_to_string function no longer accepts the dictionary and
# context_instance parameters in Django 1.10.
#
# https://docs.djangoproject.com/en/dev/releases/1.8/
if django.VERSION >= (1, 8):
from django.template.loader import render_to_string # noqa
else:
from django.template import loader # noqa
from django.template import RequestContext # noqa
def render_to_string(template_name, context=None, request=None):
context_instance = RequestContext(request) if request else None
return loader.render_to_string(template_name, context,
context_instance)
# DjangoFilterBackend
#
# The DjangoFilterBackend was provided in Django REST Framework from 3.0 to
# 3.4, was marked as pending deprecation in 3.5, was deprecated in 3.6 and will
# be removed in 3.7. However, the equivalent DjangoFilterBackend found in
# django-filter is only available since 1.0 of that package.
#
# http://www.django-rest-framework.org/topics/3.6-announcement/
if settings.ENABLE_REST_API:
import rest_framework # noqa
if rest_framework.VERSION >= '3.5':
from django_filters.rest_framework import DjangoFilterBackend # noqa
else:
from rest_framework.filters import DjangoFilterBackend # noqa
# LOOKUP_FIELD
#
# The django-filter library uses the 'lookup_expr' attribute to determine which
# lookup type to use, e.g. exact, gt, lt etc. However, until 0.13 this was
# called 'lookup_type', and 0.13 supported both but gave a deprecation warning.
# We need to support these versions for use with older versions of DRF.
#
# https://github.com/carltongibson/django-filter/blob/v0.13/django_filters\
# /filters.py#L35-L36
if settings.ENABLE_REST_API:
import django_filters # noqa
if django_filters.VERSION >= (1, 0):
LOOKUP_FIELD = 'lookup_expr'
else:
LOOKUP_FIELD = 'lookup_type'
# reverse
#
# The reverse function has been moved to django.urls in Django 1.10 and
# backwards compatible imports will be removed in Django 2.0
if django.VERSION >= (1, 10):
from django.urls import NoReverseMatch # noqa
from django.urls import reverse # noqa
else:
from django.core.urlresolvers import NoReverseMatch # noqa
from django.core.urlresolvers import reverse # noqa
# is_authenticated
#
# models.User.is_authenticated is now an attribute in Django 1.10 instead of a
# function
#
# https://docs.djangoproject.com/en/dev/releases/1.10/
def is_authenticated(user):
if django.VERSION >= (1, 10):
return user.is_authenticated
else:
return user.is_authenticated()