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