Done most on #59

This commit is contained in:
tidusjar 2016-03-23 13:43:27 +00:00
commit c7ac8a7d99
32 changed files with 345 additions and 2283 deletions

View file

@ -26,6 +26,7 @@
#endregion
using System.Dynamic;
using System.Linq;
using System.Web.UI.WebControls;
using MarkdownSharp;
@ -44,6 +45,8 @@ using PlexRequests.Core;
using PlexRequests.Core.SettingModels;
using PlexRequests.Helpers;
using PlexRequests.Services.Notification;
using PlexRequests.Store.Models;
using PlexRequests.Store.Repository;
using PlexRequests.UI.Helpers;
using PlexRequests.UI.Models;
@ -63,6 +66,7 @@ namespace PlexRequests.UI.Modules
private ISonarrApi SonarrApi { get; }
private PushbulletApi PushbulletApi { get; }
private ICouchPotatoApi CpApi { get; }
private IRepository<LogEntity> LogsRepo { get; }
private static Logger Log = LogManager.GetCurrentClassLogger();
public AdminModule(ISettingsService<PlexRequestSettings> rpService,
@ -76,7 +80,8 @@ namespace PlexRequests.UI.Modules
IPlexApi plexApi,
ISettingsService<PushbulletNotificationSettings> pbSettings,
PushbulletApi pbApi,
ICouchPotatoApi cpApi) : base("admin")
ICouchPotatoApi cpApi,
IRepository<LogEntity> logsRepo) : base("admin")
{
RpService = rpService;
CpService = cpService;
@ -90,6 +95,7 @@ namespace PlexRequests.UI.Modules
PushbulletApi = pbApi;
CpApi = cpApi;
SickRageService = sickrage;
LogsRepo = logsRepo;
#if !DEBUG
this.RequiresAuthentication();
@ -126,6 +132,10 @@ namespace PlexRequests.UI.Modules
Get["/pushbulletnotification"] = _ => PushbulletNotifications();
Post["/pushbulletnotification"] = _ => SavePushbulletNotifications();
Get["/logs"] = _ => Logs();
Get["/loglevel"] = _ => GetLogLevels();
Post["/loglevel"] = _ => UpdateLogLevels(Request.Form.level);
}
private Negotiator Authentication()
@ -246,8 +256,8 @@ namespace PlexRequests.UI.Modules
}
var result = CpService.SaveSettings(couchPotatoSettings);
return Response.AsJson(result
? new JsonResponseModel { Result = true, Message = "Successfully Updated the Settings for CouchPotato!" }
return Response.AsJson(result
? new JsonResponseModel { Result = true, Message = "Successfully Updated the Settings for CouchPotato!" }
: new JsonResponseModel { Result = false, Message = "Could not update the settings, take a look at the logs." });
}
@ -422,5 +432,24 @@ namespace PlexRequests.UI.Modules
return Response.AsJson(profiles);
}
private Negotiator Logs()
{
var allLogs = LogsRepo.GetAll().OrderByDescending(x => x.Id).Take(20);
return View["Logs", allLogs];
}
private Response GetLogLevels()
{
var levels = LogManager.Configuration.LoggingRules.FirstOrDefault(x => x.NameMatches("database"));
return Response.AsJson(levels.Levels);
}
private Response UpdateLogLevels(int level)
{
var newLevel = LogLevel.FromOrdinal(level);
LoggingHelper.ReconfigureLogLevel(newLevel);
return Response.AsJson(new JsonResponseModel { Result = true, Message = $"The new log level is now {newLevel}"});
}
}
}

View file

@ -169,18 +169,14 @@ namespace PlexRequests.UI.Modules
private Response RequestMovieAndUpdateStatus(RequestedModel request)
{
if (!Context.CurrentUser.IsAuthenticated())
{
return Response.AsJson(new JsonResponseModel { Result = false, Message = "You are not an Admin, so you cannot approve any requests." });
}
var cpSettings = CpService.GetSettings();
var cp = new CouchPotatoApi();
Log.Info("Adding movie to CP : {0}", request.Title);
Log.Info("Adding movie to CouchPotato : {0}", request.Title);
if (!cpSettings.Enabled)
{
// Approve it
request.Approved = true;
Log.Warn("We approved movie: {0} but could not add it to CouchPotato because it has not been setup", request.Title);
// Update the record
var inserted = Service.UpdateRequest(request);
@ -226,6 +222,11 @@ namespace PlexRequests.UI.Modules
/// <returns></returns>
private Response ApproveAll()
{
if (!Context.CurrentUser.IsAuthenticated())
{
return Response.AsJson(new JsonResponseModel { Result = false, Message = "You are not an Admin, so you cannot approve any requests." });
}
var requests = Service.GetAll().Where(x => x.Approved == false);
var requestedModels = requests as RequestedModel[] ?? requests.ToArray();
if (!requestedModels.Any())

View file

@ -51,7 +51,7 @@ namespace PlexRequests.UI.Modules
model.AdminExists = adminCreated;
return View["Login/Index", model];
}
};
Get["/logout"] = x => this.LogoutAndRedirect("~/");
@ -76,7 +76,8 @@ namespace PlexRequests.UI.Modules
return this.LoginAndRedirect(userId.Value, expiry);
};
Get["/register"] = x => {
Get["/register"] = x =>
{
{
dynamic model = new ExpandoObject();
model.Errored = Request.Query.error.HasValue;
@ -87,13 +88,13 @@ namespace PlexRequests.UI.Modules
Post["/register"] = x =>
{
var username = (string) Request.Form.Username;
var username = (string)Request.Form.Username;
var exists = UserMapper.DoUsersExist();
if (exists)
{
return Context.GetRedirect("~/register?error=true&username=" + username);
return Context.GetRedirect("~/register?error=true");
}
var userId = UserMapper.CreateUser(username, Request.Form.Password);
var userId = UserMapper.CreateUser(username, Request.Form.Password, new[] { "Admin" });
Session[SessionKeys.UsernameKey] = username;
return this.LoginAndRedirect((Guid)userId);
};
@ -116,7 +117,7 @@ namespace PlexRequests.UI.Modules
var newPasswordAgain = Request.Form.NewPasswordAgain;
if (!newPassword.Equals(newPasswordAgain))
{
}
var result = UserMapper.UpdateUser(username, oldPass, newPassword);