예제 #1
0
def show():
    user = User.findById(app.auth.user.id)
    	
    if user is not None:
        return json(user.to_self_dict())
    else:
        return json({'message': 'There is no authenticated user'}, 404)
예제 #2
0
def show():
    user = User.findById(app.auth.user.id)

    if user is not None:
        return json(user.to_self_dict())
    else:
        return json({'message': 'There is no authenticated user'}, 404)
예제 #3
0
def conversations():
    form = ConversationListForm(request)
    if not form.validate():
        return json({
            'message': 'Validation Failed',
            'errors': form.errors
        }, 422)

    total = Conversation.countAll(order_by=form.order,
                                  location=form.location,
                                  radius=form.radius,
                                  after=form.after)
    pg = paginate(form, total)

    if total > 0:
        if pg.page_valid:
            data = Conversation.findAll(order_by=form.order,
                                        location=form.location,
                                        radius=form.radius,
                                        offset=pg.offset,
                                        limit=pg.per_page,
                                        after=form.after)
        else:
            return json({'message': "Invalid interest or end of page list"},
                        404)
    else:
        data = []

    return json(dict(conversations=data, **pg.info))
예제 #4
0
def search(query):
    form = InterestSearchForm(url={'query': query})
    if not form.validate():
        return json({'message': 'Validation Failed', 'errors': form.errors}, 422)

    data = Interest.searchByUser(form.query, user_id=app.auth.user.id)
    return json({'interests': data})
예제 #5
0
def list_replies(interest_id, id):
    form = ConversationRepliesForm(request,
                                   url={
                                       'interest_id': interest_id,
                                       'id': id
                                   })
    if not form.validate():
        return json({
            'message': 'Validation Failed',
            'errors': form.errors
        }, 422)

    total = Conversation.countReplies(form.interest_id, form.id, form.after)
    pg = paginate(form, total)

    if total > 0:
        if pg.page_valid:
            data = Conversation.replies(form.interest_id, form.id, form.after,
                                        pg.offset, pg.per_page)
        else:
            return json({'message': "Invalid interest or end of page list"},
                        404)
    else:
        data = []

    return json(dict(replies=data, **pg.info))
예제 #6
0
def create():
    form = UserCreateForm(request)
    if not form.validate():
        return json({
            'message': 'Validation Failed',
            'errors': form.errors
        }, 422)

    try:
        user = User.create(User(**form.data))
        if user:
            if form.avatar:
                user.saveAvatar(form.avatar)
            return json(user, 201)
        else:
            return json(
                {'message': 'An error ocurred while creating the user'}, 400)
    except ValidationException, e:
        return json(
            {
                'message': 'Validation Failed',
                'errors': [{
                    'reason': e.reason,
                    'field': e.field_name
                }]
            }, 422)
예제 #7
0
def create(interest_id):
    form = ConversationCreateForm(request, url={'interest_id': interest_id})
    if not form.validate():
        return json({'message': 'Validation Failed', 'errors': form.errors}, 422)

    data = Conversation.create(Conversation.parse(form.data, user_id=app.auth.user.id))
    if data is False:
        return json({'message': 'An error ocurred while creating the conversation'}, 400)

    return json(data, 201)
예제 #8
0
def show_user(name):
    form = UserShowForm(url={'name': name})
    if not form.validate():
        return json({'message': 'Validation Failed', 'errors': form.errors}, 422)

    data = User.findByName(form.name)
    if data is not False:
        return json(data)
    else:
        return json({'message': 'User with given name not found'}, 404)
예제 #9
0
def create():
    form = InterestCreateForm(request)
    if not form.validate():
        return json({'message': 'Validation Failed', 'errors': form.errors}, 422)

    data = Interest.create(Interest(name=form.name))
    if data.id == 0:
        return json({'message': 'Interest already exists'}, 409)

    return json(data, 201)
예제 #10
0
def search(query):
    form = InterestSearchForm(url={'query': query})
    if not form.validate():
        return json({
            'message': 'Validation Failed',
            'errors': form.errors
        }, 422)

    data = Interest.searchByUser(form.query, user_id=app.auth.user.id)
    return json({'interests': data})
예제 #11
0
def show(id):
    form = InterestIdForm(url={'id': id})
    if not form.validate():
        return json({'message': 'Validation Failed', 'errors': form.errors}, 422)

    data = Interest.find(form.id, app.auth.user.id)
    if data is False:
        return json({'message': 'Interest with given ID not found'}, 404)

    return json(data)
