def get(self, request): user = request.user room_id = request.GET.get('room_id', None) currency = request.GET.get('display_currency', None) check_in = request.GET.get('checkin') check_in_date = datetime.datetime.strptime(check_in, '%Y-%m-%d') check_out = request.GET.get('checkout') check_out_date = datetime.datetime.strptime(check_out, '%Y-%m-%d') if check_out else None guests_total = request.GET.get('adults') room = Room.objects.prefetch_related( 'bedroom_set', 'bath_set', 'review_set', 'roomimage_set' ).get(id=room_id) user_currency = UserProfile.objects.get(user=user).currency.name if user else "USD" price = room.price if currency: backend = OpenExchangeRatesBackend(OPEN_EXCHANGE_RATES_URL) price_currency = convert_money(Money(room.price, user_currency), currency) price_split = str(price_currency).replace(',', '').split(' ') price_numeric = re.sub("[^0-9.]", "", price_split[0]) price = str(int(float(price_numeric))) + " " + currency num_beds = 0 for bedroom in room.bedroom_set.all(): num_beds += bedroom.bed_set.aggregate(Sum('quantity'))['quantity__sum'] ratings_dict = room.review_set.aggregate(\ *[Avg(field) for field in ['rating__cleanliness', 'rating__communication', \ 'rating__check_in', 'rating__accuracy', 'rating__location', 'rating__value']]) overall_rating = 0 if None in ratings_dict.values() else sum(ratings_dict.values())/6 booking_information = { 'room_id' : room.id, 'title' : room.title, 'room_picture' : room.roomimage_set.values_list('image_url', flat=True)[0], 'check_in_time' : room.check_in.strftime("%-I:%M"), 'check_out_time' : room.check_out.strftime("%-I:%M"), 'check_in_date' : check_in, 'check_out_date' : check_out, 'place_type' : room.place_type.name, 'guests_total' : guests_total, 'bedrooms' : room.bedroom_set.count(), 'beds' : num_beds, 'baths' : room.bath_set.count(), 'currency' : currency if currency else user_currency, 'price' : price, 'discount_rate' : calculate_discounts(room, check_in_date, check_out_date), 'rules' : room.rules, 'overall_rating' : overall_rating, 'num_reviews' : room.review_set.count(), 'host_name' : room.host.fullname, 'host_avatar' : room.host.profile.avatar_image, 'profile_header' : room.host.profile.profile_header, 'user_since' : room.host.created_at.strftime("%Y"), 'payment_methods' : list(PaymentMethod.objects.values_list('name', flat=True)) } return JsonResponse({'booking_information':booking_information}, status=200)
import os import django os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'FinTrack.settings') django.setup() from Main.models import Currency from djmoney.contrib.exchange.backends import OpenExchangeRatesBackend Currency.objects.get_or_create(name='USD', long_name='United States Dollar') Currency.objects.get_or_create(name='ILS', long_name='New Israeli Shekel') backend = OpenExchangeRatesBackend() backend.update_rates()
def update_rates(): OpenExchangeRatesBackend().update_rates()
def default_openexchange_rates(): with mock_backend(OPEN_EXCHANGE_RATES_RESPONSE): OpenExchangeRatesBackend().update_rates()