def get_device(self, request): """Gets a device using any identifier in device_messages.DeviceRequest.""" device = _get_device(request) if not device.enrolled: raise endpoints.BadRequestException( device_model.DEVICE_NOT_ENROLLED_MSG % device.identifier) user_email = user_lib.get_user_email() datastore_user = user_model.User.get_user(user_email) if (permissions.Permissions.READ_DEVICES not in datastore_user.get_permissions()): if device.assigned_user != user_email: raise endpoints.UnauthorizedException( 'You do not have the proper permission to perform this action. ' 'Please contact your IT administrator if you feel like this is in ' 'error.') directory_client = directory.DirectoryApiClient(user_email) try: given_name = directory_client.given_name(user_email) except (directory.DirectoryRPCError, directory.GivenNameDoesNotExistError): given_name = None message = api_utils.build_device_message_from_model( device, config_model.Config.get('allow_guest_mode')) message.given_name = given_name return message
def test_build_device_message_from_model(self): """Test the construction of a device message from a device entity.""" test_device = device_model.Device( serial_number='test_serial_value', asset_tag='test_asset_tag_value', enrolled=True, device_model='test model value', due_date=datetime.datetime(year=2018, month=1, day=1), last_known_healthy=datetime.datetime(year=2018, month=1, day=2), shelf=self.test_shelf_model.key, assigned_user='******', assignment_date=datetime.datetime(year=2018, month=1, day=3), current_ou=constants.ORG_UNIT_DICT['GUEST'], ou_changed_date=datetime.datetime(year=2018, month=1, day=4), locked=True, lost=False, mark_pending_return_date=datetime.datetime(year=2018, month=1, day=5), chrome_device_id='device id value', last_heartbeat=datetime.datetime(year=2018, month=1, day=6), damaged=None, damaged_reason='Not damaged', last_reminder=device_model.Reminder(level=1), next_reminder=device_model.Reminder(level=2), ).put().get() expected_message = device_messages.Device( serial_number='test_serial_value', asset_tag='test_asset_tag_value', identifier='test_asset_tag_value', enrolled=True, device_model='test model value', due_date=datetime.datetime(year=2018, month=1, day=1), last_known_healthy=datetime.datetime(year=2018, month=1, day=2), shelf=self.expected_shelf_message, assigned_user='******', assignment_date=datetime.datetime(year=2018, month=1, day=3), current_ou=constants.ORG_UNIT_DICT['GUEST'], ou_changed_date=datetime.datetime(year=2018, month=1, day=4), locked=True, lost=False, mark_pending_return_date=datetime.datetime(year=2018, month=1, day=5), chrome_device_id='device id value', last_heartbeat=datetime.datetime(year=2018, month=1, day=6), damaged=None, damaged_reason='Not damaged', last_reminder=device_messages.Reminder(level=1), next_reminder=device_messages.Reminder(level=2), guest_permitted=True, guest_enabled=True, max_extend_date=device_model.calculate_return_dates( test_device.assignment_date).max, overdue=True, ) actual_message = api_utils.build_device_message_from_model( test_device, True) self.assertEqual(actual_message, expected_message)
def list_user_devices(self, request): """Lists all devices assigned to the user.""" self.check_xsrf_token(self.request_state) user = user_lib.get_user_email() guest_permitted = config_model.Config.get('allow_guest_mode') device_messages = [] for device in device_model.Device.list_by_user(user): device_messages.append( api_utils.build_device_message_from_model(device, guest_permitted)) return device_message.ListUserDeviceResponse(devices=device_messages)
def get_device(self, request): """Gets a device using any identifier in device_message.DeviceRequest.""" device = _get_device(request) if not device.enrolled: raise endpoints.BadRequestException( device_model.DEVICE_NOT_ENROLLED_MSG % device.identifier) user_email = user_lib.get_user_email() directory_client = directory.DirectoryApiClient(user_email) try: given_name = directory_client.given_name(user_email) except ( directory.DirectoryRPCError, directory.GivenNameDoesNotExistError): given_name = None message = api_utils.build_device_message_from_model( device, config_model.Config.get('allow_guest_mode')) message.given_name = given_name return message
def list_devices(self, request): """Lists all devices based on any device attribute.""" self.check_xsrf_token(self.request_state) query, sort_options, returned_fields = ( search_utils.set_search_query_options(request.query)) if not query: shelf_query = '' if request.shelf: shelf_urlsafe_key = request.shelf.shelf_request.urlsafe_key if not shelf_urlsafe_key: shelf_urlsafe_key = shelf_api.get_shelf( request.shelf.shelf_request).key.urlsafe() request.shelf = None shelf_query = ':'.join(('shelf', shelf_urlsafe_key)) query = search_utils.to_query(request, device_model.Device) query = ' '.join((query, shelf_query)) cursor = search_utils.get_search_cursor(request.page_token) search_results = device_model.Device.search( query_string=query, query_limit=request.page_size, cursor=cursor, sort_options=sort_options, returned_fields=returned_fields) new_search_cursor = None if search_results.cursor: new_search_cursor = search_results.cursor.web_safe_string guest_permitted = config_model.Config.get('allow_guest_mode') messages = [] for document in search_results.results: device = api_utils.get_ndb_key(document.doc_id).get() messages.append( api_utils.build_device_message_from_model(device, guest_permitted)) return device_message.ListDevicesResponse( devices=messages, additional_results=bool(new_search_cursor), page_token=new_search_cursor)