Пример #1
0
    def test_list_posts(self):
        posts = Post.objects.skip(2).limit(2).all()
        posts = Post.list_posts()
        response = list(posts)
        print(response)

        if __name__ == "__main__":
            unittest.main()
Пример #2
0
 def test_create_post(self, verify_id_token):
     """Checks to create a post properly."""
     # insert user then create post
     response = self.ping_to_create_post(user=mock_user_1)
     post = Post.objects().first()
     self.assertEqual(response.status_code, 200)
     self.assertEqual(post["title"], "mock_title")
     self.assertEqual(post["description"], "hello world")
     self.assertRegex(post["url"], "https://storage.googleapis.com/.*")
Пример #3
0
def route_create_post():
    uid = request.headers.get("uid", None)
    if not uid:
        abort(401)

    params = request.form.to_dict(flat=True)
    post_image = request.files.get("post_image", None)
    description = params.get("description", None)
    title = params.get("title", None)
    enable_comment = params.get("enable_comment", 'false')
    enable_comment = True if enable_comment == "true" else False

    user = User.objects(uid=uid).get_or_404()

    url = None
    if post_image:
        # update to image server
        bucket = storage.bucket()
        blob = bucket.blob('post_images/{uid}/{uuid}_{timestamp}'.format(
            uid=uid, uuid=uuid.uuid1(),
            timestamp=pendulum.now().int_timestamp))
        blob.upload_from_file(post_image)
        url = blob.public_url

    if not post_image and not params.get("description", ""):
        raise ValueError("이미지 및 게시글 중 최소 한개는 충족 되어야 합니다.")

    # create a post in mongo db.
    post = Post(author=user,
                title=title,
                description=description,
                url=url,
                created_at=pendulum.now().int_timestamp,
                enable_comment=enable_comment)
    post.save()

    return Response(post.to_json(follow_reference=True, max_depth=1),
                    mimetype="application/json")
Пример #4
0
def route_list_posts():
    uid = request.headers.get("uid", None)
    user = User.get(uid=uid)
    opposite_sex = "M" if user.sex == "F" else "F"

    last_id: str = request.args.get("last_id", None)
    per_page: int = int(request.args.get("per_page", 30))

    params = dict(author_sex=opposite_sex, limit=per_page, is_deleted=False)
    if last_id:
        params["id__lt"] = last_id

    result = Post.list_posts(**params)

    response = encode(list(result))
    return Response(response, mimetype="application/json")
Пример #5
0
def route_list_posts():
    """Lists all posts not filtering opposite sex things."""
    uid = request.headers.get("uid", None)
    admin = Admin.objects.get_or_404(uid=uid)

    if not admin.available:
        abort(401)

    last_id: str = request.args.get("last_id", None)
    per_page: int = int(request.args.get("per_page", 30))

    params = dict(is_deleted=False, limit=per_page)
    if last_id:
        params["id__lt"] = last_id

    result = Post.list_posts(**params)

    response = encode(list(result))
    return Response(response, mimetype="application/json")
Пример #6
0
def route_create_post():
    uid = request.headers.get("uid", None)
    if not uid:
        abort(401)

    params = request.form.to_dict(flat=True)
    post_images = request.files.values()
    description = params.get("description", None)
    title = params.get("title", None)
    enable_comment = params.get("enable_comment", 'false')
    enable_comment = True if enable_comment == "true" else False

    user = User.objects(uid=uid).get_or_404()

    resources = []
    for post_image in post_images:
        # update to image server
        bucket = storage.bucket()
        blob = bucket.blob('post_images/{uid}/{uuid}_{timestamp}'.format(
            uid=uid, uuid=uuid.uuid1(),
            timestamp=pendulum.now().int_timestamp))
        blob.upload_from_file(post_image)
        resource = Resource(type="IMAGE", url=blob.public_url)
        resources.append(resource)

    if not post_images and not params.get("description", ""):
        raise ValueError("이미지 및 게시글 중 최소 한개는 충족 되어야 합니다.")

    # create a post in mongo db.
    post = Post.create(author=user,
                       title=title,
                       description=description,
                       resources=resources,
                       created_at=pendulum.now().int_timestamp,
                       enable_comment=enable_comment)

    response = json.dumps(dict(_id=str(post.id)))
    return Response(response, mimetype="application/json")
Пример #7
0
 def test_post_page(self):
     params = dict(id__lt="60218f0c11062b81bd9e28ca")
     posts = Post.objects(**params).order_by('-id').limit(20).all()
Пример #8
0
def route_get_post(post_id):
    post = Post.get_post(id=post_id)
    response = encode(post)
    return Response(response, mimetype="application/json")