Link generator using django or any python module

I want to create a temporary download link for the user. Is it ok if I use django to create a link using url patterns? Maybe this is the right way to do this. Because it may happen that I do not understand some processes, how it works. And it will fill my memory or something else. Some example or tools will be appreciated. Perhaps some nginx, apache modules?

So, I want to make the url template dependent on the user and time. Decript it end returns the file into view.

+3
source share
3 answers

Yes, it would be ok to allow django to generate urls. This excludes URL handling from urls.py. As a rule, you don’t want django to handle a portion of files, see Static docs [1] for more on this, so get an idea of ​​using url patterns from the head.

What you can do is generate a random key using a hash like md5 / sha1 for example. Save the file and key, date and time added to the database, create the download directory in the root directory accessible from your web server, for example apache or nginx ... suggest nginx). Since it is temporary, you need to add a cron job that checks if the time has passed since the url was created, clears the file and deletes the db entry. This should be the django command for manage.py

, , ! , , , . , dl pw, httpbasic auth. " " httpd.auth, htpasswd .

import hashlib, random, datetime, os, shutil
# model to hold link info. has these fields: key (charfield), filepath (filepathfield)
# datetime (datetimefield), url (charfield), orgpath (filepathfield of the orignal path
# or a foreignkey to the files model.
from models import MyDlLink 
# settings.py for the app
from myapp import settings as myapp_settings

# full path and name of file to dl.
def genUrl(filepath):
  # create a onetime salt for randomness
  salt = ''.join(['{0}'.format(random.randrange(10) for i in range(10)])
  key = hashlib('{0}{1}'.format(salt, filepath).hexdigest()
  newpath = os.path.join(myapp_settings.DL_ROOT, key)
  shutil.copy2(fname, newpath)
  newlink = MyDlink()
  newlink.key = key
  newlink.date = datetime.datetime.now()
  newlink.orgpath = filepath
  newlink.newpath = newpath
  newlink.url = "{0}/{1}/{2}".format(myapp_settings.DL_URL, key, os.path.basename(fname))

  newlink.save()
  return newlink


# in commands
def check_url_expired():
  maxage = datetime.timedelta(days=7)
  now = datetime.datetime.now()
  for link in MyDlink.objects.all():
     if(now - link.date) > maxage:
       os.path.remove(link.newpath)
           link.delete()

[1] http://docs.djangoproject.com/en/1.2/howto/static-files/

+2

- :

from datetime import datetime
from hashlib import sha1

user = 'bob'
time = datetime.now().isoformat()
plain = user + '\0' + time
token = sha1(plain)
print token.hexdigest()
"1e2c5078bd0de12a79d1a49255a9bff9737aa4a4"

memcache . , - , . , URL- Django '^ download/.+', memcache, , . memcache.

+4

, - URL-.

, URL-, , /?

(r'^download/(?P<encrypted_id>(.*)/$', 'download_file'), # use your own regexp


def download_file(request, encrypted_id):
    decrypted = decrypt(encrypted_id)
    _file = get_file(decrypted)
    return _file

get param.

www.example.com/download_file/?09248903483o8a908423028a0df8032

, : Django

In case of allocation of apache x-sendfile module.


Another alternative is to simply redirect to a static file served by any means from django.

+1
source

All Articles