Completed
Push — master ( 4f7ee6...646424 )
by Paolo
08:30 queued 06:53
created

uid.views.protected_view()   A

Complexity

Conditions 4

Size

Total Lines 54
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 54
rs 9.232
c 0
b 0
f 0
cc 4
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
18
from django.conf import settings
19
from django.http import HttpResponse
20
21
from common.storage import ProtectedFileSystemStorage
22
from common.constants import COMPLETED
23
24
from .models import Submission, uid_report
25
26
# Get an instance of a logger
27
logger = logging.getLogger(__name__)
28
29
30
class IndexView(TemplateView):
31
    # Just set this Class Object Attribute to the template page.
32
    # template_name = 'app_name/site.html'
33
    template_name = 'uid/index.html'
34
35
36
class AboutView(TemplateView):
37
    # Just set this Class Object Attribute to the template page.
38
    # template_name = 'app_name/site.html'
39
    template_name = 'uid/about.html'
40
41
42
class PrivacyView(TemplateView):
43
    template_name = "uid/privacy_policy.html"
44
45
46
class TermsView(TemplateView):
47
    template_name = "uid/terms_and_conditions.html"
48
49
50
class AboutUploadingView(TemplateView):
51
    template_name = "uid/uploading_data.html"
52
53
54
class DashBoardView(LoginRequiredMixin, TemplateView):
55
    template_name = "uid/dashboard.html"
56
57
58
class SummaryView(LoginRequiredMixin, TemplateView):
59
    template_name = "uid/summary.html"
60
61
    def get_context_data(self, **kwargs):
62
        # Call the base implementation first to get a context
63
        context = super(SummaryView, self).get_context_data(**kwargs)
64
        # add content to context
65
66
        # Add info for datasource
67
        # count object for a certain user
68
        context['datasource_count'] = Submission.objects.filter(
69
            owner=self.request.user).count()
70
71
        # count loaded objects into biosample
72
        context['datasource_completed'] = Submission.objects.filter(
73
            status=COMPLETED, owner=self.request.user).count()
74
75
        # call report from UID model
76
        context["uid_report"] = uid_report(self.request.user)
77
78
        return context
79
80
81
# https://gist.github.com/cobusc/ea1d01611ef05dacb0f33307e292abf4
82
@login_required
83
def protected_view(request, path):
84
    """
85
    Redirect the request to the path used by nginx for protected media.
86
    """
87
88
    logger.debug("Received path %s for download" % (path))
89
90
    # test for submission ownership
91
    dirname = os.path.dirname(path)
92
93
    if dirname == 'data_source':
94
        # I got a submission. Get a submission belonging to owner or 404
95
        submission = get_object_or_404(
96
            Submission,
97
            owner=request.user,
98
            uploaded_file=path)
99
100
        logger.debug("Got submission %s" % (submission))
101
102
    # derive downloadable path. Applies to any protected file
103
    full_path = os.path.join(
104
        settings.PROTECTED_MEDIA_LOCATION_PREFIX, path
105
    )
106
107
    file_name = os.path.basename(path)
108
109
    # try to determine file type
110
    file_type, encoding = mimetypes.guess_type(path)
111
112
    logger.debug("Detected content type: %s" % (file_type))
113
114
    # get file size using protected storage
115
    storage = ProtectedFileSystemStorage()
116
    file_size = storage.size(path)
117
118
    # try to set file type to response
119
    # https://djangosnippets.org/snippets/1710/
120
    if file_type is None:
121
        file_type = 'application/octet-stream'
122
123
    # force django to attach file in response
124
    response = HttpResponse(content_type=file_type)
125
126
    if encoding is not None:
127
        response['Content-Encoding'] = encoding
128
129
    # https://stackoverflow.com/a/1158750/4385116
130
    response["X-Accel-Redirect"] = smart_str(full_path)
131
    response['Content-Disposition'] = 'attachment; filename="{}"'.format(
132
        file_name)
133
    response['Content-Length'] = file_size
134
135
    return response
136