예제 #1
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)
    def testCreate(self):
        name = 'teste_%s' % str(datetime.now())
        obj = Interest.create(Interest(name=name))
        self.assertEqual(obj.name, name)
        self.assertTrue(is_integer(obj.id), 'ID was supposed to be int. Value: %s' % str(obj.id))

        try:
            name = 't' * 255
            obj = Interest.create(Interest(name=name))
            self.fail("Create interest should not succeed with invalid data")
        except ValidationException, e:
            self.assertTrue(True)
예제 #3
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)
예제 #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})
 def testFollowers(self):
     followers = Interest.followers(1)
     following = False
     for follower in followers:
         if follower.id == 6:
             following = True
     self.assertTrue(following)
예제 #6
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))
예제 #7
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))
예제 #8
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))
예제 #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 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)
예제 #11
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})
예제 #12
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))
예제 #13
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))
예제 #14
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))
예제 #15
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))
예제 #16
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)
예제 #17
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)
예제 #18
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))
예제 #19
0
    def parse(row, **kwargs):
        if not row:
            return None

        if len(kwargs) > 0:
            row.update(kwargs)

        obj = Conversation(**row)
        obj.author = User(id=row.get('user_id'),
                          name=row.get('username'),
                          avatar_url=row.get('avatar_url'))

        if row.get('lat') and row.get('lon'):
            obj.location = {'lat': row.get('lat'), 'lon': row.get('lon')}

        if row.get('interest_id') or row.get('interest_name'):
            obj.interest = Interest(id=row.get('interest_id'),
                                    name=row.get('interest_name'))

        if row.get('parent_id') or row.get('parent_name'):
            obj.parent = Conversation(id=row.get('parent_id'),
                                      name=row.get('parent_name'))

        return obj
예제 #20
0
 def testUnfollow(self):
     self.assertTrue(Interest.unfollow(1, 6))
예제 #21
0
from boardhood.helpers.response import json
from boardhood.helpers.image import resize
from boardhood.helpers.pagination import paginate
from boardhood.helpers.auth import require_user_auth
from boardhood.models.applications import Application, require_app_auth
from boardhood.models.users import User
from boardhood.models.interests import Interest
from boardhood.models.conversations import Conversation
from boardhood.models.base import ValidationException, UniqueViolationException
from boardhood.forms import (UserCreateForm, UserUpdateForm, UserListForm,
                             UserNameListForm, UserShowForm,
                             InterestSearchForm, UserActivityForm)

module = Blueprint('users', __name__)
User.register_app(module, app)
Interest.register_app(module, app)
Conversation.register_app(module, app)


# POST /users  -----------------------------------------------------------------
@module.route('/users', methods=['POST'])
@require_app_auth(app, Application.LEVEL_ALL)
def create():
    form = UserCreateForm(request)
    if not form.validate():
        return json({
            'message': 'Validation Failed',
            'errors': form.errors
        }, 422)

    try:
예제 #22
0
 def testCountAll(self):
     count = Interest.countAll()
     self.assertTrue(is_integer(count))
예제 #23
0
 def testCountAllAround(self):
     count = Interest.countAll(location=[-27.86403, -54.4593889], radius=20000)
     self.assertTrue(is_integer(count))
예제 #24
0
def interest_opaque_to_id(value):
    return Interest(id=value).id
예제 #25
0
 def testFind(self):
     obj = Interest.find(1)
     self.assertEqual(obj.name, 'Photography')
     self.assertTrue(is_integer(obj.id))
예제 #26
0
    def testFindAllByUserName(self):
        objs = Interest.findAllByUserName('john')
        self.assertTrue(len(objs) > 0)

        for obj in objs:
            self.assertTrue(isinstance(obj, Interest))
예제 #27
0
                cursor.execute(CREATE_NO_LOCATION, c.to_sqldict())
            else:
                cursor.execute(CREATE, c.to_sqldict())

            Conversation.db.commit()
            data = cursor.fetchone()
            c.id = data['id']
            c.created_at = data['created_at']
            return c
        except ShieldException, e:
            raise ValidationException(e)
        except DatabaseError, e:
            if e.pgcode == '23505':  # already exists
                return c
            else:
                Interest.report_error(e, cursor)
                return False
        except Exception, e:
            print e
            Conversation.report_error(e, cursor)
            return False

    @staticmethod
    def countAll(interest_id=None,
                 order_by='recent',
                 location=None,
                 radius=None,
                 user_id=None,
                 after=None):
        try:
            cursor = Conversation.cursor()
