Able to create new Newznab indexers

This commit is contained in:
Mark McDowall 2013-05-28 17:44:29 -07:00
parent 5660b5086c
commit e4410d8cb7
8 changed files with 117 additions and 13 deletions

View File

@ -0,0 +1,27 @@
using System.Collections.Generic;
using NzbDrone.Common.Reflection;
using NzbDrone.Core.Annotations;
namespace NzbDrone.Api.ClientSchema
{
public static class SchemaDeserializer
{
public static object DeserializeSchema(object model, List<Field> fields)
{
var properties = model.GetType().GetSimpleProperties();
foreach (var propertyInfo in properties)
{
var fieldAttribute = propertyInfo.GetAttribute<FieldDefinitionAttribute>(false);
if (fieldAttribute != null)
{
var field = fields.Find(f => f.Name == propertyInfo.Name);
propertyInfo.SetValue(model, field.Value, null);
}
}
return model;
}
}
}

View File

@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using NzbDrone.Api.ClientSchema;
using NzbDrone.Core.Indexers;
using Omu.ValueInjecter;
@ -13,6 +15,7 @@ namespace NzbDrone.Api.Indexers
{
_indexerService = indexerService;
GetResourceAll = GetAll;
CreateResource = Create;
}
private List<IndexerResource> GetAll()
@ -32,5 +35,28 @@ namespace NzbDrone.Api.Indexers
return result;
}
private IndexerResource Create(IndexerResource indexerResource)
{
var indexer = _indexerService.Schema()
.SingleOrDefault(i =>
i.Implementation.Equals(indexerResource.Implementation,
StringComparison.InvariantCultureIgnoreCase));
//TODO: How should be handle this error?
if (indexer == null)
{
throw new InvalidOperationException();
}
indexer.Name = indexerResource.Name;
indexer.Enable = indexerResource.Enable;
indexer.Settings = (IIndexerSetting)SchemaDeserializer.DeserializeSchema(indexer.Settings, indexerResource.Fields);
indexer = _indexerService.Create(indexer);
indexerResource.Id = indexer.Id;
return indexerResource;
}
}
}

View File

@ -10,5 +10,6 @@ namespace NzbDrone.Api.Indexers
public Boolean Enable { get; set; }
public String Name { get; set; }
public List<Field> Fields { get; set; }
public String Implementation { get; set; }
}
}

View File

@ -95,6 +95,7 @@
<Compile Include="Authentication\NzbDroneUser.cs" />
<Compile Include="AutomapperBootstraper.cs" />
<Compile Include="Calendar\CalendarModule.cs" />
<Compile Include="ClientSchema\SchemaDeserializer.cs" />
<Compile Include="ClientSchema\FieldDefinitionAttribute.cs" />
<Compile Include="ClientSchema\Field.cs" />
<Compile Include="ClientSchema\SchemaBuilder.cs" />

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Common.Messaging;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Indexers.Newznab;
using NzbDrone.Core.Lifecycle;
@ -15,6 +16,7 @@ namespace NzbDrone.Core.Indexers
public bool Enable { get; set; }
public IIndexerSetting Settings { get; set; }
public IIndexer Instance { get; set; }
public string Implementation { get; set; }
}
public interface IIndexerService
@ -23,6 +25,7 @@ namespace NzbDrone.Core.Indexers
List<IIndexer> GetAvailableIndexers();
Indexer Get(string name);
List<Indexer> Schema();
Indexer Create(Indexer indexer);
}
public class IndexerService : IIndexerService, IHandle<ApplicationStartedEvent>
@ -63,12 +66,29 @@ namespace NzbDrone.Core.Indexers
newznab.Id = 1;
newznab.Name = "Newznab";
newznab.Settings = new NewznabSettings();
newznab.Implementation = "Newznab";
indexers.Add(newznab);
return indexers;
}
public Indexer Create(Indexer indexer)
{
var definition = new IndexerDefinition
{
Name = indexer.Name,
Enable = indexer.Enable,
Implementation = indexer.Implementation,
Settings = Json.Serialize(indexer.Settings)
};
definition = _indexerRepository.Insert(definition);
indexer.Id = definition.Id;
return indexer;
}
private Indexer ToIndexer(IndexerDefinition definition)
{
var indexer = new Indexer();
@ -76,6 +96,7 @@ namespace NzbDrone.Core.Indexers
indexer.Enable = definition.Enable;
indexer.Instance = GetInstance(definition);
indexer.Name = definition.Name;
indexer.Implementation = definition.Implementation;
if (indexer.Instance.GetType().GetMethod("ImportSettingsFromJson") != null)
{

View File

@ -5,23 +5,22 @@ define(['app', 'Settings/SettingsModel', 'Shared/Messenger'], function () {
template: 'Settings/General/GeneralTemplate',
initialize: function () {
NzbDrone.vent.on(NzbDrone.Commands.SaveSettings, this.saveSettings, this);
},
saveSettings: function () {
if (!this.model.isSaved) {
this.model.save(undefined, this.syncNotification("Naming Settings Saved", "Couldn't Save Naming Settings"));
this.model.save(undefined, this.syncNotification("General Settings Saved", "Couldn't Save General Settings"));
}
},
syncNotification: function (success, error) {
return {
success: function () {
NzbDrone.Shared.Messenger.show({message: 'General Settings Saved'});
NzbDrone.Shared.Messenger.show({message: success});
},
error : function () {
NzbDrone.Shared.Messenger.show({message: "Couldn't Save General Settings", type: 'error'});
NzbDrone.Shared.Messenger.show({message: error, type: 'error'});
}
};
}

View File

@ -12,7 +12,12 @@ define(['app',
'click .x-add': 'openSchemaModal'
},
initialize: function () {
NzbDrone.vent.on(NzbDrone.Commands.SaveSettings, this.saveSettings, this);
},
openSchemaModal: function () {
var self = this;
//TODO: Is there a better way to deal with changing URLs?
var schemaCollection = new NzbDrone.Settings.Indexers.Collection();
schemaCollection.url = '/api/indexer/schema';
@ -23,10 +28,29 @@ define(['app',
model.set('id', undefined);
model.set('name', '');
var view = new NzbDrone.Settings.Indexers.EditView({ model: model});
var view = new NzbDrone.Settings.Indexers.EditView({ model: model, indexerCollection: self.collection});
NzbDrone.modalRegion.show(view);
}
});
},
saveSettings: function () {
//TODO: check if any models in the collection have changed and sync them only
// this.collection.sync();
// if (!this.model.isSaved) {
// this.model.save(undefined, this.syncNotification("Naming Settings Saved", "Couldn't Save Naming Settings"));
// }
},
syncNotification: function (success, error) {
return {
success: function () {
NzbDrone.Shared.Messenger.show({message: 'General Settings Saved'});
},
error : function () {
NzbDrone.Shared.Messenger.show({message: "Couldn't Save General Settings", type: 'error'});
}
};
}
});
});

View File

@ -13,18 +13,23 @@ define([
'click .x-save': 'save'
},
save: function () {
this.model.save();
// window.alert('saving');
// this.model.save(undefined, this.syncNotification("Notification Settings Saved", "Couldn't Save Notification Settings"));
initialize: function (options) {
this.indexerCollection = options.indexerCollection;
},
save: function () {
this.model.save(undefined, this.syncNotification("Indexer Saved", "Couldn't Save Indexer", this));
},
syncNotification: function (success, error) {
syncNotification: function (success, error, context) {
return {
success: function () {
window.alert(success);
NzbDrone.Shared.Messenger.show({
message: success
});
context.indexerCollection.add(context.model);
context.$el.parent().modal('hide');
},
error: function () {