1 from datetime import datetime
5 from django import forms
6 from django.utils.translation import ugettext_lazy as _
9 from archive.models import Audiobook
10 from archive.settings import FILES_PATH, NEW_PATH
11 from archive.utils import ExistingFile, sha1_file
13 class AudiobookForm(forms.ModelForm):
17 def save(self, commit=True, path=None):
18 """ Performs normal save, with given file as an source audiobook.
20 `path' is relative to NEW_PATH.
22 m = super(AudiobookForm, self).save(commit=False)
23 m.modified = datetime.now()
26 # adding a new audiobook
27 if not os.path.isdir(FILES_PATH):
28 os.makedirs(FILES_PATH)
29 # save the file in model
31 abs_path = os.path.join(NEW_PATH, path)
34 ExistingFile(abs_path))
36 f = open(m.source_file.path)
37 m.source_sha1 = sha1_file(f)