Пример #1
0
def _save_item_model(request, item: Item, form, change) -> None:
    prev_status = False
    if not item.pk:
        item.user = request.user
        if not item.issue:
            la = lna = False
            qs = Issue.objects
            try:
                # последний активный
                la = qs.filter(status='active').order_by('-pk')[0:1].get()
                # последний неактивный
                lna = qs.filter(pk__gt=la.pk).order_by('pk')[0:1].get()
            except Issue.DoesNotExist as e:
                logger.warning('Not found last or recent issue')

            if la or lna:
                item.issue = lna or la
    else:
        old_obj = Item.objects.get(pk=item.pk)
        prev_status = old_obj.status

    # Обновление времени модификации при смене статуса на активный
    new_status = form.cleaned_data.get('status')
    if not prev_status == 'active' and new_status == 'active':
        item.modified_at = datetime.now()
Пример #2
0
def _save_item_model(request, item: Item, form, change) -> None:
    prev_status = False
    if not item.pk:
        item.user = request.user
        if not item.issue:
            la = lna = False
            qs = Issue.objects
            try:
                # последний активный
                la = qs.filter(status='active').order_by('-pk')[0:1].get()
                # последний неактивный
                lna = qs.filter(pk__gt=la.pk).order_by('pk')[0:1].get()
            except Issue.DoesNotExist as e:
                logger.warning('Not found last or recent issue')

            if la or lna:
                item.issue = lna or la
    else:
        old_obj = Item.objects.get(pk=item.pk)
        prev_status = old_obj.status

    # Обновление времени модификации при смене статуса на активный
    new_status = form.cleaned_data.get('status')
    if not prev_status == 'active' and new_status == 'active':
        item.modified_at = datetime.now()
Пример #3
0
def save_item(item):
    if not item or item.get('link') is None:
        return

    time = datetime.datetime.now() + datetime.timedelta(days=-14)
    assert 'title' in item
    assert 'resource' in item
    assert 'link' in item

    if not Item.objects.filter(link=item.get('link'),
                               related_to_date__gt=time).exists():

        _a = Item(
            title=item.get('title'),
            resource=item.get('resource'),
            link=item.get('link'),
            description=item.get('description', ''),
            status=item.get('status', 'autoimport'),
            user_id=settings.BOT_USER_ID,
            section=item.get('section', None),
            additionally=item.get('additionally', None),
            language=item.get('language') if item.get('language') else 'en')

        _a.save()

        if item.get('tags'):
            _a.tags.add(*item.get('tags'))
            _a.save()
        elif item.get('status') == 'active':
            _a.save()
Пример #4
0
def save_item(item):
    assert 'title' in item
    assert 'resource' in item
    assert 'link' in item

    if not Item.objects.filter(title=item.get('title'),
                               link=item.get('link'),
                               description=item.get('description')).exists():
        _a = Item(
            title=item.get('title'),
            resource=item.get('resource'),
            link=item.get('link'),
            description=item.get('description', ''),
            status=item.get('status', 'autoimport'),
            user_id=settings.BOT_USER_ID,
            section=item.get('section', None),
            language=item.get('language') if item.get('language') else 'en')

        _a.save()

        if item.get('tags'):
            _a.tags.add(*item.get('tags'))
            _a.save()
        elif item.get('status') == 'active':
            _a.save()
Пример #5
0
def save_item(item):
    assert 'title' in item
    assert 'resource' in item
    assert 'link' in item

    if not Item.objects.filter(title=item.get('title'),
                               link=item.get('link'),
                               description=item.get('description')).exists():
        _a = Item(
            title=item.get('title'),
            resource=item.get('resource'),
            link=item.get('link'),
            description=item.get('description', ''),
            status=item.get('status', 'autoimport'),
            user_id=settings.BOT_USER_ID,
            section=item.get('section', None),
            additionally=item.get('additionally', None),
            language=item.get('language') if item.get('language') else 'en')

        _a.save()

        if item.get('tags'):
            _a.tags.add(*item.get('tags'))
            _a.save()
        elif item.get('status') == 'active':
            _a.save()
Пример #6
0
def save_item(item):
    if not item or item.get('link') is None:
        return

    time = datetime.datetime.now() + datetime.timedelta(days=-14)
    assert 'title' in item
    assert 'resource' in item
    assert 'link' in item

    if not Item.objects.filter(link=item.get('link'),
                               related_to_date__gt=time).exists():

        _a = Item(
            title=item.get('title'),
            resource=item.get('resource'),
            link=item.get('link'),
            description=item.get('description', ''),
            status=item.get('status', 'autoimport'),
            user_id=settings.BOT_USER_ID,
            section=item.get('section', None),
            additionally=item.get('additionally', None),
            language=item.get('language') if item.get('language') else 'en')

        _a.save()

        if item.get('tags'):
            _a.tags.add(*item.get('tags'))
            _a.save()
        elif item.get('status') == 'active':
            _a.save()
Пример #7
0
def save_item(item):
    if not item or item.get("link") is None:
        return

    time = datetime.datetime.now() + datetime.timedelta(days=-14)
    assert "title" in item
    assert "resource" in item
    assert "link" in item

    if not Item.objects.filter(link=item.get("link"), related_to_date__gt=time).exists():

        _a = Item(
            title=item.get("title"),
            resource=item.get("resource"),
            link=item.get("link"),
            description=item.get("description", ""),
            status=item.get("status", "autoimport"),
            user_id=settings.BOT_USER_ID,
            section=item.get("section", None),
            additionally=item.get("additionally", None),
            language=item.get("language") if item.get("language") else "en",
        )

        _a.save()

        if item.get("tags"):
            _a.tags.add(*item.get("tags"))
            _a.save()
        elif item.get("status") == "active":
            _a.save()
Пример #8
0
    def test_filter_exists_news(self):
        rss_items = get_items_from_rss(self.res_rss.link)

        Item(title=rss_items[0]['title'],
             link=rss_items[0]['link'],
             section=self.section).save()
        Item(title=rss_items[1]['title'],
             link=rss_items[1]['link'],
             section=self.section).save()
        Item(title=rss_items[2]['title'],
             link=rss_items[2]['link'],
             section=self.section).save()
        Item(title=rss_items[3]['title'],
             link=rss_items[3]['link'],
             section=self.section).save()
        Item(title=rss_items[4]['title'],
             link=rss_items[4]['link'],
             related_to_date=datetime.date(2005, 7, 14),
             section=self.section).save()
        Item(title=rss_items[5]['title'],
             link=rss_items[5]['link'],
             related_to_date=datetime.date(2016, 4, 12),
             section=self.section).save()

        fil = partial(is_not_exists_rss_item,
                      minimum_date=datetime.date(2016, 4, 11))
        self.assertEqual(len(list(filter(fil, rss_items))), 20)
Пример #9
0
    def test_type(self):
        section = Section(title='Статьи')

        object = Item(title='Title1',
                      link='https://pythondigest.ru',
                      section=section)