mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Added ImportStep abstraction and extracted recipe upload step.
This commit is contained in:
@@ -1,6 +1,4 @@
|
|||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Web.Mvc;
|
using System.Web.Mvc;
|
||||||
using Orchard.ContentManagement;
|
using Orchard.ContentManagement;
|
||||||
@@ -8,13 +6,13 @@ using Orchard.ImportExport.Services;
|
|||||||
using Orchard.ImportExport.ViewModels;
|
using Orchard.ImportExport.ViewModels;
|
||||||
using Orchard.Localization;
|
using Orchard.Localization;
|
||||||
using Orchard.Recipes.Services;
|
using Orchard.Recipes.Services;
|
||||||
using Orchard.UI.Notify;
|
|
||||||
|
|
||||||
namespace Orchard.ImportExport.Controllers {
|
namespace Orchard.ImportExport.Controllers {
|
||||||
public class AdminController : Controller, IUpdateModel {
|
public class AdminController : Controller, IUpdateModel {
|
||||||
private readonly IImportExportService _importExportService;
|
private readonly IImportExportService _importExportService;
|
||||||
private readonly IRecipeResultAccessor _recipeResultAccessor;
|
private readonly IRecipeResultAccessor _recipeResultAccessor;
|
||||||
private readonly IEnumerable<IExportAction> _exportActions;
|
private readonly IEnumerable<IExportAction> _exportActions;
|
||||||
|
private readonly IEnumerable<IImportAction> _importActions;
|
||||||
private readonly IRecipeParser _recipeParser;
|
private readonly IRecipeParser _recipeParser;
|
||||||
|
|
||||||
public AdminController(
|
public AdminController(
|
||||||
@@ -22,11 +20,13 @@ namespace Orchard.ImportExport.Controllers {
|
|||||||
IImportExportService importExportService,
|
IImportExportService importExportService,
|
||||||
IRecipeResultAccessor recipeResultAccessor,
|
IRecipeResultAccessor recipeResultAccessor,
|
||||||
IEnumerable<IExportAction> exportActions,
|
IEnumerable<IExportAction> exportActions,
|
||||||
|
IEnumerable<IImportAction> importActions,
|
||||||
IRecipeParser recipeParser) {
|
IRecipeParser recipeParser) {
|
||||||
|
|
||||||
_importExportService = importExportService;
|
_importExportService = importExportService;
|
||||||
_recipeResultAccessor = recipeResultAccessor;
|
_recipeResultAccessor = recipeResultAccessor;
|
||||||
_exportActions = exportActions;
|
_exportActions = exportActions;
|
||||||
|
_importActions = importActions;
|
||||||
_recipeParser = recipeParser;
|
_recipeParser = recipeParser;
|
||||||
Services = services;
|
Services = services;
|
||||||
T = NullLocalizer.Instance;
|
T = NullLocalizer.Instance;
|
||||||
@@ -36,7 +36,13 @@ namespace Orchard.ImportExport.Controllers {
|
|||||||
public Localizer T { get; set; }
|
public Localizer T { get; set; }
|
||||||
|
|
||||||
public ActionResult Import() {
|
public ActionResult Import() {
|
||||||
var viewModel = new ImportViewModel();
|
var actions = _importActions.OrderBy(x => x.Priority).Select(x => new ImportActionViewModel {
|
||||||
|
Editor = x.BuildEditor(Services.New)
|
||||||
|
}).Where(x => x != null).ToList();
|
||||||
|
|
||||||
|
var viewModel = new ImportViewModel {
|
||||||
|
Actions = actions
|
||||||
|
};
|
||||||
|
|
||||||
return View(viewModel);
|
return View(viewModel);
|
||||||
}
|
}
|
||||||
@@ -46,17 +52,23 @@ namespace Orchard.ImportExport.Controllers {
|
|||||||
if (!Services.Authorizer.Authorize(Permissions.Import, T("Not allowed to import.")))
|
if (!Services.Authorizer.Authorize(Permissions.Import, T("Not allowed to import.")))
|
||||||
return new HttpUnauthorizedResult();
|
return new HttpUnauthorizedResult();
|
||||||
|
|
||||||
if (String.IsNullOrEmpty(Request.Files["RecipeFile"].FileName)) {
|
var actions = _importActions.OrderBy(x => x.Priority).ToList();
|
||||||
ModelState.AddModelError("RecipeFile", T("Please choose a recipe file to import.").Text);
|
var viewModel = new ImportViewModel {
|
||||||
Services.Notifier.Error(T("Please choose a recipe file to import."));
|
Actions = actions.Select(x => new ImportActionViewModel {
|
||||||
|
Editor = x.UpdateEditor(Services.New, this)
|
||||||
|
}).Where(x => x != null).ToList()
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!ModelState.IsValid) {
|
||||||
|
return View(viewModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ModelState.IsValid) {
|
var context = new ImportActionContext { ActionResult = RedirectToAction("Import") };
|
||||||
var executionId = _importExportService.Import(new StreamReader(Request.Files["RecipeFile"].InputStream).ReadToEnd());
|
foreach(var action in actions) {
|
||||||
return RedirectToAction("ImportResult", new { executionId = executionId });
|
action.Execute(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
return View(new ImportViewModel());
|
return context.ActionResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ActionResult ImportResult(string executionId) {
|
public ActionResult ImportResult(string executionId) {
|
||||||
|
|||||||
@@ -72,10 +72,14 @@
|
|||||||
<Compile Include="Permissions.cs" />
|
<Compile Include="Permissions.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="Providers\ExportActions\RecipeBuilder.cs" />
|
<Compile Include="Providers\ExportActions\RecipeBuilder.cs" />
|
||||||
|
<Compile Include="Providers\ImportActions\UploadRecipe.cs" />
|
||||||
<Compile Include="Recipes\Builders\CustomStepsStep.cs" />
|
<Compile Include="Recipes\Builders\CustomStepsStep.cs" />
|
||||||
|
<Compile Include="Services\ImportAction.cs" />
|
||||||
<Compile Include="Services\ExportAction.cs" />
|
<Compile Include="Services\ExportAction.cs" />
|
||||||
|
<Compile Include="Services\ImportActionContext.cs" />
|
||||||
<Compile Include="Services\ExportActionContext.cs" />
|
<Compile Include="Services\ExportActionContext.cs" />
|
||||||
<Compile Include="Services\ExportContext.cs" />
|
<Compile Include="Services\ExportContext.cs" />
|
||||||
|
<Compile Include="Services\IImportAction.cs" />
|
||||||
<Compile Include="Services\IExportAction.cs" />
|
<Compile Include="Services\IExportAction.cs" />
|
||||||
<Compile Include="ViewModels\CustomStepEntry.cs" />
|
<Compile Include="ViewModels\CustomStepEntry.cs" />
|
||||||
<Compile Include="Services\ICustomExportStep.cs" />
|
<Compile Include="Services\ICustomExportStep.cs" />
|
||||||
@@ -83,6 +87,7 @@
|
|||||||
<Compile Include="Services\IImportExportService.cs" />
|
<Compile Include="Services\IImportExportService.cs" />
|
||||||
<Compile Include="Services\ImportExportService.cs" />
|
<Compile Include="Services\ImportExportService.cs" />
|
||||||
<Compile Include="ViewModels\CustomStepsViewModel.cs" />
|
<Compile Include="ViewModels\CustomStepsViewModel.cs" />
|
||||||
|
<Compile Include="ViewModels\ImportActionViewModel.cs" />
|
||||||
<Compile Include="ViewModels\ExportActionViewModel.cs" />
|
<Compile Include="ViewModels\ExportActionViewModel.cs" />
|
||||||
<Compile Include="ViewModels\ExportStepViewModel.cs" />
|
<Compile Include="ViewModels\ExportStepViewModel.cs" />
|
||||||
<Compile Include="ViewModels\ExportViewModel.cs" />
|
<Compile Include="ViewModels\ExportViewModel.cs" />
|
||||||
@@ -130,7 +135,7 @@
|
|||||||
<Content Include="Views\Admin\ImportResult.cshtml" />
|
<Content Include="Views\Admin\ImportResult.cshtml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Providers\ImportActions\" />
|
<Content Include="Views\EditorTemplates\ImportActions\UploadRecipe.cshtml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Content Include="Views\EditorTemplates\ExportSteps\CustomSteps.cshtml" />
|
<Content Include="Views\EditorTemplates\ExportSteps\CustomSteps.cshtml" />
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Web;
|
||||||
|
using System.Web.Mvc;
|
||||||
|
using System.Web.Routing;
|
||||||
|
using Orchard.ContentManagement;
|
||||||
|
using Orchard.ImportExport.Services;
|
||||||
|
using Orchard.UI.Notify;
|
||||||
|
|
||||||
|
namespace Orchard.ImportExport.Providers.ImportActions {
|
||||||
|
public class UploadRecipe : ImportAction {
|
||||||
|
private readonly IOrchardServices _orchardServices;
|
||||||
|
private readonly IImportExportService _importExportService;
|
||||||
|
|
||||||
|
public UploadRecipe(IOrchardServices orchardServices, IImportExportService importExportService) {
|
||||||
|
_orchardServices = orchardServices;
|
||||||
|
_importExportService = importExportService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string Name { get { return "UploadRecipe"; } }
|
||||||
|
|
||||||
|
public HttpPostedFileBase File { get; set; }
|
||||||
|
|
||||||
|
public override dynamic BuildEditor(dynamic shapeFactory) {
|
||||||
|
return UpdateEditor(shapeFactory, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override dynamic UpdateEditor(dynamic shapeFactory, IUpdateModel updater) {
|
||||||
|
|
||||||
|
if (updater != null) {
|
||||||
|
var request = _orchardServices.WorkContext.HttpContext.Request;
|
||||||
|
var file = request.Files["RecipeFile"];
|
||||||
|
if (String.IsNullOrEmpty(file.FileName)) {
|
||||||
|
updater.AddModelError("RecipeFile", T("Please choose a recipe file to import."));
|
||||||
|
_orchardServices.Notifier.Error(T("Please choose a recipe file to import."));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
File = file;
|
||||||
|
}
|
||||||
|
|
||||||
|
return shapeFactory.EditorTemplate(TemplateName: "ImportActions/UploadRecipe", Prefix: Prefix);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Execute(ImportActionContext context) {
|
||||||
|
var executionId = _importExportService.Import(new StreamReader(File.InputStream).ReadToEnd());
|
||||||
|
context.ActionResult = new RedirectToRouteResult(new RouteValueDictionary(new { action = "ImportResult", controller = "Admin", area = "Orchard.ImportExport", executionId = executionId }));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
using Orchard.ContentManagement;
|
using Orchard.ContentManagement;
|
||||||
|
|
||||||
namespace Orchard.ImportExport.Services {
|
namespace Orchard.ImportExport.Services {
|
||||||
public abstract class ExportAction : IExportAction {
|
public abstract class ExportAction : Component, IExportAction {
|
||||||
public virtual int Priority { get { return 0; } }
|
public virtual int Priority { get { return 0; } }
|
||||||
public abstract string Name { get; }
|
public abstract string Name { get; }
|
||||||
|
|
||||||
@@ -17,6 +17,6 @@ namespace Orchard.ImportExport.Services {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void Execute(ExportActionContext exportActionContext);
|
public abstract void Execute(ExportActionContext context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,6 +7,6 @@ namespace Orchard.ImportExport.Services {
|
|||||||
|
|
||||||
dynamic BuildEditor(dynamic shapeFactory);
|
dynamic BuildEditor(dynamic shapeFactory);
|
||||||
dynamic UpdateEditor(dynamic shapeFactory, IUpdateModel updater);
|
dynamic UpdateEditor(dynamic shapeFactory, IUpdateModel updater);
|
||||||
void Execute(ExportActionContext exportActionContext);
|
void Execute(ExportActionContext context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
using Orchard.ContentManagement;
|
||||||
|
|
||||||
|
namespace Orchard.ImportExport.Services {
|
||||||
|
public interface IImportAction : IDependency {
|
||||||
|
int Priority { get; }
|
||||||
|
string Name { get; }
|
||||||
|
|
||||||
|
dynamic BuildEditor(dynamic shapeFactory);
|
||||||
|
dynamic UpdateEditor(dynamic shapeFactory, IUpdateModel updater);
|
||||||
|
void Execute(ImportActionContext context);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
using Orchard.ContentManagement;
|
||||||
|
|
||||||
|
namespace Orchard.ImportExport.Services {
|
||||||
|
public abstract class ImportAction : Component, IImportAction {
|
||||||
|
public virtual int Priority { get { return 0; } }
|
||||||
|
public abstract string Name { get; }
|
||||||
|
|
||||||
|
protected virtual string Prefix {
|
||||||
|
get { return GetType().Name; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual dynamic BuildEditor(dynamic shapeFactory) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual dynamic UpdateEditor(dynamic shapeFactory, IUpdateModel updater) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void Execute(ImportActionContext context);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
using System.Web.Mvc;
|
||||||
|
|
||||||
|
namespace Orchard.ImportExport.Services {
|
||||||
|
public class ImportActionContext {
|
||||||
|
public ActionResult ActionResult { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
namespace Orchard.ImportExport.ViewModels {
|
namespace Orchard.ImportExport.ViewModels {
|
||||||
public class ExportViewModel {
|
public class ExportViewModel {
|
||||||
|
public ExportViewModel() {
|
||||||
|
Actions = new List<ExportActionViewModel>();
|
||||||
|
}
|
||||||
|
|
||||||
public IList<ExportActionViewModel> Actions { get; set; }
|
public IList<ExportActionViewModel> Actions { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
namespace Orchard.ImportExport.ViewModels {
|
||||||
|
public class ImportActionViewModel {
|
||||||
|
public dynamic Editor { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,11 @@
|
|||||||
namespace Orchard.ImportExport.ViewModels {
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Orchard.ImportExport.ViewModels {
|
||||||
public class ImportViewModel {
|
public class ImportViewModel {
|
||||||
|
public ImportViewModel() {
|
||||||
|
Actions = new List<ImportActionViewModel>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IList<ImportActionViewModel> Actions { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
<p>
|
||||||
|
@T("Choose a recipe file to import. Please consider {0} or backing up your data first.", @Html.Link(T("exporting").Text, Url.Action("Export", "Admin", new { area = "Orchard.ImportExport" })))
|
||||||
|
</p>
|
||||||
|
<fieldset>
|
||||||
|
<label for="RecipeFile">@T("Recipe File:")</label>
|
||||||
|
<input type="file" id="RecipeFile" size="64" name="RecipeFile" value="@T("Browse...")" />
|
||||||
|
</fieldset>
|
||||||
Reference in New Issue
Block a user