Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check permissions for /rest/documents #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/jane/documents/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,45 @@ def get_queryset(self):
queryset = queryset.defer('data')
return queryset

def get_filtered_queryset(self, document_type, queryset=None, negate=False,
**kwargs):
"""
Returns a queryset filtered on the items in the JSON index field.

For all args/kwargs see
:meth:`DocumentIndexManager.+get_filtered_queryset`.
"""
# Only create if necessary.
if queryset is None:
queryset = Document.objects

# filter by document type
res_type = get_object_or_404(DocumentType, name=document_type)
queryset = queryset.filter(document_type=res_type)

# Nothing to do.
if not kwargs:
return queryset

# now do the respective filtering on the document indices and get
# a list of document ids that match
# XXX not sure if this is safe, need to check what happens if database
# XXX gets changed while evaluating the request (e.g. table row gets
# XXX deleted during request == ids of rows change??)
indices_queryset = DocumentIndex.objects.get_filtered_queryset(
document_type=document_type, queryset=None, negate=negate,
**kwargs)

# XXX TODO this does not cover the case of multiple indices for one
# XXX TODO single document yet!
document_indices = [doc_ind.document.id
for doc_ind in indices_queryset]

# now restrict document query to respective document ids
queryset = queryset.filter(id__in=document_indices)

return queryset

def delete_document(self, document_type, name, user):
"""
For convenience reasons, offer that method here, including
Expand Down
5 changes: 3 additions & 2 deletions src/jane/quakeml/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ def filter_queryset_user_does_not_have_permission(self, queryset,
model_type, user):
# model_type can be document or document index.
if model_type == "document":
# XXX: Find a good way to do this.
pass
queryset = queryset.model.objects.get_filtered_queryset(
document_type="quakeml",
queryset=queryset, public=True)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might have to be changed now with 533339b

elif model_type == "index":
# Modify the queryset to only contain indices that are public.
# Events that have null for public are considered to be private
Expand Down