Skip to content

Instantly share code, notes, and snippets.

@gameame
Created November 18, 2011 10:24
Show Gist options
  • Select an option

  • Save gameame/1376105 to your computer and use it in GitHub Desktop.

Select an option

Save gameame/1376105 to your computer and use it in GitHub Desktop.

Revisions

  1. gameame revised this gist Nov 22, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,6 @@ class MyModel(models.Model):
    import os, base64, datetime
    def _func(instance, filename):
    name, ext = os.path.splitext(filename)
    name = base64.urlsafe_b64encode(name.encode("utf-8") + unicode(datetime.datetime.now()))
    name = base64.urlsafe_b64encode(name.encode("utf-8") + str(datetime.datetime.now()))
    return os.path.join(path, name + ext)
    return _func
  2. gameame revised this gist Nov 18, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -11,5 +11,5 @@ class MyModel(models.Model):
    def _func(instance, filename):
    name, ext = os.path.splitext(filename)
    name = base64.urlsafe_b64encode(name.encode("utf-8") + unicode(datetime.datetime.now()))
    return os.path.join(path, name, ext)
    return os.path.join(path, name + ext)
    return _func
  3. gameame created this gist Nov 18, 2011.
    15 changes: 15 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    def unique_filename(path):
    """
    Enforce unique upload file names.
    Usage:
    class MyModel(models.Model):
    file = ImageField(upload_to=unique_filename("path/to/upload/dir"))
    """
    import os, base64, datetime
    def _func(instance, filename):
    name, ext = os.path.splitext(filename)
    name = base64.urlsafe_b64encode(name.encode("utf-8") + unicode(datetime.datetime.now()))
    return os.path.join(path, name, ext)
    return _func