Skip to content

Commit

Permalink
Add TablePreferences entity
Browse files Browse the repository at this point in the history
  • Loading branch information
lhellebr committed Jun 6, 2023
1 parent 27932aa commit 062c07a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
42 changes: 42 additions & 0 deletions nailgun/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
4 changes: 2 additions & 2 deletions nailgun/entity_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 062c07a

Please sign in to comment.