-
Notifications
You must be signed in to change notification settings - Fork 4
/
views.py
63 lines (54 loc) · 2.24 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
__author__ = 'ibrahim (at) sikilabs (dot) com'
__licence__ = 'MIT'
from django.shortcuts import RequestContext
from django.template import loader
from django.http import HttpResponse
from django.conf import settings
import datetime
import os
from main.unique.models import UniqueUrl
# import unique url object
modlist = settings.UNIQUE_URL_OBJECT.split(".")
_module = ".".join(modlist[:len(modlist) - 1])
_class = modlist[-1]
module_to_import_from = __import__(_module, fromlist=[_class])
class_to_import = getattr(module_to_import_from, _class)
def get_file(request, unique_url):
"""
get file or redirect to error page
"""
my_unique = UniqueUrl.objects.get(url=unique_url)
if my_unique.clics < settings.UNIQUE_MAX_CLICS:
if datetime.date.today() < my_unique.expiration_date:
my_object_id = my_unique.decode_url(my_unique.url_hash,
my_unique.url)[2]
my_object = class_to_import.objects.get(id=my_object_id)
myfile = open(settings.MEDIA_ROOT + my_object.path, 'r')
fname, extension = os.path.splitext(my_object.path)
response = HttpResponse(myfile,
content_type='application/' + extension)
response['Content-Disposition'] = "attachment; filename=" + my_object.original_filename
my_unique.clics += 1
my_unique.save()
return response
else:
t = loader.get_template('unique/download_error.html')
c = RequestContext(request, {'status': 1}) # expiration passed
return HttpResponse(t.render(c))
else:
t = loader.get_template('unique/download_error.html')
c = RequestContext(request, {'status': 2}) # max download reached
return HttpResponse(t.render(c))
def generate_url(request, object_id):
"""
generate unique url from object id
"""
unique_object = class_to_import.objects.get(id=object_id)
try:
my_unique = UniqueUrl.objects.get(user=request.user,
ref_object=unique_object)
except UniqueUrl.DoesNotExist:
my_unique = UniqueUrl.objects.create(user=request.user,
expiration_date=settings.UNIQUE_EXP_DATE,
ref_object=unique_object, clics=0)
my_unique.encode_url()