From f058b8f3642cbf759d0e33c9a1700d666dbeccba Mon Sep 17 00:00:00 2001 From: Qstick Date: Mon, 21 May 2018 04:32:20 -0400 Subject: [PATCH] =?UTF-8?q?Fixed:=20Don=E2=80=99t=20allow=20profiles=20in?= =?UTF-8?q?=20use=20by=20lists=20to=20be=20deleted=20(#2797)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Profiles/ProfileServiceFixture.cs | 39 +++++++++++++++++-- src/NzbDrone.Core/Profiles/ProfileService.cs | 11 ++++-- .../Profile/Edit/EditProfileLayout.js | 13 ++++++- 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/src/NzbDrone.Core.Test/Profiles/ProfileServiceFixture.cs b/src/NzbDrone.Core.Test/Profiles/ProfileServiceFixture.cs index 437a2d576..4fceebe4a 100644 --- a/src/NzbDrone.Core.Test/Profiles/ProfileServiceFixture.cs +++ b/src/NzbDrone.Core.Test/Profiles/ProfileServiceFixture.cs @@ -1,4 +1,4 @@ -using System.Linq; +using System.Linq; using FizzWare.NBuilder; using Moq; using NUnit.Framework; @@ -6,6 +6,7 @@ using NzbDrone.Core.Lifecycle; using NzbDrone.Core.Profiles; using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Movies; +using NzbDrone.Core.NetImport; namespace NzbDrone.Core.Test.Profiles { @@ -46,8 +47,35 @@ namespace NzbDrone.Core.Test.Profiles .With(c => c.ProfileId = 2) .Build().ToList(); + var netImportList = Builder.CreateListOfSize(3) + .All() + .With(c => c.ProfileId = 1) + .Build().ToList(); Mocker.GetMock().Setup(c => c.GetAllMovies()).Returns(movieList); + Mocker.GetMock().Setup(c => c.All()).Returns(netImportList); + + Assert.Throws(() => Subject.Delete(2)); + + Mocker.GetMock().Verify(c => c.Delete(It.IsAny()), Times.Never()); + + } + + [Test] + public void should_not_be_able_to_delete_profile_if_assigned_to_list() + { + var movieList = Builder.CreateListOfSize(3) + .All() + .With(c => c.ProfileId = 1) + .Build().ToList(); + + var netImportList = Builder.CreateListOfSize(3) + .Random(1) + .With(c => c.ProfileId = 2) + .Build().ToList(); + + Mocker.GetMock().Setup(c => c.GetAllMovies()).Returns(movieList); + Mocker.GetMock().Setup(c => c.All()).Returns(netImportList); Assert.Throws(() => Subject.Delete(2)); @@ -57,19 +85,24 @@ namespace NzbDrone.Core.Test.Profiles [Test] - public void should_delete_profile_if_not_assigned_to_movie() + public void should_delete_profile_if_not_assigned_to_movie_or_list() { var movieList = Builder.CreateListOfSize(3) .All() .With(c => c.ProfileId = 2) .Build().ToList(); + var netImportList = Builder.CreateListOfSize(3) + .All() + .With(c => c.ProfileId = 2) + .Build().ToList(); Mocker.GetMock().Setup(c => c.GetAllMovies()).Returns(movieList); + Mocker.GetMock().Setup(c => c.All()).Returns(netImportList); Subject.Delete(1); Mocker.GetMock().Verify(c => c.Delete(1), Times.Once()); } } -} \ No newline at end of file +} diff --git a/src/NzbDrone.Core/Profiles/ProfileService.cs b/src/NzbDrone.Core/Profiles/ProfileService.cs index 69421e5c4..62a25911c 100644 --- a/src/NzbDrone.Core/Profiles/ProfileService.cs +++ b/src/NzbDrone.Core/Profiles/ProfileService.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using NLog; using NzbDrone.Core.Lifecycle; @@ -6,6 +6,7 @@ using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Parser; using NzbDrone.Core.Qualities; using NzbDrone.Core.Movies; +using NzbDrone.Core.NetImport; namespace NzbDrone.Core.Profiles { @@ -23,12 +24,14 @@ namespace NzbDrone.Core.Profiles { private readonly IProfileRepository _profileRepository; private readonly IMovieService _movieService; + private readonly INetImportFactory _netImportFactory; private readonly Logger _logger; - public ProfileService(IProfileRepository profileRepository, IMovieService movieService, Logger logger) + public ProfileService(IProfileRepository profileRepository, IMovieService movieService, INetImportFactory netImportFactory, Logger logger) { _profileRepository = profileRepository; _movieService = movieService; + _netImportFactory = netImportFactory; _logger = logger; } @@ -44,7 +47,7 @@ namespace NzbDrone.Core.Profiles public void Delete(int id) { - if (_movieService.GetAllMovies().Any(c => c.ProfileId == id)) + if (_movieService.GetAllMovies().Any(c => c.ProfileId == id) || _netImportFactory.All().Any(c => c.ProfileId == id)) { throw new ProfileInUseException(id); } @@ -153,4 +156,4 @@ namespace NzbDrone.Core.Profiles ); } } -} \ No newline at end of file +} diff --git a/src/UI/Settings/Profile/Edit/EditProfileLayout.js b/src/UI/Settings/Profile/Edit/EditProfileLayout.js index 14770eafb..5390a144a 100644 --- a/src/UI/Settings/Profile/Edit/EditProfileLayout.js +++ b/src/UI/Settings/Profile/Edit/EditProfileLayout.js @@ -8,6 +8,7 @@ var QualitySortableCollectionView = require('./QualitySortableCollectionView'); var EditProfileView = require('./EditProfileView'); var DeleteView = require('../DeleteProfileView'); var FullMovieCollection = require('../../../Movies/FullMovieCollection'); +var NetImportCollection = require('../../NetImport/NetImportCollection'); var Config = require('../../../Config'); var AsEditModalView = require('../../../Mixins/AsEditModalView'); @@ -28,7 +29,10 @@ var view = Marionette.Layout.extend({ initialize : function(options) { this.profileCollection = options.profileCollection; this.itemsCollection = new Backbone.Collection(_.toArray(this.model.get('items')).reverse()); + this.netImportCollection = new NetImportCollection; + this.netImportCollection.fetch(); this.listenTo(FullMovieCollection, 'all', this._updateDisableStatus); + this.listenTo(this.netImportCollection, 'all', this._updateDisableStatus); }, onRender : function() { @@ -102,9 +106,10 @@ var view = Marionette.Layout.extend({ }, _updateDisableStatus : function() { - if (this._isQualityInUse()) { + if (this._isQualityInUse() || this._isQualityInUsebyList()) { + this.ui.deleteButton.attr('disabled', 'disabled'); this.ui.deleteButton.addClass('disabled'); - this.ui.deleteButton.attr('title', 'Can\'t delete a profile that is attached to a movie.'); + this.ui.deleteButton.attr('title', 'Can\'t delete a profile that is attached to a movie or list.'); } else { this.ui.deleteButton.removeClass('disabled'); } @@ -112,6 +117,10 @@ var view = Marionette.Layout.extend({ _isQualityInUse : function() { return FullMovieCollection.where({ 'profileId' : this.model.id }).length !== 0; + }, + + _isQualityInUsebyList : function() { + return this.netImportCollection.where({ 'profileId' : this.model.id }).length !== 0; } }); module.exports = AsEditModalView.call(view);