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)
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)
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)
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))
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))
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))
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)
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)
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 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))
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))
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))
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))
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)
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 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))
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
def testUnfollow(self): self.assertTrue(Interest.unfollow(1, 6))
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:
def testCountAll(self): count = Interest.countAll() self.assertTrue(is_integer(count))
def testCountAllAround(self): count = Interest.countAll(location=[-27.86403, -54.4593889], radius=20000) self.assertTrue(is_integer(count))
def interest_opaque_to_id(value): return Interest(id=value).id
def testFind(self): obj = Interest.find(1) self.assertEqual(obj.name, 'Photography') self.assertTrue(is_integer(obj.id))
def testFindAllByUserName(self): objs = Interest.findAllByUserName('john') self.assertTrue(len(objs) > 0) for obj in objs: self.assertTrue(isinstance(obj, Interest))
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()
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:
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))
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))
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))
def testNameExists(self): self.assertTrue(Interest.nameExists('Photography'))
def testExists(self): self.assertTrue(Interest.exists(1))
def testSearch(self): objs = Interest.search('m') self.assertTrue(len(objs) > 0) for obj in objs: self.assertTrue(isinstance(obj, Interest))
def testCountAllByUserName(self): count = Interest.countAllByUserName('john') self.assertTrue(is_integer(count))
def testFindAllRecent(self): objs = Interest.findAll() self.assertTrue(len(objs) > 0) for obj in objs: self.assertTrue(isinstance(obj, Interest))
def testFollowing(self): self.assertTrue(Interest.following(1, 6))
def testFindAllPopular(self): objs = Interest.findAll(order_by='popular') self.assertTrue(len(objs) > 0) for obj in objs: self.assertTrue(isinstance(obj, Interest))
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)