@@ -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,29 @@ 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+ return super ().form_valid (form )
297+
298+
299+ class AddSourceView (EditSourceMixin , CreateView ):
300+ '''
301+ Adds a new source, optionally takes some initial data querystring values to
302+ prepopulate some of the more unclear values.
303+ '''
304+
305+ template_name = 'sync/source-add.html'
306+
290307 def __init__ (self , * args , ** kwargs ):
291308 self .prepopulated_data = {}
292309 super ().__init__ (* args , ** kwargs )
@@ -312,20 +329,6 @@ def get_initial(self):
312329 initial [k ] = v
313330 return initial
314331
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-
329332 def get_success_url (self ):
330333 url = reverse_lazy ('sync:source' , kwargs = {'pk' : self .object .pk })
331334 return append_uri_params (url , {'message' : 'source-created' })
@@ -364,33 +367,9 @@ def get_context_data(self, *args, **kwargs):
364367 return data
365368
366369
367- class UpdateSourceView (UpdateView ):
370+ class UpdateSourceView (EditSourceMixin , UpdateView ):
368371
369372 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 )
394373
395374 def get_success_url (self ):
396375 url = reverse_lazy ('sync:source' , kwargs = {'pk' : self .object .pk })
@@ -416,7 +395,7 @@ def post(self, request, *args, **kwargs):
416395 for media in Media .objects .filter (source = source ):
417396 if media .media_file :
418397 # Delete the media file
419- delete_file (media .media_file .name )
398+ delete_file (media .media_file .path )
420399 # Delete thumbnail copy if it exists
421400 delete_file (media .thumbpath )
422401 # Delete NFO file if it exists
0 commit comments