~dooferlad/+junk/license_protection_2

« back to all changes in this revision

Viewing changes to license_protected_downloads/views.py

  • Committer: James Tunnicliffe
  • Date: 2012-06-22 18:07:26 UTC
  • Revision ID: james.tunnicliffe@linaro.org-20120622180726-q3nezkll0ayhmhjn
Added wsgi.py - needed for Apache + WSGI deployment. Probably wrong in many ways, but it was enough to do some quick testing.
Updated views.py to cope with hiding more files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
from models import License
18
18
from django.template import RequestContext
19
19
 
 
20
def _hidden_file(file_name):
 
21
    hidden_files = ["BUILD-INFO.txt", "EULA.txt", "OPEN-EULA.txt"]
 
22
    for pattern in hidden_files:
 
23
        if re.search(pattern, file_name):
 
24
            return True
 
25
    return False
 
26
 
 
27
def _hidden_dir(file_name):
 
28
    hidden_files = [".*openid.*", ".*restricted.*", ".*private.*"]
 
29
    for pattern in hidden_files:
 
30
        if re.search(pattern, file_name):
 
31
            return True
 
32
    return False
 
33
 
20
34
def dir_list(path):
21
35
    files = os.listdir(path)
22
36
    listing = []
23
 
    hidden_files = ["BUILD-INFO.txt", "EULA.txt", "OPEN-EULA.txt"]
 
37
 
24
38
    for file in files:
25
 
        if file in hidden_files:
26
 
            continue # Ignore...
 
39
        if _hidden_file(file):
 
40
            continue
 
41
 
27
42
        name = file
28
43
        file = os.path.join(path, file)
29
44
        mtime = time.ctime(os.path.getmtime(file))
97
112
        cookie_name = "license_accepted_" + d.encode("ascii")
98
113
        response.set_cookie(cookie_name,
99
114
                            max_age=60*60*24, # 1 day expiry
100
 
                            path=os.path.dirname(request.GET['url']))
 
115
                            path="/" + os.path.dirname(request.GET['url']))
101
116
    else:
102
117
        response = render_to_response('licenses/nolicense.html')
103
118
 
142
157
                response = redirect('/license?lic=' + digest + "&url=" + url)
143
158
 
144
159
        if not response:
145
 
            response = HttpResponse(mimetype='application/force-download')
 
160
            response = HttpResponse(mimetype='text/plain')
146
161
            response['Content-Disposition'] = ('attachment; filename=%s' %
147
162
                                               smart_str(file_name))
148
163
            response['X-Sendfile'] = smart_str(path)
 
164
            # TODO: Is it possible to add a redirect to response so we can take
 
165
            # the user back to the original directory this file is in?
149
166
    return response
150
167
 
151
168