コード例 #1
0
ファイル: crud.py プロジェクト: burakhanaksoy/FastAPITutorial
def put_blog_post(id: int, request: schemas.CreateBlog, response: Response,
                  db: Session):
    check = db.query(models.Blog).filter(models.Blog.id == id).first()
    if not check == None:
        db.query(models.Blog).filter(models.Blog.id == id).update({
            'title':
            request.title,
            'user':
            request.user,
            'likes':
            request.likes,
            'published':
            request.published
        })
        db.commit()
        response.status_code = 200
    else:
        new_blog = models.Blog(title=request.title,
                               user=request.user,
                               likes=request.likes,
                               published=request.published)
        db.add(new_blog)
        db.commit()
        db.refresh(new_blog)
        response.status_code = 201
コード例 #2
0
ファイル: notification.py プロジェクト: yoCruzer/wuditoo
    def text(self):
        user = models.User().find(self.operator_id)
        text = ''
        if self.action == ACTIVITY_ACTION['PHOTO_LIKE']:
            photo = models.Photo().find(self.target_id)
            text = u'<a href="/user/{user.username}">{user.fullname}</a> 喜欢照片 <a href="/photo/{photo_id}">{photo_title}</a>'.format(
                user=user,
                photo_id=photo.id,
                photo_title=photo.title or u"无标题",
            )
        elif self.action == ACTIVITY_ACTION['PHOTO_COMMENT_ADD']:
            photo = models.Photo().find(self.context_id)
            text = u'<a href="/user/{user.username}">{user.fullname}</a> 评论了照片 <a href="/photo/{photo_id}">{photo_title}</a>'.format(
                user=user,
                photo_id=photo.id,
                photo_title=photo.title or u"无标题",
            )
        elif self.action == ACTIVITY_ACTION['BLOG_COMMENT_ADD']:
            post = models.Blog().find(self.context_id)
            title = u'评论了你在博文'
            url = '/blog/post/{post.id}'
            if post.status == 1:
                title = u'评论了你在反馈'
                url = '/feedback'
            text = u'<a href="/user/{user.username}">{user.fullname}</a> {title} <a href="{url}">{post.title}</a> 中的评论'.format(
                user=user,
                title=title,
                post=post,
                url=url,
            )
        elif self.action == ACTIVITY_ACTION['USER_FOLLOW']:
            text = u'<a href="/user/{user.username}">{user.fullname}</a> 关注了你'.format(
                user=user)

        return text
コード例 #3
0
ファイル: crud.py プロジェクト: burakhanaksoy/FastAPITutorial
def create_blog(request: schemas.CreateBlog, db: Session):
    new_blog = models.Blog(title=request.title,
                           user=request.user,
                           likes=request.likes,
                           published=request.published)
    db.add(new_blog)
    db.commit()
    db.refresh(new_blog)
    return request
コード例 #4
0
async def create(
    request: schemas.Blog,
    db: Session = Depends(database.get_db),
    current_user: schemas.User = Depends(get_current_active_user)):
    new_blog = models.Blog(title=request.title, body=request.body, user_id=1)
    db.add(new_blog)
    db.commit()
    db.refresh(new_blog)
    return new_blog
コード例 #5
0
    def get(self, blog_id=0):
        if not blog_id:
            blog = models.Blog().findall(limit=1).to_list()
            if blog:
                blog = blog[0]
        else:
            blog = models.Blog().find(blog_id)

        blog_comments = None
        if blog:
            blog_comments = models.Blog_Comment().findall_by_blog_id(blog.id)

        blogs = models.Blog().select(['id', 'title']).findall(limit=100)
        return self.render(
            'blog/blog.html',
            blog=blog,
            blogs=blogs,
            blog_comments=blog_comments,
        )
コード例 #6
0
    def post(self):
        if not self.current_user.is_admin:
            raise tornado.web.HTTPError(403)

        blog = models.Blog()
        if self.get_argument('id', ''):
            blog.id = self.get_argument('id')
        blog.title = self.get_argument('title')
        blog.content = self.get_argument('content')
        blog.created = blog.updated = time.time()
        blog.user_id = self.current_user.id
        blog.save()
        return self.redirect('/blog/{0}'.format(blog.id))
コード例 #7
0
ファイル: cs3320a2.py プロジェクト: 2sourapples/WebDev-Blogs
def add_blog():
    if 'auth_user' not in flask.session:
        app.logger.warn('unauthorized user tried to add blog')
        flask.abort(401)

    title = flask.request.form['title']
    entry = flask.request.form['blog']
    # create a new blog
    blog = models.Blog()
    # set its properties
    blog.title = title
    blog.entry = entry
    # add it to the database
    db.session.add(blog)
    # commit the database session
    db.session.commit()
    return flask.redirect(flask.url_for('blog', bid=blog.id), code=303)
