hagsteel / swampdragon

swampdragon
Other
556 stars 73 forks source link

how to implement swampdragon properly #188

Closed andy-io closed 8 years ago

andy-io commented 8 years ago

Hi, I am django postman for user to user messaging. I want to push notification to user when a new message is received. I am using angular in front end. I tried the following:

models.py

class Message(SelfPublishModel, models.Model): SUBJECT_MAX_LENGTH = 120 serializerclass = MessageSerializer subject = models.CharField(("subject"), max_length=SUBJECT_MAXLENGTH) body = models.TextField(("body"), blank=True) sender = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='sent_messages', null=True, blank=True, verbosename=("sender")) recipient = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='received_messages', null=True, blank=True, verbosename=("recipient")) email = models.EmailField(_("visitor"), blank=True) # instead of either sender or recipient, for an AnonymousUser parent = models.ForeignKey('self', related_name='next_messages', null=True, blank=True, verbosename=("parent message")) thread = models.ForeignKey('self', related_name='child_messages', null=True, blank=True, verbosename=("root message")) sentat = models.DateTimeField(("sent at"), default=now) readat = models.DateTimeField(("read at"), null=True, blank=True) repliedat = models.DateTimeField(("replied at"), null=True, blank=True) senderarchived = models.BooleanField(("archived by sender"), default=False) recipientarchived = models.BooleanField(("archived by recipient"), default=False) sender_deletedat = models.DateTimeField(("deleted by sender at"), null=True, blank=True) recipient_deletedat = models.DateTimeField(("deleted by recipient at"), null=True, blank=True)

moderation fields

moderation_status = models.CharField(_("status"), max_length=1, choices=STATUS_CHOICES, default=STATUS_PENDING)
moderation_by = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='moderated_messages',
    null=True, blank=True, verbose_name=_("moderator"))
moderation_date = models.DateTimeField(_("moderated at"), null=True, blank=True)
moderation_reason = models.CharField(_("rejection reason"), max_length=120, blank=True)
objects = MessageManager()

serializers.py

class MessageSerializer(ModelSerializer): class Meta: model = 'postman.Message' publish_fields = ['sender', 'subject', 'body']

from swampdragon import route_handler from swampdragon.route_handler import ModelRouter from .serializers import MessageSerializer from .models import Message from django.contrib.auth.models import User

class MessageRouter(ModelRouter): serializer_class = MessageSerializer model = Message route_name = 'message-router'

def get_object(self, **kwargs):
    return self.model.objects.get(pk=kwargs['id'])

def get_query_set(self, **kwargs):
    return self.model.objects.all()

route_handler.register(MessageRouter)

controller.js

var MessageControllers = angular.module('MessageControllers', []);

MessageControllers.controller('MsgCtrl', ['$scope', '$dragon', function ($scope, $dragon) { $scope.msgs = {}; $scope.channel = 'messages';

$dragon.onReady(function () {
       $dragon.subscribe('message-router', $scope.channel, {user_id:1}).then(function(response) {
        $scope.dataMapper = new DataMapper(response.data);
    });

    $dragon.getSingle('message-router', {id:1}).then(function (response) {
         $scope.msgs = response.data;
    });

});

$dragon.onChannelMessage(function (channels, message) {
    if (indexOf.call(channels, $scope.channel) > -1) {
        $scope.$apply(function () {
            $scope.dataMapper.mapData($scope.msg, message);
            $scope.$apply();
        });
    }
});

}]);

In the template I am printing {{ msg.body }}

This is not working and I am unable to understand the basic of how swampdragon works and how i should pass the current user to the router to filter the queryset. Kindly help me to figure it out and to implement it correctly.

Regards Andy