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

Add Teams Endpoints to manage teams #1031

Merged
merged 1 commit into from
Dec 10, 2024
Merged

Conversation

tdrivas
Copy link
Contributor

@tdrivas tdrivas commented Sep 17, 2024

New endpoints have been created for managing both teams and member under organization for CRUD actions

@suricactus suricactus changed the title Add Teams Endpoints [WIP] Add Teams Endpoints Oct 15, 2024
@tdrivas
Copy link
Contributor Author

tdrivas commented Oct 30, 2024

Planned Tasks

  • Task 1: Create missing serializers for each entity
  • Task 2: Create tests for each case

Copy link
Collaborator

@suricactus suricactus left a comment

Choose a reason for hiding this comment

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

Hey! There are a few things cleanup to be done before you continue working on these. Would you mind to address the feedback first, so it will be much easier for review.

In short:

  • don't leave random commented code here and there
  • no need to comment obvious things
  • please install pre-commit
  • please rebase on master
  • remove docker-compose.override.local.yml changes

.gitignore Outdated Show resolved Hide resolved
docker-app/qfieldcloud/core/serializers.py Outdated Show resolved Hide resolved
docker-app/qfieldcloud/core/views/users_views.py Outdated Show resolved Hide resolved
docker-app/qfieldcloud/core/views/users_views.py Outdated Show resolved Hide resolved
docker-app/qfieldcloud/core/views/users_views.py Outdated Show resolved Hide resolved
docker-app/qfieldcloud/core/views/users_views.py Outdated Show resolved Hide resolved
docker-app/qfieldcloud/core/views/users_views.py Outdated Show resolved Hide resolved
docker-app/qfieldcloud/core/views/users_views.py Outdated Show resolved Hide resolved
docker-app/qfieldcloud/core/views/users_views.py Outdated Show resolved Hide resolved
docker-compose.override.local.yml Outdated Show resolved Hide resolved
Copy link
Collaborator

@suricactus suricactus left a comment

Choose a reason for hiding this comment

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

General comment:

  • there are still archeological treasures from the initial development, please make sure you remove these and push them
  • merge all the tests in one test_teams.py file
  • the views can be a bit more django-rest-framework-like (DRF) , e.g. you can use RetrieveUpdateDestroyAPIView or similar for the views baseclasses for "teams" and "team members". This will greatly reduce your codebase and simplify maintainence.
  1. cleanup the initial comments in this PR and push all the changes.
  2. rebase to the latest master and push
  3. run pre-commit and push
  4. address most of the requests not related the the views.py or tests
  5. try to automate views behavior with DRF
  6. continue with tests

@tdrivas tdrivas requested a review from suricactus December 2, 2024 11:42
@opengisch opengisch deleted a comment from tdrivas Dec 3, 2024
Copy link
Collaborator

@suricactus suricactus left a comment

Choose a reason for hiding this comment

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

Can you please rebase on the latest master and squash all the changes in a single commit?

Please add typing on all the methods that you add in this PR, except the tests.

Also, unfortunately I see some of our verbal conversation feedback was not addressed.

