def _execute_on_valid(self): data = self.cleaned_data with transaction.atomic(): use = Use(arrive=data['arrive'], depart=data['depart'], user=self.issuing_user, resource=data['resource'], location=data['resource'].location) use.save() booking = Booking(use=use, comments=data.get('comments'), rate=data['resource'].default_rate) booking.save() self.result_data = {"booking": booking, "use": use}
def post(self, request): booking_date = request.POST["book_date"] check_in = request.POST["check_in_date"] check_out = request.POST["check_out_date"] adults = request.POST["adults"] childrens = request.POST["childs"] print(booking_date, check_in, check_out, adults, childrens) data = Booking( booking_date=booking_date, check_in=check_in, check_out=check_out, adults=adults, childrens=childrens, ) data.save() return render(request, 'core/booking.html')
def _execute_on_valid(self): data = self.cleaned_data with transaction.atomic(): use = Use( arrive=data['arrive'], depart=data['depart'], user=self.issuing_user, resource=data['resource'], location=data['resource'].location ) use.save() booking = Booking( use=use, comments=data.get('comments'), rate=data['resource'].default_rate ) booking.save() self.result_data = { "booking": booking, "use": use }
def create(**kwargs): # kwargs = nest_dicts(kwargs) gym = kwargs.pop("gym", {}) if isinstance(gym, dict): gym = gym_factory(**gym) defaults = { "gym": gym, "start": "10:00", "end": "11:00", "phone_number": "+491234567890", } if use_db: return Booking.objects.create(**{**defaults, **kwargs}) return Booking(**{**defaults, **kwargs})
def BookingSubmit(request, location_slug): if not request.method == "POST": return HttpResponseRedirect("/404") location = get_object_or_404(Location, slug=location_slug) form = BookingUseForm(location, request.POST) if form.is_valid(): comments = request.POST.get('comments') use = form.save(commit=False) use.location = location booking = Booking(use=use, comments=comments) # reset_rate also generates the bill. if request.user.is_authenticated(): use.user = request.user if use.suggest_drft(): use.accounted_by = Use.DRFT use.save() # we already set the value of 'use' when creating the Booking, # but it wasn't saved at that point, and Django complains about # a missing primary key here otherwise, so re-setting. booking.use = use booking.save() booking.reset_rate() new_booking_notify(booking) messages.add_message( request, messages.INFO, 'Thanks! Your booking was submitted. You will receive an email when it has been reviewed. You may wish to <a href="/people/%s/edit/">update your profile</a> if your projects or ideas have changed since your last visit.' % booking.use.user.username) return HttpResponseRedirect( reverse('booking_detail', args=(location_slug, booking.id))) else: booking_data = booking.serialize() request.session['booking'] = booking_data messages.add_message( request, messages.INFO, 'Thank you! Please make a profile to complete your booking request.' ) return HttpResponseRedirect(reverse('registration_register')) else: logger.debug('form was not valid') logger.debug(request.POST) logger.debug(form.errors) raise Exception(str(form.errors.as_data()))
def BookingSubmit(request, location_slug): if not request.method == "POST": return HttpResponseRedirect("/404") location = get_object_or_404(Location, slug=location_slug) form = BookingUseForm(location, request.POST) if form.is_valid(): comments = request.POST.get('comments') use = form.save(commit=False) use.location = location booking = Booking(use=use, comments=comments) # reset_rate also generates the bill. if request.user.is_authenticated(): use.user = request.user if use.suggest_drft(): use.accounted_by = Use.DRFT use.save() # we already set the value of 'use' when creating the Booking, # but it wasn't saved at that point, and Django complains about # a missing primary key here otherwise, so re-setting. booking.use = use booking.save() booking.reset_rate() new_booking_notify(booking) messages.add_message( request, messages.INFO, 'Thanks! Your booking was submitted. You will receive an email when it has been reviewed. You may wish to <a href="/people/%s/edit/">update your profile</a> if your projects or ideas have changed since your last visit.' % booking.use.user.username ) return HttpResponseRedirect(reverse('booking_detail', args=(location_slug, booking.id))) else: booking_data = booking.serialize() request.session['booking'] = booking_data messages.add_message(request, messages.INFO, 'Thank you! Please make a profile to complete your booking request.') return HttpResponseRedirect(reverse('registration_register')) else: print 'form was not valid' logger.debug(request.POST) logger.debug(form.errors) raise Exception(str(form.errors.as_data()))
def import_bookings(booking_file, payment_file, location_id, room_offset): json_file = open(booking_file) booking_data = json.load(json_file) json_file.close() json_file = open(payment_file) payment_data = json.load(json_file) json_file.close() location = Location.objects.get(pk=location_id) print "location=%s" % location.name ''' { "pk": 3, "model": "core.payment", "fields": { "automatic_invoice": false, "payment_date": "2014-04-20T01:45:29.635Z", "paid_amount": "225", "payment_method": "Visa", "booking": 3, "payment_service": "Stripe", "transaction_id": "ch_103svy24GAclB9sbIZjFL5LY" } } ''' payments_by_booking = {} for p in payment_data: payment = p['fields'] payment_id = p['pk'] if 'booking' in payment: booking_id = payment['booking'] payments_by_booking[booking_id] = payment else: print "payment %s has no booking!" % payment_id #print payments_by_booking ''' { "pk": 1, "model": "core.booking", "fields": { "status": "canceled", "updated": "2014-08-26T22:46:47.303Z", "depart": "2014-04-05", "room": 1, "last_msg": "2014-04-02T20:40:06.221Z", "created": "2014-04-02T20:34:16.979Z", "purpose": "kdjkj", "tags": "", "comments": "", "arrival_time": "", "rate": 75, "user": [ "meganlipsett" ], "arrive": "2014-04-02", "location": 1 } }, ''' for r in booking_data: old_booking = r['fields'] old_booking_id = r['pk'] old_room_id = old_booking['room'] new_room_id = int(old_room_id) + room_offset print "old_booking_id = %s, old_room_id = %s, new_room_id = %s" % (old_booking_id, old_room_id, new_room_id) new_room = Room.objects.get(id=new_room_id) #print "room = %s" % new_room.name username = r['fields']['user'][0] user = User.objects.filter(username=username).first() new_booking = Booking(status=old_booking['status'], updated=old_booking['updated'], depart=old_booking['depart'], last_msg=old_booking['last_msg'], created=old_booking['created'], purpose=old_booking['purpose'], tags=old_booking['tags'], comments=old_booking['comments'], rate=old_booking['rate'], arrive=old_booking['arrive'], user=user, location=location, room=new_room, ) new_booking.save() #print new_booking # Now create the payment for this new booking if old_booking_id in payments_by_booking: old_payment = payments_by_booking[old_booking_id] print "booking %s has payment of $%s" % (old_booking_id, old_payment['paid_amount']) new_payment = Payment(payment_method=old_payment['payment_method'], payment_service=old_payment['payment_service'], transaction_id=old_payment['transaction_id'], paid_amount=old_payment['paid_amount'], booking=new_booking, ) new_payment.save() new_payment.payment_date = old_payment['payment_date'] new_payment.save() # Pull the booking back out of the DB and recalculate the bill pulled_booking = Booking.objects.get(id=new_booking.id) pulled_booking.generate_bill()
def import_bookings(booking_file, payment_file, location_id, room_offset): json_file = open(booking_file) booking_data = json.load(json_file) json_file.close() json_file = open(payment_file) payment_data = json.load(json_file) json_file.close() location = Location.objects.get(pk=location_id) print "location=%s" % location.name ''' { "pk": 3, "model": "core.payment", "fields": { "automatic_invoice": false, "payment_date": "2014-04-20T01:45:29.635Z", "paid_amount": "225", "payment_method": "Visa", "booking": 3, "payment_service": "Stripe", "transaction_id": "ch_103svy24GAclB9sbIZjFL5LY" } } ''' payments_by_booking = {} for p in payment_data: payment = p['fields'] payment_id = p['pk'] if 'booking' in payment: booking_id = payment['booking'] payments_by_booking[booking_id] = payment else: print "payment %s has no booking!" % payment_id #print payments_by_booking ''' { "pk": 1, "model": "core.booking", "fields": { "status": "canceled", "updated": "2014-08-26T22:46:47.303Z", "depart": "2014-04-05", "room": 1, "last_msg": "2014-04-02T20:40:06.221Z", "created": "2014-04-02T20:34:16.979Z", "purpose": "kdjkj", "tags": "", "comments": "", "arrival_time": "", "rate": 75, "user": [ "meganlipsett" ], "arrive": "2014-04-02", "location": 1 } }, ''' for r in booking_data: old_booking = r['fields'] old_booking_id = r['pk'] old_room_id = old_booking['room'] new_room_id = int(old_room_id) + room_offset print "old_booking_id = %s, old_room_id = %s, new_room_id = %s" % ( old_booking_id, old_room_id, new_room_id) new_room = Room.objects.get(id=new_room_id) #print "room = %s" % new_room.name username = r['fields']['user'][0] user = User.objects.filter(username=username).first() new_booking = Booking( status=old_booking['status'], updated=old_booking['updated'], depart=old_booking['depart'], last_msg=old_booking['last_msg'], created=old_booking['created'], purpose=old_booking['purpose'], tags=old_booking['tags'], comments=old_booking['comments'], rate=old_booking['rate'], arrive=old_booking['arrive'], user=user, location=location, room=new_room, ) new_booking.save() #print new_booking # Now create the payment for this new booking if old_booking_id in payments_by_booking: old_payment = payments_by_booking[old_booking_id] print "booking %s has payment of $%s" % ( old_booking_id, old_payment['paid_amount']) new_payment = Payment( payment_method=old_payment['payment_method'], payment_service=old_payment['payment_service'], transaction_id=old_payment['transaction_id'], paid_amount=old_payment['paid_amount'], booking=new_booking, ) new_payment.save() new_payment.payment_date = old_payment['payment_date'] new_payment.save() # Pull the booking back out of the DB and recalculate the bill pulled_booking = Booking.objects.get(id=new_booking.id) pulled_booking.generate_bill()
def test_booking_required_fields(self): booking = Booking(name='abc') with self.assertRaises(ValidationError): booking.full_clean()