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))
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))
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)
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)
def testReplies(self): replies = Conversation.replies(6, 2) self.assertTrue(is_array(replies)) self.assertTrue(len(replies) > 0) for reply in replies: self.assertTrue(isinstance(reply, Conversation))
def testFindAllRecentByInterest(self): objs = Conversation.findAll(interest_id=6) self.assertTrue(is_array(objs)) self.assertTrue(len(objs) > 0) for obj in objs: self.assertTrue(isinstance(obj, Conversation))
def testFindAllAroundRadiusByUser(self): objs = Conversation.findAll(location=[-27.8727, -54.4951], radius=20000, user_id=2) self.assertTrue(is_array(objs)) self.assertTrue(len(objs) > 0) for obj in objs: self.assertTrue(isinstance(obj, Conversation))
def testFindAllBySubscription(self): objs = Conversation.findAllBySubscription(1) self.assertTrue(is_array(objs)) self.assertTrue(len(objs) > 0) for obj in objs: self.assertTrue(isinstance(obj, Conversation))
def testFindAllAround(self): objs = Conversation.findAll(location=[-27.8727, -54.4951]) self.assertTrue(is_array(objs)) self.assertTrue(len(objs) > 0) for obj in objs: self.assertTrue(isinstance(obj, Conversation))
def testFindAllPopularByUser(self): objs = Conversation.findAll(user_id=2, order_by='popular') self.assertTrue(is_array(objs)) self.assertTrue(len(objs) > 0) for obj in objs: self.assertTrue(isinstance(obj, Conversation))
def testFindAllRecentByUser(self): objs = Conversation.findAll(user_id=2) self.assertTrue(is_array(objs)) self.assertTrue(len(objs) > 0) for obj in objs: self.assertTrue(isinstance(obj, Conversation))
def testFindAllByUserName(self): objs = Conversation.findAllByUserName('john') self.assertTrue(is_array(objs)) self.assertTrue(len(objs) > 0) for obj in objs: self.assertTrue(isinstance(obj, Conversation))
def testFindAllAfter(self): date = datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M") objs = Conversation.findAll(after=date) self.assertTrue(is_array(objs)) self.assertTrue(len(objs) > 0) for obj in objs: self.assertTrue(isinstance(obj, Conversation))
def testFindAllAroundByInterest(self): interest_id = 6 objs = Conversation.findAll(interest_id=interest_id, location=[-27.8727, -54.4951]) self.assertTrue(is_array(objs)) self.assertTrue(len(objs) > 0) for obj in objs: self.assertTrue(isinstance(obj, Conversation)) self.assertTrue(obj.interest.id == interest_id)
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))
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))
def testFindAllPopular(self): objs = Conversation.findAll(order_by='popular') self.assertTrue(is_array(objs)) self.assertTrue(len(objs) > 0) for obj in objs: self.assertTrue(isinstance(obj, Conversation)) if len(objs) > 2: self.assertTrue(objs[0].score >= objs[1].score, 'First object should have a greater score')
def testFindAllRecent(self): objs = Conversation.findAll() self.assertTrue(is_array(objs)) self.assertTrue(len(objs) > 0) for obj in objs: self.assertTrue(isinstance(obj, Conversation)) if len(objs) > 2: self.assertTrue(objs[0].created_at >= objs[1].created_at, 'First object should be newer')
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)
def testFindAllAroundOrderByDistance(self): objs = Conversation.findAll(location=[-27.8727, -54.4951], order_by='distance') self.assertTrue(is_array(objs)) self.assertTrue(len(objs) > 0) for obj in objs: self.assertTrue(isinstance(obj, Conversation)) if len(objs) > 2: self.assertTrue(objs[0].distance <= objs[1].distance, 'First object should have a smaller distance')
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))
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))
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))
def testCreate(self): # create without location data obj = Conversation.parse({ 'interest_id' : 1, 'user_id' : 1, 'message' : "Hello there, this is just a test, ok?" }) obj = Conversation.create(obj) self.assertTrue(is_integer(obj.id)) # create a reply to the conversation above obj2 = Conversation.parse({ 'interest_id' : 1, 'user_id' : 2, 'message' : "And this is a reply.", 'lat' : -27.8738, 'lon' : -54.4761, 'parent_id' : obj.id }) obj2 = Conversation.create(obj2) self.assertTrue(is_integer(obj2.id))
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))
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)
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))
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))
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: user.saveAvatar(form.avatar)
def testFindReply(self): obj = Conversation.findReply(6, 2, 7) self.assertTrue(isinstance(obj, Conversation)) self.assertTrue(is_integer(obj.id))
def testCountAllByUser(self): count = Conversation.countAllByUser(1) self.assertTrue(is_integer(count))
def testCountAllByUserName(self): count = Conversation.countAllByUserName('john') self.assertTrue(is_integer(count))
def testCountAllBySubscription(self): count = Conversation.countAllBySubscription(1) self.assertTrue(is_integer(count))
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))
def conversation_opaque_to_id(value): return Conversation(id=value).id
def testCountAllByInterest(self): count = Conversation.countAll(interest_id=1) self.assertTrue(is_integer(count)) self.assertTrue(count > 0)
def testCountAllAfter(self): date = datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M") count = Conversation.countAll(after=date) self.assertTrue(is_integer(count)) self.assertTrue(count > 0)
def setUp(self): self.model = Conversation() Conversation.db = app.db.get_db() Conversation.logger = app.logger
def testCountAllByUser(self): count = Conversation.countAll(user_id=2) self.assertTrue(is_integer(count)) self.assertTrue(count > 0)
def testCountAll(self): count = Conversation.countAll() self.assertTrue(is_integer(count)) self.assertTrue(count > 0)