-
Notifications
You must be signed in to change notification settings - Fork 12
/
views.py
297 lines (241 loc) · 11.2 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import os
import datetime
import requests
from django.core.mail import send_mail
from django.conf import settings
import django_filters
from rest_framework import filters, generics, mixins
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.exceptions import NotFound, ParseError
from rest_framework.pagination import LimitOffsetPagination
from api.auth import UserAccessSelfOnly, ClassifierCreatePermission, ClassifierRetrievePermission, MLWorkerOnlyPermission
from api.models import User, Classifier, Disease, Sample, Mutation, Gene
from api.serializers import ClassifierSerializer, UserSerializer, GeneSerializer, DiseaseSerializer, MutationSerializer, SampleSerializer
from api import queue
# This URL including the list ID is hardcoded,
# but we don't plan on changing it any time soon.
MAILCHIMP_LIST_URL = "https://us14.api.mailchimp.com/3.0/lists/074bca87ce/members"
# Classifier
class ClassifierFilter(filters.FilterSet):
created_at__gte = django_filters.IsoDateTimeFilter(name='created_at', lookup_expr='gte')
created_at__lte = django_filters.IsoDateTimeFilter(name='created_at', lookup_expr='lte')
updated_at__gte = django_filters.IsoDateTimeFilter(name='updated_at', lookup_expr='gte')
updated_at__lte = django_filters.IsoDateTimeFilter(name='updated_at', lookup_expr='lte')
class Meta:
model = Classifier
fields = ['user', 'created_at', 'updated_at']
class ClassifierCreate(generics.CreateAPIView, mixins.CreateModelMixin):
permission_classes = (ClassifierCreatePermission,)
serializer_class = ClassifierSerializer
def add_user_to_mailchimp_list(self, email_address, mailchimp_api_key):
requests.post(MAILCHIMP_LIST_URL,
json={"email_address": email_address,
"status": "subscribed"},
auth=("anystring", mailchimp_api_key))
def perform_create(self, serializer):
mailchimp_api_key = os.getenv("MAILCHIMP_API_KEY")
try:
should_subscribe = self.request.data["subscribe"]
if mailchimp_api_key is not None and should_subscribe == True:
self.add_user_to_mailchimp_list(self.request.user.email, mailchimp_api_key)
except KeyError:
pass
serializer.save()
class RetrieveClassifier(generics.RetrieveAPIView):
permission_classes = (ClassifierRetrievePermission,)
queryset = Classifier.objects.all()
serializer_class = ClassifierSerializer
lookup_field = 'id'
class UploadCompletedNotebookToClassifier(APIView):
permission_classes = (MLWorkerOnlyPermission,)
def post(self, request, id):
try:
classifier = Classifier.objects.get(id=id)
except Classifier.DoesNotExist:
raise NotFound('Classifier not found')
serializer = ClassifierSerializer(classifier,
data={
'notebook_file': request.FILES['notebook_file'],
'completed_at': datetime.datetime.utcnow()
}, partial=True)
serializer.is_valid(raise_exception=True)
serializer.save()
download_link = classifier.notebook_file.url
nbviewer_link = 'https://nbviewer.jupyter.org/urls/' + download_link.replace('https://', '')
email_message = 'Cognoma has completed processing your classifier.\n' + \
'Visit {notebook_link} to download your notebook.\n'.format(notebook_link=download_link) + \
'Visit {nbviewer_link} to view your notebook online.'.format(nbviewer_link=nbviewer_link)
send_mail(subject='Cognoma Classifier {id} Processing Complete'.format(id=classifier.id),
message=email_message,
from_email=settings.FROM_EMAIL,
recipient_list=[classifier.user.email],
fail_silently=True)
return Response(serializer.data, status=201)
class PullClassifierTaskQueue(APIView):
permission_classes = (MLWorkerOnlyPermission,)
def get(self, request, format=None):
if 'title' in request.query_params:
title = request.query_params.getlist('title')
else:
raise ParseError('`title` query parameter required')
if 'worker_id' in request.query_params:
try:
worker_id = str(request.query_params['worker_id'])
except ValueError:
raise ParseError('`worker_id` query parameter must be a string')
else:
raise ParseError('`worker_id` query parameter required')
if 'limit' in request.query_params:
try:
limit = int(request.query_params['limit'])
except ValueError:
raise ParseError('`limit` query parameter must be an integer')
else:
limit = 1
if limit < 1 or limit > 10:
raise ParseError('`limit` must be between 1 and 10')
raw_classifiers = queue.get_classifiers(title,
worker_id,
limit)
classifiers = []
for classifier in raw_classifiers:
serializer = ClassifierSerializer(classifier)
classifiers.append(serializer.data)
return Response(classifiers)
class ReleaseClassifierTask(APIView):
permission_classes = (MLWorkerOnlyPermission,)
def post(self, request, id):
try:
classifier = Classifier.objects.get(id=id)
except Classifier.DoesNotExist:
raise NotFound('Task not found')
classifier.status = 'queued'
classifier.locked_at = None
classifier.worker_id = None
classifier.save()
return Response(data={'message': 'Classifier task released.'}, status=200)
class FailClassifierTask(APIView):
permission_classes = (MLWorkerOnlyPermission,)
def post(self, request, id):
try:
classifier = Classifier.objects.get(id=id)
except Classifier.DoesNotExist:
raise NotFound('Task not found')
serializer = ClassifierSerializer(classifier, data={
'failed_at': datetime.datetime.utcnow(),
'fail_reason': request.data['fail_reason'],
'fail_message': request.data['fail_message']
}, partial=True)
serializer.is_valid(raise_exception=True)
serializer.save()
if classifier.attempts >= classifier.max_attempts:
email_message = 'An error has occurred and your classifier could not be processed.\n' + \
'Error: ' + classifier.fail_message + '\n' + \
'Support is available at https://github.com/cognoma.'
send_mail(subject='Cognoma Classifier {id} Processing Failure'.format(id=classifier.id),
message=email_message,
from_email=settings.FROM_EMAIL,
recipient_list=[classifier.user.email],
fail_silently=True)
return Response(data=serializer.data, status=200)
# User
class UserFilter(filters.FilterSet):
created_at__gte = django_filters.IsoDateTimeFilter(name='created_at', lookup_expr='gte')
created_at__lte = django_filters.IsoDateTimeFilter(name='created_at', lookup_expr='lte')
updated_at__gte = django_filters.IsoDateTimeFilter(name='updated_at', lookup_expr='gte')
updated_at__lte = django_filters.IsoDateTimeFilter(name='updated_at', lookup_expr='lte')
class Meta:
model = User
fields = ['email', 'created_at', 'updated_at']
class UserCreate(generics.CreateAPIView):
permission_classes = []
serializer_class = UserSerializer
class UserRetrieveUpdateFromSlug(generics.RetrieveUpdateAPIView):
permission_classes = []
queryset = User.objects.all()
serializer_class = UserSerializer
lookup_field = 'random_slugs'
def get_object(self):
random_slug = self.kwargs['random_slug']
try:
return self.queryset.get(random_slugs__contains=[random_slug])
except User.DoesNotExist:
raise NotFound("No user exists with random slug of {slug}.".format(slug=random_slug))
# Genes
class GeneFilter(filters.FilterSet):
class Meta:
model = Gene
fields = ['entrez_gene_id', 'symbol', 'chromosome', 'gene_type']
class GenePagination(LimitOffsetPagination):
default_limit = 10
max_limit = 10
class GeneList(generics.ListAPIView):
queryset = Gene.objects.all()
serializer_class = GeneSerializer
filter_backends = (filters.DjangoFilterBackend,)
filter_class = GeneFilter
pagination_class = GenePagination
ordering_fields = ('entrez_gene_id', 'symbol', 'chromosome')
ordering = ('entrez_gene_id',)
class GeneRetrieve(generics.RetrieveAPIView):
queryset = Gene.objects.all()
serializer_class = GeneSerializer
lookup_field = 'entrez_gene_id'
# Diseases
class DiseaseFilter(filters.FilterSet):
class Meta:
model = Disease
fields = ['acronym', 'name']
class DiseaseList(generics.ListAPIView):
queryset = Disease.objects.all()
serializer_class = DiseaseSerializer
filter_backends = (filters.DjangoFilterBackend,)
filter_class = DiseaseFilter
ordering_fields = ('acronym', 'name',)
ordering = ('acronym',)
class DiseaseRetrieve(generics.RetrieveAPIView):
queryset = Disease.objects.all()
serializer_class = DiseaseSerializer
lookup_field = 'acronym'
# Mutations
class MutationFilter(filters.FilterSet):
class Meta:
model = Mutation
fields = ['gene', 'sample']
class MutationList(generics.ListAPIView):
queryset = Mutation.objects.all()
serializer_class = MutationSerializer
filter_backends = (filters.DjangoFilterBackend,)
filter_class = MutationFilter
ordering_fields = ('id',)
ordering = ('id',)
class MutationRetrieve(generics.RetrieveAPIView):
queryset = Mutation.objects.all()
serializer_class = MutationSerializer
lookup_field = 'id'
# Samples
# via https://github.com/carltongibson/django-filter/issues/137#issuecomment-38158832
# Courtesy of @miserlou
class ListFilter(django_filters.Filter):
def filter(self, qs, value):
value_list = value.split(u',')
return super(ListFilter, self).filter(qs, django_filters.fields.Lookup(value_list, 'in'))
class SampleFilter(filters.FilterSet):
age_diagnosed__gte = django_filters.IsoDateTimeFilter(name='age_diagnosed', lookup_expr='gte')
age_diagnosed__lte = django_filters.IsoDateTimeFilter(name='age_diagnosed', lookup_expr='lte')
any_mutations = ListFilter(name='mutations__gene') # Return union
class Meta:
model = Sample
fields = ['sample_id', 'disease', 'gender', 'age_diagnosed', 'any_mutations', 'mutations__gene', 'mutations__gene__entrez_gene_id']
class SampleList(generics.ListAPIView):
queryset = Sample.objects.all()
serializer_class = SampleSerializer
filter_backends = (filters.DjangoFilterBackend,)
filter_class = SampleFilter
ordering_fields = ('sample_id', 'disease', 'age_diagnosed',)
ordering = ('sample_id',)
class SampleRetrieve(generics.RetrieveAPIView):
queryset = Sample.objects.all()
serializer_class = SampleSerializer
lookup_field = 'sample_id'