Skip to content

Commit

Permalink
source edition refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
biolds committed Jan 17, 2023
1 parent 2f475bf commit 419c4c5
Showing 1 changed file with 25 additions and 46 deletions.
71 changes: 25 additions & 46 deletions tubesync/sync/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,7 @@ def get_success_url(self):
return append_uri_params(url, fields)


class AddSourceView(CreateView):
'''
Adds a new source, optionally takes some initial data querystring values to
prepopulate some of the more unclear values.
'''

template_name = 'sync/source-add.html'
class EditSourceMixin:
model = Source
fields = ('source_type', 'key', 'name', 'directory', 'media_format',
'index_schedule', 'download_media', 'download_cap', 'delete_old_media',
Expand All @@ -287,6 +281,29 @@ class AddSourceView(CreateView):
'this page for valid media name variables'),
}

def form_valid(self, form):
# Perform extra validation to make sure the media_format is valid
obj = form.save(commit=False)
source_type = form.cleaned_data['media_format']
example_media_file = obj.get_example_media_format()
if example_media_file == '':
form.add_error(
'media_format',
ValidationError(self.errors['invalid_media_format'])
)
if form.errors:
return super().form_invalid(form)
return super().form_valid(form)


class AddSourceView(EditSourceMixin, CreateView):
'''
Adds a new source, optionally takes some initial data querystring values to
prepopulate some of the more unclear values.
'''

template_name = 'sync/source-add.html'

def __init__(self, *args, **kwargs):
self.prepopulated_data = {}
super().__init__(*args, **kwargs)
Expand All @@ -312,20 +329,6 @@ def get_initial(self):
initial[k] = v
return initial

def form_valid(self, form):
# Perform extra validation to make sure the media_format is valid
obj = form.save(commit=False)
source_type = form.cleaned_data['media_format']
example_media_file = obj.get_example_media_format()
if example_media_file == '':
form.add_error(
'media_format',
ValidationError(self.errors['invalid_media_format'])
)
if form.errors:
return super().form_invalid(form)
return super().form_valid(form)

def get_success_url(self):
url = reverse_lazy('sync:source', kwargs={'pk': self.object.pk})
return append_uri_params(url, {'message': 'source-created'})
Expand Down Expand Up @@ -364,33 +367,9 @@ def get_context_data(self, *args, **kwargs):
return data


class UpdateSourceView(UpdateView):
class UpdateSourceView(EditSourceMixin, UpdateView):

template_name = 'sync/source-update.html'
model = Source
fields = ('source_type', 'key', 'name', 'directory', 'media_format',
'index_schedule', 'download_media', 'download_cap', 'delete_old_media',
'days_to_keep', 'source_resolution', 'source_vcodec', 'source_acodec',
'prefer_60fps', 'prefer_hdr', 'fallback', 'copy_thumbnails', 'write_nfo', 'write_json')
errors = {
'invalid_media_format': _('Invalid media format, the media format contains '
'errors or is empty. Check the table at the end of '
'this page for valid media name variables'),
}

def form_valid(self, form):
# Perform extra validation to make sure the media_format is valid
obj = form.save(commit=False)
source_type = form.cleaned_data['media_format']
example_media_file = obj.get_example_media_format()
if example_media_file == '':
form.add_error(
'media_format',
ValidationError(self.errors['invalid_media_format'])
)
if form.errors:
return super().form_invalid(form)
return super().form_valid(form)

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

0 comments on commit 419c4c5

Please sign in to comment.