Ejemplo n.º 1
0
def seed(db=DATABASE_URI):
    """Seed the database with some seed data."""
    connect(db)
    user = User.create(email='*****@*****.**', password='******')

    # Create seed subscriptions through json file.
    with open('resources/seed/subscriptions.json', 'r') as f:
        subscriptions = Subscription.from_json(f.read())
        for subscription in subscriptions:
            if random.choice([True, False]):
                user.subscribe(subscription)

        # Ensure at least one subscription
        user.subscribe(subscriptions[0])

    # Create seed topics through json file.
    with open('resources/seed/topics.json', 'r') as f:
        topics = Topic.from_json(f.read())
        for topic in topics:
            user.subscribe(subscriptions[0], topic)

    # Create seed articles through json file.
    with open('resources/seed/articles.json', 'r') as f:
        articles = Article.from_json(f.read())
        for article in articles:
            topic = random.choice(list(article.source.topics()))
            ArticleTopic.create(article=article, topic=topic)

            user.add_article(article)
Ejemplo n.º 2
0
 def insert(article, topic, source):
     db_subscription = Subscription.update_or_create(Subscription.url == source.url,
                                                     url=source.url,
                                                     name=source.name,
                                                     image=source.image_url)
     db_article = Article.update_or_create(Article.url == article.url,
                                           source=db_subscription,
                                           publish_date=article.publish_date,
                                           url=article.url,
                                           author="",
                                           title=article.title,
                                           description=article.description,
                                           body=article.body,
                                           preview_image=article.preview_image_url)
     db_topic = Topic.update_or_create(Topic.source == db_subscription and
                                       Topic.name == topic.name and
                                       Topic.url == topic.url,
                                       source=db_subscription,
                                       name=topic.name,
                                       url=topic.url)
     try:
         ArticleTopic.get(article=db_article, topic=db_topic)
     except ArticleTopic.DoesNotExist:
         ArticleTopic.create(article=db_article, topic=db_topic)