def get_payment_info(rental_order, user): payment_info = {} if rental_order.total_price_usd < user.coastalbucket.balance: payment_info['payment_list'] = ['coastal', 'stripe'] payment_info['coastal'] = { 'coastal_dollar': '$%s' % format(int(user.coastalbucket.balance), ','), 'amount': '$%s' % format(int(rental_order.total_price_usd), ','), } else: payment_info['payment_list'] = ['stripe'] stripe_amount = get_stripe_amount( rental_order.total_price) # add the stripe process fee payment_info['stripe'] = { 'total_price': rental_order.total_price, 'total_price_display': rental_order.get_total_price_display(), 'updated_amount': stripe_amount, 'updated_amount_display': price_display(stripe_amount, rental_order.currency), } return payment_info
def calc_total_price(request): data = request.GET.copy() if 'product_id' in data: data['product'] = data.get('product_id') form = RentalBookForm(data) if not form.is_valid(): return CoastalJsonResponse(form.errors, status=400) start_datetime = form.cleaned_data['start_datetime'] end_datetime = form.cleaned_data['end_datetime'] rental_unit = form.cleaned_data['rental_unit'] product = form.cleaned_data['product'] guest_count = form.cleaned_data['guest_count'] total_amount = calc_price(product, rental_unit, start_datetime, end_datetime, guest_count)[1] data = [{ 'amount': total_amount, 'currency': product.currency, 'amount_display': price_display(total_amount, product.currency), }] return CoastalJsonResponse(data)
def get_price_display(self): return price_display(self.price, self.product.currency)
def product_list(request): form = ProductListFilterForm(request.GET) if not form.is_valid(): return CoastalJsonResponse(form.errors, status=response.STATUS_400) lon = form.cleaned_data['lon'] lat = form.cleaned_data['lat'] distance = form.cleaned_data['distance'] or defs.DISTANCE guests = form.cleaned_data['guests'] arrival_date = form.cleaned_data['arrival_date'] checkout_date = form.cleaned_data['checkout_date'] min_price = form.cleaned_data['min_price'] max_price = form.cleaned_data['max_price'] category = form.cleaned_data['category'] category_exp = form.cleaned_data.get('category_exp') for_sale = form.cleaned_data['for_sale'] for_rental = form.cleaned_data['for_rental'] max_coastline_distance = form.cleaned_data['max_coastline_distance'] min_coastline_distance = form.cleaned_data['min_coastline_distance'] products = Product.objects.filter(status='published') if lon and lat: point = Point(lon, lat, srid=4326) products = products.filter(point__distance_lte=(Point(lon, lat), D(mi=distance))) else: point = None if arrival_date and checkout_date: products = products.exclude( blackoutdate__start_date__lte=arrival_date, blackoutdate__end_date__gte=checkout_date).exclude( rentaloutdate__start_date__lte=arrival_date, rentaloutdate__end_date__gte=checkout_date) elif checkout_date: arrival_date = datetime.now().replace(hour=0, minute=0, second=0) products = products.exclude( blackoutdate__start_date__lte=arrival_date, blackoutdate__end_date__gte=checkout_date).exclude( rentaloutdate__start_date__lte=arrival_date, rentaloutdate__end_date__gte=checkout_date) if max_coastline_distance and min_coastline_distance: products = products.filter( distance_from_coastal__gte=min_coastline_distance, distance_from_coastal__lte=max_coastline_distance) elif min_coastline_distance: products = products.filter( distance_from_coastal__gte=min_coastline_distance) elif max_coastline_distance: products = products.filter( distance_from_coastal__lte=max_coastline_distance) if guests: products = products.filter(max_guests__gte=guests) query, query_exp = None, None if category: query = Q(category__in=category) if for_rental and not for_sale: query &= Q(for_rental=True) elif for_sale and not for_rental: query &= Q(for_sale=True) if min_price: query &= Q( **{"%s__gte" % form.cleaned_data['price_field']: min_price}) if max_price: query &= Q( **{"%s__lte" % form.cleaned_data['price_field']: max_price}) if category_exp: query_exp = Q(category=category_exp) if min_price: query_exp &= Q(rental_usd_price__gte=min_price) if max_price: query_exp &= Q(rental_usd_price__lte=max_price) if query and query_exp: products = products.filter(query | query_exp) elif query: products = products.filter(query) elif query_exp: products = products.filter(query_exp) if point: products = products.order_by(Distance('point', point), '-score', '-rental_usd_price', '-sale_usd_price') else: products = products.order_by('-score', '-rental_usd_price', '-sale_usd_price') bind_product_image(products) page = request.GET.get('page', 1) item = defs.PER_PAGE_ITEM paginator = Paginator(products, item) try: products = paginator.page(page) except PageNotAnInteger: products = paginator.page(1) except EmptyPage: products = paginator.page(paginator.num_pages) if int(page) >= paginator.num_pages: next_page = 0 else: next_page = int(page) + 1 data = [] for product in products: if product.category_id == product_defs.CATEGORY_ADVENTURE: product_data = model_to_dict( product, fields=['id', 'exp_time_unit', 'exp_time_length']) product_data.update({ 'rental_price': product.rental_price, 'rental_price_display': price_display(product.rental_price, product.currency), 'exp_time_unit': product.exp_time_unit, 'max_guests': product.max_guests or 0, }) elif product.category_id == product_defs.CATEGORY_BOAT_SLIP: product_data = model_to_dict(product, fields=[ 'id', 'for_rental', 'for_sale', 'length', 'max_guests' ]) if not product.length: product_data['length'] = 0 else: product_data = model_to_dict( product, fields=['id', 'for_rental', 'for_sale', 'beds', 'max_guests']) if product.for_rental and product.category_id != product_defs.CATEGORY_ADVENTURE: product_data.update({ 'rental_price': int(product.get_price('day')), 'rental_unit': 'Day', 'rental_price_display': price_display(int(product.get_price('day')), product.currency), }) rental_price = product.rental_price if product.rental_unit == "half-day": rental_price *= 4 if product.rental_unit == 'hour': rental_price *= 24 if product.for_sale and product.category_id != product_defs.CATEGORY_ADVENTURE: product_data.update({ 'sale_price': product.sale_price, 'sale_price_display': product.get_sale_price_display(), }) product_data.update({ "category": product.category_id, "images": [i.image.url for i in product.images], "lon": product.point[0], "lat": product.point[1], }) data.append(product_data) result = { 'next_page': next_page, 'products': data, } return CoastalJsonResponse(result)
def product_detail(request, pid): try: product = Product.objects.get(id=pid) except Product.DoesNotExist: return CoastalJsonResponse(status=response.STATUS_404, message="The product does not exist.") liked_product_id_list = [] if request.POST.get('preview') != '1': count_product_view(product) if request.user.is_authenticated(): user = request.user liked_product_id_list = FavoriteItem.objects.filter( favorite__user=request.user).values_list('product_id', flat=True) if RecentlyViewed.objects.filter(user=user, product=product): RecentlyViewed.objects.filter( user=user, product=product).update(date_created=datetime.now()) else: RecentlyViewed.objects.create(user=user, product=product, date_created=datetime.now()) if product.category_id == product_defs.CATEGORY_ADVENTURE: data = model_to_dict(product, fields=[ 'id', 'max_guests', 'exp_time_length', 'category', 'currency' ]) data[ 'exp_start_time'] = product.exp_start_time and product.exp_start_time.strftime( '%I:%M %p') or '' data[ 'exp_end_time'] = product.exp_end_time and product.exp_end_time.strftime( '%I:%M %p') or '' data['exp_time_unit'] = product.get_exp_time_unit_display() data['city'] = product.locality or '' else: data = model_to_dict(product, fields=[ 'category', 'id', 'for_rental', 'for_sale', 'sale_price', 'currency' ]) data['city'] = product.locality or '' if product.max_guests: data['max_guests'] = product.max_guests if product.category_id in (product_defs.CATEGORY_HOUSE, product_defs.CATEGORY_APARTMENT): data['room'] = product.rooms or 0 data['bathrooms'] = product.bathrooms or 0 if product.category_id in (product_defs.CATEGORY_ROOM, product_defs.CATEGORY_YACHT): data['beds'] = product.beds or 0 data['bathrooms'] = product.bathrooms or 0 if product.point: data['lon'] = product.point[0] data['lat'] = product.point[1] if product.get_amenities_display(): data['amenities'] = product.get_amenities_display() data['short_desc'] = product.short_desc if product.new_rental_unit(): data['rental_unit'] = product.new_rental_unit() else: data['rental_unit'] = 'Day' if product.description: data['description'] = product.description else: data['description'] = 'Description' if product.rental_price: data['rental_price'] = product.rental_price else: data['rental_price'] = 0 data['liked'] = product.id in liked_product_id_list data['rental_price_display'] = product.get_rental_price_display() data['sale_price_display'] = product.get_sale_price_display() data['city_address'] = product.city_address or '' images = [] views = [] for pi in ProductImage.objects.filter(product=product): if pi.image_type != ProductImage.CAPTION_360: images.append(pi.image.url) else: views.append(pi.image.url) data['images_360'] = views data['images'] = images if product.name: data['name'] = product.name else: data['name'] = 'Your Listing Name' data['owner'] = { 'user_id': product.owner_id, 'name': product.owner.basic_info()['name'], 'photo': product.owner.basic_info()['photo'], 'purpose': product.owner.userprofile.purpose, } reviews = Review.objects.filter(product=product).order_by('-date_created') last_review = reviews.first() avg_score = reviews.aggregate(Avg('score'), Count('id')) data['reviews'] = { 'count': avg_score['id__count'], 'avg_score': avg_score['score__avg'] or 0, } if last_review: start_time = last_review.order.start_datetime end_time = last_review.order.end_datetime data['reviews']['latest_review'] = { "stayed_range": '%s - %s' % (datetime.strftime(start_time, '%Y/%m/%d'), datetime.strftime(end_time, '%Y/%m/%d')), "score": last_review.score, "content": last_review.content } data['reviews']['latest_review'].update( last_review.owner.basic_info(prefix='reviewer_')) data['extra_info'] = { 'rules': { 'name': '%s Rules' % product.category.name, 'content': product.rental_rule or 'Nothing is set', }, 'cancel_policy': { 'name': 'Cancellation Policy', 'content': 'Coastal does not provide online cancellation service. Please contact us if you have any needs.' }, } if product.for_rental and product.category_id != product_defs.CATEGORY_ADVENTURE: price = get_product_discount(product.rental_price, product.rental_unit, product.discount_weekly, product.discount_monthly) discount = { 'discount': { 'name': 'Additional Price', 'weekly_discount': product.discount_weekly or 0, 'updated_weekly_price': price[0], 'updated_weekly_price_display': price_display(price[0], product.currency), 'monthly_discount': product.discount_monthly or 0, 'updated_monthly_price': price[1], 'updated_monthly_price_display': price_display(price[1], product.currency), } } else: discount = {'discount': {}} data.get('extra_info').update(discount) similar_products = get_similar_products(product) bind_product_image(similar_products) bind_product_main_image(similar_products) similar_product_dict = [] for p in similar_products: content = model_to_dict(p, fields=[ 'id', 'category', 'liked', 'for_rental', 'for_sale', 'rental_price', 'sale_price', 'city', 'max_guests' ]) content['reviews_count'] = 0 content['reviews_avg_score'] = 0 content['liked'] = p.id in liked_product_id_list content['max_guests'] = p.max_guests or 0 content['length'] = p.length or 0 content['rooms'] = p.rooms or 0 content['rental_price_display'] = p.get_rental_price_display() content['sale_price_display'] = p.get_sale_price_display() content['rental_unit'] = p.new_rental_unit() content['image'] = p.main_image and p.main_image.image.url or '' similar_product_dict.append(content) data['similar_products'] = similar_product_dict return CoastalJsonResponse(data)
def all_detail(request): try: product = Product.objects.get(id=request.GET.get('product_id')) except Product.DoesNotExist: return CoastalJsonResponse(status=response.STATUS_404) except ValueError: return CoastalJsonResponse(status=response.STATUS_404) images = [] views = [] for pi in ProductImage.objects.filter(product=product): if pi.image_type != ProductImage.CAPTION_360: image = { 'image_id': pi.id, 'url': pi.image.url, 'caption': pi.caption, } images.append(image) else: view = { 'image_id': pi.id, 'url': pi.image.url, 'caption': pi.caption, } views.append(view) if product.for_rental: price = get_product_discount(product.rental_price, product.rental_unit, product.discount_weekly, product.discount_monthly) discount = { 'discount': { 'weekly_discount': product.discount_weekly or 0, 'updated_weekly_price': price[0], 'updated_weekly_price_display': price_display(price[0], product.currency), 'monthly_discount': product.discount_monthly or 0, 'updated_monthly_price': price[1], 'updated_monthly_price_display': price_display(price[1], product.currency), } } else: discount = {'discount': {}} black_out_dates = BlackOutDate.objects.filter(product=product) if black_out_dates: content = [] for i in black_out_dates: data = [] data.append(localtime(i.start_date).date()) data.append(localtime(i.end_date).date()) content.append(data) else: content = [] result = { 'id': product.id, 'category': product.category_id, 'images': images, '360-images': views, 'for_rental': product.for_rental, 'for_sale': product.for_sale, 'rental_price': product.rental_price or 0, 'rental_price_display': product.get_rental_price_display(), 'rental_unit': product.new_rental_unit(), 'currency': product.currency, 'sale_price': product.sale_price or 0, 'sale_price_display': product.get_sale_price_display(), 'lon': product.point and product.point[0] or 0, 'lat': product.point and product.point[1] or 0, 'address': product.address or '', 'name': product.name, 'description': product.description or '', 'amenities': list(product.amenities.values_list('id', flat=True)), 'rental_rule': product.rental_rule, 'black_out_dates': content, 'rental_type': product.rental_type, 'desc_about_it': product.desc_about_it or '', 'desc_guest_access': product.desc_guest_access or '', 'desc_interaction': product.desc_interaction or '', 'desc_getting_around': product.desc_getting_around or '', 'desc_other_to_note': product.desc_other_to_note or '', 'exp_time_unit': product.get_exp_time_unit_display() or '', 'exp_time_length': product.exp_time_length or 0, } if product.exp_start_time is not None: result['exp_start_time'] = product.exp_start_time.strftime('%I:%M %p') else: result['exp_start_time'] = '' if product.check_exp_end_time(): result['exp_end_time'] = '12:00 AM' else: result[ 'exp_end_time'] = product.exp_end_time and product.exp_end_time.strftime( '%I:%M %p') or '' result.update(discount) return CoastalJsonResponse(result)
def get_sale_price_display(self): return price_display(self.sale_price, self.currency)
def get_rental_price_display(self): return price_display(self.rental_price, self.currency)