def on_road(self, car_online, car_location): cls = type(self) # Print the initial location and Car info with cls.print_lock: info(_(u'Start to work: %s, driver %s, start from %s') % \ (car_online, car_online.driver, car_location)) # Endless Car's loop while True: # - select the timeout (min <-> sec) when the Car is silent and only # move; # - select the speed; # - select the direction (one from 8: north, north-east etc; # - change the location and tell the system about it. old_car_location = car_location next_time_delta = random.randint(5, 15) speed = random.uniform(40.0, 80.0) * 1000/ 60 # in one minute direction = random.randint(0, 7) distance = speed * next_time_delta car_location = add_distance(old_car_location, direction, distance) verified_distance = get_distance(car_location, old_car_location) msg = u"""Car %s, driver %s, move for %d minute(s) from %s to %s on %d meters to %s""" % (car_online, car_online.driver, next_time_delta, cs(old_car_location), cs(car_location) int(verified_distance), get_direction_name(direction)) time.sleep(next_time_delta) with update_lock: self._cars_position[car_online.id] = car_location with cls.print_lock: info(msg)
def create_admin_user(self): """ Create admin User. """ prompt(_(u'Create the admin user')) item = list(RandomItemSupplier(1))[0] user_data = UserSerializer(item).data user_data['is_staff'] = True user_data['is_active'] = True user_data['password'] = RandomItemSupplier.ADMIN_PASSWORD info(_(u'Done')) return self.create_user_by_model(**user_data)
def create_clients(self): _drivers_ids = [p.pk for p in self._drivers] _clients = filter(lambda p: p.id not in _drivers_ids, self._persons) how_many = len(_clients) prompt(_(u'Create the clients (%d)' % how_many)) for person in _clients: item = {'person': person.pk, } client_serializer = DriverSerializer(data=item) if client_serializer.is_valid(): client_data = client_serializer.data client = self.create_client_by_api(**client_data) if client: self._clients.append(client) info(_(u'Done'))
def create_drivers(self, how_many): prompt(_(u'Create the drivers (%d)' % how_many)) for person in take_part(self._persons, how_many): license_num_data_list = list(RandomItemSupplier(1)\ .iterator(data_type=RandomItemSupplier.LICENSE_NUM_DATA)) if license_num_data_list: license_num_data = license_num_data_list[0] assert isinstance(license_num_data, dict), \ u'Invalid result type' if 'license_no' in license_num_data: item = {'person': person.pk, 'license_no': license_num_data['license_no']} driver_serializer = DriverSerializer(data=item) if driver_serializer.is_valid(): driver_data = driver_serializer.data driver = self.create_driver_by_api(**driver_data) if driver: self._drivers.append(driver) info(_(u'Done'))
def create_users(self): """ Create ordinary Users. """ prompt(_(u'Create the users (%d)' % self._total_users)) for item in RandomItemSupplier(self._total_users): user_serializer = UserSerializer(data=item) if user_serializer.is_valid(): user_data = user_serializer.data user_data['is_staff'] = False user_data['is_active'] = True user_data['password'] = RandomItemSupplier.USER_PASSWORD user = self.create_user_by_api(**user_data) if user: self._users.append(user) item['user'] = user.pk person_serializer = PersonSerializer(data=item) if person_serializer.is_valid(): person_data = person_serializer.data person = self.create_person_by_api(**person_data) if person: self._persons.append(person) info(_(u'Done'))
next_time_delta, cs(old_car_location), cs(car_location) int(verified_distance), get_direction_name(direction)) time.sleep(next_time_delta) with update_lock: self._cars_position[car_online.id] = car_location with cls.print_lock: info(msg) def run(self): if Car.objects.count() < DEFAULT_TOTAL_CARS: self._admin = self.create_admin_user() self.create_users() self.create_drivers(DEFAULT_TOTAL_DRIVERS) self.create_clients() self.create_cars() else: self._cars = Car.objects.all()[:DEFAULT_TOTAL_CARS] self.run_cars() self.run_orders() if __name__ == '__main__': logger = logging.getLogger(__name__) info(_(u'Start the Demo')) demo = DemoWrapper() demo.run()