예제 #12
0
def show_reply(interest_id, parent_id, id):
    form = ConversationShowReplyForm(url={'interest_id': interest_id, 'parent_id': parent_id, 'id': id})
    if not form.validate():
        return json({'message': 'Validation Failed', 'errors': form.errors}, 422)

    data = Conversation.findReply(form.interest_id, form.parent_id, form.id)
    if data is False:
        return json({'message': 'Conversation with given ID not found'}, 404)

    return json(data)
예제 #13
0
def show(id):
    form = InterestIdForm(url={'id': id})
    if not form.validate():
        return json({
            'message': 'Validation Failed',
            'errors': form.errors
        }, 422)

    data = Interest.find(form.id, app.auth.user.id)
    if data is False:
        return json({'message': 'Interest with given ID not found'}, 404)

    return json(data)
예제 #14
0
def show_user(name):
    form = UserShowForm(url={'name': name})
    if not form.validate():
        return json({
            'message': 'Validation Failed',
            'errors': form.errors
        }, 422)

    data = User.findByName(form.name)
    if data is not False:
        return json(data)
    else:
        return json({'message': 'User with given name not found'}, 404)
예제 #15
0
def create():
    form = InterestCreateForm(request)
    if not form.validate():
        return json({
            'message': 'Validation Failed',
            'errors': form.errors
        }, 422)

    data = Interest.create(Interest(name=form.name))
    if data.id == 0:
        return json({'message': 'Interest already exists'}, 409)

    return json(data, 201)
예제 #16
0
def show(interest_id, id):
    form = ConversationShowForm(url={'interest_id': interest_id, 'id': id})
    if not form.validate():
        return json({
            'message': 'Validation Failed',
            'errors': form.errors
        }, 422)

    data = Conversation.find(form.interest_id, form.id)
    if data is not False:
        return json(data)
    else:
        return json({'message': 'Conversation with given ID not found'}, 404)
예제 #17
0
        def decorated(*args, **kwargs):
            api_key = request.args.get('api_key')
            if not api_key:
                return json({"message": "You need an API Key to do this"}, 401)

            Application.set_config(app)

            application = Application.findByKey(api_key)
            if not application:
                return json({"message": "Your API Key is not valid"}, 401)

            if application.level < level:
                return json({"message": "Your application doesn't have enough rights to do this"}, 401)
            return f(*args, **kwargs)
예제 #18
0
def create():
    form = UserCreateForm(request)
    if not form.validate():
        return json({'message': 'Validation Failed', 'errors': form.errors}, 422)

    try:
        user = User.create(User(**form.data))
        if user:
            if form.avatar:
                user.saveAvatar(form.avatar)
            return json(user, 201)
        else:
            return json({'message': 'An error ocurred while creating the user'}, 400)
    except ValidationException, e:
        return json({'message': 'Validation Failed',
                     'errors': [{'reason': e.reason, 'field': e.field_name}]}, 422)
예제 #19
0
def create(interest_id):
    form = ConversationCreateForm(request, url={'interest_id': interest_id})
    if not form.validate():
        return json({
            'message': 'Validation Failed',
            'errors': form.errors
        }, 422)

    data = Conversation.create(
        Conversation.parse(form.data, user_id=app.auth.user.id))
    if data is False:
        return json(
            {'message': 'An error ocurred while creating the conversation'},
            400)

    return json(data, 201)
예제 #20
0
def followers(id):
    form = InterestFollowersForm(request, url={'id': id})
    if not form.validate():
        return json({'message': 'Validation Failed', 'errors': form.errors}, 422)

    total = Interest.countFollowers(form.id)
    pg = paginate(form, total)

    if total > 0:
        if pg.page_valid:
            data = Interest.followers(form.id, pg.offset, pg.per_page)
        else:
            return json({'message': "This is the end of the list"}, 404)
    else:
        data = []

    return json(dict(followers=data, **pg.info))
예제 #21
0
def following(id):
    form = InterestIdForm(url={'id': id})
    if not form.validate():
        return json({
            'message': 'Validation Failed',
            'errors': form.errors
        }, 422)

    user_id = app.auth.user.id

    if request.method == 'GET':
        if Interest.following(form.id, user_id):
            return json(code=204)
        else:
            return json(code=404)

    elif request.method == 'POST':
        if Interest.follow(form.id, user_id):
            return json(code=201)
        else:
            return json({'message': 'Interest with given ID not found'}, 400)

    elif request.method == 'DELETE':
        if Interest.unfollow(form.id, user_id):
            return json(code=204)
        else:
            return json({'message': 'Interest with given ID not found'}, 400)
