Ejemplo n.º 1
0
def update(request, post_id):
    user = request.user 
    if request.method != 'POST':
        raise Http404 

    post = Post.get_by_id(int(post_id))
    if not post or not post.belongs_to(user):
        return util.respond(False, 'Post doesn\'t exist')

    post_data = {}
    for k in ('title', 'body'):
        if not k in request.POST:
            return util.respond(False, 'Missing argument \'%s\''%k)
        
        post_data[k] = request.POST[k]

        
    title_len = len(post_data['title'])
    if title_len > 80 or title_len < 1:
        return util.respond(False, 'Post title must be between 1 and 80 letters')

    body_len = len(post_data['body'])
    if body_len > 1024 or body_len < 1:
        return util.respond(False, 'Post body must be between 1 and 1024 letters')
    
    post.title = util.xssescape(post_data['title'])
    post.body = util.xssescape(post_data['body'])

    post.put()
    
    return util.respond(True, post.to_dict())
Ejemplo n.º 2
0
def get(request, post_id):
    if request.method != 'POST':
        raise Http404 

    post = Post.get_by_id(int(post_id))
    if not post:
        return util.respond(False, 'Post doesn\'t exist')
    
    return util.respond(True, post.to_dict())
Ejemplo n.º 3
0
def get(request, user_id):
    if request.method != 'GET':
        raise Http404

    user = User.get_by_id(int(user_id))
    if not user:
        return util.respond(False, 'User doesn\'t exist')

    return util.respond(True, user.to_dict())
Ejemplo n.º 4
0
def fetch(request):
    if request.method != 'GET':
        raise Http404

    params = {}
    for k in ('since_id', 'until_id', 'count'):
        if k in request.GET:
            params[k] = request.GET[k]

    since_post = None
    until_post = None 
    if 'since_id' in params:
        since_post = Post.get_by_id(int(params['since_id']))

    elif 'until_id' in params:
        until_post = Post.get_by_id(int(params['until_id']))

    count = 8
    if 'count' in params and re.match('\d+', params['count']):
        count = int(params['count'])
        if not (count >= 1 and count <= 8):
            count = 8
        
    posts = Post.fetch(**{
        'since_post' : since_post, 
        'until_post' : until_post,
        'count' : count
    })
    
    return util.respond(True, [post.to_dict() for post in posts])
Ejemplo n.º 5
0
def destroy(request, post_id):
    user = request.user 
    if request.method != 'POST':
        raise Http404 

    post = Post.get_by_id(int(post_id))
    if not post or not post.belongs_to(user):
        return util.respond(False, 'Post doesn\'t exist')
    
    data = post.to_dict()
    post.delete()

    user.posts_count -= 1
    user.put(True)
    
    return util.respond(True, data)
Ejemplo n.º 6
0
def create(request):
    if request.method != 'POST':
        raise Http404

    user = {}
    for k in ('password', 'email', 'name'):
        if not k in request.POST:
            return util.respond(False, 'Missing argument \'%s\''%k)

        user[k] = request.POST[k]

    if not re.match(email_pattern, user['email']):
        return util.respond(False, 'That doesn\'t look like an email')

    user = User(**user)
    try:
        user.put()
        request.session['user'] = user.to_dict()

    except Exception, e:
        return util.respond(False, e.message)
Ejemplo n.º 7
0
def create(request):
    user = request.user 
    if request.method != 'POST':
        raise Http404

    post = {}
    for k in ('title', 'body'):
        if not k in request.POST:
            return util.respond(False, 'Missing argument \'%s\''%k)
        
        post[k] = request.POST[k]

    title_len = len(post['title'])
    if title_len > 80 or title_len < 1:
        return util.respond(False, 'Post title must be between 1 and 80 letters')

    body_len = len(post['body'])
    if body_len > 1024 or body_len < 1:
        return util.respond(False, 'Post body must be between 1 and 1024 letters')
    
    if user.has_post(post['title']):
        return util.respond(False, 'You already have a post with that title')

    post['title'] = util.xssescape(post['title'])
    post['body'] = util.xssescape(post['body'])

    post['author'] = user
    post = Post(**post)
    try:
        post.put()

        user.posts_count += 1
        user.put(True)

    except Exception, e:
        return util.respond(False, e.message)
Ejemplo n.º 8
0
def login(request):
    if request.method != 'POST':
        raise Http404

    user = {}
    for k in ('password', 'email'):
        if not k in request.POST:
            return util.respond(False, 'Missing argument \'%s\''%k)

        user[k] = request.POST[k]

    if 'user' in request.session:
        return HttpResponseNotModified()

    try:
        user = User.exists(**user)
        if not user:
            return util.respond(False, 'Email or password incorrect')

        request.session['user'] = user.to_dict()
        return util.respond(True, user.to_dict())
    
    except Exception, e:
        return util.respond(False, e.message)
Ejemplo n.º 9
0
    post['title'] = util.xssescape(post['title'])
    post['body'] = util.xssescape(post['body'])

    post['author'] = user
    post = Post(**post)
    try:
        post.put()

        user.posts_count += 1
        user.put(True)

    except Exception, e:
        return util.respond(False, e.message)
    
    return util.respond(True, post.to_dict())

def fetch(request):
    if request.method != 'GET':
        raise Http404

    params = {}
    for k in ('since_id', 'until_id', 'count'):
        if k in request.GET:
            params[k] = request.GET[k]

    since_post = None
    until_post = None 
    if 'since_id' in params:
        since_post = Post.get_by_id(int(params['since_id']))
Ejemplo n.º 10
0
            return util.respond(False, 'Missing argument \'%s\''%k)

        user[k] = request.POST[k]

    if not re.match(email_pattern, user['email']):
        return util.respond(False, 'That doesn\'t look like an email')

    user = User(**user)
    try:
        user.put()
        request.session['user'] = user.to_dict()

    except Exception, e:
        return util.respond(False, e.message)

    return util.respond(True, user.to_dict())

def get(request, user_id):
    if request.method != 'GET':
        raise Http404

    user = User.get_by_id(int(user_id))
    if not user:
        return util.respond(False, 'User doesn\'t exist')

    return util.respond(True, user.to_dict())

def login(request):
    if request.method != 'POST':
        raise Http404