예제 #28
0
from boardhood.helpers.response import json
from boardhood.helpers.image import resize
from boardhood.helpers.pagination import paginate
from boardhood.helpers.auth import require_user_auth
from boardhood.models.applications import Application, require_app_auth
from boardhood.models.users import User
from boardhood.models.interests import Interest
from boardhood.models.conversations import Conversation
from boardhood.models.base import ValidationException, UniqueViolationException
from boardhood.forms import (UserCreateForm, UserUpdateForm, UserListForm,
                             UserNameListForm, UserShowForm, InterestSearchForm,
                             UserActivityForm)

module = Blueprint('users', __name__)
User.register_app(module, app)
Interest.register_app(module, app)
Conversation.register_app(module, app)


# POST /users  -----------------------------------------------------------------
@module.route('/users', methods=['POST'])
@require_app_auth(app, Application.LEVEL_ALL)
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:
예제 #29
0
    def testFindAllPopularAround(self):
        objs = Interest.findAll(order_by='popular', location=[-27.86403, -54.4593889], radius=20000)
        self.assertTrue(len(objs) > 0)

        for obj in objs:
            self.assertTrue(isinstance(obj, Interest))
예제 #30
0
    def testFindAllRecentAround(self):
        objs = Interest.findAll(location=[-27.86403, -54.4593889], radius=20000)
        self.assertTrue(len(objs) > 0)

        for obj in objs:
            self.assertTrue(isinstance(obj, Interest))
예제 #31
0
    def testFindAllOrderByDistance(self):
        objs = Interest.findAll(order_by='distance', location=[-27.86403, -54.4593889])
        self.assertTrue(len(objs) > 0)

        for obj in objs:
            self.assertTrue(isinstance(obj, Interest))
예제 #32
0
 def testNameExists(self):
     self.assertTrue(Interest.nameExists('Photography'))
예제 #33
0
 def testExists(self):
     self.assertTrue(Interest.exists(1))
예제 #34
0
    def testSearch(self):
        objs = Interest.search('m')
        self.assertTrue(len(objs) > 0)

        for obj in objs:
            self.assertTrue(isinstance(obj, Interest))
예제 #35
0
 def testCountAllByUserName(self):
     count = Interest.countAllByUserName('john')
     self.assertTrue(is_integer(count))
예제 #36
0
    def testFindAllRecent(self):
        objs = Interest.findAll()
        self.assertTrue(len(objs) > 0)

        for obj in objs:
            self.assertTrue(isinstance(obj, Interest))
예제 #37
0
 def testFollowing(self):
     self.assertTrue(Interest.following(1, 6))
예제 #38
0
    def testFindAllPopular(self):
        objs = Interest.findAll(order_by='popular')
        self.assertTrue(len(objs) > 0)

        for obj in objs:
            self.assertTrue(isinstance(obj, Interest))
예제 #39
0
                cursor.execute(CREATE_NO_LOCATION, c.to_sqldict())
            else:
                cursor.execute(CREATE, c.to_sqldict())

            Conversation.db.commit()
            data = cursor.fetchone()
            c.id = data['id']
            c.created_at = data['created_at']
            return c
        except ShieldException, e:
            raise ValidationException(e)
        except DatabaseError, e:
            if e.pgcode == '23505': # already exists
                return c
            else:
                Interest.report_error(e, cursor)
                return False
        except Exception, e:
            print e
            Conversation.report_error(e, cursor)
            return False

    @staticmethod
    def countAll(interest_id=None, order_by='recent', location=None, radius=None,
                             user_id=None, after=None):
        try:
            cursor = Conversation.cursor()
            sql, args = build_find_all_query('count', interest_id=interest_id,
                                                                             order_by=order_by, location=location,
                                                                             radius=radius, user_id=user_id,
                                                                             after=after)