在Django Image Upoads中使用时间戳使file_names唯一

时间:2019-04-22 04:17:47

标签: python django django-forms django-views django-file-upload

简介:我有一小段代码,可以将要添加的任何图像放大并保存。我为此使用了一个名为Filepond的外部库。

问题:如果2位用户在其图像(不同的图像)中添加了相同的名称。第二个用户图像替换了第一个用户图像,并且两个用户看到相同的图像。

我想要的内容:添加唯一的图像名称。我的尝试低于当前代码。我需要最好的解决方案,因此名称不是太大但很独特

当前代码:

fields.py:

class FilePondField(forms.FileField):
widget = forms.TextInput(attrs={'class': 'fileid'})

def __init__(self, name, *args, **kwargs):
    super(FilePondField, self).__init__(*args, **kwargs)
    self.name = name

def prepare_value(self, data):
    if not data:
        return None

    if isinstance(data, str):
        try:
            tu = TU.objects.get(upload_id=data)
        except TU.DoesNotExist:
            return None
        return tu.upload_id

    name = data.name
    base = os.path.basename(name)
    file_id = "%s_%s" % (self.name, data.instance.pk)
    try:
        tu = TU.objects.get(file_id=file_id)
    except TU.DoesNotExist:
        upload_id = uuid()
        tu = TU(upload_id=upload_id, file_id=file_id,  # uuid(),
                upload_name=base, upload_type=TU.FILE_DATA)
        try:
            with data.storage.open(name, 'rb') as f:
                rd_data = File(f)
                tu.file.save(tu.file_id, rd_data, True)
            tu.save()
        except:
            pass

    return tu.upload_id

def clean(self, data, initial=None):
    self.initial = initial
    if not data:
        if self.required:
            raise ValidationError(self.error_messages['required'], code='required')
        return None
    return data

def save_cb(self, instance, modfld, tu):
    prename = os.path.join(modfld.upload_to, tu.upload_name)
    ffile = ImageFieldFile(instance, modfld, prename)

    try:
        with open(tu.get_file_path(), 'rb') as f:
            data = File(f)
            ffile.save(tu.upload_name, data, False)
    except:
        pass
    return ffile

def do_tmp(self, instance, modfld, value, cb):
    try:
        tu = TU.objects.get(upload_id=value)
        ffile = cb(instance, modfld, tu) if cb else None
    except TU.DoesNotExist:
        ffile = None
    else:
        tu.delete()

    file_id = "%s_%s" % (self.name, instance.pk)
    try:
        ogtu = TU.objects.get(file_id=file_id)
    except TU.DoesNotExist:
        pass
    else:
        ogtu.delete()

    return ffile

def save(self, instance, modfld, value):
    return self.do_tmp(instance, modfld, value, self.save_cb)

def del_tmp(self, instance, modfld, value):
    self.do_tmp(instance, modfld, value, None)

def bound_data(self, data, initial):
    return data

def has_changed(self, initial, data):
    if not initial:
        return data
    return initial != data

forms.py

class ImageForm(forms.ModelForm):
img_fields = []

def __init__(self, *args, **kwargs):
    super(ImageForm, self).__init__(*args, **kwargs)
    for (fld, fargs) in self.img_fields:
        self.fields[fld] = FilePondField(fld, **fargs)

def save(self, *args, **kwargs):
    commit = kwargs.get('commit', True)
    for (fld_nm, fargs) in self.img_fields:
        fld = dict([(f.name, f) for f in self._meta.model._meta.fields])[fld_nm]
        if isinstance(self.fields[fld_nm], FilePondField):
            self.fields[fld_nm] = self.fields[fld_nm].save(self.instance, fld, self.cleaned_data[fld_nm])

    return super(ImageForm, self).save(*args, **kwargs)

def del_tmp (self):
    for (fld_nm, fargs) in self.img_fields:
        fld = dict([(f.name, f) for f in self._meta.model._meta.fields])[fld_nm]
        if isinstance(self.fields[fld_nm], FilePondField):
            self.fields[fld_nm].del_tmp(self.instance, fld, self.cleaned_data[fld_nm])

我的方法:

在我导入的 fields.py

在函数def prepare_value(self, data):def do_tmp(self, instance, modfld, value, cb):中,我进行了以下更改

...

file_id = "%s_%s_%s" % (self.name, data.instance.pk, datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S_%f"))
...
  

有人可以对此发表评论还是提出更好的选择

2 个答案:

答案 0 :(得分:1)

只需使用class a { b() { console.log("called b") } c() { console.log("called c") } d() { console.log("called d") } } for (const n of Object.getOwnPropertyNames(a.prototype)) { const f = a.prototype[n]; if (typeof f === "function" && n !== "b") { a.prototype[n] = function(...args) { this.b(); return f.apply(this, args); } } } var aa = new a(); aa.c(); aa.d();值作为文件名,如下所示:

datetime.now()

结果:它将创建一个名为from datetime import datetime open(str(datetime.now()) + ".txt", "w+")

的文件

答案 1 :(得分:0)

将名称设置为这样的变量

name = "Your-General-Name-{}".format((int(time.time())))

,然后将其放入循环中,以便time.time()值每次都更改。您显然不必使用time.time()。您可以使用datetime.datetime.now()等,然后只需替换时间函数即可。