コード例 #8
0
ファイル: notification.py プロジェクト: yoCruzer/wuditoo
 def _blog_comment_add(blog_comment):
     receiver_ids = set([models.Blog().find(blog_comment.blog_id).user_id])
     mention_users = re.findall(r'@[^\(]+\((.*?)\)', blog_comment.content)
     if mention_users:
         for username in mention_users:
             user_id = models.User().find_by_username(username).id
         receiver_ids.add(user_id)
     for receiver_id in receiver_ids:
         if blog_comment.user_id != receiver_id:
             Notification(
                 operator_id=blog_comment.user_id,
                 receiver_id=receiver_id,
                 action=ACTIVITY_ACTION['BLOG_COMMENT_ADD'],
                 created=blog_comment.created,
                 target_id=blog_comment.id,
                 context_id=blog_comment.blog_id,
                 is_new=1,
             ).save()
コード例 #9
0
ファイル: blog.py プロジェクト: zlindacz/multi-user-blog
 def post(self):
     self.redirect_if_not_logged_in()
     title = self.request.get("subject")
     blog = self.request.get("content")
     username = self.get_current_user()
     user = models.User.get_by("username", username)
     if title and blog and username:
         b = models.Blog(title=title, blog=blog, author=user)
         b.put()
         self.redirect("/blog/%s" % b.key().id())
     else:
         error = "We need both a title and a blog in order to publish this entry."
         self.render("main.html",
                     form=True,
                     action="/blog/newpost",
                     cancel="/blog",
                     username=username,
                     title=title,
                     blog=blog,
                     error=error,
                     submit="Publish")
コード例 #10
0
ファイル: blog.py プロジェクト: maithedung/fastapi_tutorial
def create(request: schemals.Blog, db: Session):
    new_blog = models.Blog(title=request.title, body=request.body, user_id=1)
    db.add(new_blog)
    db.commit()
    db.refresh(new_blog)
    return new_blog
コード例 #11
0
ファイル: activity.py プロジェクト: yoCruzer/wuditoo
 def text(self):
     user = models.User().find(self.user_id)
     text = ''
     if self.action == ACTIVITY_ACTION['PHOTO_LIKE']:
         photo = models.Photo().find(self.target_id)
         photo_url = get_photo_url(self, photo, 's')
         text = u'<a href="/user/{user.username}">{user.fullname}</a> 喜欢照片 <a href="/photo/{photo_id}"><img src="{photo_url}" /></a>'.format(
                 user = user,
                 photo_id = photo.id,
                 photo_url = photo_url,
                 )
     if self.action == ACTIVITY_ACTION['PHOTO_UNLIKE']:
         photo = models.Photo().find(self.target_id)
         photo_url = get_photo_url(self, photo, 's')
         text = u'<a href="/user/{user.username}">{user.fullname}</a> 不喜欢照片 <a href="/photo/{photo_id}"><img src="{photo_url}" /></a>'.format(
                 user = user,
                 photo_id = photo.id,
                 photo_url = photo_url,
                 )
     elif self.action == ACTIVITY_ACTION['PHOTO_COMMENT_ADD']:
         photo = models.Photo().find(self.context_id)
         photo_url = get_photo_url(self, photo, 's')
         text = u'<a href="/user/{user.username}">{user.fullname}</a> 评论了照片 <a href="/photo/{photo_id}"><img src="{photo_url}" /></a>'.format(
                 user = user,
                 photo_id = photo.id,
                 photo_url = photo_url,
                 )
     elif self.action == ACTIVITY_ACTION['BLOG_COMMENT_ADD']:
         post = models.Blog().find(self.context_id)
         title = u'评论了博文'
         url = '/blog/post/{post.id}'
         if post.status == 1:
             title = u'评论了反馈'
             url = '/feedback'
         text = u'<a href="/user/{user.username}">{user.fullname}</a> {title} <a href="{url}">{post.title}</a> 中的评论'.format(
                 user = user,
                 title = title,
                 post = post,
                 url = url,
                 )
     elif self.action == ACTIVITY_ACTION['USER_FOLLOW']:
         dest_user = models.User().find(self.target_id)
         text = u'<a href="/user/{user.username}">{user.fullname}</a> 关注了 <a href="/user/{user.username}">{dest_user.fullname}</a>'.format(user = user, dest_user = dest_user)
     elif self.action == ACTIVITY_ACTION['USER_UNFOLLOW']:
         dest_user = models.User().find(self.target_id)
         text = u'<a href="/user/{user.username}">{user.fullname}</a> 取消关注了 <a href="/user/{user.username}">{dest_user.fullname}</a>'.format(user = user, dest_user = dest_user)
     elif self.action == ACTIVITY_ACTION['USER_CREATE']:
         text = u'<a href="/user/{user.username}">{user.fullname}</a> 创建了账号'
     elif self.action == ACTIVITY_ACTION['PHOTO_CREATE']:
         photo = models.Photo().find(self.target_id)
         photo_url = get_photo_url(self, photo, 's')
         text = u'<a href="/user/{user.username}">{user.fullname}</a> 发布了照片 <a href="/photo/{photo.id}"><img src="{photo_url}" /></a>'.format(user = user, photo = photo, photo_url = photo_url)
     elif self.action == ACTIVITY_ACTION['BLOG_EDIT']:
         post = models.Blog().find(self.target_id)
         text = u'<a href="/user/{user.username}">{user.fullname}</a> 编辑了博文 <a href="/blog/post/{post.id}">{post.title}</a>'.format(user = user, post = post)
     elif self.action == ACTIVITY_ACTION['BLOG_ADD']:
         post = models.Blog().find(self.target_id)
         text = u'<a href="/user/{user.username}">{user.fullname}</a> 发布了博文 <a href="/blog/post/{post.id}">{post.title}</a>'.format(user = user, post = post)
     elif self.action == ACTIVITY_ACTION['BLOG_DELETE']:
         post = models.Blog().find(self.target_id)
         text = u'<a href="/user/{user.username}">{user.fullname}</a> 删除了博文 <a href="/blog/post/{post.id}">{post.title}</a>'.format(user = user, post = post)
     return text
