6
6
from typing import Any , Dict , Optional , Union
7
7
from wsgiref .util import FileWrapper
8
8
9
+ from django .conf import settings
9
10
from django .db .models import Q
10
11
from django .http import Http404 , HttpResponse
11
12
from ispyb .connector .mysqlsp .main import ISPyBMySQLSPConnector as Connector
@@ -91,6 +92,7 @@ def get_conn() -> Optional[Connector]:
91
92
# Assume the credentials are invalid if there is no host.
92
93
# If a host is not defined other properties are useless.
93
94
if not credentials ["host" ]:
95
+ logger .debug ("No ISPyB host - cannot return a connector" )
94
96
return None
95
97
96
98
conn : Optional [Connector ] = None
@@ -114,19 +116,28 @@ def get_configured_connector() -> Optional[Union[Connector, SSHConnector]]:
114
116
return None
115
117
116
118
119
+ def ping_configured_connector () -> bool :
120
+ """Pings the connector. If a connection can be obtained it is immediately closed.
121
+ The ping simply provides a way to check the credentials are valid and
122
+ a connection can be made.
123
+ """
124
+ conn : Optional [Union [Connector , SSHConnector ]] = None
125
+ if connector == 'ispyb' :
126
+ conn = get_conn ()
127
+ elif connector == 'ssh_ispyb' :
128
+ conn = get_remote_conn ()
129
+ if conn is not None :
130
+ conn .stop ()
131
+ return conn is not None
132
+
133
+
117
134
class ISpyBSafeQuerySet (viewsets .ReadOnlyModelViewSet ):
118
135
def get_queryset (self ):
119
136
"""
120
137
Optionally restricts the returned purchases to a given proposals
121
138
"""
122
139
# The list of proposals this user can have
123
140
proposal_list = self .get_proposals_for_user (self .request .user )
124
- # Add in the ones everyone has access to
125
- # (unless we already have it)
126
- for open_proposal in self .get_open_proposals ():
127
- if open_proposal not in proposal_list :
128
- proposal_list .append (open_proposal )
129
-
130
141
logger .debug (
131
142
'is_authenticated=%s, proposal_list=%s' ,
132
143
self .request .user .is_authenticated ,
@@ -141,12 +152,10 @@ def get_queryset(self):
141
152
def get_open_proposals (self ):
142
153
"""
143
154
Returns the list of proposals anybody can access.
155
+ They are defined via an environment variable
156
+ and are made available as a list of strings (Project titles)
144
157
"""
145
- if os .environ .get ("TEST_SECURITY_FLAG" , False ):
146
- return ["lb00000" ]
147
- else :
148
- # All of well-known (built-in) public Projects (Proposals/Visits)
149
- return ["lb27156" ]
158
+ return settings .PUBLIC_TAS_LIST
150
159
151
160
def get_proposals_for_user_from_django (self , user ):
152
161
# Get the list of proposals for the user
@@ -278,20 +287,27 @@ def get_proposals_for_user(self, user):
278
287
"""Returns a list of proposals (public and private) that the user has access to."""
279
288
assert user
280
289
290
+ proposals = []
281
291
ispyb_user = os .environ .get ("ISPYB_USER" )
282
292
logger .debug ("ispyb_user=%s" , ispyb_user )
283
293
if ispyb_user :
284
294
if user .is_authenticated :
285
295
logger .info ("Getting proposals from ISPyB..." )
286
- return self .get_proposals_for_user_from_ispyb (user )
296
+ proposals = self .get_proposals_for_user_from_ispyb (user )
287
297
else :
288
- logger .info (
289
- "No proposals (user %s is not authenticated)" , user .username
290
- )
291
- return []
298
+ username = user .username or "UNKNOWN"
299
+ logger .info ("No proposals (user '%s' is not authenticated)" , username )
292
300
else :
293
301
logger .info ("Getting proposals from Django..." )
294
- return self .get_proposals_for_user_from_django (user )
302
+ proposals = self .get_proposals_for_user_from_django (user )
303
+
304
+ # Now add in Target Access Strings that everyone has access to
305
+ # (unless we already have them)
306
+ for open_proposal in self .get_open_proposals ():
307
+ if open_proposal not in proposals :
308
+ proposals .append (open_proposal )
309
+
310
+ return proposals
295
311
296
312
def get_q_filter (self , proposal_list ):
297
313
"""Returns a Q expression representing a (potentially complex) table filter."""
0 commit comments