Skip to content

Commit

Permalink
Fix copy issue with dict
Browse files Browse the repository at this point in the history
  • Loading branch information
c-e-p committed Feb 25, 2024
1 parent 6b66c41 commit d29d338
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
42 changes: 32 additions & 10 deletions ourchive_app/api/search/search.py
Expand Up @@ -157,15 +157,15 @@ def build_filters(self, filters, mode, include):
full_filters = Q(full_filters & join_filters)
return full_filters

def process_results(self, resultset, page, obj):
def process_results(self, resultset, page, obj, base_string='/search/?'):
page_size = settings.REST_FRAMEWORK['PAGE_SIZE']
paginator = Paginator(resultset, page_size)
count = paginator.count if resultset else 0
resultset = paginator.get_page(page) if resultset else []
next_params = None if not resultset or not resultset.has_next(
) else f"/search/?limit={page_size}&page={page+1}&object_type={obj.__name__}"
) else f"{base_string}limit={page_size}&page={page+1}&object_type={obj.__name__}"
prev_params = None if not resultset or not resultset.has_previous(
) else f"/search/?limit={page_size}&page={page-1}&object_type={obj.__name__}"
) else f"{base_string}limit={page_size}&page={page-1}&object_type={obj.__name__}"
return [resultset, {"count": count, "prev_params": prev_params, "next_params": next_params}]

# todo: move to kwargs or obj. my god.
Expand Down Expand Up @@ -498,13 +498,23 @@ def filter_by_tag(self, **kwargs):
collection_filters = self.get_filters(collection_search)

page = kwargs['page'] if 'page' in kwargs else 1


if 'page' in kwargs['work_search']:
work_search.page = int(kwargs['work_search']['page'])
if 'page' in kwargs['bookmark_search']:
bookmark_search.page = int(kwargs['bookmark_search']['page'])
if 'page' in kwargs['collection_search']:
collection_search.page = int(kwargs['collection_search']['page'])

tag = Tag.objects.get(pk=kwargs['tag_id'])
works = Work.objects.filter(tags__id__exact=tag.id)

if work_filters[0]:
works = works.filter(work_filters[0])
if work_filters[1]:
works = works.filter(work_filters[1])
works = works.filter(draft=False).order_by('-updated_on').distinct()
works = works.filter(draft=False).distinct().order_by('-updated_on')
bookmarks = Bookmark.objects.filter(tags__id__exact=tag.id)
if bookmark_filters[0]:
bookmarks = bookmarks.filter(bookmark_filters[0])
Expand All @@ -518,9 +528,11 @@ def filter_by_tag(self, **kwargs):
collections = collections.filter(collection_filters[1])
collections = collections.filter(draft=False).order_by('-updated_on').distinct()

works_processed = self.process_results(works, page, Work)
bookmarks_processed = self.process_results(bookmarks, page, Bookmark)
collections_processed = self.process_results(collections, page, BookmarkCollection)
base_string = f'/tags/{kwargs["tag_id"]}?tag_id={kwargs["tag_id"]}&'

works_processed = self.process_results(works, work_search.page, Work, base_string)
bookmarks_processed = self.process_results(bookmarks, bookmark_search.page, Bookmark, base_string)
collections_processed = self.process_results(collections, collection_search.page, BookmarkCollection, base_string)

# tag result object
result_json = []
Expand Down Expand Up @@ -555,6 +567,14 @@ def filter_by_attribute(self, **kwargs):
collection_filters = self.get_filters(collection_search)

page = kwargs['page'] if 'page' in kwargs else 1

if 'page' in kwargs['work_search']:
work_search.page = int(kwargs['work_search']['page'])
if 'page' in kwargs['bookmark_search']:
bookmark_search.page = int(kwargs['bookmark_search']['page'])
if 'page' in kwargs['collection_search']:
collection_search.page = int(kwargs['collection_search']['page'])

