Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to the imports Endpoint. #35

Merged
merged 2 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion kpi/deployment_backends/kc_access/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 6 additions & 4 deletions kpi/deployment_backends/kobocat_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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())
Expand Down
10 changes: 8 additions & 2 deletions kpi/models/import_export_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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}
Expand Down