예제 #22
0
def index():
    form = InterestIndexForm(request)
    if not form.validate():
        return json({'message': 'Validation Failed', 'errors': form.errors}, 422)

    total = Interest.countAll(form.location, form.radius)
    pg = paginate(form, total)

    if total > 0:
        if pg.page_valid:
            data = Interest.findAll(form.order, form.location, form.radius, pg.offset,
                                    pg.per_page, app.auth.user.id)
        else:
            return json({'message': "This is the end of the list"}, 404)
    else:
        data = []

    return json(dict(interests=data, **pg.info))
예제 #23
0
        def decorated(*args, **kwargs):
            api_key = request.args.get('api_key')
            if not api_key:
                return json({"message": "You need an API Key to do this"}, 401)

            Application.set_config(app)

            application = Application.findByKey(api_key)
            if not application:
                return json({"message": "Your API Key is not valid"}, 401)

            if application.level < level:
                return json(
                    {
                        "message":
                        "Your application doesn't have enough rights to do this"
                    }, 401)
            return f(*args, **kwargs)
예제 #24
0
def show_interests():
    form = UserListForm(request)
    if not form.validate():
        return json({'message': 'Validation Failed', 'errors': form.errors}, 422)

    user = app.auth.user
    total = Interest.countAllByUser(user.id)
    pg = paginate(form, total)

    if total > 0:
        if pg.page_valid:
            data = Interest.findAllByUser(user.id, pg.offset, pg.per_page)
        else:
            return json({'message': "This is the end of the list"}, 404)
    else:
        data = []

    return json(dict(interests=data, **pg.info))
예제 #25
0
def list_replies(interest_id, id):
    form = ConversationRepliesForm(request, url={'interest_id':interest_id, 'id':id})
    if not form.validate():
        return json({'message': 'Validation Failed', 'errors': form.errors}, 422)

    total = Conversation.countReplies(form.interest_id, form.id, form.after)
    pg = paginate(form, total)

    if total > 0:
        if pg.page_valid:
            data = Conversation.replies(form.interest_id, form.id, form.after,
                                        pg.offset, pg.per_page)
        else:
            return json({'message': "Invalid interest or end of page list"}, 404)
    else:
        data = []

    return json(dict(replies=data, **pg.info))
예제 #26
0
def show_conversations():
    form = UserActivityForm(request)
    if not form.validate():
        return json({'message': 'Validation Failed', 'errors': form.errors}, 422)

    user_id = app.auth.user.id
    total = Conversation.countAllBySubscription(user_id, form.after)
    pg = paginate(form, total)

    if total > 0:
        if pg.page_valid:
            data = Conversation.findAllBySubscription(user_id, pg.offset, pg.per_page, form.after)
        else:
            return json({'message': "This is the end of the list"}, 404)
    else:
        data = []

    return json(dict(conversations=data, **pg.info))
예제 #27
0
def index(interest_id):
    form = ConversationIndexForm(request, url={'interest_id': interest_id})
    if not form.validate():
        return json({'message': 'Validation Failed', 'errors': form.errors}, 422)

    total = Conversation.countAll(form.interest_id, form.order, form.location,
                                  form.radius, None, form.after)
    pg = paginate(form, total)

    if total > 0:
        if pg.page_valid:
            data = Conversation.findAll(form.interest_id, form.order, form.location,
                                        form.radius, pg.offset, pg.per_page, None, form.after)
        else:
            return json({'message': "Invalid interest or end of page list"}, 404)
    else:
        data = []

    return json(dict(conversations=data, **pg.info))
예제 #28
0
def show_user_conversations(name):
    form = UserNameListForm(request, url={'name': name})
    if not form.validate():
        return json({'message': 'Validation Failed', 'errors': form.errors}, 422)

    if not User.nameExists(form.name):
        return json({'message': 'User with given name not found'}, 404)

    total = Conversation.countAllByUserName(form.name)
    pg = paginate(form, total)

    if total > 0:
        if pg.page_valid:
            data = Conversation.findAllByUserName(form.name, pg.offset, pg.per_page)
        else:
            return json({'message': "This is the end of the list"}, 404)
    else:
        data = []

    return json(dict(conversations=data, **pg.info))
