Exemplo n.º 1
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))
Exemplo n.º 2
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))
Exemplo n.º 3
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)
Exemplo n.º 4
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)
Exemplo n.º 5
0
    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))
Exemplo n.º 6
0
    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))
Exemplo n.º 7
0
    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))
Exemplo n.º 8
0
    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))
Exemplo n.º 9
0
    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))
Exemplo n.º 10
0
    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))
Exemplo n.º 11
0
    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))
Exemplo n.º 12
0
    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))
Exemplo n.º 13
0
    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))
Exemplo n.º 14
0
    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)
Exemplo n.º 15
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))
Exemplo n.º 16
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))
Exemplo n.º 17
0
    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')
Exemplo n.º 18
0
    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')
Exemplo n.º 19
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)
Exemplo n.º 20
0
    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')
Exemplo n.º 21
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))
Exemplo n.º 22
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))
Exemplo n.º 23
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))
Exemplo n.º 24
0
    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))
Exemplo n.º 25
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))
Exemplo n.º 26
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)
Exemplo n.º 27
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))
Exemplo n.º 28
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))
Exemplo n.º 29
0
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))
Exemplo n.º 32
0
 def testCountAllByUserName(self):
     count = Conversation.countAllByUserName('john')
     self.assertTrue(is_integer(count))
Exemplo n.º 33
0
 def testCountAllBySubscription(self):
     count = Conversation.countAllBySubscription(1)
     self.assertTrue(is_integer(count))
Exemplo n.º 34
0
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))
Exemplo n.º 35
0
def conversation_opaque_to_id(value):
    return Conversation(id=value).id
Exemplo n.º 36
0
 def testCountAllByInterest(self):
     count = Conversation.countAll(interest_id=1)
     self.assertTrue(is_integer(count))
     self.assertTrue(count > 0)
Exemplo n.º 37
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)
Exemplo n.º 38
0
 def setUp(self):
     self.model = Conversation()
     Conversation.db = app.db.get_db()
     Conversation.logger = app.logger
Exemplo n.º 39
0
 def testCountAllByUser(self):
     count = Conversation.countAll(user_id=2)
     self.assertTrue(is_integer(count))
     self.assertTrue(count > 0)
Exemplo n.º 40
0
 def testCountAll(self):
     count = Conversation.countAll()
     self.assertTrue(is_integer(count))
     self.assertTrue(count > 0)