1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from django.db import models
6 from django.db.models.fields.files import FieldFile
9 class OverwritingFieldFile(FieldFile):
11 Deletes the old file before saving the new one.
14 def save(self, name, content, *args, **kwargs):
15 leave = kwargs.pop('leave', None)
16 # delete if there's a file already and there's a new one coming
17 if not leave and self and (not hasattr(content, 'path') or
18 content.path != self.path):
19 self.delete(save=False)
20 return super(OverwritingFieldFile, self).save(
21 name, content, *args, **kwargs)
24 class OverwritingFileField(models.FileField):
25 attr_class = OverwritingFieldFile
30 from south.modelsinspector import add_introspection_rules
32 add_introspection_rules([], ["^catalogue\.fields\.OverwritingFileField"])