예제 #29
0
def conversations():
    form = ConversationListForm(request)
    if not form.validate():
        return json({'message': 'Validation Failed', 'errors': form.errors}, 422)

    total = Conversation.countAll(order_by=form.order, location=form.location,
                                  radius=form.radius, after=form.after)
    pg = paginate(form, total)

    if total > 0:
        if pg.page_valid:
            data = Conversation.findAll(order_by=form.order, location=form.location,
                                        radius=form.radius, offset=pg.offset,
                                        limit=pg.per_page, after=form.after)
        else:
            return json({'message': "Invalid interest or end of page list"}, 404)
    else:
        data = []

    return json(dict(conversations=data, **pg.info))
예제 #30
0
def followers(id):
    form = InterestFollowersForm(request, url={'id': id})
    if not form.validate():
        return json({
            'message': 'Validation Failed',
            'errors': form.errors
        }, 422)

    total = Interest.countFollowers(form.id)
    pg = paginate(form, total)

    if total > 0:
        if pg.page_valid:
            data = Interest.followers(form.id, pg.offset, pg.per_page)
        else:
            return json({'message': "This is the end of the list"}, 404)
    else:
        data = []

    return json(dict(followers=data, **pg.info))
예제 #31
0
def index():
    form = InterestIndexForm(request)
    if not form.validate():
        return json({
            'message': 'Validation Failed',
            'errors': form.errors
        }, 422)

    total = Interest.countAll(form.location, form.radius)
    pg = paginate(form, total)

    if total > 0:
        if pg.page_valid:
            data = Interest.findAll(form.order, form.location, form.radius,
                                    pg.offset, pg.per_page, app.auth.user.id)
        else:
            return json({'message': "This is the end of the list"}, 404)
    else:
        data = []

    return json(dict(interests=data, **pg.info))
예제 #32
0
def show_conversations():
    form = UserListForm(request)
    if not form.validate():
        return json({
            'message': 'Validation Failed',
            'errors': form.errors
        }, 422)

    user = app.auth.user
    total = Conversation.countAllByUser(user.id)
    pg = paginate(form, total)

    if total > 0:
        if pg.page_valid:
            data = Conversation.findAllByUser(user.id, pg.offset, pg.per_page)
        else:
            return json({'message': "This is the end of the list"}, 404)
    else:
        data = []

    return json(dict(conversations=data, **pg.info))
예제 #33
0
def update():
    form = UserUpdateForm(request)
    if not form.validate():
        return json({'message': 'Validation Failed', 'errors': form.errors}, 422)

    user = app.auth.user

    if user.name != form.name and User.nameExists(form.name):
        return json({'message': 'Validation Failed', 'errors': [
                {'code': 'already_exists', 'field': 'name'}]}, 422)

    if user.email != form.email and User.emailExists(form.email):
        return json({'message': 'Validation Failed', 'errors': [
                {'code': 'already_exists', 'field': 'email'}]}, 422)

    user.update_values(form.data)

    try:
        if form.avatar:
            user.saveAvatar(form.avatar)

        if user.update():
            app.auth.user = user
            return json(user, 200)
        else:
            return json({'message': 'An error ocurred while creating the user'}, 400)
    except ValidationException, e:
        return json({'message': 'Validation Failed', 'errors': [
                                                        {'reason': e.reason, 'field': e.field_name}]}, 422)
예제 #34
0
    def before_request(self):
        auth  = request.authorization
        token = request.args.get('auth_token')

        if token:
            if not self.check_token(token):
                return self.authenticate("Invalid token")
        elif auth:
            if not self.check_auth(auth):
                return self.authenticate()
            else:
                return json({"token": self.create_token(auth)}, 200)
        else:
            return self.authenticate()
예제 #35
0
def index(interest_id):
    form = ConversationIndexForm(request, url={'interest_id': interest_id})
    if not form.validate():
        return json({
            'message': 'Validation Failed',
            'errors': form.errors
        }, 422)

    total = Conversation.countAll(form.interest_id, form.order, form.location,
                                  form.radius, None, form.after)
    pg = paginate(form, total)

    if total > 0:
        if pg.page_valid:
            data = Conversation.findAll(form.interest_id, form.order,
                                        form.location, form.radius, pg.offset,
                                        pg.per_page, None, form.after)
        else:
            return json({'message': "Invalid interest or end of page list"},
                        404)
    else:
        data = []

    return json(dict(conversations=data, **pg.info))
