# Path: admin/nodes/views.py
# class NodeRemoveContributorView(NodeMixin, View):
#     """ Allows authorized users to remove contributors from nodes.
#     """
#     permission_required = ('osf.view_node', 'osf.change_node')
#     raise_exception = True
# 
#     def post(self, request, *args, **kwargs):
#         node = self.get_object()
#         user = OSFUser.objects.get(id=self.kwargs.get('user_id'))
#         if not node._get_admin_contributors_query(node._contributors.all()).exclude(user=user).exists():
#             messages.error(self.request, 'Must be at least one admin on this node.')
#             return redirect(self.get_success_url())
# 
#         if node.remove_contributor(user, None, log=False):
#             update_admin_log(
#                 user_id=self.request.user.id,
#                 object_id=node.pk,
#                 object_repr='Contributor',
#                 message=f'User {user.pk} removed from {node.__class__.__name__.lower()} {node.pk}.',
#                 action_flag=CONTRIBUTOR_REMOVED
#             )
#             # Log invisibly on the OSF.
#             self.add_contributor_removed_log(node, user)
#         return redirect(self.get_success_url())
# 
#     def add_contributor_removed_log(self, node, user):
#         NodeLog(
#             action=NodeLog.CONTRIB_REMOVED,
#             user=None,
#             params={
#                 'project': node.parent_id,
#                 'node': node.pk,
#                 'contributors': user.pk
#             },
#             date=timezone.now(),
#             should_hide=True,
#         ).save()
# 
# Path: osf/models/admin_log_entry.py
# def update_admin_log(user_id, object_id, object_repr, message, action_flag=UNKNOWN):
#     AdminLogEntry.objects.log_action(
#         user_id=user_id,
#         content_type_id=None,
#         object_id=object_id,
#         object_repr=object_repr,
#         change_message=message,
#         action_flag=action_flag
#     )
# 
# REINDEX_ELASTIC = 51
# 
# REINDEX_SHARE = 50
# 
# PREPRINT_REMOVED = 70
# 
# PREPRINT_RESTORED = 71
# 
# CONFIRM_SPAM = 20
# 
# CONFIRM_HAM = 21
# 
# APPROVE_WITHDRAWAL = 60
# 
# REJECT_WITHDRAWAL = 61
# 
# UNFLAG_SPAM = 22


