Esempio n. 1
0
 def new_post(self, title, content, date=datetime.datetime.utcnow()):
     post = Post(blog_id=self._id,
                 title=title,
                 content=content,
                 author=self.author,
                 created_date=date)
     post.save_to_mongo()
Esempio n. 2
0
def news_from_url(url):
    site_url = "http://127.0.0.1:5000"
    base_html = cached_url(url)
    # 是否有缓存
    if base_html[1] is False:
        sleep(random() * 10)
    e = pq(base_html[0])
    p_list = e('article .topicContent p').text()
    title = e('.art_tit').text()
    time = e('.time_box').text()
    content = p_list.replace('。', '。\n')
    # 图片的已知几种情况
    img_url = e('.vg_insert_img img').attr('src')
    if img_url is None:
        img_url = e('.vg_short_img_box img').attr('alt')

    img_path = download_image(img_url)

    if img_path != 'noimage':
        content = '![]({})\n'.format(site_url + img_path) + p_list.replace(
            '。', '。\n')

    board_id = Board.query.filter_by(title='games').first()
    form = {
        'title': title,
        'content': content,
        'board_id': board_id.id if board_id is not None else 1,
        'user_id': 1,
        'ct': time,
        'image_url': img_path,
    }
    item = Post(form)
    safe_commit(item)
    return item
Esempio n. 3
0
def updatePost():
    input = request.form
    id = input['id']
    post = Post(input['title'], input['content'])
    post.updatePost(id)
    flash('Update post is successful')
    return redirect(url_for('formEditPost', id=id))
Esempio n. 4
0
def add_post():

    #get处理 显示catagory

    categorys = db.session.query(Category).all()

    content = request.form.get('ckeditor')
    if request.method == 'POST':
        # 文章页面
        post = Post()

        post.title = request.form.get('title')
        post.content = content
        post.author = '风'
        post.create_time = 1

        #文章目录关系
        # postcate = Post_category()
        # postcate.post = post
        # postcate.category = Category.query.filter_by(name=request.values.get('sel')).first()

        post.categorys.append(
            Category.query.filter_by(name=request.values.get('sel')).first())

        # db.session.add(postcate)

        db.session.add(post)
        db.session.commit()
        return redirect(url_for('web.index'))

    return render_template('realpost.html', categorys=categorys)
Esempio n. 5
0
def post(id_post):
    p = Post(id_post)
    if not p.e_valido():
        abort(404)
    elif p.comentario():
        return redirect(url_for('post', id_post=p.get_postagem().id_post()))
    return render_template("home.html", posts=[p])
Esempio n. 6
0
def post(user):
    post = Post()
    post.title = "Fast API + GraphQL"
    post.body = "this is the post body and can be as long as possible"

    user.posts().save(post)
    return post
Esempio n. 7
0
def index():
    form = PostForm()

    if (form.validate_on_submit()):
        body = form.post.data
        post = Post(body=body, author=current_user)

        db.session.add(post)
        db.session.commit()

        flash('Your post is now online')
        return redirect(url_for('index'))

    page = request.args.get('page', 1, type=int)
    posts = current_user.get_followed_posts().paginate(
        page, app.config["POSTS_PER_PAGE"], False)

    if (posts.has_next): next_url = url_for('index', page=posts.next_num)
    else: next_url = None

    if (posts.has_prev): prev_url = url_for('index', page=posts.prev_num)
    else: prev_url = None

    return render_template('index.html',
                           title='Home Page',
                           posts=posts.items,
                           form=form,
                           next_url=next_url,
                           prev_url=prev_url)
Esempio n. 8
0
def test_post_pos():
    post = Post(
        {
            "id": 54,
            "text": "Post text",
            "date": 1620827160,
            "attachments": [
                {
                    "type": "photo",
                    "photo": {"sizes": [{"url": ""}, {"url": "photo_link"}]},
                },
                {"type": "link", "link": {"url": "link"}},
                {"type": "audio", "audio": {"url": "audio/25"}},
                {"type": "doc", "doc": {"url": "docs/m"}},
                {"type": "video", "video": {"owner_id": "-27", "id": "video"}},
            ],
            "reposts": {"count": 1},
            "likes": {"count": 2},
            "comments": {"count": 3},
        }
    )
    assert post.id == 54
    assert post.text == "Post text"
    assert post.date == 1620827160
    assert post.attachments == [
        "photo_link",
        "link",
        "audio/25",
        "docs/m",
        "https://vk.com/video-27_video",
    ]
    assert post.reposts == 1
    assert post.likes == 2
    assert post.comments == 3
