Exemplo n.º 1
0
def create():
    """Create a new post for the current user."""
    if request.method == "POST":
        title = request.form["title"]
        body = request.form["body"]
        error = None

        if not title:
            error = "Title is required."

        if error is not None:
            flash(error)
        else:
            db = get_db()
            # - db.execute(
            # -     "INSERT INTO post (title, body, author_id) VALUES (?, ?, ?)",
            # -     (title, body, g.user["id"]),
            # - )
            # - db.commit()
            post = models.Post(title=title, body=body, author_id=g.user.id)
            db.session.add(post)
            db.session.commit()
            return redirect(url_for("blog.index"))

    return render_template("blog/create.html")
Exemplo n.º 2
0
def add_post():
    g.title = 'New status'
    if request.method == 'POST':
        if len(request.form['title']) == 0 or len(request.form['text']) == 0:
            flash('Title and text cannot be empty!!!')
            return redirect(request.referrer)
        uid = session.get('crtUser')
        u = models.User.query.get(uid)
        p = models.Post(title=request.form['title'],
                        body=request.form['text'],
                        timestamp=datetime.utcnow(),
                        author=u)
        db.session.add(p)
        db.session.commit()
        flash('New entry was successfully posted')
        return redirect(request.referrer)
    return render_template('index.html',
                           providers=app.config['OPENID_PROVIDERS'])
Exemplo n.º 3
0
def create():
    if request.method == 'POST':
        title = request.form['title']
        body = request.form['body']
        error = None

        if not title:
            error = 'Title is required.'

        if error is not None:
            flash(error)
        else:
            new_post = models.Post(title=title, body=body, author_id=g.user.id)
            db.session.add(new_post)
            db.session.commit()
            return redirect(url_for('blog.index'))

    return render_template('blog/create.html')
Exemplo n.º 4
0
def app():
    """Create and configure a new app instance for each test."""
    # create a temporary file to isolate the database for each test
    # db_fd, db_path = tempfile.mkstemp(dir=os.environ['PYTHONPATH'])
    # create the app with common test config
    # app = create_app({"TESTING": True, "DATABASE": db_path})
    app = create_app({
        "TESTING": True,
        "SQLALCHEMY_DATABASE_URI": os.environ.get('TEST_DATABASE_URL'),
    })

    # create the database and load test data
    with app.app_context():
        init_db()
        # - get_db().executescript(_data_sql)

        # INSERT INTO user (username, password)
        # VALUES
        #   ('test', 'pbkdf2:sha256:50000$TCI4GzcX$0de171a4f4dac32e3364c7ddc7c14f3e2fa61f2d17574483f7ffbb431b4acb2f'),
        #   ('other', 'pbkdf2:sha256:50000$kJPKsz6N$d2d4784f1b030a9761f5ccaeeaca413f27f2ecb76d6168407af962ddce849f79');
        # 
        # INSERT INTO post (title, body, author_id, created)
        # VALUES
        #   ('test title', 'test' || x'0a' || 'body', 1, '2018-01-01 00:00:00');
        db = get_db()

        test_user = models.User(
                username='******',
                password='******')
        other_user = models.User(
            username='******',
            password='******')
        db.session.add(test_user)
        db.session.add(other_user)
        db.session.flush()
        post = models.Post(
                title='test title',
                body='test' + '\n' + 'body',
                author_id=test_user.id,
                created='2018-01-01 00:00:00')
        db.session.add(post)
        db.session.commit()

    yield app
Exemplo n.º 5
0
def fetch_latest_feed(source_url: str):
    """Celery task that extracrs latest source posts.

    Args:
        source_url: Source to extract posts for.
    """
    source = models.Source.query.filter_by(url=source_url).first()
    if not source:
        raise errors.SourceNotFoundError()
    parser = feedparser.FeedParser()
    data = parser.parse_source(source.feed_link)

    links = (entry['link'] for entry in data['entries'])
    post_records = models.Post.query.filter(models.Post.id.in_(links)).all()
    existing_links = {post.link for post in post_records}

    posts = []
    for entry in data['entries']:
        if entry['title'] and entry['link'] not in existing_links:
            post = models.Post(**entry)
            post.source_id = source.id
            posts.append(post)
    db.session.bulk_save_objects(posts)
    db.session.commit()