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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

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'},
)

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.

None yet

3 participants