Ejemplo n.º 1
0
    def handle(self, *args, **options):
        print('DB command!')  # проверили работу, все ли правильно

        #выведем все статьи, которые есть на данный момент
        articles = Article.objects.all()
        print(articles)

        # сделаем запрос на конкретную статью
        art_ = Article.objects.get(article_name='Адаптогены')
        print(art_)

        # возвращает все теги, кроме указанного
        tags = Tag.objects.exclude(tag_name='адаптогены')
        print(tags)

        # возвращает указанный тег
        tag1 = Tag.objects.get(tag_name__icontains='фитотерапия')
        print(tag1)

        # Показывает статью, созданную последней по времени
        last_art = Article.objects.latest('article_data')
        print(last_art)

        # Выводит статьи, начиная с пятой
        art2 = Article.objects.all()[4:]
        print(art2)

        # сортируем статьи по алфавиту
        art_sort = Article.objects.order_by('article_name')
        print(art_sort)

        # выводит все названия статей по указанному тегу
        art2_ = Article.objects.filter(article_tag__tag_name="фитотерапия")
        print(art2_)

        # добавляем в базу данных
        #новый тег
        Tag.objects.create(tag_name='статьи')
        tags = Tag.objects.all()
        print(tags)

        t = Tag(tag_name='седативные')
        t.save()
        tags3 = Tag.objects.all()
        print(tags3)

        # новая статья (другие добавляются аналогично)
        a = Article.objects.create(
            article_name='Седативные',
            article_text='Описание, что это такое и список трав со ссылками',
            article_data=timezone.now())
        a.article_tag.add(t)
        articles = Article.objects.all()
        print(articles)
Ejemplo n.º 2
0
    def readContent(self):
        with open(staticfiles_storage.path('content_api.json'),
                  encoding="utf-8") as f:
            data = json.load(f)

        for article in data['results']:
            if not Article.objects.filter(uuid=article['uuid']).exists():
                newArticle = Article()
                newArticle.authorByLine = article['byline']
                newArticle.body = article['body']
                newArticle.publishDate = article['publish_at'].split('T')[0]
                newArticle.headline = article['headline']
                newArticle.promo = article['promo']
                newArticle.disclosure = article['disclosure']
                newArticle.uuid = article['uuid']
                newArticle.slug = slugify(article['headline'])

                for img in article['images']:
                    if img['featured']:
                        newArticle.featuredImage = img['url']

                for ins in article['instruments']:
                    newArticle.save()
                    if Instrument.objects.filter(
                            instrumentId=ins['instrument_id']).exists():
                        newArticle.relatedInstruments.add(
                            Instrument.objects.get(
                                instrumentId=ins['instrument_id']))
                    else:
                        i = Instrument()
                        i.instrumentId = ins['instrument_id']
                        i.companyName = ins['company_name']
                        i.symbol = ins['symbol']
                        i.exchange = ins['exchange']
                        i.save()
                        newArticle.relatedInstruments.add(i)

                for tag in article['tags']:
                    newArticle.save()
                    if Tag.objects.filter(uuid=tag['uuid']).exists():
                        newArticle.tags.add(Tag.objects.get(uuid=tag['uuid']))
                    else:
                        t = Tag()
                        t.uuid = tag['uuid']
                        t.tagName = tag['name']
                        t.tagSlug = tag['slug']
                        t.tagTypeName = tag['tag_type']['name']
                        t.tagTypeSlug = tag['tag_type']['slug']
                        t.save()
                        newArticle.tags.add(t)
                newArticle.save()
            for tag in article['tags']:
                if self.first == '' and tag['slug'] == '10-promise':
                    self.first = Article.objects.get(uuid=article['uuid'])
Ejemplo n.º 3
0
 def handle(self, *args, **options):
     Tag.objects.bulk_create([Tag(tag=t[0], slug=t[1]) for t in tags],
                             ignore_conflicts=True)
     names = generate_username(int(options["num_users"]))
     User = get_user_model()
     users = [
         User.objects.create_user(username=n, password=self.password)
         for n in names
     ]
     print(users)
     gen = DocumentGenerator()
     gen.init_word_cache(5000)
     gen.init_sentence_cache(5000)
     for user in users:
         user = User.objects.get(username=user.username)
         user.profile.bio = gen.sentence()
         user.profile.save()
         articles = Article.objects.bulk_create([
             Article(
                 slug=make_slug(gen.sentence()),
                 title=gen.sentence(),
                 description=gen.sentence(),
                 body=gen.paragraph(),
                 author=user.profile,
             )
             # Make sure every user has at least 1 article
             for _ in range(random.randrange(1, self.article_upper_bound))
         ])
         print(articles)
         # Make sure every article has 1 tag, could add more later
         for article in articles:
             article.tags.add(Tag.objects.get(slug=random.choice(tags)[1]))
     self.stdout.write(self.style.SUCCESS(f"Created {len(users)} users"))
