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 load(cls, key): from website.models import Node try: node = Node.find_one(Q('institution_id', 'eq', key), allow_institution=True) return cls(node) except NoResultsFound: return None
def sanction_handler(kind, action, payload, encoded_token, auth, **kwargs): from website.models import ( Node, 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(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(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 = Node.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 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 load(cls, key): from website.models import Node try: node = Node.find_one(Q('institution_id', 'eq', key), allow_institution=True) return cls(node) except NoResultsFound: return None
def sanction_handler(kind, action, payload, encoded_token, auth, **kwargs): from website.models import (Node, 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 = Node.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 find_one(cls, query=None, **kwargs): from website.models import Node if query and getattr(query, 'nodes', False): for node in query.nodes: replacement_attr = cls.attribute_map.get(node.attribute, False) node.attribute = replacement_attr if replacement_attr else node.attribute elif isinstance(query, RawQuery): replacement_attr = cls.attribute_map.get(query.attribute, False) query.attribute = replacement_attr if replacement_attr else query.attribute query = query & Q('institution_id', 'ne', None) if query else Q('institution_id', 'ne', None) node = Node.find_one(query, allow_institution=True, **kwargs) return cls(node)
def find_one(cls, query=None, deleted=False, **kwargs): from website.models import Node if query and getattr(query, 'nodes', False): for node in query.nodes: if node._Q__key in cls.attribute_map: node._Q__key = cls.attribute_map[node._Q__key] elif isinstance(query, RawQuery) and query._Q__key in cls.attribute_map: query._Q__key = cls.attribute_map[query._Q__key] query = query & Q('institution_id', 'ne', None) if query else Q('institution_id', 'ne', None) query = query & Q('is_deleted', 'ne', True) if not deleted else query node = Node.find_one(query, allow_institution=True, **kwargs) return cls(node)
def find_one(cls, query=None, **kwargs): from website.models import Node if query and getattr(query, 'nodes', False): for node in query.nodes: replacement_attr = cls.attribute_map.get(node.attribute, False) node.attribute = replacement_attr if replacement_attr else node.attribute elif isinstance(query, RawQuery): replacement_attr = cls.attribute_map.get(query.attribute, False) query.attribute = replacement_attr if replacement_attr else query.attribute query = query & Q('institution_id', 'ne', None) if query else Q( 'institution_id', 'ne', None) node = Node.find_one(query, allow_institution=True, **kwargs) return cls(node)
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('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('contributors', 'eq', user._id) ) 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 website.models import Node, RegistrationApproval, Embargo, Retraction Model = {"registration": RegistrationApproval, "embargo": Embargo, "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(Model.DISPLAY_NAME) elif sanction.is_approved: err_code = http.BAD_REQUEST if kind in ["registration", "embargo"] else http.GONE err_message = "This registration is not pending {0}.".format(sanction.DISPLAY_NAME) 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(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 = Node.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, "retraction": retraction_handler, }[kind](action, registration, registered_from)
def find_bookmark_collection(user): return Node.find_one( Q('is_bookmark_collection', 'eq', True) & Q('contributors', 'eq', user._id) & Q('is_deleted', 'eq', False))