def handle(self, *args, **options): reservations = 0 new = 0 failed = 0 existing = 0 for reservation in raw_data: r = Reservation() res = reservation.split(";") # 0: Koie, 1: epost, 2: dato reservations += 1 # Find which koie this is try: koie = Koie.objects.get(name=res[0]) except: self.stdout.write("Koie %s not found, continuing" % res[0]) failed += 1 continue r.koie_ordered = koie # Find or create a user user = get_or_create_user(res[1]) r.ordered_by = user # Parse the date # @ TODO: +1 fails if wrong date format ## I hope our users use the DD/MM/YYYY format. input_date = res[2].split("/") for x in range(0, len(input_date)): input_date[x] = int(input_date[x]) reservation_date = date(input_date[2], input_date[1], input_date[0]) r.rent_start = reservation_date r.rent_end = reservation_date if existing_reservation(user, r): # self.stderr.write("This user already has a reservation for this date: %s" % existing_reservation(user, r)) existing += 1 continue r.save() new += 1 self.stdout.write( "%s reservations looked at. %s reservations were already in the system, %s reservations failed to parse, therefore %s new reservations were added." % (reservations, existing, failed, new) )
def reserve_koie(request, reservation_id=None, koie_id=None): """ View to reserve a koie """ if reservation_id == None: reservation = Reservation() else: reservation = get_object_or_404(Reservation, pk=reservation_id) if request.method == 'POST': form = ReservationForm(request.POST) if form.is_valid(): reservation = form.save(commit=False) reservation.ordered_by = get_or_create_user(form.cleaned_data['email']) if validate_reservation(request, reservation): reservation.save() report = generate_report(reservation) messages.success(request, _('%(koie)s reserved for %(date)s.' % {'koie': reservation.koie_ordered, 'date': reservation.rent_date})) messages.info(request, _('You will have to fill out a report after your stay. Please check your email.')) send_report_email(report) return redirect('koie_detail', koie_id=reservation.koie_ordered.id) else: form = ReservationForm(request.POST) else: messages.error(request, _('Please verify all the fields')) form = ReservationForm(request.POST) else: form = ReservationForm() if koie_id is not None: koie = get_object_or_404(Koie, id=koie_id) form.fields['koie_ordered'].initial = koie return render(request, 'reservation.html', { 'active': 'reserve_koie', 'breadcrumbs': [ {'name': _('home').capitalize(), 'url': 'index'}, {'name': _('reservation').capitalize()} ], 'form': form })