예제 #36
0
def show_user_interests(name):
    form = UserNameListForm(request, url={'name': name})
    if not form.validate():
        return json({
            'message': 'Validation Failed',
            'errors': form.errors
        }, 422)

    if not User.nameExists(form.name):
        return json({'message': 'User with given name not found'}, 404)

    total = Interest.countAllByUserName(form.name)
    pg = paginate(form, total)

    if total > 0:
        if pg.page_valid:
            data = Interest.findAllByUserName(form.name, pg.offset,
                                              pg.per_page)
        else:
            return json({'message': "This is the end of the list"}, 404)
    else:
        data = []

    return json(dict(interests=data, **pg.info))
예제 #37
0
def update():
    form = UserUpdateForm(request)
    if not form.validate():
        return json({
            'message': 'Validation Failed',
            'errors': form.errors
        }, 422)

    user = app.auth.user

    if user.name != form.name and User.nameExists(form.name):
        return json(
            {
                'message': 'Validation Failed',
                'errors': [{
                    'code': 'already_exists',
                    'field': 'name'
                }]
            }, 422)

    if user.email != form.email and User.emailExists(form.email):
        return json(
            {
                'message': 'Validation Failed',
                'errors': [{
                    'code': 'already_exists',
                    'field': 'email'
                }]
            }, 422)

    user.update_values(form.data)

    try:
        if form.avatar:
            user.saveAvatar(form.avatar)

        if user.update():
            app.auth.user = user
            return json(user, 200)
        else:
            return json(
                {'message': 'An error ocurred while creating the user'}, 400)
    except ValidationException, e:
        return json(
            {
                'message': 'Validation Failed',
                'errors': [{
                    'reason': e.reason,
                    'field': e.field_name
                }]
            }, 422)
예제 #38
0
def following(id):
    form = InterestIdForm(url={'id': id})
    if not form.validate():
        return json({'message': 'Validation Failed', 'errors': form.errors}, 422)

    user_id = app.auth.user.id

    if request.method == 'GET':
        if Interest.following(form.id, user_id):
            return json(code=204)
        else:
            return json(code=404)

    elif request.method == 'POST':
        if Interest.follow(form.id, user_id):
            return json(code=201)
        else:
            return json({'message': 'Interest with given ID not found'}, 400)

    elif request.method == 'DELETE':
        if Interest.unfollow(form.id, user_id):
            return json(code=204)
        else:
            return json({'message': 'Interest with given ID not found'}, 400)
예제 #39
0
def authenticate():
    return json({'message': 'You are already authenticated.'})
예제 #40
0
def main():
    return json({'url': '/'})
예제 #41
0
def authenticate():
    return json({'message': 'You are already authenticated.'})
예제 #42
0
 def authenticate(self, message="Bad credentials"):
     return json({"message":message}, 401, {'WWW-Authenticate': 'Basic realm="Login Required"'})
예제 #43
0
def main():
    return json({'url': '/'})
예제 #44
0
                user.saveAvatar(form.avatar)
            return json(user, 201)
        else:
            return json(
                {'message': 'An error ocurred while creating the user'}, 400)
    except ValidationException, e:
        return json(
            {
                'message': 'Validation Failed',
                'errors': [{
                    'reason': e.reason,
                    'field': e.field_name
                }]
            }, 422)
    except UniqueViolationException, e:
        return json({'message': 'User name or email already exists'}, 409)


# GET  /users/:username  -------------------------------------------------------
@module.route('/users/<name>')
@require_user_auth(app)
def show_user(name):
    form = UserShowForm(url={'name': name})
    if not form.validate():
        return json({
            'message': 'Validation Failed',
            'errors': form.errors
        }, 422)

    data = User.findByName(form.name)
    if data is not False:
예제 #45
0
    if not form.validate():
        return json({'message': 'Validation Failed', 'errors': form.errors}, 422)

    try:
        user = User.create(User(**form.data))
        if user:
            if form.avatar:
                user.saveAvatar(form.avatar)
            return json(user, 201)
        else:
            return json({'message': 'An error ocurred while creating the user'}, 400)
    except ValidationException, e:
        return json({'message': 'Validation Failed',
                     'errors': [{'reason': e.reason, 'field': e.field_name}]}, 422)
    except UniqueViolationException, e:
        return json({'message': 'User name or email already exists'}, 409)


# GET  /users/:username  -------------------------------------------------------
@module.route('/users/<name>')
@require_user_auth(app)
def show_user(name):
    form = UserShowForm(url={'name': name})
    if not form.validate():
        return json({'message': 'Validation Failed', 'errors': form.errors}, 422)

    data = User.findByName(form.name)
    if data is not False:
        return json(data)
    else:
        return json({'message': 'User with given name not found'}, 404)