Ejemplo n.º 4
0
def _articles(request, articles):
    paginator = Paginator(articles, 10)
    page = request.GET.get('page')
    try:
        articles = paginator.page(page)
    except PageNotAnInteger:
        articles = paginator.page(1)
    except EmptyPage:
        articles = paginator.page(paginator.num_pages)
    popular_tags = Tag.get_popular_tags()
    return render(request, 'articles/articles.html', {'articles': articles, 'popular_tags': popular_tags})
Ejemplo n.º 5
0
def _articles(request, articles):
    paginator = Paginator(articles, 10)
    page = request.GET.get('page')
    try:
        articles = paginator.page(page)
    except PageNotAnInteger:
        articles = paginator.page(1)
    except EmptyPage:
        articles = paginator.page(paginator.num_pages)
    popular_tags = Tag.get_popular_tags()
    return render(request, 'articles/articles.html', {'articles': articles, 'popular_tags': popular_tags})
Ejemplo n.º 6
0
def handleTag(tagStr, newEssay):
    tagEssayRelation.objects.filter(essayId = newEssay.id).delete()
    tag_id = 0
    final_tag = Tag()
    tagList = tagStr.split(',')
    for item in tagList:
        essay_tag = item.strip().lower()
        try:
            old_tag = Tag.objects.get(tag_name = essay_tag)
        except ObjectDoesNotExist:
            new_tag = Tag(
                tag_name = essay_tag,
                category = 0,
                )
            new_tag.save()
            final_tag = new_tag
        else:
            final_tag = old_tag
        tagessay_relation = tagEssayRelation(
            essayId = newEssay,
            tagId = final_tag,
            )
        tagessay_relation.save()
Ejemplo n.º 7
0
def handleTag(tagStr, newEssay):
    tagEssayRelation.objects.filter(essayId=newEssay.id).delete()
    tag_id = 0
    final_tag = Tag()
    tagList = tagStr.split(',')
    for item in tagList:
        essay_tag = item.strip().lower()
        try:
            old_tag = Tag.objects.get(tag_name=essay_tag)
        except ObjectDoesNotExist:
            new_tag = Tag(
                tag_name=essay_tag,
                category=0,
            )
            new_tag.save()
            final_tag = new_tag
        else:
            final_tag = old_tag
        tagessay_relation = tagEssayRelation(
            essayId=newEssay,
            tagId=final_tag,
        )
        tagessay_relation.save()
Ejemplo n.º 8
0
def tag(name):
    """Returns a Tag object for the given name"""

    slug = Tag.clean_tag(name)

    log.debug('Looking for Tag with slug "%s"...' % (slug,))
    t, created = Tag.objects.get_or_create(slug=slug, defaults={'name': name})
    log.debug('Found Tag %s. Name: %s Slug: %s Created: %s' % (t.pk, t.name, t.slug, created))

    if not t.name:
        t.name = name
        t.save()

    return t
Ejemplo n.º 9
0
def article_tags(tagstr):
    """
    Convert a space-delimited list of tags to their :class:`Tag` equivalients.

    TODO:   Improve tags to support multiple words (make ``django-articles`` use
            ``django-taggit``!)

    :param tagstr: Tags in the form, ``one two three``.
    """
    from publish.conf import DB
    from articles.models import Tag

    tag = lambda n: Tag.objects.using(DB).get_or_create(name=Tag.clean_tag(n))[0]
    return [tag(t) for t in tagstr.split()]
Ejemplo n.º 10
0
    def handle(self, *args, **options):
        articles = Article.objects.filter(article_url__isnull=False)

        for i, article in enumerate(articles):
            try:
                prediction_input = article.prediction_input
                if prediction_input is None:
                    raise Exception("No prediction_input")

                # Make tag predictions
                prediction_input = prediction_input.encode('utf-8')
                predicted_tags = text_tagger.text_to_tags(prediction_input)

            except Exception as e:
                self.stdout.write(
                    self.style.ERROR('Failed to tag article %s. Error: %s.' %
                                     (article.hn_id, e)))
                continue

            # Make tag predictions
            prediction_input = prediction_input.decode('utf-8')
            predicted_tags = text_tagger.text_to_tags(prediction_input)

            # Add tags to db (only matters if there's a previously unseen tag)
            existing_tags = Tag.objects.filter(name__in=predicted_tags)
            new_tags = set(predicted_tags) - set(
                [t.name for t in existing_tags])
            new_tags = Tag.objects.bulk_create(
                [Tag(name=t, lowercase_name=t.lower()) for t in new_tags])

            # Associate tags with article (many-to-many)
            article_tags = list(existing_tags) + new_tags
            article_tags = Tag.objects.filter(
                id__in=[t.id for t in article_tags])
            article.tags.add(*article_tags)

            article.tagged = True
            article.save()

            self.stdout.write(
                self.style.SUCCESS('Tagged article %s (%s of %s)\n%s\n%s' %
                                   (article.hn_id, i + 1, articles.count(),
                                    article.title, article_tags)))
Ejemplo n.º 11
0
 def test_get_absolute_url(self):
     name = 'Hi There'
     t = Tag.objects.create(name=name)
     self.assertEqual(t.get_absolute_url(), reverse('articles_display_tag', args=[Tag.clean_tag(name)]))