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