Пример #1
0
    def security_hash(self, request, form):
        """
        Calculates the security hash for the given HttpRequest and Form instances.

        Subclasses may want to take into account request-specific information,
        such as the IP address.
        """
        return security_hash(form)
def preview_full_clean(form):
    """
    In ``preview_post`` stage after running ``full_clean`` on original form
        - it drops all file fields and fill-in *_path fields

    because after in ``post_post`` stage it wont pass validation anyway,
    user doesn't upload file twice

    """

    super(form.__class__, form).full_clean()

    file_fields = [(fname, field) for fname, field in form.fields.iteritems()
        if isinstance(field, forms.FileField)]

    for fname, field in file_fields:
        tmp_dir = os.path.join(
            UPLOAD_DIR,
            TODAYS_DIR,
            security_hash(form)) 
        mkdir_p(tmp_dir)

        upload_cleanup()

        upload = form.cleaned_data[fname]

        if not upload:
            return  # just nop

        fd_name = os.sep.join((tmp_dir, upload.name))
        fd = open(fd_name, 'w')
        for chunk in upload.chunks():
            fd.write(chunk)
        fd.flush()

        # required for ImageField: django uses just .read() 
        # into PIL.Image() - otherwise it will be given empty file
        upload.seek(0)

        form.fields.pop(fname)

        path_fname = fname + PATH_SUFFIX
        form.cleaned_data[path_fname] = fd.name  # in view
        form.data[path_fname] = fd.name  # in template, as initial

        if isinstance(field, PreviewField):
            preview_fname = fname + PREVIEW_SUFFIX
            form.cleaned_data[preview_fname] = fd.name  # in view
            form.data[preview_fname] = fd.name  # in template, as initial