Mercurial > altnet-hispano
changeset 285:c8f378272407
#123: Patrocinadores de las vans.
line wrap: on
line diff
--- a/Agendas/trunk/src/Agendas.Tests/Agendas.Tests.csproj Wed Dec 28 09:35:27 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Tests/Agendas.Tests.csproj Sat Dec 31 14:45:55 2011 -0300 @@ -103,6 +103,9 @@ <Content Include="images\jetbrains.png"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </Content> + <Content Include="images\oralogo_bk200.gif"> + <CopyToOutputDirectory>Always</CopyToOutputDirectory> + </Content> </ItemGroup> <ItemGroup /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
--- a/Agendas/trunk/src/Agendas.Web.Tests/Agendas.Web.Tests.csproj Wed Dec 28 09:35:27 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Web.Tests/Agendas.Web.Tests.csproj Sat Dec 31 14:45:55 2011 -0300 @@ -45,9 +45,11 @@ <HintPath>..\packages\NUnit.2.5.10.11092\lib\nunit.framework.dll</HintPath> </Reference> <Reference Include="System" /> + <Reference Include="System.Configuration" /> <Reference Include="System.Core" /> <Reference Include="System.Drawing" /> <Reference Include="System.Web" /> + <Reference Include="System.Web.Extensions" /> <Reference Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" /> <Reference Include="System.Xml.Linq" /> <Reference Include="System.Data.DataSetExtensions" /> @@ -60,6 +62,7 @@ <Compile Include="Controllers\EventoControllerTests.cs" /> <Compile Include="Controllers\HttpPostedFileBaseModelBinder.cs" /> <Compile Include="Controllers\MvcControllerTestsBase.cs" /> + <Compile Include="Controllers\PatrocinadorApiControllerTests.cs" /> <Compile Include="Controllers\PatrocinadorControllerTests.cs" /> <Compile Include="Controllers\PersonaControllerTests.cs" /> <Compile Include="Helpers\HttpServerStub.cs" />
--- a/Agendas/trunk/src/Agendas.Web.Tests/AutorizationsTests.cs Wed Dec 28 09:35:27 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Web.Tests/AutorizationsTests.cs Sat Dec 31 14:45:55 2011 -0300 @@ -41,7 +41,7 @@ "HomeController.Index", "HomeController.About", "EventoController.Index", "AccountController.LogOn", "AccountController.LogOff", "AccountController.TwitterLogOn", "HistoricoController.Index", "PersonaController.Index", "ErrorController.NoAutorizado", "PersonaController.Ver", - "PatrocinadorController.Index", "PatrocinadorController.GetLogo" + "PatrocinadorController.Index", "PatrocinadorController.GetLogo", "PatrocinadorApiController.Nuevo" }; #region Asserts
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Web.Tests/Controllers/PatrocinadorApiControllerTests.cs Sat Dec 31 14:45:55 2011 -0300 @@ -0,0 +1,136 @@ +using System.Linq; +using System.Web; +using System.Web.Mvc; +using Agendas.NHibernate; +using Agendas.Repositories.Tests.Infraestructure; +using Agendas.Web.Tests.Helpers; +using AltNetHispano.Agendas.Domain; +using AltNetHispano.Agendas.Repositories.NHibernate; +using AltNetHispano.Agendas.Web.Controllers; +using AltNetHispano.Agendas.Web.Models; +using Moq; +using NUnit.Framework; + +namespace Agendas.Web.Tests.Controllers +{ + [TestFixture] + public class PatrocinadorApiControllerTests: MvcControllerTestsBase + { + internal void CrearPatrocinadorApress() + { + var patrocinadorController = new PatrocinadorController(new HttpServerStub()) + { + ControllerContext = ControllerCtx + }; + var form = new FormCollection + { + {"Nombre", "APRESS"}, + {"Logo", @"images\apress.gif"} + }; + var patrocinadorNew = BindModel<PatrocinadorNewModel>(patrocinadorController, form); + + using (new RequestEmulator(NhHelper.GetSessionFactory())) + { + var resultNuevo = patrocinadorController.Nuevo(patrocinadorNew); + } + } + + internal void RegisterBinders() + { + ModelBinders.Binders[typeof(HttpPostedFileBase)] = new HttpPostedFileBaseModelBinder(); + } + + [SetUp] + public void SetUpTests() + { + NhHelperTest.CleanDb(); + RegisterBinders(); + } + + [Test] + public void PatrocinadorNuevoGetSusseful() + { + var patrocinadorController = new PatrocinadorApiController { ControllerContext = ControllerCtx }; + using (new RequestEmulator(NhHelper.GetSessionFactory())) + { + var resultNuevo = patrocinadorController.Nuevo(); + Assert.IsInstanceOf(typeof(PartialViewResult), resultNuevo); + Assert.AreEqual(string.Empty, ((PartialViewResult)resultNuevo).ViewName); + } + } + + [Test] + public void PatrocinadorNuevoPostErrorRequeriedFields() + { + var patrocinadorApiController = new PatrocinadorApiController(new HttpServerStub()) + { + ControllerContext = ControllerCtx + }; + var form = new FormCollection + { + {"Nombre", ""}, + {"Logo", "" } + }; + var patrocinadorApiNew = BindModel<PatrocinadorNewModel>(patrocinadorApiController, form); + + using (new RequestEmulator(NhHelper.GetSessionFactory())) + { + var resultNuevo = patrocinadorApiController.Nuevo(patrocinadorApiNew); + Assert.IsInstanceOf(typeof(JsonResult), resultNuevo); + var resultadoJsonNuevo = (Resultado)resultNuevo.Data; + Assert.IsFalse(resultadoJsonNuevo.Succeful); + //Assert.AreEqual("El Nombre es obligatorio,El Logo es obligatorio", resultadoJsonNuevo.Message); + } + } + + [Test] + public void PatrocinadorNuevoPostErrorPatrocinadorExists() + { + CrearPatrocinadorApress(); + + var patrocinadorApiController = new PatrocinadorApiController(new HttpServerStub()) + { + ControllerContext = ControllerCtx + }; + var form = new FormCollection + { + {"Nombre", "Apress"}, + {"Logo", @"images\apress.gif" } + }; + var patrocinadorApiNew = BindModel<PatrocinadorNewModel>(patrocinadorApiController, form); + + using (new RequestEmulator(NhHelper.GetSessionFactory())) + { + var resultNuevo = patrocinadorApiController.Nuevo(patrocinadorApiNew); + Assert.IsInstanceOf(typeof(JsonResult), resultNuevo); + var resultadoJsonNuevo = (Resultado)resultNuevo.Data; + Assert.IsFalse(resultadoJsonNuevo.Succeful); + Assert.AreEqual("El patrocinador ya existe.", resultadoJsonNuevo.Message); + } + } + + [Test] + public void PatrocinadorNuevoPostSusseful() + { + var patrocinadorApiController = new PatrocinadorApiController(new HttpServerStub()) + { + ControllerContext = ControllerCtx + }; + var form = new FormCollection + { + {"Nombre", "Apress"}, + {"Logo", @"images\apress.gif" } + }; + var patrocinadorApiNew = BindModel<PatrocinadorNewModel>(patrocinadorApiController, form); + + using (new RequestEmulator(NhHelper.GetSessionFactory())) + { + var resultNuevo = patrocinadorApiController.Nuevo(patrocinadorApiNew); + Assert.IsInstanceOf(typeof(JsonResult), resultNuevo); + var resultadoJsonNuevo = (Resultado)resultNuevo.Data; + Assert.IsTrue(resultadoJsonNuevo.Succeful); + Assert.AreEqual(string.Empty, resultadoJsonNuevo.Message); + } + } + } +}
--- a/Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj Wed Dec 28 09:35:27 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj Sat Dec 31 14:45:55 2011 -0300 @@ -67,6 +67,7 @@ <Compile Include="Controllers\EventoController.cs" /> <Compile Include="Controllers\HistoricoController.cs" /> <Compile Include="Controllers\HomeController.cs" /> + <Compile Include="Controllers\PatrocinadorApiController.cs" /> <Compile Include="Controllers\PatrocinadorController.cs" /> <Compile Include="Controllers\PerfilController.cs" /> <Compile Include="Controllers\PersonaApiController.cs" /> @@ -257,6 +258,12 @@ <ItemGroup> <Content Include="Views\Shared\EditorTemplates\HttpPostedFileBase.cshtml" /> </ItemGroup> + <ItemGroup> + <Content Include="Views\Shared\EditorTemplates\Patrocinadores.cshtml" /> + </ItemGroup> + <ItemGroup> + <Content Include="Views\PatrocinadorApi\Nuevo.cshtml" /> + </ItemGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.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.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Web/Controllers/PatrocinadorApiController.cs Sat Dec 31 14:45:55 2011 -0300 @@ -0,0 +1,72 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.Mvc; +using System.IO; +using AltNetHispano.Agendas.Domain; +using AltNetHispano.Agendas.Factories; +using AltNetHispano.Agendas.Web.Models; + +namespace AltNetHispano.Agendas.Web.Controllers +{ + public class PatrocinadorApiController : Controller + { + private HttpServerUtilityBase _server; + + public HttpServerUtilityBase HttpServer + { + get + { + if (_server == null) + { + var httpServer = HttpContext.Server; + _server = httpServer; + } + + return _server; + } + } + + public PatrocinadorApiController() + { } + + public PatrocinadorApiController(HttpServerUtilityBase server) + { + _server = server; + } + + //[CustomAuthorize(Roles = Roles.Administrador)] + public PartialViewResult Nuevo() + { + var model = new PatrocinadorNewModel(); + return PartialView(model); + } + + [HttpPost] + //[CustomAuthorize(Roles = Roles.Administrador)] + public JsonResult Nuevo(PatrocinadorNewModel model) + { + if (ModelState.IsValid) + { + var patrocinadores = AgendaFactory.GetPatrocinadorRepository(); + if (patrocinadores.GetByNombre(model.Nombre) == null) + { + var tempLogoFile = Path.Combine(HttpServer.MapPath("~/App_Data"), + model.Nombre + + model.Logo.FileName.Substring(model.Logo.FileName.LastIndexOf("."))); + model.Logo.SaveAs(tempLogoFile); + var patrocinador = new Patrocinador(model.Nombre); + patrocinador.LoadLogo(tempLogoFile); + + patrocinadores.Save(patrocinador); + + return Json(new Resultado(true, string.Empty)); + } + return Json(new Resultado(false, "El patrocinador ya existe.")); + } + + return Json(new Resultado(false, string.Join(",", ModelState.Values))); + } + } +}
--- a/Agendas/trunk/src/Agendas.Web/DataProviders.cs Wed Dec 28 09:35:27 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Web/DataProviders.cs Sat Dec 31 14:45:55 2011 -0300 @@ -46,5 +46,28 @@ { return typeof (DataProviders).Assembly.GetName().Version.ToString(); } + + public static IEnumerable<SelectListItem> GetPatrocinadores(this HtmlHelper helper, IEnumerable<Guid> ids) + { + var repository = AgendaFactory.GetPatrocinadorRepository(); + + var patrocinadores = repository.GetAll(); + if (ids == null || !ids.Any()) return new SelectListItem[] { }; + + return from p in patrocinadores + select new SelectListItem {Text = p.Nombre, Value = p.Id.ToString()}; + } + + public static IEnumerable<SelectListItem> GetPatrocinadoresExcepto(this HtmlHelper helper, IEnumerable<Guid> ids) + { + var repository = AgendaFactory.GetPatrocinadorRepository(); + + var patrocinadores = repository.GetAll(); + if (ids == null) ids = new List<Guid>(); + + return from p in patrocinadores + where !ids.Contains(p.Id) + select new SelectListItem { Text = p.Nombre, Value = p.Id.ToString() }; + } } }
--- a/Agendas/trunk/src/Agendas.Web/Scripts/subpanels-0.9.js Wed Dec 28 09:35:27 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Web/Scripts/subpanels-0.9.js Sat Dec 31 14:45:55 2011 -0300 @@ -8,6 +8,7 @@ var main = null; var loading = null; + this.create = function () { // botón para abrir el panel openLink = $('<a></a>').addClass('ajax-button ui-state-default ui-corner-all').click(this.open).appendTo(panel); @@ -25,7 +26,7 @@ // panel principal main = $('<div></div>') -// .addClass('ui-accordion') + // .addClass('ui-accordion') .addClass('sp-main ui-widget-content ui-corner-bottom') .width(settings.width || '400px') .css('display', 'none') @@ -89,18 +90,18 @@ // buscamos el form que contiene el panel var form = $('form', content); -/* + /* if ($('h3', content).length == 0) - form.before('<h3><h3>'); + form.before('<h3><h3>'); $('h3', content).each(function() { - $(this).addClass('ui-accordion-header ui-helper-reset ui-state-active'); + $(this).addClass('ui-accordion-header ui-helper-reset ui-state-active'); }); // Agregamos un div que va a contener el form var body = $('<div></div>'); body.addClass('ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active'); form.wrap(body); -*/ + */ // modificamos el evento submit form.submit(dopost); @@ -130,7 +131,7 @@ } }); }; - + function dopost(event) { // prevenimos que se ejecute el submit ya que lo haremos manualmente. event.preventDefault(); @@ -147,7 +148,8 @@ var action = self.attr("action"); var data = self.serialize(); - console.log(data); + if (typeof (console) != 'undefined' && console && console.log) + console.log(data); // ejecutamos el POST $.post(action, data, function (data) {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Web/Views/PatrocinadorApi/Nuevo.cshtml Sat Dec 31 14:45:55 2011 -0300 @@ -0,0 +1,6 @@ +@model AltNetHispano.Agendas.Web.Models.PatrocinadorNewModel +@using (Html.BeginForm()) +{ + @Html.EditorForModel() + <input type="submit" value="agregar" /> +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Web/Views/Shared/EditorTemplates/Patrocinadores.cshtml Sat Dec 31 14:45:55 2011 -0300 @@ -0,0 +1,72 @@ +@model IEnumerable<Guid> +<script type="text/javascript"> + $(document).ready(function () { + $("#agregarPatrocinador").subpanel({ success: add_new_otro_patrocinador, url: '@Url.Action("Nuevo", "PatrocinadorApi")' }); + $("#add").click(select_otro_patrocinador); + $("#remove").click(remove_otro_patrocinador); + + var width = $("#posiblesPatrocinadores").width(); + $("#@Html.ViewData.ModelMetadata.PropertyName").width(width); + }); + + function add_new_otro_patrocinador(data) { + if (data.Succeful) { + var opcion = $("<option></option>").attr("value", data.Id).attr("selected", "selected").html(data.Nombre); + add_otro_patrocinador(opcion); + } + else { + alert(data.Message); + return false; + } + } + + function select_otro_patrocinador(event) { + event.preventDefault(); + var personas = $("#posiblesPatrocinadores"); + var current = $("option:selected", personas); + add_otro_patrocinador(current); + } + + function add_otro_patrocinador(option) { + var Patrocinadores = $("#@Html.ViewData.ModelMetadata.PropertyName"); + option.appendTo(Patrocinadores); + } + + function remove_otro_patrocinador(event) { + event.preventDefault(); + + $("option:selected", $("#@Html.ViewData.ModelMetadata.PropertyName")).each(function () { + var current = $(this); + var posiblesPatrocinadores = $("#posiblesPatrocinadores"); + current.appendTo(posiblesPatrocinadores); + + // ordenamos los patrocinadores + var options = $("option", posiblesPatrocinadores); + + options.sort(function (a, b) { + if (a.text > b.text) return 1; + else if (a.text < b.text) return -1; + else return 0 + }); + + posiblesPatrocinadores.empty().append(options); + + posiblesPatrocinadores.attr("value", current.val()); + }); + } +</script> +@Html.ListBox(string.Empty, Html.GetPatrocinadores(Model), new { style = "vertical-align: top;" }) +<div style="display: inline-block"> + <div> + <a class="ajax-button ui-state-default ui-corner-all" id="add"><span class="ui-icon ui-icon-triangle-1-w"></span></a> + @Html.DropDownList("posiblesPatrocinadores", Html.GetPatrocinadoresExcepto(Model), new { style = "vertical-align: top;", id = "posiblesPatrocinadores" }) + </div> + <div> + <a class="ajax-button ui-state-default ui-corner-all" id="remove"><span class="ui-icon ui-icon-triangle-1-e"></span></a> + </div> +<!-- TODO: Hacer que el sub-panel funcione con controles HttpPostFile + <div> + <div id="agregarPatrocinador"></div> + </div> +--> +</div> \ No newline at end of file