set up FileType properly, select on in DB, rather than add new ones each time, ouch! Also renamed type to type_id (better as it is a new foreign key) and use type as the relationship to hold the object anyway
This commit is contained in:
@@ -159,7 +159,8 @@ class Entry(Base):
|
|||||||
__tablename__ = "entry"
|
__tablename__ = "entry"
|
||||||
id = Column(Integer, Sequence('file_id_seq'), primary_key=True )
|
id = Column(Integer, Sequence('file_id_seq'), primary_key=True )
|
||||||
name = Column(String, unique=True, nullable=False )
|
name = Column(String, unique=True, nullable=False )
|
||||||
type = Column(String, unique=False, nullable=False)
|
type_id = Column(Integer, ForeignKey("file_type.id"))
|
||||||
|
type=relationship("FileType")
|
||||||
dir_details = relationship( "Dir")
|
dir_details = relationship( "Dir")
|
||||||
file_details = relationship( "New_File" )
|
file_details = relationship( "New_File" )
|
||||||
in_dir = relationship ("Dir", secondary="entry_dir_link" )
|
in_dir = relationship ("Dir", secondary="entry_dir_link" )
|
||||||
@@ -182,6 +183,9 @@ class FileType(Base):
|
|||||||
id = Column(Integer, Sequence('file_type_id_seq'), primary_key=True )
|
id = Column(Integer, Sequence('file_type_id_seq'), primary_key=True )
|
||||||
name = Column(String, unique=True, nullable=False )
|
name = Column(String, unique=True, nullable=False )
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "<id: {}, name={}>".format(self.id, self.name )
|
||||||
|
|
||||||
class File(Base):
|
class File(Base):
|
||||||
__tablename__ = "file"
|
__tablename__ = "file"
|
||||||
id = Column(Integer, Sequence('file_id_seq'), primary_key=True )
|
id = Column(Integer, Sequence('file_id_seq'), primary_key=True )
|
||||||
@@ -370,8 +374,9 @@ def MakeSymlink(job,path):
|
|||||||
|
|
||||||
def AddDir(job, dirname, path_prefix, in_dir):
|
def AddDir(job, dirname, path_prefix, in_dir):
|
||||||
dir=Dir( path_prefix=path_prefix )
|
dir=Dir( path_prefix=path_prefix )
|
||||||
dtype = FileType(name='Directory')
|
dtype = session.query(FileType).filter(FileType.name=='Directory').first()
|
||||||
e=Entry( name=dirname, type=dtype.id )
|
e=Entry( name=dirname, type=dtype )
|
||||||
|
print( dtype)
|
||||||
e.dir_details.append(dir)
|
e.dir_details.append(dir)
|
||||||
# this occurs when we Add the actual Dir for the import_path
|
# this occurs when we Add the actual Dir for the import_path
|
||||||
if in_dir:
|
if in_dir:
|
||||||
@@ -380,7 +385,8 @@ def AddDir(job, dirname, path_prefix, in_dir):
|
|||||||
session.add(e)
|
session.add(e)
|
||||||
return dir
|
return dir
|
||||||
|
|
||||||
def AddFile(job, fname, ftype, fsize, in_dir ):
|
def AddFile(job, fname, type_str, fsize, in_dir ):
|
||||||
|
ftype = session.query(FileType).filter(FileType.name==type_str).first()
|
||||||
e=Entry( name=fname, type=ftype )
|
e=Entry( name=fname, type=ftype )
|
||||||
f=New_File( size_mb=fsize )
|
f=New_File( size_mb=fsize )
|
||||||
e.file_details.append(f)
|
e.file_details.append(f)
|
||||||
@@ -402,7 +408,7 @@ def JobImportDir(job):
|
|||||||
if os.path.exists( path ):
|
if os.path.exists( path ):
|
||||||
symlink=MakeSymlink(job,path)
|
symlink=MakeSymlink(job,path)
|
||||||
dir=AddDir(job, os.path.basename(path[0:-1]), symlink, None )
|
dir=AddDir(job, os.path.basename(path[0:-1]), symlink, None )
|
||||||
for file in glob.glob(path + '**', recursive=True):
|
for file in sorted(glob.glob(path + '**', recursive=True)):
|
||||||
if file == path:
|
if file == path:
|
||||||
continue
|
continue
|
||||||
fname=file.replace(path, "")
|
fname=file.replace(path, "")
|
||||||
@@ -415,13 +421,13 @@ def JobImportDir(job):
|
|||||||
dir=AddDir( job, fname, path_prefix, dir )
|
dir=AddDir( job, fname, path_prefix, dir )
|
||||||
else:
|
else:
|
||||||
if isImage(file):
|
if isImage(file):
|
||||||
ftype = FileType(name='Image')
|
type_str = 'Image'
|
||||||
elif isVideo(file):
|
elif isVideo(file):
|
||||||
ftype = FileType(name='Video')
|
type_str = 'Video'
|
||||||
else:
|
else:
|
||||||
ftype = FileType('File')
|
type_str = 'File'
|
||||||
fsize = round(os.stat(file).st_size/(1024*1024))
|
fsize = round(os.stat(file).st_size/(1024*1024))
|
||||||
e=AddFile( job, os.path.basename(fname), ftype.id, fsize, dir )
|
e=AddFile( job, os.path.basename(fname), type_str, fsize, dir )
|
||||||
else:
|
else:
|
||||||
AddLogForJob(job, "DEBUG: {} - {} is OLDER than {}".format( file, stat.st_ctime, last_import_date ), file )
|
AddLogForJob(job, "DEBUG: {} - {} is OLDER than {}".format( file, stat.st_ctime, last_import_date ), file )
|
||||||
print("DEBUG: {} - {} is OLDER than {}".format( file, stat.st_ctime, last_import_date ), file )
|
print("DEBUG: {} - {} is OLDER than {}".format( file, stat.st_ctime, last_import_date ), file )
|
||||||
|
|||||||
Reference in New Issue
Block a user