From 6e66a7a27fda54574f4490fd504ba98d6cc043f8 Mon Sep 17 00:00:00 2001 From: markus101 Date: Thu, 10 Feb 2011 17:22:29 -0800 Subject: [PATCH] Working on validation for forms, issues with server side, not sure how to post back model with AJAX submit, yet. Split out settings model to support validation. --- NzbDrone.Core/NzbDrone.Core.csproj | 1 + .../Repository/Quality/QualityProfile.cs | 2 + .../Controllers/SettingsController.cs | 133 ++++---- NzbDrone.Web/Models/DownloadSettingsModel.cs | 101 ++++++ NzbDrone.Web/Models/IndexerSettingsModel.cs | 67 ++++ NzbDrone.Web/Models/QualityModel.cs | 1 + NzbDrone.Web/Models/SettingsModels.cs | 300 +++++++++--------- NzbDrone.Web/NzbDrone.Web.csproj | 2 + NzbDrone.Web/Views/Settings/Downloads.ascx | 16 +- NzbDrone.Web/Views/Settings/Indexers.ascx | 2 +- .../Views/Settings/UserProfileSection.ascx | 4 +- 11 files changed, 402 insertions(+), 227 deletions(-) create mode 100644 NzbDrone.Web/Models/DownloadSettingsModel.cs create mode 100644 NzbDrone.Web/Models/IndexerSettingsModel.cs diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index eb6424ab9..63e65c0c4 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -142,6 +142,7 @@ D:\OpenSource\sabscripts\SABSync\References\SubSonic.Core.dll + diff --git a/NzbDrone.Core/Repository/Quality/QualityProfile.cs b/NzbDrone.Core/Repository/Quality/QualityProfile.cs index 43e0c7f00..2ddfc0cc0 100644 --- a/NzbDrone.Core/Repository/Quality/QualityProfile.cs +++ b/NzbDrone.Core/Repository/Quality/QualityProfile.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.ComponentModel.DataAnnotations; using SubSonic.SqlGeneration.Schema; namespace NzbDrone.Core.Repository.Quality @@ -23,6 +24,7 @@ namespace NzbDrone.Core.Repository.Quality public string AllowedString { get; set; } [DisplayName("Cutoff")] + [Required(ErrorMessage = "Valid Cutoff is Required")] public QualityTypes Cutoff { get; set; } [EditorBrowsable(EditorBrowsableState.Never)] diff --git a/NzbDrone.Web/Controllers/SettingsController.cs b/NzbDrone.Web/Controllers/SettingsController.cs index 3fa24ca7e..f08b63b83 100644 --- a/NzbDrone.Web/Controllers/SettingsController.cs +++ b/NzbDrone.Web/Controllers/SettingsController.cs @@ -5,6 +5,7 @@ using System.Threading; using System.Web; using System.Web.Mvc; using NLog; +using NzbDrone.Core.Model; using NzbDrone.Core.Providers; using NzbDrone.Core.Repository.Quality; using NzbDrone.Web.Models; @@ -53,7 +54,7 @@ namespace NzbDrone.Web.Controllers public ActionResult Indexers() { ViewData["viewName"] = "Indexers"; - return View("Index", new SettingsModel + return View("Index", new IndexerSettingsModel { NzbMatrixUsername = _configProvider.GetValue("NzbMatrixUsername", String.Empty, false), NzbMatrixApiKey = _configProvider.GetValue("NzbMatrixApiKey", String.Empty, false), @@ -68,30 +69,22 @@ namespace NzbDrone.Web.Controllers public ActionResult Downloads() { ViewData["viewName"] = "Downloads"; - return View("Index", new SettingsModel - { - //Sync Frequency - //Download Propers? - //Retention - //SAB Host/IP - //SAB Port - //SAB APIKey - //SAB Username - //SAB Password - //SAB Category - //SAB Priority - - SyncFrequency = Convert.ToInt32(_configProvider.GetValue("SyncFrequency", "15", true)), - DownloadPropers = Convert.ToBoolean(_configProvider.GetValue("DownloadPropers", "false", true)), - Retention = Convert.ToInt32(_configProvider.GetValue("Retention", "500", true)), - SabHost = _configProvider.GetValue("SabHost", "localhost", false), - SabPort = Convert.ToInt32(_configProvider.GetValue("SabPort", "8080", true)), - SabApiKey = _configProvider.GetValue("SabApiKey", String.Empty, false), - SabUsername = _configProvider.GetValue("SabUsername", String.Empty, false), - SabPassword = _configProvider.GetValue("SabPassword", String.Empty, false), - SabCategory = _configProvider.GetValue("SabCategory", String.Empty, false), - //SabPriority = _configProvider.GetValue("SabPriority", String.Empty, false) - }); + + var model = new DownloadSettingsModel + { + SyncFrequency = Convert.ToInt32(_configProvider.GetValue("SyncFrequency", "15", true)), + DownloadPropers = Convert.ToBoolean(_configProvider.GetValue("DownloadPropers", "false", true)), + Retention = Convert.ToInt32(_configProvider.GetValue("Retention", "500", true)), + SabHost = _configProvider.GetValue("SabHost", "localhost", false), + SabPort = Convert.ToInt32(_configProvider.GetValue("SabPort", "8080", true)), + SabApiKey = _configProvider.GetValue("SabApiKey", String.Empty, false), + SabUsername = _configProvider.GetValue("SabUsername", String.Empty, false), + SabPassword = _configProvider.GetValue("SabPassword", String.Empty, false), + SabCategory = _configProvider.GetValue("SabCategory", String.Empty, false), + SabPriority = (SabnzbdPriorityType)Enum.Parse(typeof(SabnzbdPriorityType), _configProvider.GetValue("SabPriority", "Normal", true)), + }; + + return View("Index", model); } public ActionResult Quality() @@ -109,7 +102,7 @@ namespace NzbDrone.Web.Controllers var userProfiles = _qualityProvider.GetAllProfiles().Where(q => q.UserProfile).ToList(); var profiles = _qualityProvider.GetAllProfiles().ToList(); - + var defaultQualityProfileId = Convert.ToInt32(_configProvider.GetValue("DefaultQualityProfile", profiles[0].ProfileId, true)); var selectList = new SelectList(profiles, "ProfileId", "Name"); @@ -150,10 +143,6 @@ namespace NzbDrone.Web.Controllers try { _configProvider.SeriesRoot = data.TvFolder; - _configProvider.SetValue("NzbMatrixUsername", data.NzbMatrixUsername); - _configProvider.SetValue("NzbMatrixApiKey", data.NzbMatrixApiKey); - _configProvider.SetValue("NzbsOrgUId", data.NzbsOrgUId); - _configProvider.SetValue("NzbsOrgHash", data.NzbsOrgHash); } catch (Exception) { @@ -191,7 +180,7 @@ namespace NzbDrone.Web.Controllers return Content("Error Saving Settings, please fix any errors"); } - + if (Request.IsAjaxRequest()) return Content("Settings Saved."); @@ -199,7 +188,7 @@ namespace NzbDrone.Web.Controllers } [HttpPost] - public ActionResult SaveIndexers(SettingsModel data) + public ActionResult SaveIndexers(IndexerSettingsModel data) { try { @@ -245,52 +234,59 @@ namespace NzbDrone.Web.Controllers } [HttpPost] - public ActionResult SaveDownloads(SettingsModel data) + public ActionResult SaveDownloads(DownloadSettingsModel data) { - try + if (ModelState.IsValid) { - if (data.SyncFrequency > 15) - _configProvider.SetValue("SyncFrequency", data.SyncFrequency.ToString()); + try + { + if (data.SyncFrequency > 15) + _configProvider.SetValue("SyncFrequency", data.SyncFrequency.ToString()); - _configProvider.SetValue("DownloadPropers", data.DownloadPropers.ToString()); + _configProvider.SetValue("DownloadPropers", data.DownloadPropers.ToString()); - if (data.Retention > 0) - _configProvider.SetValue("Retention", data.Retention.ToString()); + if (data.Retention > 0) + _configProvider.SetValue("Retention", data.Retention.ToString()); - if (data.SabHost != null) - _configProvider.SetValue("SabHost", data.SabHost); + if (data.SabHost != null) + _configProvider.SetValue("SabHost", data.SabHost); - if (data.SabPort > 0) - _configProvider.SetValue("SabPort", data.SabPort.ToString()); + if (data.SabPort > 0) + _configProvider.SetValue("SabPort", data.SabPort.ToString()); - if (data.SabApiKey != null) - _configProvider.SetValue("SabApiKey", data.SabApiKey); + if (data.SabApiKey != null) + _configProvider.SetValue("SabApiKey", data.SabApiKey); - if (data.SabUsername != null) - _configProvider.SetValue("SabUsername", data.SabUsername); + if (data.SabUsername != null) + _configProvider.SetValue("SabUsername", data.SabUsername); - if (data.SabPassword != null) - _configProvider.SetValue("SabPassword", data.SabPassword); + if (data.SabPassword != null) + _configProvider.SetValue("SabPassword", data.SabPassword); - if (data.SabCategory != null) - _configProvider.SetValue("SabCategory", data.SabCategory); + if (data.SabCategory != null) + _configProvider.SetValue("SabCategory", data.SabCategory); - //if (data.SabPriority != null) - // _configProvider.SetValue("SabPriority", data.SabPriority.ToString()); - } - catch (Exception e) - { - Logger.ErrorException(e.Message, e); - if (Request.IsAjaxRequest()) - return Content("Error Saving Settings, please fix any errors"); + _configProvider.SetValue("SabPriority", data.SabPriority.ToString()); - return Content("Error Saving Settings, please fix any errors"); - } + if (Request.IsAjaxRequest()) + return Content("Settings Saved."); - if (Request.IsAjaxRequest()) - return Content("Settings Saved."); + return Content("Settings Saved."); + } - return Content("Settings Saved."); + catch (Exception e) + { + Logger.ErrorException(e.Message, e); + if (Request.IsAjaxRequest()) + return Content("Error Saving Settings, please fix any errors"); + + return Content("Error Saving Settings, please fix any errors"); + } + } + + //ViewData["viewName"] = "Downloads"; + //return View("Index", data); + return Content("Error Saving Settings, please fix any errors"); } [HttpPost] @@ -314,14 +310,14 @@ namespace NzbDrone.Web.Controllers profile.Allowed = new List(); foreach (var quality in profile.AllowedString.Split(',')) { - var qType = (QualityTypes)Enum.Parse(typeof (QualityTypes), quality); + var qType = (QualityTypes)Enum.Parse(typeof(QualityTypes), quality); profile.Allowed.Add(qType); } //If the Cutoff value selected is not in the allowed list then use the last allowed value, this should be validated on submit if (!profile.Allowed.Contains(profile.Cutoff)) throw new InvalidOperationException("Invalid Cutoff Value"); - //profile.Cutoff = profile.Allowed.Last(); + //profile.Cutoff = profile.Allowed.Last(); if (profile.ProfileId > 0) _qualityProvider.Update(profile); @@ -334,6 +330,7 @@ namespace NzbDrone.Web.Controllers catch (Exception e) { Logger.ErrorException(e.Message, e); + if (Request.IsAjaxRequest()) return Content("Error Saving Settings, please fix any errors"); @@ -347,9 +344,9 @@ namespace NzbDrone.Web.Controllers } [HttpPost] - public ActionResult SortedList(List items) + public ActionResult SortedList(List items) { return Content("Settings Saved."); - } + } } } diff --git a/NzbDrone.Web/Models/DownloadSettingsModel.cs b/NzbDrone.Web/Models/DownloadSettingsModel.cs new file mode 100644 index 000000000..559ebd3f7 --- /dev/null +++ b/NzbDrone.Web/Models/DownloadSettingsModel.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Web; +using System.Web.Mvc; +using NzbDrone.Core.Model; + +namespace NzbDrone.Web.Models +{ + public class DownloadSettingsModel + { + [Required] + [Range(15, 120, ErrorMessage = "Must be between 15 and 120 minutes")] + [DataType(DataType.Text)] + [DisplayName("Sync Frequency")] + public int SyncFrequency + { + get; + set; + } + + [DisplayName("Download Propers")] + public bool DownloadPropers + { + get; + set; + } + + [Required (ErrorMessage = "Please enter a valid number")] + [DataType(DataType.Text)] + [DisplayName("Retention")] + public int Retention + { + get; + set; + } + + [Required (ErrorMessage = "Please enter a valid host")] + [DataType(DataType.Text)] + [DisplayName("SABnzbd Host")] + public String SabHost + { + get; + set; + } + + [Required(ErrorMessage = "Please enter a valid port")] + [DataType(DataType.Text)] + [DisplayName("SABnzbd Port")] + public int SabPort + { + get; + set; + } + + [DataType(DataType.Text)] + [DisplayName("SABnzbd API Key")] + public String SabApiKey + { + get; + set; + } + + [DataType(DataType.Text)] + [DisplayName("SABnzbd Username")] + public String SabUsername + { + get; + set; + } + + [DataType(DataType.Text)] + [DisplayName("SABnzbd Password")] + public String SabPassword + { + get; + set; + } + + [DataType(DataType.Text)] + [DisplayName("SABnzbd Category")] + public String SabCategory + { + get; + set; + } + + [Required(ErrorMessage = "Please select a valid priority")] + [DataType(DataType.Text)] + [DisplayName("SABnzbd Priority")] + public SabnzbdPriorityType SabPriority + { + get; + set; + } + + public SelectList PrioritySelectList = new SelectList(new string[] { "Low", "Normal", "High" }); + } +} \ No newline at end of file diff --git a/NzbDrone.Web/Models/IndexerSettingsModel.cs b/NzbDrone.Web/Models/IndexerSettingsModel.cs new file mode 100644 index 000000000..6db99f55a --- /dev/null +++ b/NzbDrone.Web/Models/IndexerSettingsModel.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Web; +using NzbDrone.Core.Repository; + +namespace NzbDrone.Web.Models +{ + public class IndexerSettingsModel + { + [DataType(DataType.Text)] + [DisplayName("NZBMatrix Username")] + public String NzbMatrixUsername + { + get; + set; + } + + [DataType(DataType.Text)] + [DisplayName("NZBMatrix API Key")] + public String NzbMatrixApiKey + { + get; + set; + } + + [DataType(DataType.Text)] + [DisplayName("NZBs.Org UID")] + public String NzbsOrgUId + { + get; + set; + } + + [DataType(DataType.Text)] + [DisplayName("NZBs.Org Hash")] + public String NzbsOrgHash + { + get; + set; + } + + [DataType(DataType.Text)] + [DisplayName("NZBsRus UID")] + public String NzbsrusUId + { + get; + set; + } + + [DataType(DataType.Text)] + [DisplayName("NZBsRus Hash")] + public String NzbsrusHash + { + get; + set; + } + + public List Indexers + { + get; + set; + } + } +} \ No newline at end of file diff --git a/NzbDrone.Web/Models/QualityModel.cs b/NzbDrone.Web/Models/QualityModel.cs index 4f000944a..58f9b7951 100644 --- a/NzbDrone.Web/Models/QualityModel.cs +++ b/NzbDrone.Web/Models/QualityModel.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; using System.Web.Mvc; diff --git a/NzbDrone.Web/Models/SettingsModels.cs b/NzbDrone.Web/Models/SettingsModels.cs index 381ff0e3f..5dcaabe42 100644 --- a/NzbDrone.Web/Models/SettingsModels.cs +++ b/NzbDrone.Web/Models/SettingsModels.cs @@ -30,156 +30,156 @@ namespace NzbDrone.Web.Models #endregion - #region Indexer Settings - - [DataType(DataType.Text)] - [DisplayName("NZBMatrix Username")] - public String NzbMatrixUsername - { - get; - set; - } - - [DataType(DataType.Text)] - [DisplayName("NZBMatrix API Key")] - public String NzbMatrixApiKey - { - get; - set; - } - - [DataType(DataType.Text)] - [DisplayName("NZBs.Org UID")] - public String NzbsOrgUId - { - get; - set; - } - - [DataType(DataType.Text)] - [DisplayName("NZBs.Org Hash")] - public String NzbsOrgHash - { - get; - set; - } - - [DataType(DataType.Text)] - [DisplayName("NZBsRus UID")] - public String NzbsrusUId - { - get; - set; - } - - [DataType(DataType.Text)] - [DisplayName("NZBsRus Hash")] - public String NzbsrusHash - { - get; - set; - } - - public List Indexers - { - get; - set; - } - #endregion - - #region Download Settings - - //Sync Frequency - //Download Propers? - //Retention - //SAB Host/IP - //SAB Port - //SAB APIKey - //SAB Username - //SAB Password - //SAB Category - //SAB Priority - - [DataType(DataType.Text)] - [DisplayName("Sync Frequency")] - public int SyncFrequency - { - get; - set; - } - - [DisplayName("Download Propers")] - public bool DownloadPropers - { - get; - set; - } - - [DataType(DataType.Text)] - [DisplayName("Retention")] - public int Retention - { - get; - set; - } - - [DataType(DataType.Text)] - [DisplayName("SABnzbd Host")] - public String SabHost - { - get; - set; - } - - [DataType(DataType.Text)] - [DisplayName("SABnzbd Port")] - public int SabPort - { - get; - set; - } - - [DataType(DataType.Text)] - [DisplayName("SABnzbd API Key")] - public String SabApiKey - { - get; - set; - } - - [DataType(DataType.Text)] - [DisplayName("SABnzbd Username")] - public String SabUsername - { - get; - set; - } - - [DataType(DataType.Text)] - [DisplayName("SABnzbd Password")] - public String SabPassword - { - get; - set; - } - - [DataType(DataType.Text)] - [DisplayName("SABnzbd Category")] - public String SabCategory - { - get; - set; - } - - [DataType(DataType.Text)] - [DisplayName("SABnzbd Priority")] - public SabnzbdPriorityType SabPriority - { - get; - set; - } - - #endregion + //#region Indexer Settings + + //[DataType(DataType.Text)] + //[DisplayName("NZBMatrix Username")] + //public String NzbMatrixUsername + //{ + // get; + // set; + //} + + //[DataType(DataType.Text)] + //[DisplayName("NZBMatrix API Key")] + //public String NzbMatrixApiKey + //{ + // get; + // set; + //} + + //[DataType(DataType.Text)] + //[DisplayName("NZBs.Org UID")] + //public String NzbsOrgUId + //{ + // get; + // set; + //} + + //[DataType(DataType.Text)] + //[DisplayName("NZBs.Org Hash")] + //public String NzbsOrgHash + //{ + // get; + // set; + //} + + //[DataType(DataType.Text)] + //[DisplayName("NZBsRus UID")] + //public String NzbsrusUId + //{ + // get; + // set; + //} + + //[DataType(DataType.Text)] + //[DisplayName("NZBsRus Hash")] + //public String NzbsrusHash + //{ + // get; + // set; + //} + + //public List Indexers + //{ + // get; + // set; + //} + //#endregion + + //#region Download Settings + + ////Sync Frequency + ////Download Propers? + ////Retention + ////SAB Host/IP + ////SAB Port + ////SAB APIKey + ////SAB Username + ////SAB Password + ////SAB Category + ////SAB Priority + + //[DataType(DataType.Text)] + //[DisplayName("Sync Frequency")] + //public int SyncFrequency + //{ + // get; + // set; + //} + + //[DisplayName("Download Propers")] + //public bool DownloadPropers + //{ + // get; + // set; + //} + + //[DataType(DataType.Text)] + //[DisplayName("Retention")] + //public int Retention + //{ + // get; + // set; + //} + + //[DataType(DataType.Text)] + //[DisplayName("SABnzbd Host")] + //public String SabHost + //{ + // get; + // set; + //} + + //[DataType(DataType.Text)] + //[DisplayName("SABnzbd Port")] + //public int SabPort + //{ + // get; + // set; + //} + + //[DataType(DataType.Text)] + //[DisplayName("SABnzbd API Key")] + //public String SabApiKey + //{ + // get; + // set; + //} + + //[DataType(DataType.Text)] + //[DisplayName("SABnzbd Username")] + //public String SabUsername + //{ + // get; + // set; + //} + + //[DataType(DataType.Text)] + //[DisplayName("SABnzbd Password")] + //public String SabPassword + //{ + // get; + // set; + //} + + //[DataType(DataType.Text)] + //[DisplayName("SABnzbd Category")] + //public String SabCategory + //{ + // get; + // set; + //} + + //[DataType(DataType.Text)] + //[DisplayName("SABnzbd Priority")] + //public SabnzbdPriorityType SabPriority + //{ + // get; + // set; + //} + + //#endregion } } diff --git a/NzbDrone.Web/NzbDrone.Web.csproj b/NzbDrone.Web/NzbDrone.Web.csproj index e3db921e6..2fd92b3f9 100644 --- a/NzbDrone.Web/NzbDrone.Web.csproj +++ b/NzbDrone.Web/NzbDrone.Web.csproj @@ -85,6 +85,8 @@ + + diff --git a/NzbDrone.Web/Views/Settings/Downloads.ascx b/NzbDrone.Web/Views/Settings/Downloads.ascx index 8b7a271c5..84ed0ea3d 100644 --- a/NzbDrone.Web/Views/Settings/Downloads.ascx +++ b/NzbDrone.Web/Views/Settings/Downloads.ascx @@ -1,4 +1,4 @@ -<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> +<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> + + + <% Html.EnableClientValidation(); %> + <% using (Html.BeginForm("SaveDownloads", "Settings", FormMethod.Post, new { id = "form", name = "form" })) {%> <%: Html.ValidationSummary(true, "Unable to save your settings. Please correct the errors and try again.") %> @@ -103,13 +107,11 @@
<%= Html.ValidationMessageFor(m => m.SabCategory)%>
- <%--
- <%= Html.DropDownListFor(m => m.SabPriority) %> +
+
<%= Html.LabelFor(m => m.SabPriority) %>
+
<%= Html.DropDownListFor(m => m.SabPriority, Model.PrioritySelectList) %>
+
<%= Html.ValidationMessageFor(m => m.SabCategory)%>
-
- <%= Html.TextBoxFor(m => m.SabCategory)%> - <%= Html.ValidationMessageFor(m => m.SabCategory)%> -
--%>

diff --git a/NzbDrone.Web/Views/Settings/Indexers.ascx b/NzbDrone.Web/Views/Settings/Indexers.ascx index 43257201c..c9af9b345 100644 --- a/NzbDrone.Web/Views/Settings/Indexers.ascx +++ b/NzbDrone.Web/Views/Settings/Indexers.ascx @@ -1,4 +1,4 @@ -<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> +<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>