Skip to content

Commit

Permalink
Gui dataset search (#23)
Browse files Browse the repository at this point in the history
* rough draft of dataset name search

* playtested
  • Loading branch information
marcmengel authored Mar 19, 2024
1 parent 2ee92ff commit c48bd8f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
7 changes: 5 additions & 2 deletions metacat/db/dbobjects2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1305,7 +1305,7 @@ def exists(db, namespace, name, transaction=None):

@staticmethod
@transactioned
def list(db, namespace=None, parent_namespace=None, parent_name=None, creator=None, namespaces=None, transaction=None):
def list(db, namespace=None, parent_namespace=None, parent_name=None, creator=None, namespaces=None, transaction=None, namelike=None):
namespace = namespace.Name if isinstance(namespace, DBNamespace) else namespace
parent_namespace = parent_namespace.Name if isinstance(parent_namespace, DBNamespace) else parent_namespace
creator = creator.Username if isinstance(creator, DBUser) else creator
Expand All @@ -1315,7 +1315,8 @@ def list(db, namespace=None, parent_namespace=None, parent_name=None, creator=No
parent_namespace=parent_namespace,
parent_name=parent_name,
creator=creator,
namespace_names=namespaces or []
namespace_names=namespaces or [],
namelike=namelike
)
columns = DBDataset.columns("ds")

Expand All @@ -1337,6 +1338,8 @@ def list(db, namespace=None, parent_namespace=None, parent_name=None, creator=No
sql += " and ds.namespace=%(namespace)s"
if namespaces is not None:
sql += " and ds.namespace=any(%(namespace_names)s)"
if namelike:
sql += " and ds.name like %(namelike)s"

#print(sql % params)
transaction.execute(sql, params)
Expand Down
20 changes: 12 additions & 8 deletions webserver/gui_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ def make_page_links(self, npages, page, page_size, all_page_links, window=2):


@sanitize()
def datasets(self, request, relpath, selection=None, page=0, page_size=1000, sort_by="Name", sort_asc="a", **args):
def datasets(self, request, relpath, selection=None, page=0, page_size=1000, sort_by="Name", sort_asc="a", namematch="", **args):
user, auth_error = self.authenticated_user()
if not user:
self.redirect(self.scriptUri() + "/auth/login?redirect=" + self.scriptUri() + relpath)
Expand All @@ -848,26 +848,30 @@ def datasets(self, request, relpath, selection=None, page=0, page_size=1000, sor
owned_namespaces = []
other_namespaces = sorted(all_namespaces.keys())
selection = selection or ("user" if user is not None else None) or "all"


namelike = None
if namematch:
namelike = "%" + namematch.replace("?","_").replace("*","%") + "%"

if user is not None:
owned_namespaces = sorted([ns.Name for ns in DBNamespace.list(db, owned_by_user=user.Username)])
other_namespaces = sorted([name for name in all_namespaces.keys() if name not in owned_namespaces])
if selection == "user":
datasets = DBDataset.list(db, namespaces=owned_namespaces)
datasets = DBDataset.list(db, namespaces=owned_namespaces, namelike=namelike)
elif selection.startswith("namespace:"):
ns = selection[len("namespace:"):]
datasets = DBDataset.list(db, namespace=ns)
datasets = DBDataset.list(db, namespace=ns, namelike=namelike)
else:
# assume selection == "all"
datasets = DBDataset.list(db)
datasets = DBDataset.list(db, namelike=namelike)

sort_by_map = {
"Name": lambda x: (x.Namespace, x.Name),
"Creator": lambda x: x.Creator,
"Created": lambda x: x.CreatedTimestamp,
"Files": lambda x: x.FileCount,
}

if not (sort_by in sort_by_map):
sort_by = "Name"

Expand All @@ -883,11 +887,11 @@ def datasets(self, request, relpath, selection=None, page=0, page_size=1000, sor
ds.GUI_OwnerRole = ns.OwnerRole
ds.GUI_Authorized = user is not None and (admin or self._namespace_authorized(db, ds.Namespace, user))

all_page_links = [f"./datasets?selection={selection}&page_size={page_size}&page={p}&sort_by={sort_by}&sort_asc={sort_asc}" for p in range(npages)]
all_page_links = [f"./datasets?selection={selection}&page_size={page_size}&page={p}&sort_by={sort_by}&sort_asc={sort_asc}&namematch={namematch}" for p in range(npages)]
page_links = self.make_page_links(npages, page, page_size, all_page_links, 2)

return self.render_to_response("datasets.html", datasets=datasets,
page=page, npages=npages, page_links=page_links,
page=page, npages=npages, page_links=page_links, namematch=namematch,
owned_namespaces = owned_namespaces, other_namespaces=other_namespaces,
selection=selection, user=user, **self.messages(args))

Expand Down
19 changes: 14 additions & 5 deletions webserver/templates/datasets.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@
</select>
</td>
</tr>
<tr>
<td>
Name Match
</td>
<td>
<input name=namematch value="{{namematch}}">
<input type="Submit" name="submit">
</td>
</tr>
</table>
</form>

Expand Down Expand Up @@ -62,13 +71,13 @@

<table class="data">
<tr>
<th>Name<a href="./datasets?sort_by=Name&sort_asc=d" style="text-decoration: none">&dArr;</a><a href="./datasets?sort_by=Name&sort_asc=a" style="text-decoration: none">&uArr;</a></th>
<th>Name<a href="./datasets?sort_by=Name&sort_asc=d&namematch={{namematch}}" style="text-decoration: none">&dArr;</a><a href="./datasets?sort_by=Name&sort_asc=a&namematch={{namematch}}" style="text-decoration: none">&uArr;</a></th>
<th>Namespace</th>
{% if logged_in %}<th>Creator<a href="./datasets?sort_by=Creator&sort_asc=d" style="text-decoration: none">&dArr;</a><a href="./datasets?sort_by=Creator&sort_asc=a" style="text-decoration: none">&uArr;</a></th>{% endif %}
<th>Created<a href="./datasets?sort_by=Created&sort_asc=d" style="text-decoration: none">&dArr;</a><a href="./datasets?sort_by=Created&sort_asc=a" style="text-decoration: none">&uArr;</a></th>
{% if logged_in %}<th>Owner<a href="./datasets?sort_by=Owner&sort_asc=d" style="text-decoration: none">&dArr;</a><a href="./datasets?sort_by=Owner&sort_asc=a" style="text-decoration: none">&uArr;</a></th>{% endif %}
{% if logged_in %}<th>Creator<a href="./datasets?sort_by=Creator&sort_asc=d&namematch={{namematch}}" style="text-decoration: none">&dArr;</a><a href="./datasets?sort_by=Creator&sort_asc=a&namematch={{namematch}}" style="text-decoration: none">&uArr;</a></th>{% endif %}
<th>Created<a href="./datasets?sort_by=Created&sort_asc=d&namematch={{namematch}}" style="text-decoration: none">&dArr;</a><a href="./datasets?sort_by=Created&sort_asc=a&namematch={{namematch}}" style="text-decoration: none">&uArr;</a></th>
{% if logged_in %}<th>Owner<a href="./datasets?sort_by=Owner&sort_asc=d&namematch={{namematch}}" style="text-decoration: none">&dArr;</a><a href="./datasets?sort_by=Owner&sort_asc=a&namematch={{namematch}}" style="text-decoration: none">&uArr;</a></th>{% endif %}
<th>Flags</th>
<th>Files<sup>*</sup><a href="./datasets?sort_by=Files&sort_asc=d" style="text-decoration: none">&dArr;</a><a href="./datasets?sort_by=Files&sort_asc=a" style="text-decoration: none">&uArr;</a></th>
<th>Files<sup>*</sup><a href="./datasets?sort_by=Files&sort_asc=d&namematch={{namematch}}" style="text-decoration: none">&dArr;</a><a href="./datasets?sort_by=Files&sort_asc=a&namematch={{namematch}}" style="text-decoration: none">&uArr;</a></th>
<th>Children</th>
<th>Subsets</th>
<th>Parents</th>
Expand Down

0 comments on commit c48bd8f

Please sign in to comment.