コード例 #12
0
def new_blog(request: schemas.Blog, db: Session = Depends(get_db)):
    new_blog = models.Blog(title=request.title, body=request.body)
    db.add(new_blog)
    db.commit()
    db.refresh(new_blog)
    return {"data_recieved": new_blog}
コード例 #13
0
ファイル: blog.py プロジェクト: bishnu1992/fastapi-blog
def create(request, db: Session = Depends(get_db)):
    new_blog = models.Blog(title = request.title, body = request.body, published = request.published, created_by = request.created_by)
    db.add(new_blog)
    db.commit()
    db.refresh(new_blog)
    return new_blog
コード例 #14
0
def create_new_blog(db: Session, blog: schemas.BlogBase):
    db_blog = models.Blog(title=blog.title, content=blog.content)
    db.add(db_blog)
    db.commit()
    db.refresh(db_blog)
    return db_blog
コード例 #15
0
def create_blog(title="Test Blog", num_posts=100):
    def paragraphs(count):
        return "\n".join("%s" % text for _ in xrange(count))

    blog = models.Blog(
        title=title,
        description=paragraphs(1),
        tagline="A blog about Life, the Universe and Everything Else",
        slug=slugify(title))

    blog.save()

    words = """random python I You like code life universe everything god """\
    """atheism hot very difficult easy hard dynamic of the in is under on through """\
    """extremely strangely hardly plainly transparently incredibly """\
    """goes remarkable not if interesting words phone computer will she he are is """\
    """entry base css html games demons juggling brown chess steal this computer """\
    """brave heart motor storm ruby on rails running beginning code plus minus are """

    words = words.split()

    def random_tags():
        num_words = randint(4, 8)
        tags = ", ".join(sample(words, num_words))
        return tags

    post = models.Post(blog=blog,
                       published=True,
                       title="Totally Excellent Blog Post",
                       tags_text=random_tags(),
                       slug="test-post",
                       content=test_post)
    post.save()

    def random_title():
        num_words = randint(2, 8)
        title = " ".join(sample(words, num_words))
        title += choice("!? ")
        return title.strip().title()

    display_time = datetime.now()
    for _ in xrange(num_posts):

        display_time -= timedelta(days=randint(2, 15))

        while True:
            title = random_title()
            slug = slugify(title)
            try:
                models.Post.objects.get(slug=slug)
            except models.Post.DoesNotExist:
                break

        post = models.Post(blog=blog,
                           published=True,
                           created_time=display_time,
                           display_time=display_time,
                           edit_time=display_time,
                           title=title,
                           tags_text=random_tags(),
                           slug=slugify(title),
                           content=paragraphs(randint(1, 5)))
        post.save()

    return blog
コード例 #16
0
def create(request:Blog,db:Session=Depends(get_db)):
    new_blog = models.Blog(title=request.title, body=request.body)
    db.add(new_blog)
    db.commit()
    db.refresh(new_blog)
    return new_blog