App_Data added to .gitignore

Added SetValue to ConfigFileProvider.
Added creating of default config file in ConfigFileProvider.
Added more ConfigFileProvider tests.
Added UI for Settings/System
This commit is contained in:
Mark McDowall 2011-10-01 00:04:06 -07:00
parent f0f706b32c
commit ec6a0e6b7f
9 changed files with 185 additions and 3 deletions

1
.gitignore vendored
View File

@ -29,6 +29,7 @@ _ReSharper*/
[Nn]zbs [Nn]zbs
[Bb]uild/ [Bb]uild/
[Ll]ogs/ [Ll]ogs/
[Aa]pp_Data/
/[Pp]ackage/ /[Pp]ackage/
#NZBDrone specific #NZBDrone specific
*.db *.db

View File

@ -1,4 +1,5 @@
using AutoMoq; using System.IO;
using AutoMoq;
using FluentAssertions; using FluentAssertions;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Core.Providers.Core; using NzbDrone.Core.Providers.Core;
@ -11,6 +12,17 @@ namespace NzbDrone.Core.Test
// ReSharper disable InconsistentNaming // ReSharper disable InconsistentNaming
public class ConfigFileProviderTest : TestBase public class ConfigFileProviderTest : TestBase
{ {
[SetUp]
public void SetUp()
{
//Reset config file
var mocker = new AutoMoqer();
var configFile = mocker.Resolve<ConfigFileProvider>().ConfigFile;
File.Delete(configFile);
mocker.Resolve<ConfigFileProvider>().CreateDefaultConfigFile();
}
[Test] [Test]
public void GetValue_Success() public void GetValue_Success()
{ {
@ -80,5 +92,37 @@ namespace NzbDrone.Core.Test
//Assert //Assert
result.Should().Be(value); result.Should().Be(value);
} }
[Test]
public void SetValue_bool()
{
const string key = "LaunchBrowser";
const bool value = false;
var mocker = new AutoMoqer();
//Act
mocker.Resolve<ConfigFileProvider>().SetValue(key, value);
//Assert
var result = mocker.Resolve<ConfigFileProvider>().LaunchBrowser;
result.Should().Be(value);
}
[Test]
public void SetValue_int()
{
const string key = "Port";
const int value = 12345;
var mocker = new AutoMoqer();
//Act
mocker.Resolve<ConfigFileProvider>().SetValue(key, value);
//Assert
var result = mocker.Resolve<ConfigFileProvider>().Port;
result.Should().Be(value);
}
} }
} }

View File

@ -9,6 +9,7 @@ using NLog;
using NzbDrone.Core.Datastore; using NzbDrone.Core.Datastore;
using NzbDrone.Core.Instrumentation; using NzbDrone.Core.Instrumentation;
using NzbDrone.Core.Providers; using NzbDrone.Core.Providers;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Providers.ExternalNotification; using NzbDrone.Core.Providers.ExternalNotification;
using NzbDrone.Core.Providers.Indexer; using NzbDrone.Core.Providers.Indexer;
using NzbDrone.Core.Providers.Jobs; using NzbDrone.Core.Providers.Jobs;
@ -62,6 +63,7 @@ namespace NzbDrone.Core
_kernel.Get<QualityProvider>().SetupDefaultProfiles(); _kernel.Get<QualityProvider>().SetupDefaultProfiles();
_kernel.Get<QualityTypeProvider>().SetupDefault(); _kernel.Get<QualityTypeProvider>().SetupDefault();
_kernel.Get<ConfigFileProvider>().CreateDefaultConfigFile();
BindExternalNotifications(); BindExternalNotifications();
BindIndexers(); BindIndexers();

View File