attribute = AttributeValue.objects.get(pk=kwargs['attr_id'])
works = Work.objects.filter(attributes__id__exact=attribute.id)
if work_filters[0]:
Expand All @@ -575,9 +595,11 @@ def filter_by_attribute(self, **kwargs):
collections = collections.filter(collection_filters[1])
collections = collections.filter(draft=False).order_by('-updated_on').distinct()

works_processed = self.process_results(works, page, Work)
bookmarks_processed = self.process_results(bookmarks, page, Bookmark)
collections_processed = self.process_results(collections, page, BookmarkCollection)
base_string = f'/attributes/{kwargs["attr_id"]}?attr_id={kwargs["attr_id"]}&'

works_processed = self.process_results(works, work_search.page, Work, base_string)
bookmarks_processed = self.process_results(bookmarks, bookmark_search.page, Bookmark, base_string)
collections_processed = self.process_results(collections, bookmark_search.page, BookmarkCollection, base_string)

# tag result object
tag_results = {'data': {}, 'page': {'count': 0}}
Expand Down
9 changes: 4 additions & 5 deletions ourchive_app/frontend/search_models.py
Expand Up @@ -16,19 +16,19 @@ def get_dict(self):


class ObjectSearch():
def __init__(self, mode, order_by, term, filters={'tags': [], 'attributes': []}):
def __init__(self, mode, order_by, term, include_filter=None, exclude_filter=None):
self.term = term
self.include_mode = mode[0]
self.exclude_mode = mode[1]
self.page = 1
self.order_by = order_by
self.include_filter = filters.copy()
self.exclude_filter = filters.copy()
self.include_filter = {'tags': [], 'attributes': []} if not include_filter else include_filter
self.exclude_filter = {'tags': [], 'attributes': []} if not exclude_filter else exclude_filter


class TagSearch(ObjectSearch):
def __init__(self, mode, order_by, term):
super(TagSearch, self).__init__(mode, order_by, term, {'tag_type': [], 'text': []})
super(TagSearch, self).__init__(mode, order_by, term, {'tag_type': [], 'text': []}, {'tag_type': [], 'text': []})


class WorkSearch(object):
Expand Down Expand Up @@ -74,7 +74,6 @@ def with_term(self, term, pagination=None, mode=('all', 'all'), order_by='-updat
return_obj.tag_search.page = pagination['page']
elif obj == 'bookmarkcollection':
return_obj.collection_search.page = pagination['page']

return return_obj.get_dict()

def get_object_type(self, filter_term):
Expand Down
12 changes: 9 additions & 3 deletions ourchive_app/frontend/searcher.py
Expand Up @@ -107,7 +107,6 @@ def iterate_facets(facets, item, excluded=True):

def get_response_facets(response_json, request_object):
facets = response_json['results']['facet']
print(request_object[1]['exclude'])
for item in request_object[1]['exclude']:
facets = iterate_facets(facets, item)
for item in request_object[1]['include']:
Expand All @@ -122,6 +121,8 @@ def get_empty_response_obj():
def get_search_request(request, request_object, request_builder):
return_keys = {'include': [], 'exclude': []}
for key in request.POST:
if key == 'tag_id' or key == 'attr_id':
continue
filter_val = request.POST.get(key, None)
include_exclude = 'exclude' if 'exclude_' in key else 'include'
key = key.replace('exclude_', '') if include_exclude == 'exclude' else key.replace('include_', '')
Expand Down Expand Up @@ -152,17 +153,22 @@ def build_and_execute_search(request):
# prepare search & preserve request data
tag_id = None
attr_id = None
valid_search = False
if 'term' in request.GET:
term = request.GET['term']
valid_search = True
elif 'term' in request.POST:
term = request.POST['term']
elif 'tag_id' in request.GET:
valid_search = True
if 'tag_id' in request.GET:
tag_id = request.GET['tag_id']
term = ""
valid_search = True
elif 'attr_id' in request.GET:
attr_id = request.GET['attr_id']
term = ""
else:
valid_search = True
if not valid_search:
return None
active_tab = request.POST.get('active_tab', None)
include_filter_any = 'any' if request.POST.get('include_any_all') == 'on' else 'all'
Expand Down

0 comments on commit d29d338

Please sign in to comment.