Leaf Input and Submit

This commit is contained in:
2016-12-06 11:53:30 -05:00
parent a3b8b7a881
commit 6fd7e46f5d
74 changed files with 56144 additions and 1291 deletions
+27
View File
@@ -0,0 +1,27 @@
using System.Linq;
using System.Web.Mvc;
using log4net;
namespace LeafWeb.WebCms.Controllers
{
public class ActionLogAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext != null)
{
var controller = filterContext.RouteData.Values["controller"].ToString();
var action = filterContext.RouteData.Values["action"].ToString();
var username = filterContext.HttpContext.User.Identity.Name;
var loggerName = $"{controller}Controller.{action}";
var @params = string.Join(", ", filterContext.ActionParameters.Select(i => $"{i.Key}: {{{i.Value}}}"));
var hostAddress = filterContext.HttpContext.Request.UserHostAddress;
LogManager.GetLogger(loggerName)
.InfoFormat("UserHostAddress: {0}, username: {1}, params: {{{2}}}", hostAddress, username, @params);
}
base.OnActionExecuting(filterContext);
}
}
}
+41
View File
@@ -0,0 +1,41 @@
using System.Net;
using System.Threading.Tasks;
using System.Web.Mvc;
using Backload.Helper;
using Umbraco.Web.Mvc;
namespace LeafWeb.WebCms.Controllers
{
/// <summary>
/// The integrated controller to handle file requests.
/// You can remove this code, if you have a custom controller or handler.
/// </summary>
public class BackloadController : SurfaceController
{
/// <summary>
/// The Backload file handler.
/// To access it in an Javascript ajax request use: <code>var url = "/{Application}/Backload/FileHandler/";</code>.
/// </summary>
//[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post | HttpVerbs.Put | HttpVerbs.Delete | HttpVerbs.Options)]
public async Task<ActionResult> FileHandler()
{
try
{
// Create and initialize the handler
var handler = Backload.FileHandler.Create();
handler.Init(System.Web.HttpContext.Current.Request);
// Call the execution pipeline and get the result
var result = await handler.Execute();
// Helper to create an ActionResult object from the IBackloadResult instance
return ResultCreator.Create(result);
}
catch
{
return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
}
}
}
}
@@ -23,7 +23,7 @@ namespace LeafWeb.WebCms.Controllers
{
var controller = filterContext.RouteData.Values["controller"].ToString();
var action = filterContext.RouteData.Values["action"].ToString();
var loggerName = $"{controller}Controller.{action}";
var loggerName = $"LeafWeb.WebCms.Controllers.{controller}Controller.{action}";
LogManager.GetLogger(loggerName).Error(filterContext.Exception);
}
@@ -0,0 +1,24 @@
using System.Linq;
using System.Web.Mvc;
namespace LeafWeb.WebCms.Controllers
{
public class FluxnetSiteController : BaseController
{
public JsonResult Autocomplete(string query)
{
var sites = DataService.GetFluxnetSitesAutocomplete(query).Take(50).ToList();
var data = new
{
suggestions =
from s in sites
select new
{
value = $"{s.SiteName} / {s.FluxnetId}",
data = s.FluxnetId
}
};
return Json(data, JsonRequestBehavior.AllowGet);
}
}
}
+70 -13
View File
@@ -1,4 +1,12 @@
using System.Web.Mvc;
using System;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using log4net;
using LeafWeb.Core.Entities;
using LeafWeb.Core.Utility;
using LeafWeb.WebCms.App_Start;
using LeafWeb.WebCms.Models;
namespace LeafWeb.WebCms.Controllers
@@ -7,12 +15,15 @@ namespace LeafWeb.WebCms.Controllers
{
public ActionResult Create()
{
// initialize the session storage to retain SessionID between requests
if (Session != null)
Session["placeholder"] = 0;
var viewModel = new LeafInputCreate();
HydrateCreateViewModel(viewModel);
return PartialView(viewModel);
}
//[HttpParamAction]
[HttpPost]
public ActionResult Submit(LeafInputCreate viewModel)
{
@@ -20,22 +31,43 @@ namespace LeafWeb.WebCms.Controllers
return CurrentUmbracoPage();
// directory name is the sessionID
//var files = GetBackloadDirectoryFiles(Session.SessionID);
var files = GetBackloadDirectoryFiles(Session.SessionID);
//if (!files.Any())
// ModelState.AddModelError("Files", "Must select at least one file");
if (!files.Any())
ModelState.AddModelError("Files", "Must select at least one file");
//if (ModelState.IsValid && !IsHttpParamActionMatch()) // HttpParamMatch indicates it's backing out from Confirm
//{
// // Go to confirmation
// var confirmViewModel = new ConfirmViewModel(viewModel, files.Select(f => f.Name).ToArray());
// HydrateCreateViewModel(confirmViewModel);
if (ModelState.IsValid) // HttpParamMatch indicates it's backing out from Confirm
{
// convert viewModel into Model
var leafInput = viewModel.GetLeafInput(DataService);
// return View("Confirm", confirmViewModel);
//}
// load files into LeafInputFile
leafInput.InputFiles =
(from f in files
let bytes = System.IO.File.ReadAllBytes(f.FullName)
select new LeafInputFile { Filename = f.Name, Contents = bytes }).ToList();
// Save to db
DataService.AddLeafInput(leafInput);
DeleteBackloadDirectory(Session.SessionID);
var msg = $"A data set has submitted for '{viewModel.Identifier}' from '{viewModel.SiteId}'. " + Environment.NewLine
+ $"When complete, an email will be delivered to {viewModel.Name} <{viewModel.Email}> with results.";
SetStatusMessage(HttpUtility.HtmlEncode(msg), StatusType.Success);
var logger = LogManager.GetLogger(GetType());
logger.Info($"LeafInput: {leafInput.Id} Added, {leafInput.Identifier}, {leafInput.SiteId}, {leafInput.Email}");
logger.Info($"LeafInputFiles: {leafInput.InputFiles.Select(f => f.Id.ToString()).Join(", ")}, Queued");
HangfireStartup.TriggerPiscalProcessQueue();
return RedirectToCurrentUmbracoPage();
}
HydrateCreateViewModel(viewModel);
return CurrentUmbracoPage();
return CurrentUmbracoPage();
}
private void HydrateCreateViewModel(dynamic viewModel)
@@ -45,5 +77,30 @@ namespace LeafWeb.WebCms.Controllers
if (viewModel.PhotosynthesisType.ListItems == null)
viewModel.PhotosynthesisType.ListItems = GetPhotosynthesisTypeSelectList();
}
// Callback from Piscal
[ActionLog]
public void NotifyComplete()
{
HangfireStartup.TriggerPiscalProcessQueue();
}
private FileInfo[] GetBackloadDirectoryFiles(string directoryName)
{
var path = Path.Combine(Server.MapPath("~/Files/"), directoryName + "\\");
var directory = new DirectoryInfo(path);
return
!directory.Exists
? new FileInfo[] { }
: directory.GetFiles();
}
private void DeleteBackloadDirectory(string directoryName)
{
var path = Path.Combine(Server.MapPath("~/Files/"), directoryName + "\\");
var directory = new DirectoryInfo(path);
if (directory.Exists)
directory.Delete(true);
}
}
}
+36
View File
@@ -0,0 +1,36 @@
using System.Linq;
using System.Web.Mvc;
using LeafWeb.Core.Entities;
using LeafWeb.Core.Utility;
using LeafWeb.WebCms.Models;
namespace LeafWeb.WebCms.Controllers
{
public class ResultsController : BaseController
{
public ActionResult Index()
{
var viewModel =
DataService.GetLeafInputs()
.OrderByDescending(f => f.Id)
.ToList()
.Select(leafInput => new ResultStatusViewModel(leafInput));
return View(viewModel);
}
[ActionLog]
public ActionResult Download(string token)
{
var leafInput = DataService.GetLeafInput(token);
if (leafInput == null)
return View("DownloadNotFound");
var zip = leafInput.GetOutputFileZip(LeafOutputFileType.ToUser);
var filename = leafInput.Identifier.FilterValidFilename() + ".zip";
return new FileContentResult(zip, "application/zip") { FileDownloadName = filename };
}
}
}