django芹菜中的基于类的任务

时间:2016-02-01 11:05:48

标签: python django django-celery

我正在使用此课程通过芹菜推送通知。 类定义是这样的。

from celery import Task
class NotifyUser(Task):
    """
    send push notification, entry point for this purpose is
    def send_notification() 
    """
    def __init__(self, users=None, notification_code=None, context=None, merchant_flag=False,
        listing_flag=False, object_type=None, object_id=None, category_id=None, *args, **kwargs):

        Task.__init__(self, *args, **kwargs)

        self.notification_code = notification_code
        self.users = users
        self.context = context
        self.merchant_flag = merchant_flag
        self.object_type = object_type
        self.object_id = object_id
        self.category_id = category_id
        self.listing_flag = listing_flag
        self.object_map = {
            'event':Event,
            'experience':Experience,
            'restaurant':Restaurant
            }

    def get_devices(self):
        gd = GCMDevice.objects.filter(user__in=self.users)
        ad = APNSDevice.objects.filter(user__in=self.users)
        return list(chain(ad, gd))

    def get_notification_message(self):
        if self.notification_code:
            return NotificationMessage.objects.get(message_code=self.notification_code)
        elif self.listing_flag:
            if self.category_id != u'0':
                return NotificationMessage.objects.get(listing_category=self.category_id)
        elif self.merchant_flag:
            if self.object_type:
                obj = self.object_map[self.object_type].objects.get(**{'id':self.object_id})
                return  NotificationMessage.objects.get(**{self.object_type:obj})

    def get_message_string(self, nf):
        template = Template(nf.message)
        context = Context(self.context)
        return template.render(context)

    def send_notification(self):

        devices = self.get_devices()
        nf = self.get_notification_message()
        nf.__dict__['message'] = self.get_message_string(nf) 
        self.send(devices, nf.__dict__)

    def run(self, source, *args, **kwargs): 
        self.send_notification()

    def send(self, devices, payload):
        print payload
        for dev in devices:
            #dev.send_message(payload)
            logger.debug("notification sent to {0} at {1}".format(dev.user.email, datetime.now()))

我创建一个NotifyUser对象,将参数传递给它:

notify_user = NotifyUser(users=queryset.values_list('id', flat=True), category_id=category_id, listing_flag=True)

然后使用延迟。

notify_user.delay(10)

在我创建一个对象后,我尝试了pdb; pdb.set_trace()我用来实例化的数据就在那里,

{'object_map': {'event': <class 'merchant.models.Event'>, 'experience': <class 'merchant.models.Experience'>, 'restaurant': <class 'merchant.models.Restaurant'>}, 'users': [2], 'notification_code': None, 'object_type': None, 'object_id': None, 'merchant_flag': False, 'context': None, 'listing_flag': True, 'category_id': u'1'}

但是一旦我使用notify_user.delay(10),(再次使用pdb!),在我的run方法中,当我自己。 dict 时,它给了我所用的所有默认值制作NotifyUSer类的对象,如下所示:

{'object_map': {'event': <class 'merchant.models.Event'>, 'experience': <class 'merchant.models.Experience'>, 'restaurant': <class 'merchant.models.Restaurant'>}, 'users': None, 'notification_code': None, 'object_type': None, 'object_id': None, 'merchant_flag': False, 'context': None, 'listing_flag': False, 'category_id': None}

为什么会发生这种情况 我是django芹菜的新手,请求指导我纠正解决方案。 感谢

0 个答案:

没有答案