-
Notifications
You must be signed in to change notification settings - Fork 139
Add user activity logs to messaging endpoint #2691
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
Open
ciremusyoka
wants to merge
4
commits into
main
Choose a base branch
from
log-user-profile-activities
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| import json | ||
|
|
||
| from django.conf import settings | ||
| from django.contrib.auth import get_user_model | ||
| from django.contrib.auth.password_validation import validate_password | ||
| from django.core.cache import cache | ||
| from django.core.validators import ValidationError | ||
|
|
@@ -32,6 +33,10 @@ | |
| from onadata.apps.api.tools import get_baseviewset_class | ||
| from onadata.apps.logger.models.instance import Instance | ||
| from onadata.apps.main.models import UserProfile | ||
| from onadata.apps.messaging.constants import ( | ||
| USER, USER_UPDATED, USER_PASSWORD_CHANGED | ||
| ) | ||
| from onadata.apps.messaging.serializers import send_message | ||
| from onadata.libs import filters | ||
| from onadata.libs.mixins.authenticate_header_mixin import AuthenticateHeaderMixin | ||
| from onadata.libs.mixins.cache_control_mixin import CacheControlMixin | ||
|
|
@@ -50,6 +55,9 @@ | |
| from onadata.libs.utils.email import get_verification_email_data, get_verification_url | ||
| from onadata.libs.utils.user_auth import invalidate_and_regen_tokens | ||
|
|
||
| # pylint: disable=invalid-name | ||
| User = get_user_model() | ||
|
|
||
| BaseViewset = get_baseviewset_class() # pylint: disable=invalid-name | ||
| LOCKOUT_TIME = getattr(settings, "LOCKOUT_TIME", 1800) | ||
| MAX_CHANGE_PASSWORD_ATTEMPTS = getattr(settings, "MAX_CHANGE_PASSWORD_ATTEMPTS", 10) | ||
|
|
@@ -211,6 +219,16 @@ def update(self, request, *args, **kwargs): | |
| username = kwargs.get("user") | ||
| response = super().update(request, *args, **kwargs) | ||
| cache.set(f"{USER_PROFILE_PREFIX}{username}", response.data) | ||
|
|
||
| # send notification on updating user profile | ||
| send_message( | ||
| instance_id=request.user.username, | ||
| target_id=request.user.id, | ||
| target_type=USER, | ||
| user=request.user, | ||
| message_verb=USER_UPDATED, | ||
| ) | ||
|
|
||
| return response | ||
|
|
||
| def retrieve(self, request, *args, **kwargs): | ||
|
|
@@ -283,6 +301,15 @@ def change_password(self, request, *args, **kwargs): # noqa | |
| user_profile.save() | ||
| data.update(invalidate_and_regen_tokens(user=user_profile.user)) | ||
|
|
||
| # send notification account password change | ||
| send_message( | ||
| instance_id=request.user.username, | ||
| target_id=request.user.id, | ||
| target_type=USER, | ||
| user=request.user, | ||
| message_verb=USER_PASSWORD_CHANGED, | ||
| ) | ||
|
|
||
| return Response(status=status.HTTP_200_OK, data=data) | ||
|
|
||
| def partial_update(self, request, *args, **kwargs): | ||
|
|
@@ -303,6 +330,16 @@ def partial_update(self, request, *args, **kwargs): | |
|
|
||
| profile.metadata = metadata | ||
| profile.save() | ||
|
|
||
| # send notification on updating user profile | ||
| send_message( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| instance_id=request.user.username, | ||
| target_id=request.user.id, | ||
| target_type=USER, | ||
| user=request.user, | ||
| message_verb=USER_UPDATED, | ||
| ) | ||
|
|
||
| return Response(data=profile.metadata, status=status.HTTP_200_OK) | ||
|
|
||
| return super().partial_update(request, *args, **kwargs) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can also move this to the serializer. Since we are sending the messaging for both
createandupdate, we can override the serializer'ssavemethod and callsend_messagethere. ExampleWe can differentiate create from update by checking self.instance. When doing an update
self.instanceis not None