from django.db.models import F
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import NoReverseMatch
from django.contrib import messages
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.shortcuts import redirect
from django.views.generic import (
    View,
    ListView,
    FormView,
)
from django.utils import timezone
from django.urls import reverse_lazy
from admin.base.views import GuidView
from admin.base.forms import GuidForm
from admin.nodes.views import NodeRemoveContributorView
from admin.preprints.forms import ChangeProviderForm
from api.share.utils import update_share
from osf.exceptions import PreprintStateError
from osf.models import (
    SpamStatus,
    Preprint,
    PreprintLog,
    PreprintRequest,
    PreprintProvider
)
from osf.models.admin_log_entry import (
    update_admin_log,
    REINDEX_ELASTIC,
    REINDEX_SHARE,
    PREPRINT_REMOVED,
    PREPRINT_RESTORED,
    CONFIRM_SPAM,
    CONFIRM_HAM,
    APPROVE_WITHDRAWAL,
    REJECT_WITHDRAWAL,
    UNFLAG_SPAM,
)
from website import search, settings
            'requests': query_set,
            'page': page,
        }

    def post(self, request, *args, **kwargs):
        data = dict(request.POST)
        action = data.pop('action')[0]
        data.pop('csrfmiddlewaretoken', None)
        request_ids = list(data.keys())
        withdrawal_requests = PreprintRequest.objects.filter(id__in=request_ids)

        if action == 'reject':
            for withdrawal_request in withdrawal_requests:
                withdrawal_request.run_reject(self.request.user, withdrawal_request.comment)
                update_admin_log(
                    user_id=self.request.user.id,
                    object_id=withdrawal_request.id,
                    object_repr='PreprintRequest',
                    message=f'Approved withdrawal request: {withdrawal_request.id} of preprint {withdrawal_request.target._id}',
                    action_flag=APPROVE_WITHDRAWAL
                )

        if action == 'approve':
            for withdrawal_request in withdrawal_requests:
                withdrawal_request.run_accept(self.request.user, withdrawal_request.comment)
                update_admin_log(
                    user_id=self.request.user.id,
                    object_id=withdrawal_request.id,
                    object_repr='PreprintRequest',
                    message=f'Rejected withdrawal request: {withdrawal_request.id} of preprint {withdrawal_request.target._id}',

# Path: admin/nodes/views.py
# class NodeRemoveContributorView(NodeMixin, View):
#     """ Allows authorized users to remove contributors from nodes.
#     """
#     permission_required = ('osf.view_node', 'osf.change_node')
#     raise_exception = True
# 
#     def post(self, request, *args, **kwargs):
#         node = self.get_object()
#         user = OSFUser.objects.get(id=self.kwargs.get('user_id'))
#         if not node._get_admin_contributors_query(node._contributors.all()).exclude(user=user).exists():
#             messages.error(self.request, 'Must be at least one admin on this node.')
#             return redirect(self.get_success_url())
# 
#         if node.remove_contributor(user, None, log=False):
#             update_admin_log(
#                 user_id=self.request.user.id,
#                 object_id=node.pk,
#                 object_repr='Contributor',
#                 message=f'User {user.pk} removed from {node.__class__.__name__.lower()} {node.pk}.',
#                 action_flag=CONTRIBUTOR_REMOVED
#             )
#             # Log invisibly on the OSF.
#             self.add_contributor_removed_log(node, user)
#         return redirect(self.get_success_url())
# 
#     def add_contributor_removed_log(self, node, user):
#         NodeLog(
#             action=NodeLog.CONTRIB_REMOVED,
#             user=None,
#             params={
#                 'project': node.parent_id,
#                 'node': node.pk,
#                 'contributors': user.pk
#             },
#             date=timezone.now(),
#             should_hide=True,
#         ).save()
# 
# Path: osf/models/admin_log_entry.py
# def update_admin_log(user_id, object_id, object_repr, message, action_flag=UNKNOWN):
#     AdminLogEntry.objects.log_action(
#         user_id=user_id,
#         content_type_id=None,
#         object_id=object_id,
#         object_repr=object_repr,
#         change_message=message,
#         action_flag=action_flag
#     )
# 
# REINDEX_ELASTIC = 51
# 
# REINDEX_SHARE = 50
# 
# PREPRINT_REMOVED = 70
# 
# PREPRINT_RESTORED = 71
# 
# CONFIRM_SPAM = 20
# 
# CONFIRM_HAM = 21
# 
# APPROVE_WITHDRAWAL = 60
# 
# REJECT_WITHDRAWAL = 61
# 
# UNFLAG_SPAM = 22


# Path: admin/preprints/views.py
from django.db.models import F
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import NoReverseMatch
from django.contrib import messages
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.shortcuts import redirect
from django.views.generic import (
    View,
    ListView,
    FormView,
)
from django.utils import timezone
from django.urls import reverse_lazy
from admin.base.views import GuidView
from admin.base.forms import GuidForm
from admin.nodes.views import NodeRemoveContributorView
from admin.preprints.forms import ChangeProviderForm
from api.share.utils import update_share
from osf.exceptions import PreprintStateError
from osf.models import (
    SpamStatus,
    Preprint,
    PreprintLog,
    PreprintRequest,
    PreprintProvider
)
from osf.models.admin_log_entry import (
    update_admin_log,
    REINDEX_ELASTIC,
    REINDEX_SHARE,
    PREPRINT_REMOVED,
    PREPRINT_RESTORED,
    CONFIRM_SPAM,
    CONFIRM_HAM,
    APPROVE_WITHDRAWAL,
    REJECT_WITHDRAWAL,
    UNFLAG_SPAM,
)
from website import search, settings

            'requests': query_set,
            'page': page,
        }

    def post(self, request, *args, **kwargs):
        data = dict(request.POST)
        action = data.pop('action')[0]
        data.pop('csrfmiddlewaretoken', None)
        request_ids = list(data.keys())
        withdrawal_requests = PreprintRequest.objects.filter(id__in=request_ids)

        if action == 'reject':
            for withdrawal_request in withdrawal_requests:
                withdrawal_request.run_reject(self.request.user, withdrawal_request.comment)
                update_admin_log(
                    user_id=self.request.user.id,
                    object_id=withdrawal_request.id,
                    object_repr='PreprintRequest',
                    message=f'Approved withdrawal request: {withdrawal_request.id} of preprint {withdrawal_request.target._id}',
                    action_flag=APPROVE_WITHDRAWAL
                )

        if action == 'approve':
            for withdrawal_request in withdrawal_requests:
                withdrawal_request.run_accept(self.request.user, withdrawal_request.comment)
                update_admin_log(
                    user_id=self.request.user.id,
                    object_id=withdrawal_request.id,
                    object_repr='PreprintRequest',
                    message=f'Rejected withdrawal request: {withdrawal_request.id} of preprint {withdrawal_request.target._id}',

                    action_flag=REJECT_WITHDRAWAL