Skip to content

Commit

Permalink
Merge pull request #848 from OpenSourceBrain/feature/828
Browse files Browse the repository at this point in the history
Feature/828 - Workspace and repositories search and filter refactor
  • Loading branch information
filippomc authored Dec 11, 2023
2 parents 644911d + ee15aac commit 815e22c
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 27 deletions.
3 changes: 0 additions & 3 deletions applications/osb-portal/src/pages/WorkspacesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ export const WorkspacesPage = (props: WorkspacesPageProps) => {
setSearchFilterValues({
...searchFilterValues,
text: newTextFilter,
tags: newTextFilter
? [...searchFilterValues?.tags, newTextFilter]
: searchFilterValues?.tags,
});
}, 500);

Expand Down
4 changes: 2 additions & 2 deletions applications/osb-portal/src/service/RepositoryService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type RepositoriesListAndPaginationDetails = InlineResponse2001;

const workspacesApiUri = "/proxy/workspaces/api";

const PER_PAGE_DEFAULT = 18;
const PER_PAGE_DEFAULT = 24;

class RepositoryService {
workspacesApi: RestApi = null;
Expand Down Expand Up @@ -108,7 +108,7 @@ class RepositoryService {
return this.workspacesApi.osbrepositoryGet({
page,
perPage: size,
q: `user_id=${userId}`,
userId: userId
});
}

Expand Down
1 change: 1 addition & 0 deletions applications/osb-portal/src/service/WorkspaceService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class WorkspaceService {

if (filter.text) {
params.name__like = filter.text;
params.description__like = filter.text;
}

if (filter.user_id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,36 +52,65 @@ def check(self):

def search_qs(self, filter=None, q=None, tags=None, user_id=None, show_all=False, *args, **kwargs):
q_base = self.model.query
q_base = self.filter_by_user_and_fieldkey(
filter, user_id, show_all, q_base)

if filter is None:
if tags:
q_base = self.filter_by_tags(tags, q_base)
else:
q_base_by_name_description = self.filter_by_name_description(
filter, q_base)
q_base = self.filter_by_search_tags(
filter, q_base, q_base_by_name_description)

if tags:
q_base = q_base.intersect(self.filter_by_tags(tags, q_base))

q_base = q_base.filter(
or_(*[self._create_filter(*f) for f in filter if (f[0].key != "name" and f[0].key != "description")]))

return q_base.order_by(desc(WorkspaceEntity.timestamp_updated))

def filter_by_user_and_fieldkey(self, filter, user_id, show_all, q_base):
if filter and any(field for field, condition, value in filter if field.key == "publicable" and value):
pass
elif user_id is not None:
# Admins see all workspaces, non admin users can see only their own workspaces or shared with them
if not show_all:
q_base = q_base.filter_by(user_id=user_id)
q_base = q_base.union(q_base.filter(
q_base = q_base.union(self.model.query.filter(
WorkspaceEntity.collaborators.any(user_id=user_id)))
else:
q_base = q_base
else:
# No logged in user, show only public (in case was not specified)
q_base = q_base.filter(WorkspaceEntity.publicable == True)
return q_base

def filter_by_tags(self, tags, q_base):
q_base = q_base.join(self.model.tags).filter(
func.lower(Tag.tag).in_(func.lower(t) for t in tags.split("+")))
return q_base

if filter is not None:
if tags:
q_base = q_base.filter(
*[self._create_filter(*f) for f in filter if f[0].key == "name"] )
q_base = q_base.intersect(self.model.query.join(self.model.tags).filter(
func.lower(Tag.tag).in_(func.lower(t) for t in tags.split("+"))))

q_base = q_base.filter(
*[self._create_filter(*f) for f in filter if f[0].key != "name"])
elif tags:
q_base = q_base.join(self.model.tags).filter(
func.lower(Tag.tag).in_(func.lower(t) for t in tags.split("+")))
def filter_by_search_tags(self, filter, q_base, q_base_by_name_description):
search_tags = self.tags_from_search(filter)
q_base = q_base_by_name_description.union(q_base.join(self.model.tags).filter(
func.lower(Tag.tag).in_(func.lower(t) for t in search_tags.split("+"))))

return q_base.order_by(desc(WorkspaceEntity.timestamp_updated))
return q_base

def tags_from_search(self, filter):
tags = ''
for field, condition, value in filter:
if field.key == "name":
tags = value.replace("+", "").replace("%", "")
break
return tags

def filter_by_name_description(self, filter, q_base):
return q_base.filter(
or_(*[self._create_filter(*f) for f in filter if (f[0].key == "name" or f[0].key == "description")]))

def delete(self, id):
super().delete(id)
Expand All @@ -100,21 +129,48 @@ def get(self, id):

def search_qs(self, filter=None, q=None, tags=None, types=None, user_id=None):
q_base = self.model.query
if tags:
q_base = q_base.join(self.model.tags).filter(
Tag.tag.in_(tags.split("+")))
if filter:
q_base = q_base.filter(
or_(*[self._create_filter(*f) for f in filter]))
if user_id is not None:
q_base = q_base.filter_by(user_id=user_id)

if filter is None:
if tags:
q_base = self.filter_by_tags(tags, q_base)
else:
q_base_by_q = self.filter_by_qfilters(filter, q_base)
q_base = self.filter_by_search_tags(filter, q_base, q_base_by_q)
if tags:
q_base = q_base.intersect(self.filter_by_tags(tags, q_base))

if types is not None:
q_base = q_base.filter(
or_(self.model.content_types.ilike(f"%{t}%") for t in types.split("+")))

if user_id is not None:
q_base = q_base.filter_by(user_id=user_id)
return q_base.order_by(desc(OSBRepositoryEntity.timestamp_updated))

def filter_by_qfilters(self, filter, q_base):
q_base = q_base.filter(
or_(*[self._create_filter(*f) for f in filter]))
return q_base

def filter_by_tags(self, tags, q_base):
q_base = q_base.join(self.model.tags).filter(
func.lower(Tag.tag).in_(func.lower(t) for t in tags.split("+")))
return q_base

def filter_by_search_tags(self, filter, q_base, q_base_by_q):
search_tags = self.tags_from_search(filter)
q_base = q_base_by_q.union(q_base.join(self.model.tags).filter(
func.lower(Tag.tag).in_(func.lower(t) for t in search_tags.split("+"))))
return q_base

def tags_from_search(self, filter):
tags = ''
for field, condition, value in filter:
if field.key == "name":
tags = value.replace("+", "").replace("%", "")
break
return tags


class VolumeStorageRepository(BaseModelRepository):
model = VolumeStorage
Expand Down

0 comments on commit 815e22c

Please sign in to comment.