示例#1
0
def update_blog_post(request):
    print("Processing {} request from {} for the HTTP service: {}, ua: {}".
          format(request.method, get_ip(request), request.url,
                 request.user_agent))

    data = build_dict(request)
    post_id = data.get('post_id')

    if MemoryDb.is_post_read_only(post_id):
        return Response(
            '{"error":"The post with id ' + str(post_id) + ' is read-only. '
            'You can only edit posts that you have created yourself via this API."}',
            status=403)

    post = MemoryDb.get_post(post_id, get_ip(request))
    if not post:
        return Response('{"error":"Post with ID not found: ' + post_id + '"}',
                        status=404)

    try:
        post_data = request.json_body
    except Exception as x:
        return Response('{"error":"Bad request {0}"}'.format(x), status=400)

    post.title = post_data.get('title', post.title)
    post.content = post_data.get('content', post.content)
    post.view_count = try_int(post_data.get('view_count', post.view_count),
                              post.view_count)
    post.published = try_date(post_data.get('published', post.published),
                              datetime.date.today().isoformat())
    trim_post_size(post)

    request.response.status_code = 204

    return post
def update_post(dom, request):
    post_id = dom.find('Body/UpdatePost/id').text

    post = MemoryDb.get_post(post_id, get_ip(request))
    if not post:
        raise exception_response(404)

    if MemoryDb.is_post_read_only(post_id):
        raise exception_response(403)

    post.title = dom.find('Body/UpdatePost/title').text
    post.content = dom.find('Body/UpdatePost/content').text
    post.view_count = int(dom.find('Body/UpdatePost/viewCount').text)

    resp_xml = """<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <UpdatePostResponse xmlns="http://tempuri.org/">
      <UpdatePostResult>
        <Id>{}</Id>
        <Title>{}</Title>
        <Published>{}</Published>
        <Content>{}</Content>
        <ViewCount>{}</ViewCount>
      </UpdatePostResult>
    </UpdatePostResponse>
  </soap:Body>
</soap:Envelope>""".format(post.id, post.title, post.published, post.content, post.view_count)

    return Response(body=resp_xml, content_type='text/xml')
示例#3
0
def blog_post_create(request):
    print("Processing {} request from {} for the HTTP service: {}, ua: {}".
          format(request.method, get_ip(request), request.url,
                 request.user_agent))

    all_posts = MemoryDb.get_posts(get_ip(request))
    if len(all_posts) > POST_LIMIT_PER_USER:
        MemoryDb.clear_posts(get_ip(request))
        return Response(
            '{"error":"Too many posts created. Your blog has reset to the three default posts. '
            'You may now add more"}',
            status=400)

    try:
        post_data = request.json_body
    except Exception as x:
        return Response('{"error":"Bad request ' + str(x) + '"}', status=400)

    post = Post(
        post_data.get('title'), post_data.get('content'),
        try_int(post_data.get('view_count', 0), 0),
        try_date(post_data.get('published'),
                 datetime.date.today().isoformat()))
    trim_post_size(post)

    MemoryDb.add_post(post, get_ip(request))
    request.response.status_code = 201

    return post
def create_post(dom, request):
    title = dom.find('Body/CreatePost/title').text
    content = dom.find('Body/CreatePost/content').text
    view_count = int(dom.find('Body/CreatePost/viewCount').text)

    now = datetime.now()
    published = "{}-{}-{}".format(now.year, str(now.month).zfill(2), str(now.day).zfill(2))

    post = Post(
        title,
        content,
        view_count,
        published
    )

    MemoryDb.add_post(post, get_ip(request))

    resp_xml = """<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <CreatePostResponse xmlns="http://tempuri.org/">
      <CreatePostResult>
        <Id>{}</Id>
        <Title>{}</Title>
        <Published>{}</Published>
        <Content>{}</Content>
        <ViewCount>{}</ViewCount>
      </CreatePostResult>
    </CreatePostResponse>
  </soap:Body>
</soap:Envelope>""".format(post.id, post.title, post.published, post.content, post.view_count)

    return Response(body=resp_xml, content_type='text/xml')
def all_post_response(request):
    posts = MemoryDb.get_posts(get_ip(request))

    post_template = """
    <Post>
      <Id>{}</Id>
      <Title>{}</Title>
      <Published>{}</Published>
      <Content>{}</Content>
      <ViewCount>{}</ViewCount>
    </Post>"""

    posts_fragments = [
        post_template.format(p.id, p.title, p.published, p.content, p.view_count)
        for p in posts
        ]

    resp_xml = """<?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
          <soap:Body>
            <AllPostsResponse xmlns="http://tempuri.org/">
              <AllPostsResult>

                {}

              </AllPostsResult>
            </AllPostsResponse>
          </soap:Body>
        </soap:Envelope>""".format("\n".join(posts_fragments))

    return Response(body=resp_xml, content_type='text/xml')
示例#6
0
def blog_posts(request):
    print("Processing {} request from {} for the HTTP service: {}, ua: {}".
          format(request.method, get_ip(request), request.url,
                 request.user_agent))
    ip = get_ip(request)
    posts = MemoryDb.get_posts(ip)

    return posts
def delete_post_response(dom, request):
    id_text = dom.find('Body/DeletePost/id').text
    post = MemoryDb.get_post(id_text, get_ip(request))
    if not post:
        raise exception_response(404)

    if MemoryDb.is_post_read_only(post.id):
        raise exception_response(403)

    MemoryDb.delete_post(post, get_ip(request))

    resp_xml = """<?xml version="1.0" encoding="utf-8"?>
                <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
                  <soap:Body>
                    <DeletePostResponse xmlns="http://tempuri.org/" />
                  </soap:Body>
                </soap:Envelope>"""

    return Response(body=resp_xml, content_type='text/xml')
示例#8
0
def blog_post(request):
    print("Processing {} request from {} for the HTTP service: {}, ua: {}".
          format(request.method, get_ip(request), request.url,
                 request.user_agent))

    data = build_dict(request)
    post_id = data.get('post_id')

    post = MemoryDb.get_post(post_id, get_ip(request))
    if not post:
        return Response('{"error":"Post with ID not found: ' + post_id + '"}',
                        status=404)

    return post
示例#9
0
def delete_blog_post(request):
    print("Processing {} request from {} for the HTTP service: {}, ua: {}".
          format(request.method, get_ip(request), request.url,
                 request.user_agent))

    data = build_dict(request)
    post_id = data.get('post_id')

    if MemoryDb.is_post_read_only(post_id):
        return Response(
            '{"error":"The post with id ' + str(post_id) + ' is read-only. '
            'You can only delete posts that you have created yourself via this API."}',
            status=403)

    post = MemoryDb.get_post(post_id, get_ip(request))
    if not post:
        return Response('{"error":"Post with ID not found: ' + post_id + '"}',
                        status=404)

    MemoryDb.delete_post(post, get_ip(request))
    request.response.status_code = 202

    return {'deleted': post_id}
示例#10
0
def reset(request):
    print("Processing reset request from {} for the HTTP service: {}, ua: {}".
          format(get_ip(request), request.url, request.user_agent))

    MemoryDb.clear_posts(get_ip(request))
    return {"status": "All private data reset."}