示例#1
0
文件: models.py 项目: mfoacs/rcwk001
    def save(self, *args, **kwargs):
        """
        Check what storage backend which are being used.
        If backend is any of the provided database backends,
        emit the `write_binary` signal to write the file to
        the blob field.
        """
        # self.backend = 'PostgreSQLStorage'
        self.slug = slugify(self.pre_slug)

        if self.backend in ["PostgreSQLStorage", "MySQLStorage", "SQLiteStorage", "OracleStorage"]:
            # If using one of the included database backends,
            # save the instance and emit the `write_binary` signal
            # to write the binary data into the blob field.
            try:
                content = self.attachment.file
                super(Attachment, self).save(*args, **kwargs)
                write_binary.send(sender=Attachment, instance=self, content=content)
            except Exception as e:
                raise e
        elif self.backend == "FileSystemStorage":
            # If using the default FileSystemStorage,
            # save some extra attributes as well.
            if not self.pk:
                super(Attachment, self).save(*args, **kwargs)
            self.slug = slugify(self.pre_slug)
            self.checksum = md5buffer(self.attachment.file)
            super(Attachment, self).save(force_update=True)
        else:
            raise UnsupportedBackend("Unsupported storage backend.")

        #          Send the post_write signal after save even if backend does not
        #          use the write_binary method (such as the FileStorageBackend), to
        #          keep consistancy between all backends.

        post_write.send(sender=Attachment, instance=self)
示例#2
0
            except Exception, e:
                raise e
        elif self.backend == "FileSystemStorage":
            # If using the default FileSystemStorage,
            # save some extra attributes as well.
            if not self.pk:
                super(Attachment, self).save(*args, **kwargs)
            self.slug = slugify(self.pre_slug)
            self.checksum = md5buffer(self.attachment.file)
            super(Attachment, self).save(force_update=True)
        else:
            raise UnsupportedBackend("Unsupported storage backend.")
        # Send the post_write signal after save even if backend does not
        # use the write_binary method (such as the FileStorageBackend), to
        # keep consistancy between all backends.
        post_write.send(sender=Attachment, instance=self)

    @property
    def pre_slug(self):
        """
        Create a nice "semi unique" slug. This is not the real slug,
        only a helper method to create the string which is slugified.
        """
        s = "-".join(
            map(str, (self.content_type, self.pk,
                      os.path.basename(self.attachment.name))))
        return re.sub("[^\w+]", "-", s)

    @property
    def filename(self):
        return os.path.basename(self.attachment.name)
示例#3
0
         except Exception, e:
             raise e
     elif self.backend == "FileSystemStorage":
         # If using the default FileSystemStorage,
         # save some extra attributes as well.
         if not self.pk:
             super(Attachment, self).save(*args, **kwargs)
         self.slug = slugify(self.pre_slug)
         self.checksum = md5buffer(self.attachment.file)
         super(Attachment, self).save(force_update=True)
     else:
         raise UnsupportedBackend("Unsupported storage backend.")
     # Send the post_write signal after save even if backend does not
     # use the write_binary method (such as the FileStorageBackend), to
     # keep consistancy between all backends.
     post_write.send(sender=Attachment, instance=self)
 
 @property
 def pre_slug(self):
     """
     Create a nice "semi unique" slug. This is not the real slug,
     only a helper method to create the string which is slugified.
     """
     s = "-".join(map(str, (self.content_type, self.pk,
                            os.path.basename(self.attachment.name))))
     return re.sub("[^\w+]", "-", s)
 
 @property
 def filename(self):
     return os.path.basename(self.attachment.name)