예제 #1
0
파일: revision.py 프로젝트: rosscdh/toolkit
    def to_native(self, value):
        if hasattr(value, 'url') is True:
            #
            # Validate its of the right type
            #
            self.validate_filename(value=value.name)
            #
            # Just download the object, the rest gets handled naturally
            #
            if urlparse(value.url).scheme:  # check where delaing with an actual url here
                _download_file(url=value.url, filename=value.name, obj=value.instance,
                              obj_fieldname=self.file_field_name)

        return getattr(value, 'url', super(FileFieldAsUrlField, self).to_native(value=value))
예제 #2
0
파일: revision.py 프로젝트: rosscdh/toolkit
    def field_to_native(self, obj, field_name):
        if obj is not None:
            field = getattr(obj, field_name, None)

            try:

                if field.name in [None, '']:
                    raise FileHasNoNameException('File has no name')

                #
                # Start download if the file does not exist
                #

                # important as we then access the "name" attribute in teh serializer
                # that allows us to name the file (as filepicker sends the name and url seperately)
                request = self.context.get('request', {})
                url = request.DATA.get(self.file_field_name)

                original_filename = _valid_filename_length(request.DATA.get('name'), whitelist=self.ext_whitelist)

                if original_filename is None:
                    #
                    # the name was not set; therefore extract it from executed_file which shoudl be a url at this point
                    #
                    try:
                        url_path = urlparse(request.DATA.get(self.file_field_name))
                    except:
                        # sometimes, yknow its not a url
                        url_path = request.DATA.get(self.file_field_name)
                    original_filename = os.path.basename(url_path.path)

                #
                # NB! we pass this into download which then brings the filedown and names it in the precribed
                # upload_to manner
                #
                file_name, file_object = _download_file(url=url, filename=original_filename, obj=obj,
                                                        obj_fieldname=field_name)
                field = getattr(obj, field_name)

                # NB! we reuse the original_filename!
                # this is to prevent filenames that repeat the original name twice
                field.save(original_filename, file_object)
                # update the object
                obj.save(update_fields=[field_name])

                return super(HyperlinkedAutoDownloadFileField, self).field_to_native(obj, field_name)

            except Exception as e:
                logger.debug('File serialized without a value: %s' % e)
        #
        # NB this must return None!
        # else it will raise attribute has no file associated with it
        # errors
        #
        return None