1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
""" |
4
|
|
|
Created on Tue Feb 6 15:04:07 2018 |
5
|
|
|
|
6
|
|
|
@author: Paolo Cozzi <[email protected]> |
7
|
|
|
""" |
8
|
|
|
|
9
|
|
|
import os |
10
|
|
|
import logging |
11
|
|
|
import mimetypes |
12
|
|
|
|
13
|
|
|
from django.utils.encoding import smart_str |
14
|
|
|
from django.contrib.auth.decorators import login_required |
15
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin |
16
|
|
|
from django.shortcuts import get_object_or_404 |
17
|
|
|
from django.views.generic import TemplateView, UpdateView |
18
|
|
|
from django.conf import settings |
19
|
|
|
from django.urls import reverse_lazy |
20
|
|
|
from django.http import HttpResponse |
21
|
|
|
|
22
|
|
|
from common.storage import ProtectedFileSystemStorage |
23
|
|
|
from common.constants import COMPLETED, BIOSAMPLE_URL |
24
|
|
|
from common.views import FormInvalidMixin |
25
|
|
|
|
26
|
|
|
from .models import Submission, Organization, uid_report |
27
|
|
|
from .forms import OrganizationForm |
28
|
|
|
|
29
|
|
|
# Get an instance of a logger |
30
|
|
|
logger = logging.getLogger(__name__) |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
class IndexView(TemplateView): |
34
|
|
|
# Just set this Class Object Attribute to the template page. |
35
|
|
|
# template_name = 'app_name/site.html' |
36
|
|
|
template_name = 'uid/index.html' |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
class AboutView(TemplateView): |
40
|
|
|
# Just set this Class Object Attribute to the template page. |
41
|
|
|
# template_name = 'app_name/site.html' |
42
|
|
|
template_name = 'uid/about.html' |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
class PrivacyView(TemplateView): |
46
|
|
|
template_name = "uid/privacy_policy.html" |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
class TermsView(TemplateView): |
50
|
|
|
template_name = "uid/terms_and_conditions.html" |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
class AboutUploadingView(TemplateView): |
54
|
|
|
template_name = "uid/uploading_data.html" |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
class DashBoardView(LoginRequiredMixin, TemplateView): |
58
|
|
|
template_name = "uid/dashboard.html" |
59
|
|
|
|
60
|
|
|
def get_context_data(self, **kwargs): |
61
|
|
|
# Call the base implementation first to get a context |
62
|
|
|
context = super().get_context_data(**kwargs) |
63
|
|
|
|
64
|
|
|
# add the base biosample urls to context |
65
|
|
|
context["biosample_url"] = BIOSAMPLE_URL + "?filter=attr:project:IMAGE" |
66
|
|
|
|
67
|
|
|
return context |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
class SummaryView(LoginRequiredMixin, TemplateView): |
71
|
|
|
template_name = "uid/summary.html" |
72
|
|
|
|
73
|
|
|
def get_context_data(self, **kwargs): |
74
|
|
|
# Call the base implementation first to get a context |
75
|
|
|
context = super(SummaryView, self).get_context_data(**kwargs) |
76
|
|
|
# add content to context |
77
|
|
|
|
78
|
|
|
# Add info for datasource |
79
|
|
|
# count object for a certain user |
80
|
|
|
context['datasource_count'] = Submission.objects.filter( |
81
|
|
|
owner=self.request.user).count() |
82
|
|
|
|
83
|
|
|
# count loaded objects into biosample |
84
|
|
|
context['datasource_completed'] = Submission.objects.filter( |
85
|
|
|
status=COMPLETED, owner=self.request.user).count() |
86
|
|
|
|
87
|
|
|
# call report from UID model |
88
|
|
|
context["uid_report"] = uid_report(self.request.user) |
89
|
|
|
|
90
|
|
|
return context |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
# https://gist.github.com/cobusc/ea1d01611ef05dacb0f33307e292abf4 |
94
|
|
|
@login_required |
95
|
|
|
def protected_view(request, path): |
96
|
|
|
""" |
97
|
|
|
Redirect the request to the path used by nginx for protected media. |
98
|
|
|
""" |
99
|
|
|
|
100
|
|
|
logger.debug("Received path %s for download" % (path)) |
101
|
|
|
|
102
|
|
|
# test for submission ownership |
103
|
|
|
dirname = os.path.dirname(path) |
104
|
|
|
|
105
|
|
|
if dirname == 'data_source': |
106
|
|
|
# I got a submission. Get a submission belonging to owner or 404 |
107
|
|
|
submission = get_object_or_404( |
108
|
|
|
Submission, |
109
|
|
|
owner=request.user, |
110
|
|
|
uploaded_file=path) |
111
|
|
|
|
112
|
|
|
logger.debug("Got submission %s" % (submission)) |
113
|
|
|
|
114
|
|
|
# derive downloadable path. Applies to any protected file |
115
|
|
|
full_path = os.path.join( |
116
|
|
|
settings.PROTECTED_MEDIA_LOCATION_PREFIX, path |
117
|
|
|
) |
118
|
|
|
|
119
|
|
|
file_name = os.path.basename(path) |
120
|
|
|
|
121
|
|
|
# try to determine file type |
122
|
|
|
file_type, encoding = mimetypes.guess_type(path) |
123
|
|
|
|
124
|
|
|
logger.debug("Detected content type: %s" % (file_type)) |
125
|
|
|
|
126
|
|
|
# get file size using protected storage |
127
|
|
|
storage = ProtectedFileSystemStorage() |
128
|
|
|
file_size = storage.size(path) |
129
|
|
|
|
130
|
|
|
# try to set file type to response |
131
|
|
|
# https://djangosnippets.org/snippets/1710/ |
132
|
|
|
if file_type is None: |
133
|
|
|
file_type = 'application/octet-stream' |
134
|
|
|
|
135
|
|
|
# force django to attach file in response |
136
|
|
|
response = HttpResponse(content_type=file_type) |
137
|
|
|
|
138
|
|
|
if encoding is not None: |
139
|
|
|
response['Content-Encoding'] = encoding |
140
|
|
|
|
141
|
|
|
# https://stackoverflow.com/a/1158750/4385116 |
142
|
|
|
response["X-Accel-Redirect"] = smart_str(full_path) |
143
|
|
|
response['Content-Disposition'] = 'attachment; filename="{}"'.format( |
144
|
|
|
file_name) |
145
|
|
|
response['Content-Length'] = file_size |
146
|
|
|
|
147
|
|
|
return response |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
class UpdateOrganizationView(FormInvalidMixin, LoginRequiredMixin, UpdateView): |
151
|
|
|
form_class = OrganizationForm |
152
|
|
|
model = Organization |
153
|
|
|
success_url = reverse_lazy('uid:dashboard') |
154
|
|
|
template_name = 'uid/organization_form.html' |
155
|
|
|
|
156
|
|
|
def get_object(self): |
157
|
|
|
"""Get the organization which this user belongs to""" |
158
|
|
|
|
159
|
|
|
return self.request.user.person.affiliation |
160
|
|
|
|