docker-app/qfieldcloud/core/models.py Show resolved Hide resolved
docker-app/qfieldcloud/core/serializers.py Outdated Show resolved Hide resolved
docker-app/qfieldcloud/core/serializers.py Outdated Show resolved Hide resolved
docker-app/qfieldcloud/core/serializers.py Outdated Show resolved Hide resolved
docker-app/qfieldcloud/core/serializers.py Outdated Show resolved Hide resolved
docker-app/qfieldcloud/core/serializers.py Outdated Show resolved Hide resolved
Comment on lines 568 to 572
raise serializers.ValidationError(
{
"member": f"A user with username '{email_or_username}' already exists."
}
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

You are not adding a user, I guess the message should be different.

Suggested change
raise serializers.ValidationError(
{
"member": f"A user with username '{email_or_username}' already exists."
}
)
raise serializers.ValidationError(
{
"member": f"A member with username '{email_or_username}' already exists."
}
)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This comments is answering all the comments above related to function internal_value:

The idea is the following:

  1. check if the username of the member that is going to be added already exists in another member
  2. check if member is indeed a user (this might be overkill due to an existing check via permissions in the view(???)
  3. otherwise use the fast_search to obtain the user
  try:
            existing_user = User.objects.fast_search(email_or_username)
            
            if TeamMember.objects.filter(member=existing_user):
                raise serializers.ValidationError(
                        {
                            "member": f"A member with username '{email_or_username}' already exists."
                        }
                )
            
            validated_data["member"] = existing_user
        
        except User.DoesNotExist:  
            
            raise serializers.ValidationError(
                        {
                            "member": f"A user with username '{email_or_username}' does not exists."
                        }
            )
        
        return validated_data

Copy link
Collaborator

Choose a reason for hiding this comment

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

What happens if we don't explicitly handle the DoesNotExist error, or we attempt to break the uniqueness of the members in the organization? Do we get a proper error or just error 500? What is the HTTP response body?

If we get 500, then we need to handle it. Your proposal is good.

Note that the messages are weird, you can get a validation message like: "A member with username [email protected] already exists". Just say 'The team member "[email protected]" already exists." We always put placeholders in double quotes in user strings.

@tdrivas
Copy link
Contributor Author

tdrivas commented Dec 3, 2024

Can you please rebase on the latest master and squash all the changes in a single commit?

Please add typing on all the methods that you add in this PR, except the tests.

Also, unfortunately I see some of our verbal conversation feedback was not addressed.

Do you want to add typing also in variables or is it overkill?

for example email_or_username: str = data.get("member", "")

@suricactus
Copy link
Collaborator

suricactus commented Dec 4, 2024

Please add typing on all the methods that you add in this PR, except the tests.

Do you want to add typing also in variables or is it overkill?

for example email_or_username: str = data.get("member", "")

Add typing only when the type cannot be guessed by the type checker. In the case you mentioned, the typechecker has no way to get what is the type at key "member", so it will be Any. So in this case you should add explicit typing that it is str.

But if you have:

def foo() -> int: ...
bar = foo()

There is no need to set the type of bar, as we already set that foo is always returning an int.

docker-app/qfieldcloud/core/serializers.py Outdated Show resolved Hide resolved
docker-app/qfieldcloud/core/serializers.py Outdated Show resolved Hide resolved

def to_internal_value(self, data: Dict[str, Any]) -> Dict[str, Any]:
validated_data = super().to_internal_value(data)
email_or_username = data.get("member")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
email_or_username = data.get("member")
email_or_username: str = data["member"]

We can safely assume the value will always be present in the dict, as the Serializer will automatically check for it. I might be wrong, to be verified.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will check it!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Are you going to check it :P

docker-app/qfieldcloud/core/serializers.py Outdated Show resolved Hide resolved
docker-app/qfieldcloud/core/serializers.py Outdated Show resolved Hide resolved
docker-app/qfieldcloud/core/tests/test_team.py Outdated Show resolved Hide resolved
docker-app/qfieldcloud/core/tests/test_team.py Outdated Show resolved Hide resolved
docker-app/qfieldcloud/core/tests/test_team.py Outdated Show resolved Hide resolved
docker-app/qfieldcloud/core/serializers.py Outdated Show resolved Hide resolved
@tdrivas tdrivas requested a review from suricactus December 5, 2024 14:52
@tdrivas tdrivas requested a review from suricactus December 5, 2024 16:56
docker-app/qfieldcloud/core/tests/test_team.py Outdated Show resolved Hide resolved
docker-app/qfieldcloud/core/tests/test_team.py Outdated Show resolved Hide resolved
@tdrivas tdrivas changed the title [WIP] Add Teams Endpoints QF-4536-Add Teams Endpoints to manage teams Dec 9, 2024
@duke-nyuki
Copy link
Collaborator

Task linked: QF-4536 Add API endpoint to manage teams

@tdrivas tdrivas changed the title QF-4536-Add Teams Endpoints to manage teams Add Teams Endpoints to manage teams Dec 9, 2024
@tdrivas tdrivas requested a review from suricactus December 9, 2024 20:29
Copy link
Collaborator

@suricactus suricactus left a comment

Choose a reason for hiding this comment

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

Good! We are ready to merge this PR. One last renaming and we are good to merge!

Copy link
Collaborator

@suricactus suricactus left a comment

Choose a reason for hiding this comment

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

We are there! Thanks a lot!

Please squash all the changes into a single commit and merge the PR.

Add .env to .gitignore

Updated serializers and views for team and organization

Delete unnecessary comments

Refactor code formatting: Add/remove line spacing for improved readability and consistency

Add test_team.py with unit tests for Team functionality

Add test_team_member.py with unit tests for Team Member functionality and permissions

Update users_views.py: Delete unnecessary comments and messages from responses

Extend serializers for team and team member functionality with access control validation.

Add static method for Team model

Fix quotes in static method for Team model

Update test for retrieving team details

Apply coding style

Delete duplicate file

Delete unneseccary comments

Apply changes after pre-commit

Delete TeamDeleteSerializer

Remove response message when team is deleted

Fix permission check by using permission_utils functions

Use TeamSerializer instead of TeamAccessSerializer

Delete TeamAccessSerializer

Add user email to the addMember validation process

Update names of urls

Delete duplicate validation checks

Update permission checks by using existing functions

Use of drf-based permission for CRUD operations

Refactor Team and Team Member views using DRF generics and centralized permissions

Change to StringRelatedField

Delete TeamDetailSerializer

Fix method headers

Delete member validation method

Delete TeamListSerializer

Optimize user validation using Q function

Optimize query

Fix: Include  field in OrganizationSerializer with SerializerMethodField

Fix tests

Add urls

Fix Team

Fix tests and other nice things :)

Fix and finalize Tests

Split teams and users in views, update urls and serializers

Remove unwanted file

Restore file to inital state

Fix noise

Add try-except in TeamMemberSerializer

Add extend_schema_view decorators

Add typing

Fix noise

Fix Serializers not to use request_method

Add typing, fix serializers and noise

Fix formatting, Team serializer and order tests in a logical way

Add typing

Group tests per user category

Add teams field in the Meta class of OrganizationSerializer

Rename serializer field
@tdrivas tdrivas merged commit 1d93bf5 into opengisch:master Dec 10, 2024
8 checks passed
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.

3 participants