def main(dry_run=True): init_app(routes=False) from osf.models import AbstractNode from website.project.utils import activity popular_activity = activity() popular_nodes = popular_activity['popular_public_projects'] popular_links_node = AbstractNode.find_one(Q('_id', 'eq', POPULAR_LINKS_NODE)) popular_registrations = popular_activity['popular_public_registrations'] popular_links_registrations = AbstractNode.find_one(Q('_id', 'eq', POPULAR_LINKS_REGISTRATIONS)) update_node_links(popular_links_node, popular_nodes, 'popular') update_node_links(popular_links_registrations, popular_registrations, 'popular registrations') try: popular_links_node.save() logger.info('Node links on {} updated.'.format(popular_links_node._id)) except (KeyError, RuntimeError) as error: logger.error('Could not migrate popular nodes due to error') logger.exception(error) try: popular_links_registrations.save() logger.info('Node links for registrations on {} updated.'.format(popular_links_registrations._id)) except (KeyError, RuntimeError) as error: logger.error('Could not migrate popular nodes for registrations due to error') logger.exception(error) if dry_run: raise RuntimeError('Dry run -- transaction rolled back.')
def setUp(self, *args, **kwargs): OsfTestCase.setUp(self, *args, **kwargs) if not self.kind: return self.sanction = self.Factory() self.reg = AbstractNode.find_one(Q(self.Model.SHORT_NAME, 'eq', self.sanction)) self.user = self.reg.creator
def setUp(self, *args, **kwargs): OsfTestCase.setUp(self, *args, **kwargs) if not self.kind: return self.sanction = self.Factory() self.reg = Node.find_one(Q(self.Model.SHORT_NAME, 'eq', self.sanction)) self.user = self.reg.creator
def sanction_handler(kind, action, payload, encoded_token, auth, **kwargs): from osf.models import (AbstractNode, Embargo, EmbargoTerminationApproval, RegistrationApproval, Retraction) Model = { 'registration': RegistrationApproval, 'embargo': Embargo, 'embargo_termination_approval': EmbargoTerminationApproval, 'retraction': Retraction }.get(kind, None) if not Model: raise UnsupportedSanctionHandlerKind sanction_id = payload.get('sanction_id', None) sanction = Model.load(sanction_id) err_code = None err_message = None if not sanction: err_code = http.BAD_REQUEST err_message = 'There is no {0} associated with this token.'.format( markupsafe.escape(Model.DISPLAY_NAME)) elif sanction.is_approved: # Simply strip query params and redirect if already approved return redirect(request.base_url) elif sanction.is_rejected: err_code = http.GONE if kind in ['registration', 'embargo' ] else http.BAD_REQUEST err_message = 'This registration {0} has been rejected.'.format( markupsafe.escape(sanction.DISPLAY_NAME)) if err_code: raise HTTPError(err_code, data=dict(message_long=err_message)) do_action = getattr(sanction, action, None) if do_action: registration = AbstractNode.find_one( Q(sanction.SHORT_NAME, 'eq', sanction)) registered_from = registration.registered_from try: do_action(auth.user, encoded_token) except TokenError as e: raise HTTPError(http.BAD_REQUEST, data={ 'message_short': e.message_short, 'message_long': e.message_long }) except PermissionsError as e: raise HTTPError(http.UNAUTHORIZED, data={ 'message_short': 'Unauthorized access', 'message_long': e.message }) sanction.save() return { 'registration': registration_approval_handler, 'embargo': embargo_handler, 'embargo_termination_approval': embargo_termination_handler, 'retraction': retraction_handler, }[kind](action, registration, registered_from)
def get_or_create_node(title, user): """Get or create node by title and creating user. :param str title: Node title :param User user: User creating node :return: Tuple of (node, created) """ try: node = AbstractNode.find_one( Q('title', 'iexact', title) & Q('is_deleted', 'ne', True) & Q('contributors', 'eq', user)) return node, False except ModularOdmException: node = new_node('project', title, user) return node, True
def get_or_create_node(title, user): """Get or create node by title and creating user. :param str title: Node title :param User user: User creating node :return: Tuple of (node, created) """ try: node = Node.find_one( Q('title', 'iexact', title) & Q('is_deleted', 'ne', True) & Q('contributors', 'eq', user) ) return node, False except ModularOdmException: node = new_node('project', title, user) return node, True
def sanction_handler(kind, action, payload, encoded_token, auth, **kwargs): from osf.models import ( AbstractNode, Embargo, EmbargoTerminationApproval, RegistrationApproval, Retraction ) Model = { 'registration': RegistrationApproval, 'embargo': Embargo, 'embargo_termination_approval': EmbargoTerminationApproval, 'retraction': Retraction }.get(kind, None) if not Model: raise UnsupportedSanctionHandlerKind sanction_id = payload.get('sanction_id', None) sanction = Model.load(sanction_id) err_code = None err_message = None if not sanction: err_code = http.BAD_REQUEST err_message = 'There is no {0} associated with this token.'.format( markupsafe.escape(Model.DISPLAY_NAME)) elif sanction.is_approved: # Simply strip query params and redirect if already approved return redirect(request.base_url) elif sanction.is_rejected: err_code = http.GONE if kind in ['registration', 'embargo'] else http.BAD_REQUEST err_message = 'This registration {0} has been rejected.'.format( markupsafe.escape(sanction.DISPLAY_NAME)) if err_code: raise HTTPError(err_code, data=dict( message_long=err_message )) do_action = getattr(sanction, action, None) if do_action: registration = AbstractNode.find_one(Q(sanction.SHORT_NAME, 'eq', sanction)) registered_from = registration.registered_from try: do_action(auth.user, encoded_token) except TokenError as e: raise HTTPError(http.BAD_REQUEST, data={ 'message_short': e.message_short, 'message_long': e.message_long }) except PermissionsError as e: raise HTTPError(http.UNAUTHORIZED, data={ 'message_short': 'Unauthorized access', 'message_long': e.message }) sanction.save() return { 'registration': registration_approval_handler, 'embargo': embargo_handler, 'embargo_termination_approval': embargo_termination_handler, 'retraction': retraction_handler, }[kind](action, registration, registered_from)