如何使用apscheduler自动实现石墨烯中的突变

时间:2019-08-27 09:15:22

标签: django graphene-python

我正在设置时间表模板以创建一个事件,以通知用户定期进行库存盘点。

我可以通过在调度程序中调用模型的实例来做到这一点,但是我的变异会自动创建一个与调度程序链接的事件。

这是我的突变班:

class CreateStockCountTemplate(graphene.Mutation):

    stock_template = graphene.Field(StockCountTemplateType)
    success = graphene.String()

    class Arguments:
        product_ids = graphene.List(graphene.Int, required=True)
        event_type = graphene.Int(required=True)
        assigned_user_ids = graphene.List(graphene.String, required=True)
        designated_user_ids = graphene.List(graphene.String, required=False)
        outlet_id = graphene.Int(required=True)
        scheduled_time = graphene.String(required=True)
        frequency = graphene.Int(required=False)

    @login_required
    @user_permission('Manager', 'Admin')
    def mutate(self, info, **kwargs):
        template_instance = StockCountTemplate()
        template_fields = stock_counts.get_template_fields(**kwargs)

        schedule = kwargs.get('scheduled_time')

        with SaveContextManager(template_instance)as stock_tempalate:
            scheduled_time = make_aware(parser.parse(schedule))
            event_type = setattr(
                stock_tempalate,
                'event_type',
                kwargs['event_type'])
            description = 'Stock count on  auto scheduling'
            event = Event(
                start_date=scheduled_time.date(),
                end_date=scheduled_time.date() + timedelta(days=7),
                start_time=scheduled_time.time(),
                end_time=scheduled_time.time(),
                event_title=f'Stock count',
                description=f'{description}',
                event_type=event_type)
            stock_tempalate = stock_counts.add_fields_to_template(
                template_instance,
                **template_fields
            )
            event.save()
            stock_tempalate.event = event
            stock_tempalate.save()
        to_email = stock_tempalate.assigned_users.values_list(
            'email', flat=True)
        email_stock_template = 'email_alerts/stocks/stock_email.html'
        subject = 'Stock counts alert'
        context = {
            'scheduled_time': datetime.strftime(
                stock_tempalate.scheduled_time, "%d-%b-%Y"),
            'by': str(info.context.user.email),
            'template_type': 'Stock counts',
            'small_text_detail': 'Hello, you have been assigned to carry'
                                 ' out stock counts'
        }
        send_mail = SendMail(email_stock_template, context, subject, to_email)
        send_mail.send()
        message = SUCCESS_RESPONSES[
            "creation_success"].format("Stock count template")
        return CreateStockCountTemplate(
            stock_template=stock_tempalate, success=message)

在我的信号中,我正在监听post_save信号,并使用它来设置时间表以一定的时间间隔(在本例中为1分钟)创建模板的另一个实例:

def notification_schedule():
    template_instance = StockCountTemplate()

    template_fields = stock_counts.get_template_fields()
    with SaveContextManager(template_instance) as stock_tempalate:
        stock_tempalate = stock_counts.add_fields_to_template(
            template_instance,
            **template_fields
        )
        stock_tempalate.save()


@receiver(post_save, sender=StockCountTemplate)
def schedule_job(sender, instance, created, **kwargs):
    """Create user profile on saving the user."""
    latest_template = StockCountTemplate.objects.latest('created_at')
    import pdb; pdb.set_trace()
    if created:  # noqa
        """We are going to start the job here"""
        scheduler = BackgroundScheduler()
        scheduler.add_job(notification_schedule, 'interval',
                          minutes=(instance.frequency), max_instances=2)
        scheduler.start()

我想要一种在这里称呼突变的方法:

def notification_schedule():
    template_instance = StockCountTemplate()

    template_fields = stock_counts.get_template_fields()
    with SaveContextManager(template_instance) as stock_tempalate:
        stock_tempalate = stock_counts.add_fields_to_template(
            template_instance,
            **template_fields
        )
        stock_tempalate.save()

代替实例化模型。有办法吗?

0 个答案:

没有答案