Esempio n. 9
0
def new_article():
    form = ArticleForm()
    if request.method == 'POST' and form.validate():
        post_new = Post(
            body=request.form['editor-markdown-doc'],
            title=form.title.data,
            author=current_user,
            summary=form.summary.data,
            is_draft=form.is_draft.data,
            category=Category.query.filter_by(id=form.category.data).first())
        tags = [tag.strip() for tag in form.tags.data.split(',')
                ] if form.tags.data else None
        if tags:
            for tag in tags:
                new_tag = Tag.query.filter_by(content=tag).first()
                if not new_tag:
                    new_tag = Tag(tag)
                    new_tag.save()
                post_new.tag(new_tag)
        post_new.ping()
        post_new.save()
        current_app.logger.info('新文章 %s|%s', post_new.title,
                                post_new.author.username)
        flash('发文成功!')
        return redirect(url_for('post.article', post_id=post_new.id))
    return render_template('post/editor.html', form=form)
Esempio n. 10
0
 def test_01_post_repr(self):
     admin = User.query.get(1)
     post = Post(body='Post is editing!',
                 author=admin,
                 title='admin\'s post')
     post.save()
     self.assertTrue('<Post admin\'s post Author Admin>' == post.__repr__())
Esempio n. 11
0
 def test_02_post_ping(self):
     post = Post(body='Post is editing!')
     post.save()
     before_timestamp = datetime.utcnow()
     post.ping()
     after_timestamp = datetime.utcnow()
     self.assertTrue(post.timestamp <= before_timestamp <= post.last_edit <=
                     after_timestamp)
Esempio n. 12
0
 def post(self):
     args = post_parser.parse_args()
     from app.models.user import User
     author = User.query.get_or_404(args.author_id)
     post = Post(body=args.body, author=author)
     from app import db
     db.session.add(post)
     db.session.commit()
     return jsonify(post.to_json())
