def top_level_subjects(self): if self.subjects.exists(): return self.subjects.filter(parent__isnull=True) else: # TODO: Delet this when all PreprintProviders have a mapping from osf.models.subject import Subject if len(self.subjects_acceptable) == 0: return Subject.find(Q('parent', 'isnull', True)) tops = set([sub[0][0] for sub in self.subjects_acceptable]) return [Subject.load(sub) for sub in tops]
def all_subjects(self): q = [] for rule in self.subjects_acceptable: if rule[1]: q.append(Q('parents', 'eq', Subject.load(rule[0][-1]))) if len(rule[0]) == 1: potential_parents = Subject.find(Q('parents', 'eq', Subject.load(rule[0][-1]))) for parent in potential_parents: q.append(Q('parents', 'eq', parent)) for sub in rule[0]: q.append(Q('_id', 'eq', sub)) return Subject.find(reduce(lambda x, y: x | y, q)) if len(q) > 1 else (Subject.find(q[0]) if len(q) else Subject.find())
def rules_to_subjects(rules): if not rules: return Subject.objects.filter(provider___id='osf') q = [] for rule in rules: if rule[1]: q.append(Q('parent', 'eq', Subject.load(rule[0][-1]))) if len(rule[0]) == 1: potential_parents = Subject.find(Q('parent', 'eq', Subject.load(rule[0][-1]))) for parent in potential_parents: q.append(Q('parent', 'eq', parent)) for sub in rule[0]: q.append(Q('_id', 'eq', sub)) return Subject.find(reduce(lambda x, y: x | y, q)) if len(q) > 1 else (Subject.find(q[0]) if len(q) else Subject.find())
def rules_to_subjects(rules): if not rules: return Subject.objects.filter(provider___id='osf') q = [] for rule in rules: if rule[1]: q.append(Q('parent', 'eq', Subject.load(rule[0][-1]))) if len(rule[0]) == 1: potential_parents = Subject.find(Q('parent', 'eq', Subject.load(rule[0][-1]))) for parent in potential_parents: q.append(Q('parent', 'eq', parent)) for sub in rule[0]: q.append(Q('_id', 'eq', sub)) return Subject.find(reduce(lambda x, y: x | y, q)) if len(q) > 1 else (Subject.find(q[0]) if len(q) else Subject.find())
def all_subjects(self): if self.subjects.exists(): return self.subjects.all() else: # TODO: Delet this when all PreprintProviders have a mapping from osf.models.subject import Subject q = [] for rule in self.subjects_acceptable: if rule[1]: q.append(Q('parent', 'eq', Subject.load(rule[0][-1]))) if len(rule[0]) == 1: potential_parents = Subject.find(Q('parent', 'eq', Subject.load(rule[0][-1]))) for parent in potential_parents: q.append(Q('parent', 'eq', parent)) for sub in rule[0]: q.append(Q('_id', 'eq', sub)) return Subject.find(reduce(lambda x, y: x | y, q)) if len(q) > 1 else (Subject.find(q[0]) if len(q) else Subject.find())
def top_level_subjects(self): if self.subjects.exists(): return self.subjects.filter(parent__isnull=True) else: # TODO: Delet this when all PreprintProviders have a mapping if len(self.subjects_acceptable) == 0: return Subject.objects.filter(parent__isnull=True, provider___id='osf') tops = set([sub[0][0] for sub in self.subjects_acceptable]) return [Subject.load(sub) for sub in tops]
def top_level_subjects(self): if self.subjects.exists(): return self.subjects.filter(parent__isnull=True) else: # TODO: Delet this when all PreprintProviders have a mapping if len(self.subjects_acceptable) == 0: return Subject.objects.filter(parent__isnull=True, provider___id='osf') tops = set([sub[0][0] for sub in self.subjects_acceptable]) return [Subject.load(sub) for sub in tops]
def get_subjects(self): ret = [] for subj_list in self.subjects: subj_hierarchy = [] for subj_id in subj_list: subj = Subject.load(subj_id) if subj: subj_hierarchy += ({'id': subj_id, 'text': subj.text}, ) if subj_hierarchy: ret.append(subj_hierarchy) return ret
def set_subjects(self, new_subjects, auth, add_log=True): """ Helper for setting M2M subjects field from list of hierarchies received from UI. Only authorized admins may set subjects. :param list[list[Subject._id]] new_subjects: List of subject hierarchies to be validated and flattened :param Auth auth: Auth object for requesting user :param bool add_log: Whether or not to add a log (if called on a Loggable object) :return: None """ AbstractNode = apps.get_model('osf.AbstractNode') PreprintService = apps.get_model('osf.PreprintService') CollectionSubmission = apps.get_model('osf.CollectionSubmission') if getattr(self, 'is_registration', False): raise PermissionsError('Registrations may not be modified.') if isinstance(self, (AbstractNode, PreprintService)): if not self.has_permission(auth.user, ADMIN): raise PermissionsError('Only admins can change subjects.') elif isinstance(self, CollectionSubmission): if not self.guid.referent.has_permission( auth.user, ADMIN) and not auth.user.has_perms( self.collection.groups[ADMIN], self.collection): raise PermissionsError('Only admins can change subjects.') old_subjects = list(self.subjects.values_list('id', flat=True)) self.subjects.clear() for subj_list in new_subjects: subj_hierarchy = [] for s in subj_list: subj_hierarchy.append(s) if subj_hierarchy: validate_subject_hierarchy(subj_hierarchy) for s_id in subj_hierarchy: self.subjects.add(Subject.load(s_id)) if add_log and hasattr(self, 'add_log'): self.add_log( action=NodeLog.SUBJECTS_UPDATED, params={ 'subjects': list(self.subjects.values('_id', 'text')), 'old_subjects': list( Subject.objects.filter(id__in=old_subjects).values( '_id', 'text')) }, auth=auth, save=False, ) self.save(old_subjects=old_subjects)
def rules_to_subjects(rules): if not rules: return Subject.objects.filter(provider___id='osf') q = [] for rule in rules: parent_from_rule = Subject.load(rule[0][-1]) if rule[1]: q.append(models.Q(parent=parent_from_rule)) if len(rule[0]) == 1: potential_parents = Subject.objects.filter(parent=parent_from_rule) for parent in potential_parents: q.append(models.Q(parent=parent)) for sub in rule[0]: q.append(models.Q(_id=sub)) return Subject.objects.filter(reduce(lambda x, y: x | y, q)) if len(q) > 1 else (Subject.objects.filter(q[0]) if len(q) else Subject.objects.all())
def rules_to_subjects(rules): if not rules: return Subject.objects.filter(provider___id='osf') q = [] for rule in rules: parent_from_rule = Subject.load(rule[0][-1]) if rule[1]: q.append(models.Q(parent=parent_from_rule)) if len(rule[0]) == 1: potential_parents = Subject.objects.filter(parent=parent_from_rule) for parent in potential_parents: q.append(models.Q(parent=parent)) for sub in rule[0]: q.append(models.Q(_id=sub)) return Subject.objects.filter(reduce(lambda x, y: x | y, q)) if len(q) > 1 else (Subject.objects.filter(q[0]) if len(q) else Subject.objects.all())
def set_subjects(self, preprint_subjects, auth, save=False): if not self.node.has_permission(auth.user, ADMIN): raise PermissionsError('Only admins can change a preprint\'s subjects.') self.subjects.clear() for subj_list in preprint_subjects: subj_hierarchy = [] for s in subj_list: subj_hierarchy.append(s) if subj_hierarchy: validate_subject_hierarchy(subj_hierarchy) for s_id in subj_hierarchy: self.subjects.add(Subject.load(s_id)) if save: self.save()
def set_subjects(self, new_subjects, auth, add_log=True): """ Helper for setting M2M subjects field from list of hierarchies received from UI. Only authorized admins may set subjects. :param list[list[Subject._id]] new_subjects: List of subject hierarchies to be validated and flattened :param Auth auth: Auth object for requesting user :param bool add_log: Whether or not to add a log (if called on a Loggable object) :return: None """ AbstractNode = apps.get_model('osf.AbstractNode') PreprintService = apps.get_model('osf.PreprintService') CollectedGuidMetadata = apps.get_model('osf.CollectedGuidMetadata') if getattr(self, 'is_registration', False): raise PermissionsError('Registrations may not be modified.') if isinstance(self, (AbstractNode, PreprintService)): if not self.has_permission(auth.user, ADMIN): raise PermissionsError('Only admins can change subjects.') elif isinstance(self, CollectedGuidMetadata): if not self.guid.referent.has_permission(auth.user, ADMIN) and not auth.user.has_perms(self.collection.groups[ADMIN], self.collection): raise PermissionsError('Only admins can change subjects.') old_subjects = list(self.subjects.values_list('id', flat=True)) self.subjects.clear() for subj_list in new_subjects: subj_hierarchy = [] for s in subj_list: subj_hierarchy.append(s) if subj_hierarchy: validate_subject_hierarchy(subj_hierarchy) for s_id in subj_hierarchy: self.subjects.add(Subject.load(s_id)) if add_log and hasattr(self, 'add_log'): self.add_log( action=NodeLog.SUBJECTS_UPDATED, params={ 'subjects': list(self.subjects.values('_id', 'text')), 'old_subjects': list(Subject.objects.filter(id__in=old_subjects).values('_id', 'text')) }, auth=auth, save=False, ) self.save(old_subjects=old_subjects)
def top_level_subjects(self): if len(self.subjects_acceptable) == 0: return Subject.find(Q('parents', 'isnull', True)) tops = set([sub[0][0] for sub in self.subjects_acceptable]) return [Subject.load(sub) for sub in tops]