@ -18,11 +18,13 @@ namespace NzbDrone.Core.Providers.Core
public virtual int Port public virtual int Port
{ {
get { return GetValueInt("Port"); } get { return GetValueInt("Port"); }
set { SetValue("Port", value); }
} }
public virtual bool LaunchBrowser public virtual bool LaunchBrowser
{ {
get { return GetValueBoolean("LaunchBrowser"); } get { return GetValueBoolean("LaunchBrowser"); }
set { SetValue("LaunchBrowser", value); }
} }
public virtual string GetValue(string key, string parent = null) public virtual string GetValue(string key, string parent = null)
@ -32,7 +34,7 @@ namespace NzbDrone.Core.Providers.Core
var parentContainer = config; var parentContainer = config;
if (parent != null) if (!String.IsNullOrEmpty(parent))
parentContainer = config.Descendants(parent).Single(); parentContainer = config.Descendants(parent).Single();
var value = parentContainer.Descendants(key).Single().Value; var value = parentContainer.Descendants(key).Single().Value;
@ -49,5 +51,44 @@ namespace NzbDrone.Core.Providers.Core
{ {
return Convert.ToBoolean(GetValue(key, parent)); return Convert.ToBoolean(GetValue(key, parent));
} }
public virtual void SetValue(string key, object value, string parent = null)
{
var xDoc = XDocument.Load(ConfigFile);
var config = xDoc.Descendants("Config").Single();
var parentContainer = config;
if (!String.IsNullOrEmpty(parent))
parentContainer = config.Descendants(parent).Single();
parentContainer.Descendants(key).Single().Value = value.ToString();
xDoc.Save(ConfigFile);
}
public virtual void CreateDefaultConfigFile()
{
//Create the config file here
Directory.CreateDirectory(Path.Combine(CentralDispatch.AppPath, "App_Data"));
if (!File.Exists(ConfigFile))
{
WriteDefaultConfig();
}
}
public virtual void WriteDefaultConfig()
{
var xDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
xDoc.Add(new XElement("Config",
new XElement("Port", 8989),
new XElement("LaunchBrowser", true)
)
);
xDoc.Save(ConfigFile);
}
} }
} }

View File

