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

fix: Add a decorator to put API user in request #1278 #1279

Merged
merged 11 commits into from
Jul 24, 2024

Conversation

vanlummelhuizen
Copy link
Collaborator

I tried to create a solution that is elegant and reusable.

@vanlummelhuizen
Copy link
Collaborator Author

A remark: as it is now implemented there are no errors that could be passed to the client like what happens in

results['errors'] = [gettext("Your Authorization Token does not match anything.")]

I think I can implement it so that errors could be passed. I will work on that next week.

Copy link
Collaborator

@Woseseltops Woseseltops left a comment

Choose a reason for hiding this comment

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

Fancy walrus operator

@vanlummelhuizen
Copy link
Collaborator Author

Fancy walrus operator

Nice that you noticed. Unfortunately (?) I removed it in my latest changes.

I think this is ready to merge now, if approved of course.

@vanlummelhuizen
Copy link
Collaborator Author

I think this is ready to merge now, if approved of course.

Hmm, I keep finding things to improve in the API. Perhaps keep this PR open until I have finished checking all API endpoints.

@susanodd
Copy link
Collaborator

susanodd commented Jul 1, 2024

Looks good to me! Much cleaner.

@vanlummelhuizen
Copy link
Collaborator Author

@Woseseltops @susanodd @Jetske

I have gone over all endpoint as documented in https://github.com/Signbank/Global-signbank/wiki/Signbank-API and added the put_api_user_in_request decorator and changed some code where applicable.

Note: The following function seems to need some work. For instance, the errors variable is a string that is never send back to the user.

def upload_videos_to_glosses(request, datasetid):
# get file as a url parameter: /dictionary/upload_videos_to_glosses/5
has_permission = True
errors = ""
if not request.user.is_authenticated:
has_permission = False
errors = 'Please login to use the requested functionality.'
# check if the user can manage this dataset
try:
group_manager = Group.objects.get(name='Dataset_Manager')
except ObjectDoesNotExist:
group_manager = None
has_permission = False
errors = 'No group Dataset_Manager found.'
groups_of_user = request.user.groups.all()
if has_permission and group_manager not in groups_of_user:
has_permission = False
errors = 'You must be in group Dataset Manager to upload a zip video archive.'
if has_permission and not errors:
dataset_id = int(datasetid)
dataset = Dataset.objects.filter(id=dataset_id).first()
# get paths of uploaded videos to import
video_file_paths = uploaded_video_filepaths(dataset)
else:
# if the user does not have permission, an empty file is generated
video_file_paths = []
pseudo_buffer = VideoImporter()
return StreamingHttpResponse(
(pseudo_buffer.write(request, vg) for vg in json_start() + video_file_paths + json_finish()),
content_type="application/json",
headers={"Content-Disposition": 'attachment; filename='+'glosses.json'},
)

@vanlummelhuizen
Copy link
Collaborator Author

Ready to merge afaic

@susanodd
Copy link
Collaborator

susanodd commented Jul 4, 2024

@Woseseltops @susanodd @Jetske

I have gone over all endpoint as documented in https://github.com/Signbank/Global-signbank/wiki/Signbank-API and added the put_api_user_in_request decorator and changed some code where applicable.

Note: The following function seems to need some work. For instance, the errors variable is a string that is never send back to the user.

def upload_videos_to_glosses(request, datasetid):
# get file as a url parameter: /dictionary/upload_videos_to_glosses/5
has_permission = True
errors = ""
if not request.user.is_authenticated:
has_permission = False
errors = 'Please login to use the requested functionality.'
# check if the user can manage this dataset
try:
group_manager = Group.objects.get(name='Dataset_Manager')
except ObjectDoesNotExist:
group_manager = None
has_permission = False
errors = 'No group Dataset_Manager found.'
groups_of_user = request.user.groups.all()
if has_permission and group_manager not in groups_of_user:
has_permission = False
errors = 'You must be in group Dataset Manager to upload a zip video archive.'
if has_permission and not errors:
dataset_id = int(datasetid)
dataset = Dataset.objects.filter(id=dataset_id).first()
# get paths of uploaded videos to import
video_file_paths = uploaded_video_filepaths(dataset)
else:
# if the user does not have permission, an empty file is generated
video_file_paths = []
pseudo_buffer = VideoImporter()
return StreamingHttpResponse(
(pseudo_buffer.write(request, vg) for vg in json_start() + video_file_paths + json_finish()),
content_type="application/json",
headers={"Content-Disposition": 'attachment; filename='+'glosses.json'},
)

Hmmm. I see that. It looks like it might be testing things that are already tested at a previous step.
Sometimes, to make type check etc it is needed to check for things that don't happen.
(Not related, but an example of, when we know something is unique, but use "first" anyway and ignore the case when there could have been multiple objects. Then test if first is empty. There is lots of code that does this for the dataset languages.)

But could it happen I guess if somebody knew about the url and didn't follow the previous steps.
But it looks like I just ignored these, but tested anyway. (Remove code? Or give a Http warning and crash out?)

@susanodd
Copy link
Collaborator

susanodd commented Jul 4, 2024

I actually don't know how to "approve" on GitHub. I don't find any place I can click. Normally I just write comments.

Copy link
Collaborator

@Woseseltops Woseseltops left a comment

Choose a reason for hiding this comment

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

Improves maintainability

@susanodd
Copy link
Collaborator

Ready to merge afaic

Okay, it's probably time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants