-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
views.py
181 lines (139 loc) · 5.6 KB
/
views.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
'''
Copyright (C) 2017-2018 Vanessa Sochat.
Copyright (C) 2017-2018 The Board of Trustees of the Leland Stanford Junior
University.
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
This program 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 Affero General Public
License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
from django.conf import settings
from shub.logger import bot
from shub.apps.main.views import get_container
from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect, render
from django.urls import reverse
from django.contrib import messages
from django.http import JsonResponse
from .actions import (
get_endpoints,
do_transfer,
search_endpoints
)
from .decorators import has_globus_association
from shub.plugins.globus.utils import (
get_client,
get_transfer_client,
associate_user
)
from globus_sdk.exc import TransferAPIError
@login_required
@has_globus_association
def globus_logout(request):
'''log the user out of globus, meaning deleting the association,
and then revoking all tokens'''
credentials = request.user.disconnect('globus')
client = get_client()
for resource, token_info in credentials.extra_data.items():
for token, token_type in token_info.items():
client.oauth2_revoke_token(
token, additional_params={'token_type_hint': token_type})
# Redirect to globus logout page?
redirect_name = "Singularity Registry"
redirect_url = "%s%s" %(settings.DOMAIN_NAME, reverse('profile'))
logout_url = 'https://auth.globus.org/v2/web/logout'
params = '?client=%s&redirect_uri=%s&redirect_name=%s' %(client.client_id,
redirect_url,
redirect_name)
return redirect("%s%s" %(logout_url, params))
@login_required
def globus_login(request):
'''
Associate the logged in user with a globus account based on email.
If the association doesn't exist, create it. Redirect to transfer
page.
'''
# redirect_uri = reverse('globus_login')
redirect_uri = "http://localhost/globus/login/"
client = get_client()
client.oauth2_start_flow(redirect_uri,
refresh_tokens=True)
# First step of authentication flow - we need code
if "code" not in request.GET:
auth_uri = client.oauth2_get_authorize_url()
return redirect(auth_uri)
else:
# Second step of authentication flow - we need to ask for token
code = request.GET.get('code')
user = associate_user(request.user,
client=client,
code=code)
return redirect('globus_transfer')
@login_required
@has_globus_association
def globus_transfer(request, cid=None, endpoints=None):
''' a main portal for working with globus. If the user has navigated
here with a container id, it is presented with option to do a
transfer. If the method is a POST, we also do a custom search
for a set of containers.
'''
container = None
if cid is not None:
container = get_container(cid)
context = {'user': request.user,
'container': container,
'endpoint_search_term': "Search for..." }
# Does the user want to search endpoints?
if request.method == "POST":
term = request.POST.get('term')
if term is not None:
endpoints = search_endpoints(term=term, user=request.user)
context['endpoint_search_term'] = term.capitalize()
# If we don't have any endpoints still
if endpoints is None:
endpoints = get_endpoints(request.user)
context['endpoints'] = endpoints
return render(request, 'globus/transfer.html', context)
@login_required
@has_globus_association
def globus_endpoint(request, endpoint_id=None, cid=None):
''' Show information for a single endpoint only.
'''
container = None
if cid is not None:
container = get_container(cid)
context = {'user': request.user,
'container': container,
'endpoint_search_term': "Search for..." }
# Get the endpoint
try:
client = get_transfer_client(request.user)
endpoints = [client.get_endpoint(endpoint_id).data]
except TransferAPIError:
endpoints = get_endpoints(request.user)
context['endpoints'] = endpoints
return render(request, 'globus/transfer.html', context)
@login_required
@has_globus_association
def submit_transfer(request, endpoint, cid):
'''submit a transfer request for a container id to an endpoint, also
based on id
'''
container = get_container(cid)
if container is None:
message = "This container could not be found."
else:
result = do_transfer(user=request.user,
endpoint=endpoint,
container=container)
link = "https://globus.org/app/activity/%s" %result['task_id']
m = result['message']
m = "%s: <a target='_blank' href='%s'>view task</a>" %(m, link)
status = {'message': m }
return JsonResponse(status)