-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathcomputed_file.py
More file actions
179 lines (157 loc) · 5.41 KB
/
Copy pathcomputed_file.py
File metadata and controls
179 lines (157 loc) · 5.41 KB
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
##
# Contains ComputedFileListView, ComputedFileDetailView, and needed serializers
##
from django.core.exceptions import ValidationError
from rest_framework import filters, generics, serializers
from django_filters.rest_framework import DjangoFilterBackend
from data_refinery_api.exceptions import InvalidFilters
from data_refinery_api.utils import check_filters
from data_refinery_api.views.relation_serializers import (
ComputationalResultNoFilesRelationSerializer,
ComputationalResultRelationSerializer,
DetailedExperimentSampleSerializer,
)
from data_refinery_common.models import APIToken, ComputedFile
class ComputedFileListSerializer(serializers.ModelSerializer):
result = ComputationalResultNoFilesRelationSerializer(many=False)
samples = DetailedExperimentSampleSerializer(many=True)
compendia_organism_name = serializers.CharField(
source="compendia_organism__name", read_only=True
)
def __init__(self, *args, **kwargs):
super(ComputedFileListSerializer, self).__init__(*args, **kwargs)
if "context" in kwargs:
# only include the field `download_url` if a valid token is specified
# the token lookup happens in the view.
if "token" not in kwargs["context"]:
self.fields.pop("download_url")
class Meta:
model = ComputedFile
fields = (
"id",
"filename",
"samples",
"size_in_bytes",
"is_qn_target",
"is_smashable",
"is_qc",
"is_compendia",
"quant_sf_only",
"compendium_version",
"compendia_organism_name",
"sha1",
"s3_bucket",
"s3_key",
"s3_url",
"download_url",
"created_at",
"last_modified_at",
"result",
)
read_only_fields = fields
extra_kwargs = {
"download_url": {
"help_text": "This will contain an url to download the file. You must send a valid [token](#tag/token) in order to receive this."
}
}
class DetailedComputedFileSerializer(serializers.ModelSerializer):
result = ComputationalResultRelationSerializer(many=False, read_only=False)
samples = DetailedExperimentSampleSerializer(many=True)
compendia_organism_name = serializers.CharField(
source="compendia_organism__name", read_only=True
)
class Meta:
model = ComputedFile
fields = (
"id",
"filename",
"samples",
"size_in_bytes",
"is_qn_target",
"is_smashable",
"is_qc",
"is_compendia",
"quant_sf_only",
"compendium_version",
"compendia_organism_name",
"sha1",
"s3_bucket",
"s3_key",
"s3_url",
"download_url",
"created_at",
"last_modified_at",
"result",
)
read_only_fields = fields
class ComputedFileListView(generics.ListAPIView):
"""
computed_files_list
ComputedFiles are representation of files created by refinebio processes.
It's possible to download each one of these files by providing a valid token. To
acquire and activate an API key see the documentation for the [/token](#tag/token) endpoint.
When a valid token is provided the url will be sent back in the field `download_url`. Example:
```py
import requests
import json
headers = {
'Content-Type': 'application/json',
'API-KEY': token_id # requested from /token
}
requests.get('https://api.refine.bio/v1/computed_files/?id=5796866', {}, headers=headers)
```
This endpoint can also be used to fetch all the compendia files we have generated with:
```
GET /computed_files?is_compendia=True&is_public=True
```
"""
queryset = ComputedFile.objects.all()
serializer_class = ComputedFileListSerializer
filter_backends = (
DjangoFilterBackend,
filters.OrderingFilter,
)
filterset_fields = (
"id",
"samples",
"is_qn_target",
"is_smashable",
"is_qc",
"is_compendia",
"quant_sf_only",
"svd_algorithm",
"compendium_version",
"created_at",
"last_modified_at",
"result__id",
)
ordering_fields = (
"id",
"created_at",
"last_modified_at",
"compendium_version",
)
ordering = ("-id",)
def get_queryset(self):
invalid_filters = check_filters(self)
if invalid_filters:
raise InvalidFilters(invalid_filters=invalid_filters)
return self.queryset
def get_serializer_context(self):
"""
Extra context provided to the serializer class.
"""
serializer_context = super(ComputedFileListView, self).get_serializer_context()
token_id = self.request.META.get("HTTP_API_KEY", None)
try:
token = APIToken.objects.get(id=token_id, is_activated=True)
return {**serializer_context, "token": token}
except (APIToken.DoesNotExist, ValidationError):
return serializer_context
class ComputedFileDetailView(generics.RetrieveAPIView):
"""
Retrieves a computed file by its ID
"""
lookup_field = "id"
queryset = ComputedFile.objects.all()
serializer_class = DetailedComputedFileSerializer