Exemplo n.º 1
0
    def setUp(self):
        self.manager = Manager.objects.create_user(uid=1, network='vk')

        self.client = Client()
        self.client.login(uid=1, network='vk')

        self.account = Account.objects.create(manager=self.manager, name='account', access_token='token')
        self.campaign = Campaign.objects.create(manager=self.manager, name='Test')

        self.post = VkPost.objects.create(
            campaign = self.campaign,
            access_token='test',
            when_to_post=datetime.now() + timedelta(seconds=60),
            when_to_delete=datetime.now() + timedelta(seconds=240), 
            owner_ids='-1',
            text='Hello, world!',
            from_group=True,
            attachments=''
        )
        VkPostEvent.objects.create(post=self.post, event_id=1)
        VkPostEvent.objects.create(post=self.post, event_id=2)

        self.client = Client()
        self.client.login(uid=1, network='vk')

        self.mox = mox.Mox()
        self.mox.StubOutWithMock(helpers, 'send_to_scheduler')

        helpers.send_to_scheduler(mox.IgnoreArg())

        self.mox.ReplayAll()
Exemplo n.º 2
0
    def setUp(self):
        self.manager = Manager.objects.create_user(uid=1, network='vk')

        self.client = Client()
        self.client.login(uid=1, network='vk')
        self.account = Account.objects.create(manager=self.manager, name='account', access_token='token')
        self.campaign = Campaign.objects.create(manager=self.manager, name='Test')

        self.mox = mox.Mox()
        self.mox.StubOutWithMock(helpers, 'send_to_scheduler')
        helpers.send_to_scheduler(mox.IgnoreArg())
        helpers.send_to_scheduler(mox.IgnoreArg())
        self.mox.ReplayAll()
Exemplo n.º 3
0
    def post(self, request):

        posts = request.POST.getlist('posts[]')

        # Получить список постов
        posts = VkPost.objects.filter(pk__in=posts).filter(campaign__manager=request.user)

        messages = []
        # Пройтись по всем постам
        for post in posts:
            messages.extend(post.delete())

        # Послать сообщения
        helpers.send_to_scheduler(messages)

        return {'error': False}
Exemplo n.º 4
0
    def post(self, request):
        add_form = VkPostForm(request.user, data=request.POST)

        if add_form.is_valid():
            # сохранить пост в базу данных
            posts = add_form.add_posts()

            # отправить сообщение в планировщик
            for post in posts:
                helpers.send_to_scheduler(
                    dict(
                        action="add",
                        trigger_time=int(time.mktime(post.when_to_post.astimezone(pytz.utc).timetuple())),
                        data=dict(
                            antigate_key=request.POST.get("antigate_key", ""),
                            captcha_attempts=request.POST.get("captcha_attempts", ""),
                            captcha_attempts_delay=request.POST.get("captcha_attempts_delay", ""),
                            posting_delay=request.POST.get("posting_delay", ""),
                            lt_post_id=post.pk,
                            text=post.text,
                            owner_ids=post.owner_ids or "",
                            from_group=post.from_group,
                            attachments=post.attachments,
                            access_token=post.access_token,
                            action="add",
                        ),
                    )
                )

                if post.when_to_delete is not None:
                    helpers.send_to_scheduler(
                        dict(
                            action="add",
                            trigger_time=int(time.mktime(post.when_to_delete.astimezone(pytz.utc).timetuple())),
                            data=dict(access_token=post.access_token, lt_post_id=post.pk, action="delete"),
                        )
                    )

            # перенаправить на страницу с таблицей постов
            return {"error": False}
        else:
            # Отправить ответ пользователю
            return dict(error=True, errors=get_all_form_errors(add_form))
Exemplo n.º 5
0
    def post(self, request):

        """ Удалить посты """

        # Сообщения в планировщик
        messages = []

        ids = request.POST.getlist('ids[]')
        campaigns = Campaign.objects.filter(manager=request.user, pk__in=ids)

        for campaign in campaigns:
            messages.extend(campaign.get_delete_messages())

        # Удалить кампании
        campaigns.delete()

        # Отправить сообщения
        helpers.send_to_scheduler(messages)

        return {'error': False}
Exemplo n.º 6
0
    def post(self, request, post_id):
        """
        Обновить данные для поста
        """

        # Найти пост
        try:
            post = VkPost.objects.get(campaign__manager=request.user, pk=post_id, status='waiting')
        except VkPost.DoesNotExist:
            raise Http404

        # Проверить корректность данных
        update_form = VkPostForm(request.user, data=request.POST)

        # Если неправильно, то 
        if not update_form.is_valid():
            # Отправить ответ пользователю
            return dict(
                error = True,
                errors = get_all_form_errors(update_form)
            )

        # Сохранить пост
        update_form.update_post(post)

        # Сформировать сообщение для планировщика
        messages = []
        posting_time, delete_time, message_data = post.get_scheduler_data()

        update_add = None
        try:
            add_event = VkPostEvent.objects.get(post=post, event_type='add').event_id
            action = dict( action="add" )
            action.update(message_data)

            update_add = dict(
                action='update',
                event_id=add_event,
                new_trigger_time=posting_time,
                new_data = message_data
            )
        except (VkPostEvent.DoesNotExist, IntegrityError):
            pass

        update_delete = None
        if delete_time:
            try:
                remove_event = VkPostEvent.objects.get(post=post, event_type='delete').event_id
                action = dict(
                    action="delete",
                    lt_post_id=post.pk,
                    access_token=message_data.get('access_token')
                )

                update_delete = dict(
                    action='update',
                    event_id=remove_event,
                    new_trigger_time=delete_time,
                    new_data=action
                )
            except (VkPostEvent.DoesNotExist, IntegrityError):
                pass

        messages = [update_add, update_delete]

        # Отправить сообщение в планировщик
        helpers.send_to_scheduler(messages)

        return {'error': False}