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):
19 def save(self, commit=True, path=None):
20 """ Performs normal save, with given file as an source audiobook.
22 `path' is relative to NEW_PATH.
24 m = super(AudiobookForm, self).save(commit=False)
25 m.modified = datetime.now()
28 # adding a new audiobook
29 if not os.path.isdir(FILES_PATH):
30 os.makedirs(FILES_PATH)
31 # save the file in model
33 abs_path = os.path.join(NEW_PATH, path)
36 ExistingFile(abs_path))
38 # f = open(force_bytes(m.source_file.path))
39 # m.source_sha1 = sha1_file(f)