def setUp(self): self.url = reverse('booking:search_results') self.client = Client() self.datetime = timezone.make_aware( datetime.now(), timezone.get_current_timezone()).replace(microsecond=0) self.restaurant1 = Restaurant(name='da mario', city='vignola', address='via baracchini, 95', n_places=50, booking_duration=120) restaurant_position = get_coordinates(self.restaurant1.city + ', ' + self.restaurant1.address) self.restaurant1.latitude = restaurant_position['lat'] self.restaurant1.longitude = restaurant_position['lng'] self.restaurant2 = Restaurant(name='da paolo', city='san cesario sul panaro', address='via della meccanica', n_places=80, booking_duration=90) restaurant_position = get_coordinates(self.restaurant2.city + ', ' + self.restaurant2.address) self.restaurant2.latitude = restaurant_position['lat'] self.restaurant2.longitude = restaurant_position['lng'] self.restaurant3 = Restaurant(name='da paolo', city='alba adriatica', address='via pompeo', n_places=120, booking_duration=150) restaurant_position = get_coordinates(self.restaurant3.city + ', ' + self.restaurant3.address) self.restaurant3.latitude = restaurant_position['lat'] self.restaurant3.longitude = restaurant_position['lng']
def setUp(self): self.url = reverse('booking:index') self.client = Client() self.password = '******' self.restaurant = Restaurant.objects.create( name='da mario', city='vignola', address='via baracchini, 95', n_places=50, booking_duration=120) restaurant_position = get_coordinates(self.restaurant.city + ', ' + self.restaurant.address) self.restaurant.latitude = restaurant_position['lat'] self.restaurant.longitude = restaurant_position['lng'] self.restaurant.save() self.client_user = User.objects.create(first_name='paolo', last_name='verdi', email='*****@*****.**', username='******', user_type=User.TYPES[0][0]) self.client_user.set_password(self.password) self.client_user.save() self.restaurant_user = User.objects.create( first_name='mario', last_name='rossi', email='*****@*****.**', username='******', user_type=User.TYPES[1][0], restaurant_information=self.restaurant) self.restaurant_user.set_password(self.password) self.restaurant_user.save()
def setUp(self): self.restaurant = Restaurant.objects.create( name='da mario', city='vignola', address='via baracchini, 95', n_places=50, booking_duration=120) self.client_user = User.objects.create(first_name='paolo', last_name='verdi', email='*****@*****.**', username='******', user_type=User.TYPES[0][0]) self.client_user.set_password('password') self.restaurant_user = User.objects.create( first_name='mario', last_name='rossi', email='*****@*****.**', username='******', user_type=User.TYPES[1][0], restaurant_information=self.restaurant) self.restaurant_user.set_password('password') restaurant_position = get_coordinates(self.restaurant.city + ', ' + self.restaurant.address) self.restaurant.latitude = restaurant_position['lat'] self.restaurant.longitude = restaurant_position['lng'] self.booking = Booking( client=self.client_user, restaurant=self.restaurant, n_places=2, start_time=timezone.make_aware( datetime.now(), timezone.get_current_timezone()).replace(microsecond=0), state=Booking.STATES[0][0]) self.booking.end_time = self.booking.calculate_end_time()
def form_valid(self, form): user = form.save(commit=False) user.set_password(form.cleaned_data['password']) user.save() if int(user.user_type) == User.TYPES[1][0]: position = get_coordinates(form.cleaned_data['city'] + ', ' + form.cleaned_data['address']) lng = None lat = None if position: lng = position['lng'] lat = position['lat'] restaurant = Restaurant.objects.create( name=form.cleaned_data['restaurant_name'], address=form.cleaned_data['address'], city=form.cleaned_data['city'], n_places=form.cleaned_data['n_places'], booking_duration=form.cleaned_data['booking_duration'], longitude=lng, latitude=lat, ) for service in form.cleaned_data['services']: restaurant.services.add(Service.objects.get(id=service)) for kitchen_type in form.cleaned_data['kitchen_types']: restaurant.kitchen_types.add( KitchenType.objects.get(id=kitchen_type)) user.restaurant_information = restaurant user.save() login(self.request, user) return super(RegistrationView, self).form_valid(form)
def test_min_distance_positive(self): distance = self.restaurant.get_distance_from_position( get_coordinates(self.restaurant.city + ', ' + self.restaurant.address)) self.assertEqual(distance < 0, False)
def form_valid(self, form): restaurant = form.save(commit=False) if restaurant.address != form.cleaned_data[ 'address'] or restaurant.city != form.cleaned_data['city']: position = get_coordinates(form.cleaned_data['city'] + ', ' + form.cleaned_data['address']) if position: restaurant.longitude = position['lng'] restaurant.latitude = position['lat'] else: restaurant.longitude = None restaurant.latitude = None files = self.request.FILES.getlist('load_image') for f in files: RestaurantImage.objects.create(image=f, restaurant=restaurant) if form.cleaned_data['remove_images']: for image_id in form.cleaned_data['remove_images'].split(','): try: image = RestaurantImage.objects.get(id=image_id) os.remove(image.image.path) image.delete() except RestaurantImage.DoesNotExist: pass add_voices = None if form.cleaned_data['add_voices']: add_voices = json.loads(form.cleaned_data['add_voices']) if form.cleaned_data['add_categories']: for category in json.loads(form.cleaned_data['add_categories']): new_category = MenuCategory.objects.create( name=category['name'], restaurant=restaurant) if add_voices: for voice in add_voices: if voice['category_id'] == category['id']: voice['category_id'] = new_category.id if form.cleaned_data['remove_categories']: for category in form.cleaned_data['remove_categories'].split(','): try: MenuCategory.objects.get(id=category).delete() except MenuCategory.DoesNotExist: pass if add_voices: for voice in add_voices: category = MenuCategory.objects.get(id=voice['category_id']) MenuVoice.objects.create(name=voice['name'], price=voice['price'], menu_category=category) if form.cleaned_data['remove_voices']: for voice in form.cleaned_data['remove_voices'].split(','): try: MenuVoice.objects.get(id=voice).delete() except MenuVoice.DoesNotExist: pass restaurant.save() messages.success(self.request, 'Modifiche effettuate con successo.') return super(RestaurantInfoView, self).form_valid(form)
def get_context_data(self, **kwargs): context = super(ResultsView, self).get_context_data(**kwargs) context['restaurants_available_title'] = 'Ristoranti disponibili' context['restaurants_busy_title'] = 'Ristoranti pieni' context['address_label'] = 'Indirizzo:' context['kitchen_types_label'] = 'Tipi di cucina:' context['services_label'] = 'Servizi:' context['button_text'] = 'Dettagli' context['webpage_title'] = 'Risultati' try: restaurants = Restaurant.objects.all() restaurants_busy = [] restaurants_available = [] if 'services' in self.request.GET: services = self.request.GET.getlist('services') restaurants = restaurants.filter( services__in=services).annotate( num_services=Count('services')).filter( num_services=services.__len__()) if 'kitchen_types' in self.request.GET: kitchen_types = self.request.GET.getlist('kitchen_types') restaurants = restaurants.filter( kitchen_types__in=kitchen_types).annotate( num_kitchen_types=Count('kitchen_types')).filter( num_kitchen_types=kitchen_types.__len__()) restaurants = restaurants.filter( n_places__gte=self.request.GET['n_clients']) search_position = get_coordinates(self.request.GET['site']) for restaurant in restaurants: distance = None if search_position: distance = restaurant.get_distance_from_position( search_position) if distance and distance > 50: continue if not distance: distance = 51 search_datetime = timezone.make_aware( datetime.strptime( self.request.GET['date'] + '-' + self.request.GET['time'], '%d/%m/%Y-%H:%M'), timezone.get_current_timezone()) end_time = search_datetime + timedelta( minutes=restaurant.booking_duration) occupied_places = restaurant.bookings.filter( (Q(start_time__lte=search_datetime) & Q(end_time__gte=search_datetime)) | (Q(start_time__lte=end_time) & Q(end_time__gte=end_time))) occupied_places = occupied_places.filter( state=Booking.STATES[1][0]) occupied_places = occupied_places.aggregate( Sum('n_places'))['n_places__sum'] if not occupied_places: occupied_places = 0 element = {'restaurant': restaurant, 'distance': distance} if occupied_places + int( self.request.GET['n_clients']) > restaurant.n_places: restaurants_busy.append(element) else: restaurants_available.append(element) context['restaurants_available'] = sorted( restaurants_available, key=itemgetter('distance')) context['restaurants_busy'] = restaurants_busy context['datetime'] = datetime.strptime( self.request.GET['date'] + '-' + self.request.GET['time'], '%d/%m/%Y-%H:%M') except KeyError: context['error_message'] = 'Problema nel passaggio dei parametri' return context