mirror of
https://github.com/Radarr/Radarr
synced 2025-03-13 07:33:18 +00:00
Notifications wired up
This commit is contained in:
parent
e4410d8cb7
commit
1d007be8fd
11 changed files with 174 additions and 64 deletions
|
@ -1,4 +1,5 @@
|
||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using NzbDrone.Common.Reflection;
|
using NzbDrone.Common.Reflection;
|
||||||
using NzbDrone.Core.Annotations;
|
using NzbDrone.Core.Annotations;
|
||||||
|
|
||||||
|
@ -17,7 +18,17 @@ namespace NzbDrone.Api.ClientSchema
|
||||||
if (fieldAttribute != null)
|
if (fieldAttribute != null)
|
||||||
{
|
{
|
||||||
var field = fields.Find(f => f.Name == propertyInfo.Name);
|
var field = fields.Find(f => f.Name == propertyInfo.Name);
|
||||||
propertyInfo.SetValue(model, field.Value, null);
|
|
||||||
|
if (propertyInfo.PropertyType == typeof (Int32))
|
||||||
|
{
|
||||||
|
var intValue = Convert.ToInt32(field.Value);
|
||||||
|
propertyInfo.SetValue(model, intValue, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
propertyInfo.SetValue(model, field.Value, null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using NzbDrone.Api.ClientSchema;
|
using NzbDrone.Api.ClientSchema;
|
||||||
using NzbDrone.Common.Reflection;
|
|
||||||
using NzbDrone.Core.Annotations;
|
|
||||||
using NzbDrone.Core.Notifications;
|
using NzbDrone.Core.Notifications;
|
||||||
using Omu.ValueInjecter;
|
using Omu.ValueInjecter;
|
||||||
|
|
||||||
|
@ -16,7 +16,9 @@ namespace NzbDrone.Api.Notifications
|
||||||
_notificationService = notificationService;
|
_notificationService = notificationService;
|
||||||
|
|
||||||
GetResourceAll = GetAll;
|
GetResourceAll = GetAll;
|
||||||
|
CreateResource = Create;
|
||||||
UpdateResource = Update;
|
UpdateResource = Update;
|
||||||
|
DeleteResource = DeleteNotification;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<NotificationResource> GetAll()
|
private List<NotificationResource> GetAll()
|
||||||
|
@ -37,32 +39,49 @@ namespace NzbDrone.Api.Notifications
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private NotificationResource Update(NotificationResource notificationResource)
|
private NotificationResource Create(NotificationResource notificationResource)
|
||||||
{
|
{
|
||||||
//Todo: Convert Resource back to Settings
|
var notification = GetNotification(notificationResource);
|
||||||
|
|
||||||
var notification = _notificationService.Get(notificationResource.Id);
|
notification = _notificationService.Create(notification);
|
||||||
|
notificationResource.Id = notification.Id;
|
||||||
notification.OnGrab = notificationResource.OnGrab;
|
|
||||||
notification.OnDownload = notificationResource.OnDownload;
|
|
||||||
|
|
||||||
var properties = notification.Settings.GetType().GetSimpleProperties();
|
|
||||||
|
|
||||||
foreach (var propertyInfo in properties)
|
|
||||||
{
|
|
||||||
var fieldAttribute = propertyInfo.GetAttribute<FieldDefinitionAttribute>(false);
|
|
||||||
|
|
||||||
if (fieldAttribute != null)
|
|
||||||
{
|
|
||||||
//Find coresponding field
|
|
||||||
|
|
||||||
var field = notificationResource.Fields.Find(f => f.Name == propertyInfo.Name);
|
|
||||||
|
|
||||||
propertyInfo.SetValue(notification.Settings, field.Value, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return notificationResource;
|
return notificationResource;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private NotificationResource Update(NotificationResource notificationResource)
|
||||||
|
{
|
||||||
|
var notification = GetNotification(notificationResource);
|
||||||
|
notification.Id = notificationResource.Id;
|
||||||
|
_notificationService.Update(notification);
|
||||||
|
|
||||||
|
return notificationResource;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DeleteNotification(int id)
|
||||||
|
{
|
||||||
|
_notificationService.Delete(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Notification GetNotification(NotificationResource notificationResource)
|
||||||
|
{
|
||||||
|
var notification = _notificationService.Schema()
|
||||||
|
.SingleOrDefault(i =>
|
||||||
|
i.Implementation.Equals(notificationResource.Implementation,
|
||||||
|
StringComparison.InvariantCultureIgnoreCase));
|
||||||
|
|
||||||
|
//TODO: How should be handle this error?
|
||||||
|
if (notification == null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
notification.Name = notificationResource.Name;
|
||||||
|
notification.OnGrab = notificationResource.OnGrab;
|
||||||
|
notificationResource.OnDownload = notificationResource.OnDownload;
|
||||||
|
notification.Settings = (INotifcationSettings)SchemaDeserializer.DeserializeSchema(notification.Settings, notificationResource.Fields);
|
||||||
|
|
||||||
|
return notification;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,6 +6,7 @@ using Newtonsoft.Json;
|
||||||
using NzbDrone.Common;
|
using NzbDrone.Common;
|
||||||
using NzbDrone.Common.Composition;
|
using NzbDrone.Common.Composition;
|
||||||
using NzbDrone.Common.Messaging;
|
using NzbDrone.Common.Messaging;
|
||||||
|
using NzbDrone.Common.Serializer;
|
||||||
using NzbDrone.Core.Download;
|
using NzbDrone.Core.Download;
|
||||||
using NzbDrone.Core.MediaFiles.Events;
|
using NzbDrone.Core.MediaFiles.Events;
|
||||||
|
|
||||||
|
@ -16,6 +17,9 @@ namespace NzbDrone.Core.Notifications
|
||||||
List<Notification> All();
|
List<Notification> All();
|
||||||
Notification Get(int id);
|
Notification Get(int id);
|
||||||
List<Notification> Schema();
|
List<Notification> Schema();
|
||||||
|
Notification Create(Notification notification);
|
||||||
|
Notification Update(Notification notification);
|
||||||
|
void Delete(int id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class NotificationService
|
public class NotificationService
|
||||||
|
@ -76,6 +80,43 @@ namespace NzbDrone.Core.Notifications
|
||||||
return notifications.OrderBy(n => n.Name).ToList();
|
return notifications.OrderBy(n => n.Name).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Notification Create(Notification notification)
|
||||||
|
{
|
||||||
|
var definition = new NotificationDefinition()
|
||||||
|
{
|
||||||
|
Name = notification.Name,
|
||||||
|
OnGrab = notification.OnGrab,
|
||||||
|
OnDownload = notification.OnDownload,
|
||||||
|
Implementation = notification.Implementation,
|
||||||
|
Settings = Json.Serialize(notification.Settings)
|
||||||
|
};
|
||||||
|
|
||||||
|
definition = _notificationRepository.Insert(definition);
|
||||||
|
notification.Id = definition.Id;
|
||||||
|
|
||||||
|
return notification;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Notification Update(Notification notification)
|
||||||
|
{
|
||||||
|
var definition = _notificationRepository.Get(notification.Id);
|
||||||
|
|
||||||
|
definition.Name = notification.Name;
|
||||||
|
definition.OnGrab = notification.OnGrab;
|
||||||
|
definition.OnDownload = notification.OnDownload;
|
||||||
|
definition.Implementation = notification.Implementation;
|
||||||
|
definition.Settings = Json.Serialize(notification.Settings);
|
||||||
|
|
||||||
|
_notificationRepository.Update(definition);
|
||||||
|
|
||||||
|
return notification;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Delete(int id)
|
||||||
|
{
|
||||||
|
_notificationRepository.Delete(id);
|
||||||
|
}
|
||||||
|
|
||||||
private Notification ToNotification(NotificationDefinition definition)
|
private Notification ToNotification(NotificationDefinition definition)
|
||||||
{
|
{
|
||||||
var notification = new Notification();
|
var notification = new Notification();
|
||||||
|
|
|
@ -7,27 +7,6 @@ define([
|
||||||
], function () {
|
], function () {
|
||||||
|
|
||||||
NzbDrone.Settings.Indexers.ItemView = Backbone.Marionette.ItemView.extend({
|
NzbDrone.Settings.Indexers.ItemView = Backbone.Marionette.ItemView.extend({
|
||||||
template : 'Settings/Indexers/ItemTemplate',
|
template : 'Settings/Indexers/ItemTemplate'
|
||||||
initialize: function () {
|
|
||||||
NzbDrone.vent.on(NzbDrone.Commands.SaveSettings, this.saveSettings, this);
|
|
||||||
},
|
|
||||||
|
|
||||||
saveSettings: function () {
|
|
||||||
|
|
||||||
//this.model.save(undefined, this.syncNotification("Naming Settings Saved", "Couldn't Save Naming Settings"));
|
|
||||||
},
|
|
||||||
|
|
||||||
|
|
||||||
syncNotification: function (success, error) {
|
|
||||||
return {
|
|
||||||
success: function () {
|
|
||||||
window.alert(success);
|
|
||||||
},
|
|
||||||
|
|
||||||
error: function () {
|
|
||||||
window.alert(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -14,10 +14,14 @@ define([
|
||||||
'click': 'addNotification'
|
'click': 'addNotification'
|
||||||
},
|
},
|
||||||
|
|
||||||
|
initialize: function (options) {
|
||||||
|
this.notificationCollection = options.notificationCollection;
|
||||||
|
},
|
||||||
|
|
||||||
addNotification: function () {
|
addNotification: function () {
|
||||||
this.model.set('id', undefined);
|
this.model.set('id', undefined);
|
||||||
this.model.set('name', '');
|
this.model.set('name', '');
|
||||||
var view = new NzbDrone.Settings.Notifications.EditView({ model: this.model});
|
var view = new NzbDrone.Settings.Notifications.EditView({ model: this.model, notificationCollection: this.notificationCollection });
|
||||||
NzbDrone.modalRegion.show(view);
|
NzbDrone.modalRegion.show(view);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -25,6 +29,16 @@ define([
|
||||||
NzbDrone.Settings.Notifications.AddView = Backbone.Marionette.CompositeView.extend({
|
NzbDrone.Settings.Notifications.AddView = Backbone.Marionette.CompositeView.extend({
|
||||||
itemView : NzbDrone.Settings.Notifications.AddItemView,
|
itemView : NzbDrone.Settings.Notifications.AddItemView,
|
||||||
itemViewContainer : '.notifications .items',
|
itemViewContainer : '.notifications .items',
|
||||||
template : 'Settings/Notifications/AddTemplate'
|
template : 'Settings/Notifications/AddTemplate',
|
||||||
|
|
||||||
|
itemViewOptions: function () {
|
||||||
|
return {
|
||||||
|
notificationCollection: this.notificationCollection
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
initialize: function (options) {
|
||||||
|
this.notificationCollection = options.notificationCollection;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -13,8 +13,9 @@ define(['app', 'Settings/Notifications/ItemView', 'Settings/Notifications/AddVie
|
||||||
var schemaCollection = new NzbDrone.Settings.Notifications.Collection();
|
var schemaCollection = new NzbDrone.Settings.Notifications.Collection();
|
||||||
schemaCollection.url = '/api/notification/schema';
|
schemaCollection.url = '/api/notification/schema';
|
||||||
schemaCollection.fetch();
|
schemaCollection.fetch();
|
||||||
|
schemaCollection.url = '/api/notification';
|
||||||
|
|
||||||
var view = new NzbDrone.Settings.Notifications.AddView({ collection: schemaCollection});
|
var view = new NzbDrone.Settings.Notifications.AddView({ collection: schemaCollection, notificationCollection: this.collection});
|
||||||
NzbDrone.modalRegion.show(view);
|
NzbDrone.modalRegion.show(view);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
11
UI/Settings/Notifications/DeleteTemplate.html
Normal file
11
UI/Settings/Notifications/DeleteTemplate.html
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<div class="modal-header">
|
||||||
|
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
|
||||||
|
<h3>Remove: {{name}}</h3>
|
||||||
|
</div>
|
||||||
|
<div class="modal-body">
|
||||||
|
<p>Are you sure you want to remove '{{name}}'?</p>
|
||||||
|
</div>
|
||||||
|
<div class="modal-footer">
|
||||||
|
<button class="btn" data-dismiss="modal">cancel</button>
|
||||||
|
<button class="btn btn-danger x-confirm-delete">delete</button>
|
||||||
|
</div>
|
24
UI/Settings/Notifications/DeleteView.js
Normal file
24
UI/Settings/Notifications/DeleteView.js
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
'use strict';
|
||||||
|
define(['app', 'Settings/Notifications/Model'], function () {
|
||||||
|
|
||||||
|
NzbDrone.Settings.Notifications.DeleteView = Backbone.Marionette.ItemView.extend({
|
||||||
|
template: 'Settings/Notifications/DeleteTemplate',
|
||||||
|
|
||||||
|
events: {
|
||||||
|
'click .x-confirm-delete': 'removeNotification'
|
||||||
|
},
|
||||||
|
|
||||||
|
removeNotification: function () {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
//Success is not getting triggered: http://stackoverflow.com/questions/6988873/backbone-model-destroy-not-triggering-success-function-on-success
|
||||||
|
this.model.destroy({
|
||||||
|
wait : true,
|
||||||
|
success: function (model) {
|
||||||
|
model.collection.remove(model);
|
||||||
|
self.$el.parent().modal('hide');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
|
@ -13,18 +13,27 @@ define([
|
||||||
'click .x-save': 'save'
|
'click .x-save': 'save'
|
||||||
},
|
},
|
||||||
|
|
||||||
save: function () {
|
initialize: function (options) {
|
||||||
this.model.save();
|
this.notificationCollection = options.notificationCollection;
|
||||||
|
|
||||||
// window.alert('saving');
|
|
||||||
// this.model.save(undefined, this.syncNotification("Notification Settings Saved", "Couldn't Save Notification Settings"));
|
|
||||||
},
|
},
|
||||||
|
|
||||||
|
save: function () {
|
||||||
|
var name = this.model.get('name');
|
||||||
|
var success = 'Notification Saved: ' + name;
|
||||||
|
var fail = 'Failed to save notification: ' + name;
|
||||||
|
|
||||||
syncNotification: function (success, error) {
|
this.model.save(undefined, this.syncNotification(success, fail, this));
|
||||||
|
},
|
||||||
|
|
||||||
|
syncNotification: function (success, error, context) {
|
||||||
return {
|
return {
|
||||||
success: function () {
|
success: function () {
|
||||||
window.alert(success);
|
NzbDrone.Shared.Messenger.show({
|
||||||
|
message: success
|
||||||
|
});
|
||||||
|
|
||||||
|
context.notificationCollection.add(context.model, { merge: true });
|
||||||
|
context.$el.parent().modal('hide');
|
||||||
},
|
},
|
||||||
|
|
||||||
error: function () {
|
error: function () {
|
||||||
|
|
|
@ -4,5 +4,5 @@
|
||||||
<td name="cutoffName"></td>
|
<td name="cutoffName"></td>
|
||||||
<td>
|
<td>
|
||||||
<i class="icon-cog x-edit" title="Edit"></i>
|
<i class="icon-cog x-edit" title="Edit"></i>
|
||||||
| Delete
|
<i class="icon-remove x-delete" title="Delete"></i>
|
||||||
</td>
|
</td>
|
|
@ -3,7 +3,8 @@
|
||||||
define([
|
define([
|
||||||
'app',
|
'app',
|
||||||
'Settings/Notifications/Collection',
|
'Settings/Notifications/Collection',
|
||||||
'Settings/Notifications/EditView'
|
'Settings/Notifications/EditView',
|
||||||
|
'Settings/Notifications/DeleteView'
|
||||||
|
|
||||||
], function () {
|
], function () {
|
||||||
|
|
||||||
|
@ -13,15 +14,15 @@ define([
|
||||||
|
|
||||||
events: {
|
events: {
|
||||||
'click .x-edit' : 'edit',
|
'click .x-edit' : 'edit',
|
||||||
'click .x-remove': 'removeNotification'
|
'click .x-delete': 'deleteNotification'
|
||||||
},
|
},
|
||||||
|
|
||||||
edit: function () {
|
edit: function () {
|
||||||
var view = new NzbDrone.Settings.Notifications.EditView({ model: this.model});
|
var view = new NzbDrone.Settings.Notifications.EditView({ model: this.model, notificationCollection: this.model.collection});
|
||||||
NzbDrone.modalRegion.show(view);
|
NzbDrone.modalRegion.show(view);
|
||||||
},
|
},
|
||||||
|
|
||||||
removeNotification: function () {
|
deleteNotification: function () {
|
||||||
var view = new NzbDrone.Settings.Notifications.DeleteView({ model: this.model});
|
var view = new NzbDrone.Settings.Notifications.DeleteView({ model: this.model});
|
||||||
NzbDrone.modalRegion.show(view);
|
NzbDrone.modalRegion.show(view);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue