Example #1
0
def reply(request):
    if request.method != 'POST':
        return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.NotPostRequest, ''))

    if not Helpers.validate_seller_session(request):
        return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InvalidSellerSession, ''))


    seller = request.session['sellerId']
    
    messageId = request.POST.get('messageId', False)


    if not messageId:
        return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InvalidMessageId, {'messageId': messageId}))

    try:
        messageId = int(messageId)
    except valueError:
        return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InvalidMessageId, {'messageId': messageId}))

    message = request.POST.get('message', False)

    if not message or not Helpers.validateMessage(message):
        return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InvalidBargainMessage, message))
    

    toId = request.POST.get('toId', False)

    if not toId:
        return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InvalidToid, {'messageId': toId}))

    try:
        toId = int(toId)
    except valueError:
        return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InvalidToid, {'messageId': toId}))

    try:
        messageObj = Bargain.models.MessageBox.objects.get(id=messageId)

    except ObjectDoesNotExist:
        return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InactiveMessage, {'messageId': messageId}))



    if messageObj.userId != toId:
        return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InvalidToid, {'messageId': toId}))

    if not messageObj.isActive:
        return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InactiveMessage, {'messageId': messageId}))


    

    shortMessage = Helpers.shortenMessage(message)

    # update message details
    messageObj.numOfResponses += 1
    messageObj.recentResponse = shortMessage


    messageObj.save()


    # create threads
    Bargain.models.MessageBoxThreads.objects.create(messageId = messageObj, fromId = seller.id, toId = toId, message=message)

    Bargain.models.NotificationsQueue.objects.create(appId=messageObj.userAppId, shortMessage=shortMessage)
 

    return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.Success, 'Replied!'))
Example #2
0
def send(request):

    if request.method != 'POST':
        return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.NotPostRequest, ''))
    
    if not Helpers.validate_user_session(request):
        return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InvalidUserSession, ''))


    selectionType = request.POST.get('selectionType', False)

    if selectionType is False:
        return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InvalidSelectionType, selectionType))

    try:
        selectionType = int(selectionType)
    except ValueError:
        return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InvalidSelectionType, selectionType))


    
    productId = None
    brandId = None
    categoryId = None
    
    # we should get either one of these
    if selectionType == Helpers.Constants.SelectionType.Product:
        productId = request.POST.get('productId', False)
        if productId is False:
            return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InvalidProductId, productId))

        try:
            productId = int(productId)
        except ValueError:
            return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InvalidProductId, productId))

    elif selectionType == Helpers.Constants.SelectionType.Brand:
        brandId = request.POST.get('brandId', False)
        if brandId is False:
            return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InvalidBrandId, brandId))

        try:
            brandId = int(brandId)
        except ValueError:
            return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InvalidBrandId, brandId))


    elif selectionType == Helpers.Constants.SelectionType.Category:
        categoryId = request.POST.get('categoryId', False)
        if categoryId is False:
            return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InvalidCategoryID, categoryId))

        try:
            categoryId = int(categoryId)
        except ValueError:
            return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InvalidCategoryID, categoryId))

    else:
        return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InvalidSelectionType, selectionType))



    if productId is None and brandId is None and categoryId is None:
        return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InvalidSelectionType, selectionType))


    interestedInSimilar = request.POST.get('interestedInSimilar', False) != False


    latitude = request.POST.get('latitude', False)
    longitude = request.POST.get('longitude', False)


     # http://stackoverflow.com/questions/6536232/validate-latitude-and-longitude
    if not latitude:
        return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InvalidLatitude, latitude))
    try:
        latitude = float(latitude)
    except ValueError:
        return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InvalidLatitude, latitude))

    if latitude < -90 or latitude > 90:
        return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InvalidLatitude, latitude))



    if not longitude:
        return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InvalidLongitude, longitude))
    try:
        longitude = float(longitude)
    except ValueError:
        return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InvalidLongitude, longitude))

    if longitude < -180 or longitude > 180:
        return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InvalidLongitude, longitude))


    message = request.POST.get('message', False)
    if not message or not Helpers.validateMessage(message):
        return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.InvalidBargainMessage, message))


    # TODO - search radius
    tiles = Helpers.overlapping_tiles(latitude, longitude)

    Helpers.logger.debug('overlapping tiles {0}'.format('\t'.join(tiles)))

    # we user selected product, we will send notification to potential
    # all sellers who sells this product, this brand and this category
    # in ranking, we use whether user is interested in similar product or 
    # not to truncate

    if productId:
        row = Product.models.Products.objects.get(id=productId)
        brandId = row.brandId_id

        row = Product.models.ProductBrands.objects.get(id=brandId)
        categoryId = row.categoryId_id

    elif brandId:
        row = Product.models.ProductBrands.objects.get(id=brandId)
        categoryId = row.categoryId_id

    
    Helpers.logger.debug('productId {} brandId {} categoryId {}'.format(productId, brandId, categoryId))

    routes = []

    if productId:
        routes.extend([(t, productId, Helpers.Constants.SelectionType.Product) for t in tiles])

    if brandId:
        routes.extend([(t, brandId, Helpers.Constants.SelectionType.Brand) for t in tiles])

    if categoryId:
        routes.extend([(t, categoryId, Helpers.Constants.SelectionType.Category) for t in tiles])


    Helpers.logger.debug('Routes {}'.format('\t'.join([str(t) for t in routes])))
    sellers = []
    sellerIds = {}
    for r in routes:
        rows = Bargain.models.MessageRouter.objects.filter(tileId = r[0], productSelectionId = r[1], productSelectionType = r[2])
        for s in rows:
            if s.sellerId in sellerIds:
                continue

            sellerIds[s.sellerId] = True

            sellers.append(s)

    Helpers.logger.debug('Sellers {}'.format('\t'.join([str(s.sellerId) for i in sellers])))

    user = request.session.get('userId')

    # do some ranking
    sellers = Helpers.rankeSellers(sellers)


    # create message
    messageObj = Bargain.models.MessageBox.objects.create(userId=user.id, headMessage=message, userAppId=user.userAppId)

    
    shortMessage = Helpers.shortenMessage(message)

    for seller in sellers:
        # add message for seller

        # create threads
        Bargain.models.MessageBoxThreads.objects.create(messageId = messageObj, fromId = user.id, toId = seller.sellerId, message=message)

        Bargain.models.NotificationsQueue.objects.create(appId=seller.sellerAppId, shortMessage=shortMessage)


    return HttpResponse(Helpers.create_json_output(Helpers.StatusCodes.Success, {'numOfSeller': len(sellers)}))