Verified Commit 63553511 authored by Dmitry Volodin's avatar Dmitry Volodin
Browse files

Remove DatabaseStorage model

parent ee99d43a
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------
# Database File Storage (PostgreSQL only)
# ---------------------------------------------------------------------
# Copyright (C) 2007-2009 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
from django.core.files.storage import Storage
from django.core.files.base import File,ContentFile
from django.conf import settings
import psycopg2
#
# Database File Storage (PostgreSQL)
# Stores files in database table.
# Table must have at least following columns:
# File Name (Character Type) - contains full virtual path
# Binary Data (BYTEA) - File content
# Size (Integer Type) - File size (in octets)
# Modification Time (Timestamp) - Last modification time
# Table name and field names must be set up via
# "option" variable. "option" is a hash with following keys:
# db_table
# name_field
# data_field
# size field
# mtime_field
#
class DatabaseStorage(Storage):
def __init__(self,option):
self.db_table = option["db_table"]
self.name_field = option["name_field"]
self.data_field = option["data_field"]
self.size_field = option["size_field"]
self.mtime_field = option["mtime_field"]
##
##
##
def get_cursor(self):
from django.db import connection
return connection.cursor()
##
## Internal implementation of the open()
## name - is a full path
## mode - is a file mode (ignored)
## Returns File object or None
##
def _open(self, name, mode="rb"):
cursor=self.get_cursor()
cursor.execute("SELECT %s FROM %s WHERE %s=%%s"%(self.data_field,self.db_table,self.name_field),[name])
row=cursor.fetchone()
if row is None:
return None
return ContentFile(row[0])
##
## Internal implementation of save()
## name - is a full path
## content - is a File object
## Returns file name
##
def _save(self, name, content):
cursor=self.get_cursor()
binary=content.read()
size=len(binary)
binary=psycopg2.Binary(binary)
if self.exists(name):
cursor.execute("UPDATE %s SET %s=%%s,%s='now',%s=%%s WHERE %s=%%s"%(self.db_table,self.data_field,
self.mtime_field,self.size_field,self.name_field),[binary,size,name])
else:
cursor.execute("INSERT INTO %s(%s,%s,%s,%s) VALUES(%%s,'now',%%s,%%s)"%(self.db_table,self.name_field,
self.mtime_field,self.size_field,self.data_field),[name,size,binary])
return name
##
## Check a file exists
## name - is a full path
## Returns boolean
##
def exists(self,name):
cursor=self.get_cursor()
cursor.execute("SELECT COUNT(*) FROM %s WHERE %s=%%s"%(self.db_table,self.name_field),[name])
return cursor.fetchone()[0]>0
##
## Returns full filesystem path as specified in Storage API
## Raises NotImplementedError because Storage is not related to filesystem
##
def path(self,name):
raise NotImplementedError
##
## Returns file size or 0
##
def size(self,name):
cursor=self.get_cursor()
cursor.execute("SELECT %s FROM %s WHERE %s=%%s"%(self.size_field,self.db_table,self.name_field),[name])
row=cursor.fetchone()
if row:
return row[0]
else:
return 0
##
##
##
def url(self,name):
pass
##
## Deletes file
## name is a full path
##
def delete(self,name):
cursor=self.get_cursor()
cursor.execute("DELETE FROM %s WHERE %s=%%s"%(self.db_table,self.name_field),[name])
##
## Returns converted file name (Required by Storage API)
##
def get_available_name(self,name):
return name
##
## Returns a directory listing
## name - is a full path of directory
## Returns empty list when directory does not exists
##
def listdir(self,name):
cursor=self.get_cursor()
if not name.endswith("/"):
name+="/"
ln=len(name)
cursor.execute("SELECT %s FROM %s WHERE %s ~ '^%s[^/]+/?$'"%(self.name_field,self.db_table,self.name_field,name.replace("'","\\'")))
return [x[0][ln:] for x in cursor.fetchall()]
##
## Returns file stats.
## stats is a hash of
## name : full path
## size : file size
## mtime : last modification time
## Returns None when file does not exists
def stat(self,name):
cursor=self.get_cursor()
cursor.execute("SELECT %s,%s FROM %s WHERE %s=%%s"%(self.size_field,self.mtime_field,self.db_table,self.name_field),[name])
row=cursor.fetchone()
if row:
return {
"name" : "name",
"size" : row[0],
"mtime": row[1],
}
else:
return None
##
## Set file's mtime
##
def set_mtime(self,name,mtime):
cursor=self.get_cursor()
cursor.execute("UPDATE %s SET %s=%%s WHERE %s=%%s"%(self.db_table,self.mtime_field,self.name_field),[mtime,name])
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------
# DatabaseStorage
# ---------------------------------------------------------------------
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
# Third-party models
from django.db import models
# NOC modules
from noc.core.model.base import NOCModel
from noc.core.model.fields import BinaryField
from noc.lib.database_storage import DatabaseStorage as DBS
class DatabaseStorage(NOCModel):
"""
Database Storage
"""
class Meta(object):
app_label = "main"
db_table = "main_databasestorage"
verbose_name = "Database Storage"
verbose_name_plural = "Database Storage"
name = models.CharField("Name", max_length=256, unique=True)
data = BinaryField("Data")
size = models.IntegerField("Size")
mtime = models.DateTimeField("MTime")
# Options for DatabaseStorage
@classmethod
def dbs_options(cls):
return {
"db_table": DatabaseStorage._meta.db_table,
"name_field": "name",
"data_field": "data",
"mtime_field": "mtime",
"size_field": "size",
}
@classmethod
def get_dbs(cls):
"""
Get DatabaseStorage instance
"""
return DBS(cls.dbs_options())
# Default database storage
database_storage = DatabaseStorage.get_dbs()
......@@ -95,7 +95,6 @@ _MODELS = {
"main.CustomField": "noc.main.models.customfield.CustomField",
"main.CustomFieldEnumGroup": "noc.main.models.customfieldenumgroup.CustomFieldEnumGroup",
"main.CustomFieldEnumValue": "noc.main.models.customfieldenumvalue.CustomFieldEnumValue",
"main.DatabaseStorage": "noc.main.models.databasestorage.DatabaseStorage",
"main.DocCategory": "noc.main.models.doccategory.DocCategory",
"main.ExtStorage": "noc.main.models.extstorage.ExtStorage",
"main.Favorites": "noc.main.models.favorites.Favorites",
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment