import json

try:
    from channels import Group
except:
    Group = None
    print('Channels is not installed, running in test mode!')

from .message import build_message


class PushNotificationOnChangeModelMixin:
    """
    This model mixin class sends rohrpost messages every time an object is created, updated, or
    deleted, to the corresponding group. The group is named:
        - As the result of get_group_name() if the object has such a method
        - As the result of group_name.format(pk=object.pk) if the object/class has a group_name
          attribute
        - As the result of '{class_name}-{pk}'.format(class_name=object.__class__.__name__.lower()
          pk=object.pk) otherwise

    The message will look like this:
    {
        "id": <some id>,
        "type": "subscription-update",
        "data": {
            "type": <create|update|delete>,
            "group": <group-name>,
            "object": <serialized object if object.serializer_class is set, else
                       {"id": <object.id>}>,
            <additional data from object.get_push_notification_data()>
        }
    }
    """
    def _get_group_name(self):
        if hasattr(self, "get_group_name"):
            return self.get_group_name()
        if hasattr(self, "group_name"):
            return self.group_name.format(pk=self.pk)
        return '{class_name}-{pk}'.format(
            class_name=self.__class__.__name__.lower(),
            pk=self.pk,
        )

    def _get_push_data(self):
        if hasattr(self, 'serializer_class'):
            obj_data = self.serializer_class(self).data
        else:
            obj_data = {'id': self.pk}
        if hasattr(self, 'get_push_notification_data'):
            obj_data.update(self.get_push_notification_data())
        return obj_data

    def _send_notify(self, message_type):
        group_name = self._get_group_name()
        message_data = self._get_push_data()
        message_data['type'] = message_type
        message_data['group'] = group_name
        Group(group_name).send({
            'text': json.dumps(build_message(
                generate_id=True,
                handler='subscription-update',
                **message_data,
            ))
        })

    def save(self, *args, **kwargs):
        message_type = 'update' if self.pk else 'create'
        ret = super().save(self, *args, **kwargs)
        self._send_notify(message_type)
        return ret

    def delete(self, *args, **kwargs):
        self._send_notify('delete')
        return super().delete(*args, **kwargs)

@@ -1029,17 +1029,45 @@
 t.id%3E%7D%3E,
+ %22id%22 being a guaranteed key
 %0A
-
         
@@ -1519,32 +1519,67 @@
 ush_data(self):%0A
+        obj_data = %7B'id': self.pk%7D%0A
         if hasat
@@ -1624,27 +1624,32 @@
     obj_data
- = 
+.update(
 self.seriali
@@ -1672,61 +1672,9 @@
 data
-%0A        else:%0A            obj_data = %7B'id': self.pk%7D
+)
 %0A   
@@ -1927,60 +1927,74 @@
 a = 
-self._get_push_data()%0A        message_data%5B
+%7B%0A            'group': self._get_group_name(),%0A            
 'type'
-%5D =
+:
  mes
@@ -2002,16 +2002,17 @@
 age_type
+,
 %0A       
@@ -2016,42 +2016,54 @@
     
-message_data%5B'group'%5D = group_name
+    'object': self._get_push_data(),%0A        %7D
 %0A   

Provide the documented data structure