From f50d4236904396c58b77fecbb94fcd2ece38da23 Mon Sep 17 00:00:00 2001
From: Jean-Baptiste Pasquier <contact@jbpasquier.eu>
Date: Thu, 15 Oct 2020 13:54:37 +0200
Subject: [PATCH] feature: command for subscriptions

---
 README.md                                     |  6 ++++++
 .../commands/create_subscriptions.py          | 20 +++++++++++++++++++
 2 files changed, 26 insertions(+)
 create mode 100644 djangoldp_notification/management/commands/create_subscriptions.py

diff --git a/README.md b/README.md
index cf1ca48..ef0ebb3 100644
--- a/README.md
+++ b/README.md
@@ -35,6 +35,12 @@ An object allowing a User to be notified of any change on a resource or a contai
 
 For convenience, when you create a subscription on an object, DjangoLDP-Notification will parse the object for any one-to-many nested field relations. It will then create nested-subscriptions, i.e. a subscription on the nested field which sends an update to the same inbox, passing the parent model. If this behaviour is undesired you can delete the `Subscription` instance
 
+You can automatically create required subscriptions based on your settings.py with this management command:
+
+```bash
+./manage.py create_subscriptions
+```
+
 # Middlewares
 
 There is a `CurrentUserMiddleware` that catches the connected user of the last performed HTTP request and adds 
diff --git a/djangoldp_notification/management/commands/create_subscriptions.py b/djangoldp_notification/management/commands/create_subscriptions.py
new file mode 100644
index 0000000..387ca21
--- /dev/null
+++ b/djangoldp_notification/management/commands/create_subscriptions.py
@@ -0,0 +1,20 @@
+from django.core.management.base import BaseCommand
+from django.conf import settings
+from djangoldp_notification.models import Subscription
+
+class Command(BaseCommand):
+  help = 'Create required subscriptions'
+
+  def handle(self, *args, **options):
+    host = getattr(settings, 'SITE_URL')
+    jabber_host = getattr(settings, 'JABBER_DEFAULT_HOST')
+    xmpp = getattr(settings, 'PROSODY_HTTP_URL')
+
+    Subscription.objects.get_or_create(object=host+"/circles/", inbox=xmpp + "/conference." + jabber_host + "/happydev_muc_admin", field=None)
+    Subscription.objects.get_or_create(object=host+"/projects/", inbox=xmpp + "/conference." + jabber_host + "/happydev_muc_admin", field=None)
+    Subscription.objects.get_or_create(object=host+"/users/", inbox=xmpp + "/" + jabber_host + "/happydev_user_admin", field=None)
+    Subscription.objects.get_or_create(object=host+"/profiles/", inbox=xmpp + "/" + jabber_host + "/happydev_user_admin", field="user")
+    Subscription.objects.get_or_create(object=host+"/chatprofiles/", inbox=xmpp + "/" + jabber_host + "/happydev_user_admin", field="user")
+    Subscription.objects.get_or_create(object=host+"/accounts/", inbox=xmpp + "/" + jabber_host + "/happydev_user_admin", field="user")
+
+    self.stdout.write(self.style.SUCCESS("Successfully  created subscriptions\nhost: "+host+"\nxmpp server: "+xmpp+"\njabber host: "+jabber_host))
-- 
GitLab