|
10 | 10 | from typing import Generator, Optional, Union
|
11 | 11 | from urllib.parse import urlparse
|
12 | 12 | from xml.etree import ElementTree as ET
|
| 13 | + |
13 | 14 | try:
|
14 | 15 | from zoneinfo import ZoneInfo
|
15 | 16 | except ImportError:
|
16 | 17 | from backports.zoneinfo import ZoneInfo
|
17 | 18 |
|
18 | 19 | import requests
|
19 | 20 | from django.conf import settings
|
| 21 | +from django.contrib.auth.models import User |
20 | 22 | from django.core.exceptions import ImproperlyConfigured
|
21 |
| -from lxml import etree |
22 | 23 | from django.core.files import File
|
23 | 24 | from django.db.models.query import QuerySet
|
24 | 25 | from django.utils.translation import gettext_lazy as t
|
| 26 | +from lxml import etree |
25 | 27 | from rest_framework import status
|
26 | 28 | from rest_framework.authtoken.models import Token
|
27 | 29 | from rest_framework.reverse import reverse
|
28 | 30 |
|
29 |
| -from kpi.constants import ( |
30 |
| - SUBMISSION_FORMAT_TYPE_JSON, |
31 |
| - SUBMISSION_FORMAT_TYPE_XML, |
32 |
| - PERM_FROM_KC_ONLY, |
33 |
| - PERM_CHANGE_SUBMISSIONS, |
34 |
| - PERM_DELETE_SUBMISSIONS, |
35 |
| - PERM_VALIDATE_SUBMISSIONS, |
36 |
| -) |
37 |
| -from kpi.exceptions import ( |
38 |
| - AttachmentNotFoundException, |
39 |
| - InvalidXPathException, |
40 |
| - SubmissionIntegrityError, |
41 |
| - SubmissionNotFoundException, |
42 |
| - XPathNotFoundException, |
43 |
| -) |
| 31 | +from kpi.constants import (PERM_CHANGE_SUBMISSIONS, PERM_DELETE_SUBMISSIONS, |
| 32 | + PERM_FROM_KC_ONLY, PERM_VALIDATE_SUBMISSIONS, |
| 33 | + SUBMISSION_FORMAT_TYPE_JSON, |
| 34 | + SUBMISSION_FORMAT_TYPE_XML) |
| 35 | +from kpi.exceptions import (AttachmentNotFoundException, InvalidXPathException, |
| 36 | + SubmissionIntegrityError, |
| 37 | + SubmissionNotFoundException, |
| 38 | + XPathNotFoundException) |
44 | 39 | from kpi.interfaces.sync_backend_media import SyncBackendMediaInterface
|
45 | 40 | from kpi.models.asset_file import AssetFile
|
46 | 41 | from kpi.models.object_permission import ObjectPermission
|
|
49 | 44 | from kpi.utils.mongo_helper import MongoHelper
|
50 | 45 | from kpi.utils.permissions import is_user_anonymous
|
51 | 46 | from kpi.utils.xml import edit_submission_xml
|
| 47 | + |
| 48 | +from ..exceptions import (BadFormatException, |
| 49 | + KobocatBulkUpdateSubmissionsClientException, |
| 50 | + KobocatDeploymentException, |
| 51 | + KobocatDuplicateSubmissionException) |
52 | 52 | from .base_backend import BaseDeploymentBackend
|
53 |
| -from .kc_access.shadow_models import ( |
54 |
| - KobocatOneTimeAuthToken, |
55 |
| - KobocatXForm, |
56 |
| - ReadOnlyKobocatAttachment, |
57 |
| - ReadOnlyKobocatInstance, |
58 |
| -) |
59 |
| -from .kc_access.utils import ( |
60 |
| - assign_applicable_kc_permissions, |
61 |
| - instance_count, |
62 |
| - last_submission_time |
63 |
| -) |
64 |
| -from ..exceptions import ( |
65 |
| - BadFormatException, |
66 |
| - KobocatBulkUpdateSubmissionsClientException, |
67 |
| - KobocatDeploymentException, |
68 |
| - KobocatDuplicateSubmissionException, |
69 |
| -) |
| 53 | +from .kc_access.shadow_models import (KobocatOneTimeAuthToken, KobocatXForm, |
| 54 | + ReadOnlyKobocatAttachment, |
| 55 | + ReadOnlyKobocatInstance) |
| 56 | +from .kc_access.utils import (assign_applicable_kc_permissions, instance_count, |
| 57 | + last_submission_time) |
70 | 58 |
|
71 | 59 |
|
72 | 60 | class KobocatDeploymentBackend(BaseDeploymentBackend):
|
@@ -1372,7 +1360,8 @@ def __kobocat_proxy_request(kc_request, user=None):
|
1372 | 1360 | :return: requests.models.Response
|
1373 | 1361 | """
|
1374 | 1362 | if not is_user_anonymous(user):
|
1375 |
| - token, created = Token.objects.get_or_create(user=user) |
| 1363 | + token = User.objects.using("kobocat").select_related( |
| 1364 | + "auth_token").get(username=user.username).auth_token |
1376 | 1365 | kc_request.headers['Authorization'] = 'Token %s' % token.key
|
1377 | 1366 | session = requests.Session()
|
1378 | 1367 | return session.send(kc_request.prepare())
|
|
0 commit comments