1 from datetime import datetime
5 from django import forms
6 from django.utils.translation import ugettext_lazy as _
8 from django.utils.encoding import force_bytes
10 from archive.models import Audiobook
11 from archive.settings import FILES_PATH, NEW_PATH
12 from archive.utils import ExistingFile, sha1_file
14 class AudiobookForm(forms.ModelForm):
18 def save(self, commit=True, path=None):
19 """ Performs normal save, with given file as an source audiobook.
21 `path' is relative to NEW_PATH.
23 m = super(AudiobookForm, self).save(commit=False)
24 m.modified = datetime.now()
27 # adding a new audiobook
28 if not os.path.isdir(FILES_PATH):
29 os.makedirs(FILES_PATH)
30 # save the file in model
32 abs_path = os.path.join(NEW_PATH, path)
35 ExistingFile(abs_path))
37 # f = open(force_bytes(m.source_file.path))
38 # m.source_sha1 = sha1_file(f)