Esempio n. 13
0
def post_new():
    form = PostForm()
    if request.method == "POST" and form.validate():
        post = Post(title=form.title.data, content=form.content.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        flash("Your post has been created!", "success")
        return redirect(url_for('post', id=post.id))
    return render_template("post_new.html", title="Create New Post", form=form)
Esempio n. 14
0
def create_posts():
    for i in range(20):
        post = Post(name="Post" + str(i),
                    image="https://source.unsplash.com/random/250x250")
        db.session.add(post)

    db.session.commit()

    return
Esempio n. 15
0
def create_post(*, account: Account, title: str, content: str,
                skills: list) -> list:
    student_account_check(account)
    p = Post(student=get_student_account(account),
             title=title,
             content=content,
             published_date=date.today())
    p.save()
    p.skills.add(*skills)
    return list_post(id=account.id)
Esempio n. 16
0
def test_post_no_data():
    post = Post({"post_type": "no_data_post"})

    assert post.id == "no_id"
    assert post.text == ""
    assert post.date == 0
    assert post.attachments == []
    assert post.reposts == 0
    assert post.likes == 0
    assert post.comments == 0
Esempio n. 17
0
 def test_00_post_save_and_delete(self):
     post = Post(body='Post is editing!')
     post.save()
     comment = Comment(post=post)
     comment.save()
     self.assertTrue(Post.query.count() == 1)
     self.assertTrue(Comment.query.count() == 1)
     post.delete()
     self.assertTrue(Post.query.count() == 0)
     self.assertTrue(Comment.query.count() == 0)
Esempio n. 18
0
 def test_09_tag_and_post(self):
     tag = Tag('test')
     tag.save()
     post = Post(body='# Post Big title')
     post.save()
     self.assertTrue(post.tags == [])
     post.tag(tag)
     self.assertTrue(post.tags.pop() == tag)
     post.not_tag(tag)
     self.assertTrue(post.tags == [])
Esempio n. 19
0
 def test_17_get_followed_posts(self):
     user = User.query.first()
     self.assertTrue(user.followed_posts.count() == 0)
     from app.models.post import Post
     post = Post(body='Post is editing!', author=user)
     post.save()
     self.assertTrue(user.followed_posts.count() == 1)
     tester = User.query.get(2)
     tester.follow(user)
     self.assertTrue(tester.followed_posts.count() == 1)
Esempio n. 20
0
    def test_follow_posts(self):
        u1 = User(username='******', email='*****@*****.**')
        u2 = User(username='******', email='*****@*****.**')
        u3 = User(username='******', email='*****@*****.**')
        u4 = User(username='******', email='*****@*****.**')

        now = datetime.utcnow()
        post1 = Post(
            body=
            'Vin, The heir of the Survivor and Empress of Luthade; Mistborn',
            author=u1,
            timestamp=now + timedelta(seconds=1))
        post2 = Post(body='Elend, the Emperor of Luthadel; Mistborn',
                     author=u2,
                     timestamp=now + timedelta(seconds=4))
        post3 = Post(body='Kelsier, the Survivor of Hathesin; Mistborn',
                     author=u3,
                     timestamp=now + timedelta(seconds=3))
        post4 = Post(body='Sazed, the Hero of Ages; Keeper of Terris',
                     author=u4,
                     timestamp=now + timedelta(seconds=2))

        db.session.add_all([u1, u2, u3, u4])
        db.session.add_all([post1, post2, post3, post4])
        db.session.commit()

        u1.follow(u2)
        u1.follow(u3)
        u2.follow(u1)
        u4.follow(u1)
        u4.follow(u2)
        u4.follow(u3)

        f1 = u1.get_followed_posts().all()
        f2 = u2.get_followed_posts().all()
        f3 = u3.get_followed_posts().all()
        f4 = u4.get_followed_posts().all()

        self.assertEqual(f1, [post2, post3, post1])
        self.assertEqual(f2, [post2, post1])
        self.assertEqual(f3, [post3])
        self.assertEqual(f4, [post2, post3, post4, post1])
Esempio n. 21
0
def posts():
    user_count = User.query.count()
    for i in range(20):
        u = User.query.offset(randint(0, user_count - 1)).first()
        for j in range(10):
            p = Post(title=fake.text(randint(8, 20)),
                     body=fake.text(100),
                     create_time=fake.past_date(),
                     author=u)
            db.session.add(p)
    db.session.commit()
Esempio n. 22
0
    def add_post(author, title, description, slug):
        """Add New Post"""
        p = Post()
        p.author = User.objects.get(username=author)
        p.title = title
        p.slug = p.set_slug(title)
        p.description = description
        p.category = [Category.objects.get(slug=slug)]
        p.save()

        print('New Post added.')
Esempio n. 23
0
def index():
  if request.method == 'POST':
    new_post = Post()
    new_post.name = request.json['name']
    new_post.price = request.json['price']
    new_post.date = datetime.now()
    new_post.url = request.json['url']
    new_post.description = request.json['description']
    new_post.put();
    response = JSONEncoder().encode(new_post.getValue())
    return response
Esempio n. 24
0
def create_post(*, account: Account, content: str) -> list:
    user_account_check(account)
    p = Post(
        user=get_user_account(account),
        content=content,
        published_date=int(time.time()),
    )
    p.save()
    create_notification(type='post', account=account, post_job_id=p.id)

    return p
Esempio n. 25
0
def fake_posts(nums=50):
    with db.auto_commit():
        for i in range(nums):
            data = Post(
                title=faker.sentence(),
                body=faker.text(2000),
                category=Category.query.get(
                    random.randint(1, Category.query.count())),
                timestamp=faker.date_time_this_year()
            )
            db.session.add(data)
Esempio n. 26
0
def save_post(item, pubtime, url):
    post = Post(
        title=item.title,
        url=item.link,
        date=pubtime,
        owner=url.owner,
        domain=url.html,
    )
    db.session.add(post)
    db.session.commit()
    print(f'News added from { item.link }')
Esempio n. 27
0
 def test_08_tag_save_and_delete(self):
     tag = Tag('test')
     self.assertTrue(Tag.query.count() == 0)
     tag.save()
     self.assertEqual(tag.__repr__(), "Tag({id})".format(id=tag.id))
     post = Post(tags=[tag])
     post.save()
     self.assertTrue(Tag.query.count() == 1)
     self.assertTrue(tag in post.tags)
     tag.delete()
     self.assertTrue(Tag.query.count() == 0)
     self.assertTrue(len(post.tags) == 0)
Esempio n. 28
0
def add():
    form = request.form
    u = current_user()
    if u is None:
        flash('需要进行登陆', 'info')
        return redirect(url_for('index.login'))
    post = Post(form)
    post.user_id = u.id
    db.session.add(post)
    db.session.commit()
    flash('提交成功', 'success')
    return redirect(url_for('.detail', id=post.id))
Esempio n. 29
0
def getFeed():
    urls = Feed.query.filter_by(approved=APPROVED).all()
    for url in urls:
        feed = parseFeed(url.rss)
        for item in feed.entries:
            pubtime = datetime.datetime(*(item.published_parsed[0:7]))
            record = Post.query.filter_by(url=item.link).first()
            if record is None:
                post = Post(title=item.title, url=item.link, date=pubtime, owner=url.owner, domain=url.html)
                db.session.add(post)
                db.session.commit()
                print('News added from {}'.format(item.link))
Esempio n. 30
0
def notas(nombre):
    form = PostForm()
    if form.validate_on_submit():
        post = Post(nota=form.post.data)
        db.session.add(post)
        db.session.commit()
        flash('Se coloc la nota !')
        return redirect(url_for('notas', nombre=current_user.nombre))
    user = Alumno.query.filter_by(nombre=nombre).first_or_404()
    return render_template('views/profile.html',
                           title=f'Perfil {nombre}',
                           user=user,
                           form=form)