cleanup document cache after upload

This commit is contained in:
j 2016-09-23 13:38:00 +02:00
parent 263b5c9ce2
commit b7659d6f14
2 changed files with 10 additions and 8 deletions

View file

@ -202,6 +202,7 @@ class Document(models.Model):
self.get_ratio() self.get_ratio()
self.oshash = ox.oshash(self.file.path) self.oshash = ox.oshash(self.file.path)
self.save() self.save()
self.delete_cache()
return True, self.file.size return True, self.file.size
return save_chunk(self, self.file, chunk, offset, name, done_cb) return save_chunk(self, self.file, chunk, offset, name, done_cb)
@ -301,13 +302,17 @@ class Document(models.Model):
Document.objects.filter(id=self.id).update(matches=matches) Document.objects.filter(id=self.id).update(matches=matches)
self.matches = matches self.matches = matches
def delete_cache(self):
if self.file:
folder = os.path.dirname(self.file.path)
for f in glob('%s/*' % folder):
if f != self.file.path:
os.unlink(f)
def delete_document(sender, **kwargs): def delete_document(sender, **kwargs):
t = kwargs['instance'] t = kwargs['instance']
if t.file: if t.file:
folder = os.path.dirname(t.file.path) t.delete_cache()
for f in glob('%s/*' % folder):
if f != t.file.path:
os.unlink(f)
t.file.delete(save=False) t.file.delete(save=False)
pre_delete.connect(delete_document, sender=Document) pre_delete.connect(delete_document, sender=Document)

View file

@ -353,10 +353,7 @@ def upload(request):
else: else:
#replace existing file #replace existing file
if file.file: if file.file:
folder = os.path.dirname(file.file.path) file.delete_cache()
for f in glob('%s/*' % folder):
if f != file.file.path:
os.unlink(f)
file.file.delete() file.file.delete()
file.uploading = True file.uploading = True
name, extension = request.POST['filename'].rsplit('.', 1) name, extension = request.POST['filename'].rsplit('.', 1)