| 1 | import django |
| 2 | from django.confscip-python python django-tastypie 0.15.1 `django.conf`/__init__: import settings |
| 3 | from django.contrib.authscip-python python django-tastypie 0.15.1 `django.contrib.auth`/__init__: import get_user_model # noqa |
| 4 | from django.utilsscip-python python django-tastypie 0.15.1 `django.utils`/__init__: import timezone |
| 5 | |
| 6 | try: |
| 7 | from django.urlsscip-python python django-tastypie 0.15.1 `django.urls`/__init__: import NoReverseMatch, reverse, Resolver404, get_script_prefix # noqa |
| 8 | except ImportErrorscip-python python python-stdlib 3.11 builtins/ImportError#: # 1.8 backwards compat |
| 9 | from django.core.urlresolversscip-python python django-tastypie 0.15.1 `django.core.urlresolvers`/__init__: import NoReverseMatch, reverse, Resolver404, get_script_prefix # noqa |
| 10 | |
| 11 | # Django 4.0 had a private _sanitize_token function whose signature is/was different than |
| 12 | # the 4.1 version (_check_token_format) - import the correct one and define a compatability |
| 13 | # function. |
| 14 | if django.VERSION < (4, 1): |
| 15 | from django.middleware.csrfscip-python python django-tastypie 0.15.1 `django.middleware.csrf`/__init__: import _sanitize_token |
| 16 | else: |
| 17 | from django.middleware.csrfscip-python python django-tastypie 0.15.1 `django.middleware.csrf`/__init__: import _check_token_format |
| 18 | |
| 19 | # Django 5.0 eliminated the former datetime_safe function, this provides |
| 20 | # some level of backwards compatability for existing tastypie use cases |
| 21 | if django.VERSION < (5, 0): |
| 22 | from django.utilsscip-python python django-tastypie 0.15.1 `django.utils`/__init__: import datetime_safe # noqa: F401 |
| 23 | else: |
| 24 | import datetimescip-python python python-stdlib 3.11 datetime/__init__: as datetime_safe # noqa: F401 |
| 25 | # Django 5.0 removed this alias - restore it for backwards compatability. |
| 26 | # Django 5.0 essentially completed a lot of the move to zoneinfo, prior to that |
| 27 | # this was an alias that was added in 4.0. |
| 28 | timezone.utc = datetime_safe.timezonescip-python python python-stdlib 3.11 datetime/timezone#.utcscip-python python python-stdlib 3.11 datetime/timezone#utc. |
| 29 | |
| 30 | AUTH_USER_MODELscip-python python django-tastypie 0.15.1 `tastypie.compat`/AUTH_USER_MODEL. = settings.AUTH_USER_MODEL |
| 31 | |
| 32 | |
| 33 | def get_username_fieldscip-python python django-tastypie 0.15.1 `tastypie.compat`/get_username_field().(): |
| 34 | return get_user_model().USERNAME_FIELD |
| 35 | |
| 36 | |
| 37 | def get_module_namescip-python python django-tastypie 0.15.1 `tastypie.compat`/get_module_name().(metascip-python python django-tastypie 0.15.1 `tastypie.compat`/get_module_name().(meta)): |
| 38 | return metascip-python python django-tastypie 0.15.1 `tastypie.compat`/get_module_name().(meta).model_name |
| 39 | |
| 40 | |
| 41 | def is_ajaxscip-python python django-tastypie 0.15.1 `tastypie.compat`/is_ajax().(requestscip-python python django-tastypie 0.15.1 `tastypie.compat`/is_ajax().(request)): |
| 42 | """ |
| 43 | Handle multiple ways of detecting an ajax request. Probably nearly useless. |
| 44 | """ |
| 45 | return requestscip-python python django-tastypie 0.15.1 `tastypie.compat`/is_ajax().(request).META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest' |
| 46 | |
| 47 | |
| 48 | atomic_decoratorscip-python python django-tastypie 0.15.1 `tastypie.compat`/atomic_decorator. = django.db.transaction.atomic |
| 49 | |
| 50 | # force_text deprecated in 2.2, removed in 3.0 |
| 51 | # note that in 1.1.x, force_str and force_text both exist, but force_str behaves |
| 52 | # much differently on python 3 than python 2. |
| 53 | if django.VERSION < (2, 2): |
| 54 | from django.utils.encodingscip-python python django-tastypie 0.15.1 `django.utils.encoding`/__init__: import force_text as force_str # noqa |
| 55 | else: |
| 56 | from django.utils.encodingscip-python python django-tastypie 0.15.1 `django.utils.encoding`/__init__: import force_str # noqa |
| 57 | |
| 58 | compare_sanitized_tokensscip-python python django-tastypie 0.15.1 `tastypie.compat`/compare_sanitized_tokens. = None |
| 59 | |
| 60 | # django 4.0 |
| 61 | try: |
| 62 | from django.middleware.csrfscip-python python django-tastypie 0.15.1 `django.middleware.csrf`/__init__: import _does_token_match, InvalidTokenFormat |
| 63 | |
| 64 | compare_sanitized_tokensscip-python python django-tastypie 0.15.1 `tastypie.compat`/compare_sanitized_tokens. = _does_token_match |
| 65 | except ImportErrorscip-python python python-stdlib 3.11 builtins/ImportError#: |
| 66 | pass |
| 67 | |
| 68 | # django 3.2 |
| 69 | if compare_sanitized_tokensscip-python python django-tastypie 0.15.1 `tastypie.compat`/compare_sanitized_tokens. is None: |
| 70 | try: |
| 71 | from django.middleware.csrfscip-python python django-tastypie 0.15.1 `django.middleware.csrf`/__init__: import _compare_masked_tokens |
| 72 | |
| 73 | compare_sanitized_tokensscip-python python django-tastypie 0.15.1 `tastypie.compat`/compare_sanitized_tokens. = _compare_masked_tokens |
| 74 | |
| 75 | class InvalidTokenFormat(Exceptionscip-python python python-stdlib 3.11 builtins/Exception#): # noqa |
| 76 | pass |
| 77 | except ImportErrorscip-python python python-stdlib 3.11 builtins/ImportError#: |
| 78 | pass |
| 79 | |
| 80 | # django 2.2 |
| 81 | if compare_sanitized_tokensscip-python python django-tastypie 0.15.1 `tastypie.compat`/compare_sanitized_tokens. is None: |
| 82 | try: |
| 83 | from django.middleware.csrfscip-python python django-tastypie 0.15.1 `django.middleware.csrf`/__init__: import _unsalt_cipher_token, constant_time_compare |
| 84 | |
| 85 | def compare_sanitized_tokensscip-python python django-tastypie 0.15.1 `tastypie.compat`/compare_sanitized_tokens.(request_csrf_tokenscip-python python django-tastypie 0.15.1 `tastypie.compat`/compare_sanitized_tokens().(request_csrf_token), csrf_tokenscip-python python django-tastypie 0.15.1 `tastypie.compat`/compare_sanitized_tokens().(csrf_token)): |
| 86 | return constant_time_compare(_unsalt_cipher_token(request_csrf_tokenscip-python python django-tastypie 0.15.1 `tastypie.compat`/compare_sanitized_tokens().(request_csrf_token)), |
| 87 | _unsalt_cipher_token(csrf_tokenscip-python python django-tastypie 0.15.1 `tastypie.compat`/compare_sanitized_tokens().(csrf_token))) |
| 88 | |
| 89 | class InvalidTokenFormat(Exceptionscip-python python python-stdlib 3.11 builtins/Exception#): # noqa |
| 90 | pass |
| 91 | except ImportErrorscip-python python python-stdlib 3.11 builtins/ImportError#: # pragma: no cover |
| 92 | raise ImportErrorscip-python python python-stdlib 3.11 builtins/ImportError#("Couldn't find a way to compare csrf tokens safely") # pragma: no cover |
| 93 | |
| 94 | |
| 95 | def check_token_formatscip-python python django-tastypie 0.15.1 `tastypie.compat`/check_token_format().(csrf_tokenscip-python python django-tastypie 0.15.1 `tastypie.compat`/check_token_format().(csrf_token)): |
| 96 | """ |
| 97 | Handle the pre-4.1 version of sanitizing the token as well as the post-4.0 version of the same. |
| 98 | """ |
| 99 | if django.VERSION < (4, 1): |
| 100 | csrf_tokenscip-python python django-tastypie 0.15.1 `tastypie.compat`/check_token_format().(csrf_token) = _sanitize_token(csrf_tokenscip-python python django-tastypie 0.15.1 `tastypie.compat`/check_token_format().(csrf_token)) |
| 101 | else: |
| 102 | _check_token_format(csrf_tokenscip-python python django-tastypie 0.15.1 `tastypie.compat`/check_token_format().(csrf_token)) |
| 103 | return csrf_tokenscip-python python django-tastypie 0.15.1 `tastypie.compat`/check_token_format().(csrf_token) |