Ejemplo n.º 1
0
def searchP2PForwarders(request):
    user = request.user
    profile = Profile.objects.get(user=user)
    locationFrom = request.data.get('locationFrom')
    locationFromList = Location.objects.filter(countryCode=locationFrom['countryCode'])
    locationFrom = locationFromList.values_list('pk', flat=True)
    locationTo = request.data.get('locationTo')
    locationToList = Location.objects.filter(countryCode=locationTo['countryCode'])
    locationTo = locationToList.values_list('pk', flat=True)
    maxWeight = request.data.get('maxWeight')
    maxSize = request.data.get('maxSize')
    maxGoodValue = request.data.get('maxGoodValue')
    if profile.currencySetting == settings.PROFILE_CURRENCY_SETTINGS[1][0]:
        maxGoodValue = Service.eurToUsdConversion(maxGoodValue)
    configMaxGoodValuePercentage = Configuration().getConfiguration("p2p_forwarder_maxgoodvalue_percentage")
    services = []
    if request.data.get('acceptedPacksFromPrivateOrCompany'):
        acceptPackagesFromprivateOrCompanyQuery = Q(acceptedPacksFromPrivate=True)
    else:
        acceptPackagesFromprivateOrCompanyQuery = Q(acceptedPacksFromCompany=True)
    servicesTmp = Service.objects.filter(
        Q(
            type=SERVICE_TYPES[0][0],
            enabled=True,
            locationFrom__in=list(locationFrom),
            locationTos__in=list(locationTo),
            maxSize__gte=maxSize,
            maxWeight__gte=maxWeight,
        )
        & acceptPackagesFromprivateOrCompanyQuery
    ).exclude(profile=profile).distinct()

    # Check Max Good Value in Service -> profile -> wallet
    for s in servicesTmp:
        wallet = Wallet.objects.get(
            profile=s.profile
        )
        if wallet.deposit > 0:
            walletMaxGoodValue = (configMaxGoodValuePercentage * wallet.deposit) / 100
            if walletMaxGoodValue >= maxGoodValue:
                services.append(s)
    shippingWeight = 0.5
    if maxWeight == 1:
        shippingWeight = 7
    elif maxWeight == 2:
        shippingWeight = 8
    for s in services:
        originCountry = CountryManager.countryByCoutryCode(s.locationFrom.countryCode)['name']
        destinationCountry = CountryManager.countryByCoutryCode(locationToList[0].countryCode)['name']
        s.setLowestPrice(
            shippingWeight=shippingWeight,
            originCountry=originCountry,
            destinationCountry=destinationCountry,
            margin=s.partnerForwarderMargin
        )

    serviceSerializer = ServiceProtectedSerializer(services, many=True, context={'request': request})


    return Response({'success': True, 'services': serviceSerializer.data})
Ejemplo n.º 2
0
def partnerOrderPrice(request):
    try:
        user = request.user
        Profile.objects.get(user=user)
        originCountry = request.data.get('originCountry')
        destinationCountry = request.data.get('destinationCountry')
        shippingWeight = request.data.get('shippingWeight')
        shippingMode = request.data.get('shippingMode')
        service = Service.objects.get(id=request.data.get('service'))
        if shippingMode == EXPRESS_SHIPPING:
            serviceCosts = Service.computeExpressCosts(
                shippingWeight,
                originCountry,
                destinationCountry,
                service.partnerForwarderMargin
            )
        else:
            serviceCosts = Service.computeStandardCosts(
                shippingWeight,
                originCountry,
                destinationCountry,
                service.partnerForwarderMargin
            )
        finalPrice = functools.reduce(lambda a, b: a + b, map(lambda p: p['amount'], serviceCosts))
        finalPrice = Service.eurToUsdConversion(finalPrice)

        return Response({'success': True, 'price': finalPrice})
    except Exception as e:
        return Response({'success': False, 'error': e.message})