Skip to content

Commit 069d14d

Browse files
committed
source edition refactoring
1 parent 2f475bf commit 069d14d

File tree

1 file changed

+24
-46
lines changed

1 file changed

+24
-46
lines changed

tubesync/sync/views.py

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,7 @@ def get_success_url(self):
269269
return append_uri_params(url, fields)
270270

271271

272-
class AddSourceView(CreateView):
273-
'''
274-
Adds a new source, optionally takes some initial data querystring values to
275-
prepopulate some of the more unclear values.
276-
'''
277-
278-
template_name = 'sync/source-add.html'
272+
class EditSourceMixin:
279273
model = Source
280274
fields = ('source_type', 'key', 'name', 'directory', 'media_format',
281275
'index_schedule', 'download_media', 'download_cap', 'delete_old_media',
@@ -287,6 +281,28 @@ class AddSourceView(CreateView):
287281
'this page for valid media name variables'),
288282
}
289283

284+
def form_valid(self, form):
285+
# Perform extra validation to make sure the media_format is valid
286+
obj = form.save(commit=False)
287+
source_type = form.cleaned_data['media_format']
288+
example_media_file = obj.get_example_media_format()
289+
if example_media_file == '':
290+
form.add_error(
291+
'media_format',
292+
ValidationError(self.errors['invalid_media_format'])
293+
)
294+
if form.errors:
295+
return super().form_invalid(form)
296+
297+
298+
class AddSourceView(EditSourceMixin, CreateView):
299+
'''
300+
Adds a new source, optionally takes some initial data querystring values to
301+
prepopulate some of the more unclear values.
302+
'''
303+
304+
template_name = 'sync/source-add.html'
305+
290306
def __init__(self, *args, **kwargs):
291307
self.prepopulated_data = {}
292308
super().__init__(*args, **kwargs)
@@ -312,20 +328,6 @@ def get_initial(self):
312328
initial[k] = v
313329
return initial
314330

315-
def form_valid(self, form):
316-
# Perform extra validation to make sure the media_format is valid
317-
obj = form.save(commit=False)
318-
source_type = form.cleaned_data['media_format']
319-
example_media_file = obj.get_example_media_format()
320-
if example_media_file == '':
321-
form.add_error(
322-
'media_format',
323-
ValidationError(self.errors['invalid_media_format'])
324-
)
325-
if form.errors:
326-
return super().form_invalid(form)
327-
return super().form_valid(form)
328-
329331
def get_success_url(self):
330332
url = reverse_lazy('sync:source', kwargs={'pk': self.object.pk})
331333
return append_uri_params(url, {'message': 'source-created'})
@@ -364,33 +366,9 @@ def get_context_data(self, *args, **kwargs):
364366
return data
365367

366368

367-
class UpdateSourceView(UpdateView):
369+
class UpdateSourceView(EditSourceMixin, UpdateView):
368370

369371
template_name = 'sync/source-update.html'
370-
model = Source
371-
fields = ('source_type', 'key', 'name', 'directory', 'media_format',
372-
'index_schedule', 'download_media', 'download_cap', 'delete_old_media',
373-
'days_to_keep', 'source_resolution', 'source_vcodec', 'source_acodec',
374-
'prefer_60fps', 'prefer_hdr', 'fallback', 'copy_thumbnails', 'write_nfo', 'write_json')
375-
errors = {
376-
'invalid_media_format': _('Invalid media format, the media format contains '
377-
'errors or is empty. Check the table at the end of '
378-
'this page for valid media name variables'),
379-
}
380-
381-
def form_valid(self, form):
382-
# Perform extra validation to make sure the media_format is valid
383-
obj = form.save(commit=False)
384-
source_type = form.cleaned_data['media_format']
385-
example_media_file = obj.get_example_media_format()
386-
if example_media_file == '':
387-
form.add_error(
388-
'media_format',
389-
ValidationError(self.errors['invalid_media_format'])
390-
)
391-
if form.errors:
392-
return super().form_invalid(form)
393-
return super().form_valid(form)
394372

395373
def get_success_url(self):
396374
url = reverse_lazy('sync:source', kwargs={'pk': self.object.pk})

0 commit comments

Comments
 (0)