|
1 | 1 | import pytz
|
| 2 | +from enum import Enum |
2 | 3 | from datetime import datetime
|
3 | 4 | from framework import status
|
4 | 5 |
|
|
26 | 27 | from api.share.utils import update_share
|
27 | 28 | from api.caching.tasks import update_storage_usage_cache
|
28 | 29 |
|
29 |
| -from osf.exceptions import NodeStateError |
| 30 | +from osf.exceptions import NodeStateError, RegistrationStuckError |
30 | 31 | from osf.models import (
|
31 | 32 | OSFUser,
|
32 | 33 | NodeLog,
|
@@ -672,44 +673,97 @@ def post(self, request, *args, **kwargs):
|
672 | 673 | return redirect(self.get_success_url())
|
673 | 674 |
|
674 | 675 |
|
675 |
| -class RestartStuckRegistrationsView(NodeMixin, TemplateView): |
676 |
| - """ Allows an authorized user to restart a registrations archive process. |
| 676 | +class RemoveStuckRegistrationsView(NodeMixin, View): |
| 677 | + """ Allows an authorized user to remove a registrations if it's stuck in the archiving process. |
677 | 678 | """
|
678 |
| - template_name = 'nodes/restart_registrations_modal.html' |
679 | 679 | permission_required = ('osf.view_node', 'osf.change_node')
|
680 | 680 |
|
681 | 681 | def post(self, request, *args, **kwargs):
|
682 |
| - # Prevents circular imports that cause admin app to hang at startup |
683 |
| - from osf.management.commands.force_archive import archive, verify |
684 | 682 | stuck_reg = self.get_object()
|
685 |
| - if verify(stuck_reg): |
686 |
| - try: |
687 |
| - archive(stuck_reg) |
688 |
| - messages.success(request, 'Registration archive processes has restarted') |
689 |
| - except Exception as exc: |
690 |
| - messages.error(request, f'This registration cannot be unstuck due to {exc.__class__.__name__} ' |
691 |
| - f'if the problem persists get a developer to fix it.') |
| 683 | + if Registration.find_failed_registrations().filter(id=stuck_reg.id).exists(): |
| 684 | + stuck_reg.delete_registration_tree(save=True) |
| 685 | + messages.success(request, 'The registration has been deleted') |
692 | 686 | else:
|
693 | 687 | messages.error(request, 'This registration may not technically be stuck,'
|
694 | 688 | ' if the problem persists get a developer to fix it.')
|
695 | 689 |
|
696 | 690 | return redirect(self.get_success_url())
|
697 | 691 |
|
698 | 692 |
|
699 |
| -class RemoveStuckRegistrationsView(NodeMixin, TemplateView): |
700 |
| - """ Allows an authorized user to remove a registrations if it's stuck in the archiving process. |
| 693 | +class CheckArchiveStatusRegistrationsView(NodeMixin, View): |
| 694 | + """Allows an authorized user to check a registration archive status. |
| 695 | + """ |
| 696 | + permission_required = ('osf.view_node', 'osf.change_node') |
| 697 | + |
| 698 | + def get(self, request, *args, **kwargs): |
| 699 | + # Prevents circular imports that cause admin app to hang at startup |
| 700 | + from osf.management.commands.force_archive import check |
| 701 | + |
| 702 | + registration = self.get_object() |
| 703 | + |
| 704 | + if registration.archived: |
| 705 | + messages.success(request, f"Registration {registration._id} is archived.") |
| 706 | + return redirect(self.get_success_url()) |
| 707 | + |
| 708 | + try: |
| 709 | + archive_status = check(registration) |
| 710 | + messages.success(request, archive_status) |
| 711 | + except RegistrationStuckError as exc: |
| 712 | + messages.error(request, str(exc)) |
| 713 | + |
| 714 | + return redirect(self.get_success_url()) |
| 715 | + |
| 716 | + |
| 717 | +class CollisionMode(Enum): |
| 718 | + NONE: str = 'none' |
| 719 | + SKIP: str = 'skip' |
| 720 | + DELETE: str = 'delete' |
| 721 | + |
| 722 | + |
| 723 | +class ForceArchiveRegistrationsView(NodeMixin, View): |
| 724 | + """Allows an authorized user to force archive registration. |
701 | 725 | """
|
702 |
| - template_name = 'nodes/remove_registrations_modal.html' |
703 | 726 | permission_required = ('osf.view_node', 'osf.change_node')
|
704 | 727 |
|
705 | 728 | def post(self, request, *args, **kwargs):
|
706 |
| - stuck_reg = self.get_object() |
707 |
| - if Registration.find_failed_registrations().filter(id=stuck_reg.id).exists(): |
708 |
| - stuck_reg.delete_registration_tree(save=True) |
709 |
| - messages.success(request, 'The registration has been deleted') |
| 729 | + # Prevents circular imports that cause admin app to hang at startup |
| 730 | + from osf.management.commands.force_archive import verify, archive, DEFAULT_PERMISSIBLE_ADDONS |
| 731 | + |
| 732 | + registration = self.get_object() |
| 733 | + force_archive_params = request.POST |
| 734 | + |
| 735 | + collision_mode = force_archive_params.get('collision_mode', CollisionMode.NONE.value) |
| 736 | + delete_collision = CollisionMode.DELETE.value == collision_mode |
| 737 | + skip_collision = CollisionMode.SKIP.value == collision_mode |
| 738 | + |
| 739 | + allow_unconfigured = force_archive_params.get('allow_unconfigured', False) |
| 740 | + |
| 741 | + addons = set(force_archive_params.getlist('addons', [])) |
| 742 | + addons.update(DEFAULT_PERMISSIBLE_ADDONS) |
| 743 | + |
| 744 | + try: |
| 745 | + verify(registration, permissible_addons=addons, raise_error=True) |
| 746 | + except ValidationError as exc: |
| 747 | + messages.error(request, str(exc)) |
| 748 | + return redirect(self.get_success_url()) |
| 749 | + |
| 750 | + dry_mode = force_archive_params.get('dry_mode', False) |
| 751 | + |
| 752 | + if dry_mode: |
| 753 | + messages.success(request, f"Registration {registration._id} can be archived.") |
710 | 754 | else:
|
711 |
| - messages.error(request, 'This registration may not technically be stuck,' |
712 |
| - ' if the problem persists get a developer to fix it.') |
| 755 | + try: |
| 756 | + archive( |
| 757 | + registration, |
| 758 | + permissible_addons=addons, |
| 759 | + allow_unconfigured=allow_unconfigured, |
| 760 | + skip_collision=skip_collision, |
| 761 | + delete_collision=delete_collision, |
| 762 | + ) |
| 763 | + messages.success(request, 'Registration archive process has finished.') |
| 764 | + except Exception as exc: |
| 765 | + messages.error(request, f'This registration cannot be archived due to {exc.__class__.__name__}: {str(exc)}. ' |
| 766 | + f'If the problem persists get a developer to fix it.') |
713 | 767 |
|
714 | 768 | return redirect(self.get_success_url())
|
715 | 769 |
|
|
0 commit comments