diff --git a/kpi/deployment_backends/kc_access/utils.py b/kpi/deployment_backends/kc_access/utils.py index 03ee31e4d3..617510d7bd 100644 --- a/kpi/deployment_backends/kc_access/utils.py +++ b/kpi/deployment_backends/kc_access/utils.py @@ -30,7 +30,8 @@ def _trigger_kc_profile_creation(user): UserProfile if none exists already """ url = settings.KOBOCAT_URL + '/api/v1/user' - token = Token.objects.using('kobocat').get(user=user) + kobo_user = User.objects.using('kobocat').get(username=user.username) + token = Token.objects.using('kobocat').get(user=kobo_user) response = requests.get( url, headers={'Authorization': 'Token ' + token.key}) if not response.status_code == 200: diff --git a/kpi/deployment_backends/kobocat_backend.py b/kpi/deployment_backends/kobocat_backend.py index 49da30f128..b0fb42fd4c 100644 --- a/kpi/deployment_backends/kobocat_backend.py +++ b/kpi/deployment_backends/kobocat_backend.py @@ -6,11 +6,12 @@ import requests from django.conf import settings -from django.core.exceptions import ImproperlyConfigured -from django.utils.translation import ugettext_lazy as _ from django.utils.six import text_type +from django.contrib.auth.models import User from rest_framework import status, serializers from rest_framework.authtoken.models import Token +from django.core.exceptions import ImproperlyConfigured +from django.utils.translation import ugettext_lazy as _ from kpi.constants import INSTANCE_FORMAT_TYPE_JSON, INSTANCE_FORMAT_TYPE_XML from kpi.utils.log import logging @@ -298,7 +299,7 @@ def redeploy(self, active=None): return self.connect(self.identifier, active) raise - self.set_asset_uid() + # self.set_asset_uid() def set_active(self, active): """ @@ -735,7 +736,8 @@ def __kobocat_proxy_request(kc_request, user=None): :return: requests.models.Response """ if not user.is_anonymous and user.pk != settings.ANONYMOUS_USER_ID: - token, created = Token.objects.get_or_create(user=user) + token = User.objects.using("kobocat").select_related("auth_token").get( + username=user.username).auth_token kc_request.headers['Authorization'] = 'Token %s' % token.key session = requests.Session() return session.send(kc_request.prepare()) diff --git a/kpi/models/import_export_task.py b/kpi/models/import_export_task.py index af253dc102..e852939350 100644 --- a/kpi/models/import_export_task.py +++ b/kpi/models/import_export_task.py @@ -13,6 +13,7 @@ import requests from django.conf import settings +from django.contrib.auth.models import User from rest_framework.authtoken.models import Token from django.core.files.base import ContentFile from django.urls import Resolver404, resolve @@ -176,7 +177,10 @@ def _run_task(self, messages): # TODO: merge with `url` handling above; currently kept separate # because `_load_assets_from_url()` uses complex logic to deal with # multiple XLS files in a directory structure within a ZIP archive - headers = {'Authorization': 'Token ' + self.user.auth_token.key} + username = self.user.username + token = User.objects.using("kobocat").select_related("auth_token").get( + username=username).auth_token.key + headers = {'Authorization': 'Token ' + token} response = requests.get( self.data['single_xls_url'], headers=headers ) @@ -265,7 +269,9 @@ def get_form_payload(self, form_id): url = '{}/api/v1/forms/{}'.format( settings.KOBOCAT_INTERNAL_URL, form_id ) - token, _ = Token.objects.get_or_create(user=self.user) + username=self.user.username + token = User.objects.using("kobocat").select_related("auth_token").get( + username=username).auth_token response = requests.get( url, headers={'Authorization': 'Token ' + token.key}