forked from CenterForOpenScience/osf.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
John Tordoff
committed
Mar 18, 2024
1 parent
e319191
commit bab4498
Showing
8 changed files
with
147 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from kombu import Exchange | ||
from framework.celery_tasks import app as celery_app | ||
from framework.celery_tasks.handlers import enqueue_task | ||
|
||
|
||
def publish_deactivated_user(user): | ||
enqueue_task( | ||
_publish_user_status_change.s( | ||
body={ | ||
'action': 'deactivate', | ||
'user_uri': user.url, | ||
}, | ||
) | ||
) | ||
|
||
|
||
def publish_reactivate_user(user): | ||
enqueue_task( | ||
_publish_user_status_change.s( | ||
body={ | ||
'action': 'reactivate', | ||
'user_uri': user.url, | ||
}, | ||
) | ||
) | ||
|
||
|
||
def publish_merged_user(user): | ||
enqueue_task( | ||
_publish_user_status_change.s( | ||
body={ | ||
'action': 'merge', | ||
'user_uri': user.url, | ||
'merged_user_uri': user.merged_by.url, | ||
}, | ||
) | ||
) | ||
|
||
|
||
@celery_app.task() | ||
def _publish_user_status_change(body: dict): | ||
with celery_app.producer_pool.acquire() as producer: | ||
producer.publish( | ||
body=body, | ||
exchange=Exchange(celery_app.conf.account_status_changes), | ||
serializer='json' | ||
) |
This file contains 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from django.core.management.base import BaseCommand | ||
from osf.models import OSFUser | ||
from osf.external.messages.celery_publishers import ( | ||
publish_deactivated_user, | ||
publish_reactivate_user, | ||
publish_merged_user, | ||
) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Sends a message to manage a user state, for test purposes only.' | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument('user_guid', type=str, help='URI of the user to post.') | ||
# Adding a new argument to specify the action to perform | ||
parser.add_argument( | ||
'action', | ||
type=str, | ||
help='The action to perform on the user (deactivate, reactivate, merge).', | ||
choices=['deactivate', 'reactivate', 'merge'] | ||
) | ||
|
||
def handle(self, *args, **options): | ||
user_guid = options['user_guid'] | ||
user = OSFUser.objects.get(guids___id=user_guid) | ||
action = options['action'] | ||
|
||
# Using a mapping of action to function to simplify the control flow | ||
actions_to_functions = { | ||
'deactivate': publish_deactivated_user, | ||
'reactivate': publish_reactivate_user, | ||
'merge': publish_merged_user, | ||
} | ||
|
||
if action in actions_to_functions: | ||
actions_to_functions[action](user) # Call the appropriate function | ||
self.stdout.write(self.style.SUCCESS(f'Successfully {action} message for user: {user._id}')) | ||
else: | ||
self.stdout.write(self.style.ERROR('Invalid action specified.')) |
This file contains 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 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 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 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 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