@ -29,15 +29,18 @@ namespace NzbDrone.Web.Controllers
private readonly ExternalNotificationProvider _externalNotificationProvider; private readonly ExternalNotificationProvider _externalNotificationProvider;
private readonly QualityTypeProvider _qualityTypeProvider; private readonly QualityTypeProvider _qualityTypeProvider;
private readonly RootDirProvider _rootDirProvider; private readonly RootDirProvider _rootDirProvider;
private readonly ConfigFileProvider _configFileProvider;
public SettingsController(ConfigProvider configProvider, IndexerProvider indexerProvider, public SettingsController(ConfigProvider configProvider, IndexerProvider indexerProvider,
QualityProvider qualityProvider, AutoConfigureProvider autoConfigureProvider, QualityProvider qualityProvider, AutoConfigureProvider autoConfigureProvider,
SeriesProvider seriesProvider, ExternalNotificationProvider externalNotificationProvider, SeriesProvider seriesProvider, ExternalNotificationProvider externalNotificationProvider,
QualityTypeProvider qualityTypeProvider, RootDirProvider rootDirProvider) QualityTypeProvider qualityTypeProvider, RootDirProvider rootDirProvider,
ConfigFileProvider configFileProvider)
{ {
_externalNotificationProvider = externalNotificationProvider; _externalNotificationProvider = externalNotificationProvider;
_qualityTypeProvider = qualityTypeProvider; _qualityTypeProvider = qualityTypeProvider;
_rootDirProvider = rootDirProvider; _rootDirProvider = rootDirProvider;
_configFileProvider = configFileProvider;
_configProvider = configProvider; _configProvider = configProvider;
_indexerProvider = indexerProvider; _indexerProvider = indexerProvider;
_qualityProvider = qualityProvider; _qualityProvider = qualityProvider;
@ -184,6 +187,15 @@ namespace NzbDrone.Web.Controllers
return View(model); return View(model);
} }
public ActionResult System()
{
var model = new SystemSettingsModel();
model.Port = _configFileProvider.Port;
model.LaunchBrowser = _configFileProvider.LaunchBrowser;
return View(model);
}
public ViewResult AddProfile() public ViewResult AddProfile()
{ {
var qualityTypes = new List<QualityTypes>(); var qualityTypes = new List<QualityTypes>();
@ -436,6 +448,20 @@ namespace NzbDrone.Web.Controllers
return GetInvalidModelResult(); return GetInvalidModelResult();
} }
[HttpPost]
public JsonResult SaveSystem(SystemSettingsModel data)
{
if (ModelState.IsValid)
{
_configFileProvider.Port = data.Port;
_configFileProvider.LaunchBrowser = data.LaunchBrowser;
return GetSuccessResult();
}
return GetInvalidModelResult();
}
private JsonResult GetSuccessResult() private JsonResult GetSuccessResult()
{ {
return Json(new NotificationResult() { Title = "Settings Saved" }); return Json(new NotificationResult() { Title = "Settings Saved" });

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace NzbDrone.Web.Models
{
public class SystemSettingsModel
{
[DisplayName("Port")]
[Description("Port that NzbDrone runs on")]
[Range(1, 65535, ErrorMessage = "Port must be between 1 and 65535")]
public int Port { get; set; }
[DisplayName("Launch Browser")]
[Description("Start default webrowser when NzbDrone starts?")]
public bool LaunchBrowser { get; set; }
}
}

View File

@ -510,6 +510,7 @@
<Compile Include="Models\SeriesModel.cs" /> <Compile Include="Models\SeriesModel.cs" />
<Compile Include="Models\SeriesSearchResultModel.cs" /> <Compile Include="Models\SeriesSearchResultModel.cs" />
<Compile Include="Models\SettingsModels.cs" /> <Compile Include="Models\SettingsModels.cs" />
<Compile Include="Models\SystemSettingsModel.cs" />
<Compile Include="Models\TvDbSearchResultModel.cs" /> <Compile Include="Models\TvDbSearchResultModel.cs" />
<Compile Include="Models\UpcomingEpisodeModel.cs" /> <Compile Include="Models\UpcomingEpisodeModel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
@ -923,6 +924,9 @@
<LastGenOutput>EditorLocalization.bg-BG.designer.cs</LastGenOutput> <LastGenOutput>EditorLocalization.bg-BG.designer.cs</LastGenOutput>
</Content> </Content>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Content Include="Views\Settings\System.cshtml" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" /> <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@ -4,6 +4,7 @@
<li>@Html.ActionLink("Quality", "Quality", "Settings")</li> <li>@Html.ActionLink("Quality", "Quality", "Settings")</li>
<li>@Html.ActionLink("Episode Sorting", "EpisodeSorting", "Settings")</li> <li>@Html.ActionLink("Episode Sorting", "EpisodeSorting", "Settings")</li>
<li>@Html.ActionLink("Notifications", "Notifications", "Settings")</li> <li>@Html.ActionLink("Notifications", "Notifications", "Settings")</li>
<li>@Html.ActionLink("System", "System", "Settings")</li>
</ul> </ul>
<div style="margin-bottom: 10px"></div> <div style="margin-bottom: 10px"></div>

View File

@ -0,0 +1,42 @@
@using NzbDrone.Web.Helpers
@model NzbDrone.Web.Models.SystemSettingsModel
@section HeaderContent{
<link rel="stylesheet" type="text/css" href="../../Content/Settings.css" />
}
@section TitleContent{
Settings
}
@section ActionMenu{
@{Html.RenderPartial("SubMenu");}
}
@section MainContent{
<div id="stylized">
@using (Html.BeginForm("SaveSystem", "Settings", FormMethod.Post, new { id = "form", name = "form", @class = "settingsForm" }))
{
@Html.ValidationSummary(true, "Unable to save your settings. Please correct the errors and try again.")
<h1>System</h1>
<p></p>
<label class="labelClass">@Html.LabelFor(m => m.LaunchBrowser)
<span class="small">@Html.DescriptionFor(m => m.LaunchBrowser)</span>
</label>
@Html.CheckBoxFor(m => m.LaunchBrowser, new { @class = "inputClass checkClass" })
<label class="labelClass">@Html.LabelFor(m => m.Port)
<span class="small">@Html.DescriptionFor(m => m.Port)</span>
</label>
@Html.TextBoxFor(m => m.Port, new { @class = "inputClass" })
<button type="submit" id="save_button" disabled="disabled">Save</button>
}
</div>
<div id="result" class="hiddenResult"></div>
}
@section Scripts{
<script src="/Scripts/settingsForm.js" type="text/javascript"></script>
}