From 062c07ad25a4c83fa771cc8b90c48124e5180900 Mon Sep 17 00:00:00 2001 From: Lukas Hellebrandt Date: Tue, 6 Jun 2023 16:19:26 +0200 Subject: [PATCH] Add TablePreferences entity --- nailgun/entities.py | 42 ++++++++++++++++++++++++++++++++++++++++ nailgun/entity_mixins.py | 4 ++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/nailgun/entities.py b/nailgun/entities.py index 77cc467e..df6bcd0d 100644 --- a/nailgun/entities.py +++ b/nailgun/entities.py @@ -8563,3 +8563,45 @@ def sync(self, synchronous=True, timeout=None, **kwargs): kwargs.update(self._server_config.get_client_kwargs()) response = client.put(self.path('sync'), **kwargs) return _handle_response(response, self._server_config, synchronous, timeout) + + +class TablePreferences( + Entity, + EntityCreateMixin, + EntityDeleteMixin, + EntityReadMixin, + EntitySearchMixin, + EntityUpdateMixin, +): + """A representation of a Table Preference entity.""" + + def __init__(self, server_config=None, **kwargs): + _check_for_value('user', kwargs) + self._fields = { + 'name': entity_fields.StringField(), + 'columns': entity_fields.ListField(), + 'created_at': entity_fields.DateTimeField(), + 'updated_at': entity_fields.DateTimeField(), + } + self._path_fields = { + 'user': entity_fields.OneToOneField(User), + } + self._fields.update(self._path_fields) + self.user = kwargs.get('user') + self._meta = { + 'api_path': f'/api/v2/users/{self.user.id}/table_preferences', + } + super().__init__(server_config, **kwargs) + + def read(self, entity=None, attrs=None, ignore=None, params=None): + """Ignore path related fields as they're never returned by the server + and are only added to entity to be able to use proper path. + """ + entity = entity or self.entity_with_parent(user=self.user) + if ignore is None: + ignore = set() + ignore.add('user') + return super().read(entity, attrs, ignore, params) + + def search(self, fields=None, query=None, filters=None): + return super().search(fields=fields, query=query, filters=filters, path_fields={'user': self.user}) diff --git a/nailgun/entity_mixins.py b/nailgun/entity_mixins.py index f7f97be0..c5a7d8b1 100644 --- a/nailgun/entity_mixins.py +++ b/nailgun/entity_mixins.py @@ -1308,7 +1308,7 @@ def search_normalize(self, results): normalized.append(attrs) return normalized - def search(self, fields=None, query=None, filters=None): + def search(self, fields=None, query=None, filters=None, path_fields={}): """Search for entities. At its simplest, this method searches for all entities of a given kind. @@ -1396,7 +1396,7 @@ def search(self, fields=None, query=None, filters=None): except TypeError: # in the event that an entity's init is overwritten # with a positional server_config - entity = type(self)(**result) + entity = type(self)(**path_fields, **result) entities.append(entity) if filters is not None: entities = self.search_filter(entities, filters)