Ejemplo n.º 1
0
	def create_order(self):
		"""
		creates an order
		:return: order object
		"""
		user_id = Auth.user('id')
		location_id = Auth.get_location()
		date_booked_for, channel, meal_period, meal_items, menu_id = self.request_params(
			'dateBookedFor', 'channel', 'mealPeriod', 'mealItems', 'menuId'
		)
		if self.order_repo.user_has_order(user_id, date_booked_for, meal_period):
			return self.handle_response('You have already booked for this meal period.', status_code=400)
		
		location = LocationRepo().get(location_id)
		current_time = current_time_by_zone(location.zone)

		if datetime.strptime(date_booked_for, "%Y-%m-%d") < datetime.now():
			return self.handle_response('You are not allowed to book for a date in the past', status_code=400)

		if int(current_time_by_zone(location.zone).strftime('%H')) > 15:
			if check_date_current_vs_date_for(current_time, datetime.strptime(date_booked_for, "%Y-%m-%d")):
				return self.handle_response('It is too late to book a meal for the selected date ', status_code=400)

		meal_object_items = self.meal_item_repo.get_meal_items_by_ids(meal_items)
		
		new_order = self.order_repo.create_order(
			user_id, date_booked_for, meal_object_items, location_id, menu_id, channel, meal_period).serialize()
		
		new_order['mealItems'] = [
			item.to_dict(only=OrderController.default_meal_item_return_fields)
			for item in meal_object_items
		]
		return self.handle_response('OK', payload={'order': new_order}, status_code=201)
Ejemplo n.º 2
0
    def test_current_time_by_time_zone_negative_timezone(
            self, mock_current_time):
        mock_current_time.utcnow = Mock(
            return_value=datetime(2019, 2, 15, 6, 0, 0))
        resp = current_time_by_zone('-4')

        self.assertEqual(resp.hour, 2)
Ejemplo n.º 3
0
    def test_current_time_by_time_zone_positive_timezone(
            self, mock_current_time):
        mock_current_time.utcnow = Mock(
            return_value=datetime(2019, 2, 15, 6, 0, 0))
        res = current_time_by_zone('+3')

        self.assertEqual(res.hour, 9)
Ejemplo n.º 4
0
    def get_menu_start_end_on(location):
        """This method takes a location id, and attempts to return a start date and an end date based on the conditions
        the application expects.

        Conditions:
            If current datetime is over 3PM , skip a day and return next days.
            If day is thursday and not yet 3PM, return only friday
            If current datetime is friday, saturday or sunday, return next week from monday till friday.
            If No conditions matches, return None for both dates.

        """
        start_on = end_on = None

        current_date = current_time_by_zone(location.zone)

        if current_date.strftime('%a') == 'Mon' and int(
                current_date.strftime('%H')) >= 15:
            start_on = current_date + timedelta(days=2)
            end_on = start_on + timedelta(days=2)

        elif current_date.strftime('%a') == 'Tue' and int(
                current_date.strftime('%H')) >= 15:
            start_on = current_date + timedelta(days=2)
            end_on = start_on + timedelta(days=1)

        elif current_date.strftime('%a') == 'Wed' and int(
                current_date.strftime('%H')) >= 15:
            start_on = end_on = current_date + timedelta(days=2)

        elif current_date.strftime('%a') == 'Thu' and int(
                current_date.strftime('%H')) >= 15:
            start_on = end_on = current_date + timedelta(days=4)

        else:

            start_on = current_date + timedelta(days=1)
            if current_date.strftime('%a') == 'Mon':
                end_on = start_on + timedelta(3)
            if current_date.strftime('%a') == 'Tue':
                end_on = start_on + timedelta(2)
            if current_date.strftime('%a') == 'Wed':
                end_on = start_on + timedelta(1)
            if current_date.strftime('%a') == 'Thu':
                end_on = start_on

            else:
                if current_date.strftime('%a') == 'Fri':
                    start_on = current_date + timedelta(days=3)
                    end_on = current_date + timedelta(days=7)

                if current_date.strftime('%a') == 'Sat':
                    start_on = current_date + timedelta(days=2)
                    end_on = current_date + timedelta(days=6)

                if current_date.strftime('%a') == 'Sun':
                    next_day = 1 if int(
                        current_date.strftime('%H')) < 15 else 2
                    start_on = current_date + timedelta(days=next_day)
                    end_on = current_date + timedelta(days=5)

        return tuple((start_on, end_on))