Find all MTM relationships on related ContentItem's and ensure the
        published M2M relationships directed back to the draft (src)
        content items are maintained for the published (dst) page's content
        items.
769191
def clone_fluent_contentitems_m2m_relationships(self, dst_obj):
        
        if not hasattr(self, 'contentitem_set'):
            return
        # We must explicitly and reliably order both the src and dst content
        # items here to ensure that we are processing the same logical item for
        # the draft and published copies. The default `ContentItem` ordering of
        # ('placeholder', 'sort_order') is not sufficient because it relies on
        # the placeholder PK remaining static, whereas we clone placeholders to
        # the published copy and may sometimes clone them with PKs in a
        # different order.
        reliable_ordering = [
            # Group items by owning placeholder using slot name, not PK
            'placeholder__slot',
            # Order items correctly within the placeholder grouping
            'sort_order'
        ]
        for src_ci, dst_ci in zip(
            self.contentitem_set.order_by(*reliable_ordering),
            dst_obj.contentitem_set.order_by(*reliable_ordering)
        ):
            for field, __ in src_ci._meta.get_m2m_with_model():
                field_name = field.name
                src_m2m = getattr(src_ci, field_name)
                dst_m2m = getattr(dst_ci, field_name)

                # It is safe to just `add` here, rather than match src and
                # dst listing exactly (i.e. potentially delete or re-order
                # items) because the destination content items are
                # re-created on publish thus always have empty M2M rels.
                dst_m2m.add(*src_m2m.all())
176959
function manyToManyDescriptor(
    declaredFromModelName,
    declaredToModelName,
    throughModelName,
    throughFields,
    reverse
) {
    return {
        get() {
            const {
                session: {
                    [declaredFromModelName]: DeclaredFromModel,
                    [declaredToModelName]: DeclaredToModel,
                    [throughModelName]: ThroughModel,
                },
            } = this.getClass();

            const ThisModel = reverse
                ? DeclaredToModel
                : DeclaredFromModel;
            const OtherModel = reverse
                ? DeclaredFromModel
                : DeclaredToModel;

            const thisReferencingField = reverse
                ? throughFields.to
                : throughFields.from;
            const otherReferencingField = reverse
                ? throughFields.from
                : throughFields.to;

            const thisId = this.getId();

            const throughQs = ThroughModel.filter({
                [thisReferencingField]: thisId,
            });

            /**
             * all IDs of instances of the other model that are
             * referenced by any instance of the current model
             */
            const referencedOtherIds = new Set(
                throughQs
                    .toRefArray()
                    .map(obj => obj[otherReferencingField])
            );

            /**
             * selects all instances of other model that are referenced
             * by any instance of the current model
             */
            const qs = OtherModel.filter(otherModelInstance => (
                referencedOtherIds.has(
                    otherModelInstance[OtherModel.idAttribute]
                )
            ));

            /**
             * Allows adding OtherModel instances to be referenced by the current instance.
             *
             * E.g. Book.first().authors.add(1, 2) would add the authors with IDs 1 and 2
             * to the first book's list of referenced authors.
             *
             * @return undefined
             */
            qs.add = function add(...entities) {
                const idsToAdd = new Set(
                    entities.map(normalizeEntity)
                );

                const existingQs = throughQs.filter(through => (
                    idsToAdd.has(through[otherReferencingField])
                ));

                if (existingQs.exists()) {
                    const existingIds = existingQs
                        .toRefArray()
                        .map(through => through[otherReferencingField]);

                    throw new Error(`Tried to add already existing ${OtherModel.modelName} id(s) ${existingIds} to the ${ThisModel.modelName} instance with id ${thisId}`);
                }

                idsToAdd.forEach((id) => {
                    ThroughModel.create({
                        [otherReferencingField]: id,
                        [thisReferencingField]: thisId,
                    });
                });
            };

            /**
             * Removes references to all OtherModel instances from the current model.
             *
             * E.g. Book.first().authors.clear() would cause the first book's list
             * of referenced authors to become empty.
             *
             * @return undefined
             */
            qs.clear = function clear() {
                throughQs.delete();
            };

            /**
             * Removes references to all passed OtherModel instances from the current model.
             *
             * E.g. Book.first().authors.remove(1, 2) would cause the authors with
             * IDs 1 and 2 to no longer be referenced by the first book.
             *
             * @return undefined
             */
            qs.remove = function remove(...entities) {
                const idsToRemove = new Set(
                    entities.map(normalizeEntity)
                );

                const entitiesToDelete = throughQs.filter(
                    through => idsToRemove.has(through[otherReferencingField])
                );

                if (entitiesToDelete.count() !== idsToRemove.size) {
                    // Tried deleting non-existing entities.
                    const entitiesToDeleteIds = entitiesToDelete
                        .toRefArray()
                        .map(through => through[otherReferencingField]);

                    const unexistingIds = [...idsToRemove].filter(
                        id => !entitiesToDeleteIds.includes(id)
                    );

                    throw new Error(`Tried to delete non-existing ${OtherModel.modelName} id(s) ${unexistingIds} from the ${ThisModel.modelName} instance with id ${thisId}`);
                }

                entitiesToDelete.delete();
            };

            return qs;
        },

        set() {
            throw new Error('Tried setting a M2M field. Please use the related QuerySet methods add, remove and clear.');
        },
    };
}
121673
void populateRelationForM2M(Object entity, EntityMetadata entityMetadata, PersistenceDelegator delegator,
            Relation relation, Object relObject, Map<String, Object> relationsMap)
    {
        // For M-M relationship of Collection type, relationship entities are
        // always fetched from Join Table.
        if (relation.getPropertyType().isAssignableFrom(List.class)
                || relation.getPropertyType().isAssignableFrom(Set.class))
        {
            if (relation.isRelatedViaJoinTable() && (relObject == null || ProxyHelper.isProxyOrCollection(relObject)))
            {
                populateCollectionFromJoinTable(entity, entityMetadata, delegator, relation);
            }

        }
        else if (relation.getPropertyType().isAssignableFrom(Map.class))
        {
            if (relation.isRelatedViaJoinTable())
            {
                // TODO: Implement Map relationships via Join Table (not
                // supported as of now)
            }
            else
            {
                populateCollectionFromMap(entity, delegator, relation, relObject, relationsMap);
            }
        }
    }
301332
protected static void applyRelationships(final ContentSpec contentSpec, final Map<Integer, Node> nodes,
            final Map<String, SpecTopic> targetTopics, final List<CSNodeWrapper> relationshipFromNodes, final List<Process> processes,
            final DataProviderFactory providerFactory) {
        // Apply the user defined relationships stored in the database
        for (final CSNodeWrapper node : relationshipFromNodes) {
            boolean initialContentTopic = node.getNodeType() == CommonConstants.CS_NODE_INITIAL_CONTENT_TOPIC;
            boolean level = EntityUtilities.isNodeALevel(node);

            // In 1.3 or lower initial content relationships were stored on the topic, however in 1.4+ they are now on the initial
            // content level, so do the migration here
            final SpecNodeWithRelationships fromNode;
            if (initialContentTopic) {
                final CSNodeWrapper parentNode = node.getParent();
                final SpecNodeWithRelationships parent = (SpecNodeWithRelationships) nodes.get(parentNode.getId());
                if (parent instanceof InitialContent) {
                    fromNode = parent;
                } else {
                    final Level parentLevel = (Level) parent;
                    if (parentLevel.getFirstSpecNode() instanceof InitialContent) {
                        fromNode = (SpecNodeWithRelationships) parentLevel.getFirstSpecNode();
                    } else {
                        fromNode = new InitialContent(parent.getLineNumber(), CSConstants.LEVEL_INITIAL_CONTENT + ":");
                        if (parentLevel.getChildNodes().isEmpty()) {
                            parentLevel.appendChild(fromNode);
                        } else {
                            parentLevel.insertBefore(fromNode, parentLevel.getChildNodes().get(0));
                        }
                    }
                }
            } else {
                fromNode = (SpecNodeWithRelationships) nodes.get(node.getId());
            }

            // Check if we have any relationships to process
            if (node.getRelatedToNodes() == null || node.getRelatedToNodes().isEmpty()) continue;
            final List<CSRelatedNodeWrapper> relatedToNodes = node.getRelatedToNodes().getItems();

            // Sort the relationships into the correct order based on the sort variable
            Collections.sort(relatedToNodes, new CSRelatedNodeSorter());

            // Add the relationships to the topic
            for (final CSRelatedNodeWrapper relatedToNode : relatedToNodes) {
                final Node toNode = nodes.get(relatedToNode.getId());
                if (toNode == null) {
                    throw new IllegalStateException("The related node does not exist in the content specification");
                } else if (toNode instanceof Level) {
                    // Relationships to levels
                    final Level toLevel = (Level) toNode;

                    // Ensure that the level has a target id if not create one.
                    if (toLevel.getTargetId() == null) {
                        toLevel.setTargetId("T00" + relatedToNode.getId());
                    }

                    // Add the relationship
                    fromNode.addRelationshipToTarget(toLevel, RelationshipType.getRelationshipType(relatedToNode.getRelationshipType()),
                            initialContentTopic || level ? null : toLevel.getTitle());
                } else {
                    // Relationships to topics
                    final SpecTopic toSpecTopic = (SpecTopic) toNode;
                    final String title = toSpecTopic.getTitle();

                    // Add the relationship
                    if (relatedToNode.getRelationshipMode().equals(CommonConstants.CS_RELATIONSHIP_MODE_TARGET)) {
                        fromNode.addRelationshipToTarget(toSpecTopic,
                                RelationshipType.getRelationshipType(relatedToNode.getRelationshipType()),
                                initialContentTopic || level ? null : title);
                    } else {
                        fromNode.addRelationshipToTopic(toSpecTopic,
                                RelationshipType.getRelationshipType(relatedToNode.getRelationshipType()),
                                initialContentTopic || level ? null : title);
                    }
                }
            }
        }

        // Create the unique id map
        final Map<String, SpecTopic> uniqueIdSpecTopicMap = ContentSpecUtilities.getUniqueIdSpecTopicMap(contentSpec);

        // Apply the process relationships
        for (final Process process : processes) {
            process.processTopics(uniqueIdSpecTopicMap, targetTopics, providerFactory.getProvider(TopicProvider.class),
                    providerFactory.getProvider(ServerSettingsProvider.class));
        }
    }
54022
def get_content_models(self):
        
        models = []

        for model in self.concrete_model.get_content_models():
            try:
                admin_url(model, "add")
            except NoReverseMatch:
                continue
            else:
                setattr(model, "meta_verbose_name", model._meta.verbose_name)
                setattr(model, "add_url", admin_url(model, "add"))
                models.append(model)

        return models
121900
def get_related_coref_relationships(self, content_id, min_strength=None):
        '''
        '''
        related_labels = self.rel_label_store.get_related(
            content_id, min_strength=min_strength)
        related_cids = [rel_label.other(content_id)
                        for rel_label in related_labels]
        related_cid_to_idents = {}
        for cid in related_cids:
            conn_labels = self.label_store.directly_connected(cid)
            idents = [label.other(cid) for label in conn_labels]
            related_cid_to_idents[cid] = idents
        return related_cid_to_idents
743673
def mv_files(src, dst):
    
    # list the files in the src directory
    files = os.listdir(src)
    # loop for each file found
    for file in files:
        # move the file from the src to the dst
        shutil.move(os.path.join(src, file), os.path.join(dst, file))
    return
591481
def enrich_items(self, ocean_backend, events=False):
        

        headers = {"Content-Type": "application/json"}

        max_items = self.elastic.max_items_bulk
        current = 0
        total = 0
        bulk_json = ""

        total_signed_off = 0
        total_multi_author = 0

        url = self.elastic.index_url + '/items/_bulk'

        logger.debug("Adding items to %s (in %i packs)", self.elastic.anonymize_url(url), max_items)

        items = ocean_backend.fetch()

        for item in items:
            if self.pair_programming:
                # First we need to add the authors field to all commits
                # Check multi author
                m = self.AUTHOR_P2P_REGEX.match(item['data']['Author'])
                n = self.AUTHOR_P2P_NEW_REGEX.match(item['data']['Author'])
                if m or n:
                    logger.debug("Multiauthor detected. Creating one commit "
                                 "per author: %s", item['data']['Author'])
                    item['data']['authors'] = self.__get_authors(item['data']['Author'])
                    item['data']['Author'] = item['data']['authors'][0]
                m = self.AUTHOR_P2P_REGEX.match(item['data']['Commit'])
                n = self.AUTHOR_P2P_NEW_REGEX.match(item['data']['Author'])
                if m or n:
                    logger.debug("Multicommitter detected: using just the first committer")
                    item['data']['committers'] = self.__get_authors(item['data']['Commit'])
                    item['data']['Commit'] = item['data']['committers'][0]
                # Add the authors list using the original Author and the Signed-off list
                if 'Signed-off-by' in item['data']:
                    authors_all = item['data']['Signed-off-by'] + [item['data']['Author']]
                    item['data']['authors_signed_off'] = list(set(authors_all))

            if current >= max_items:
                try:
                    total += self.elastic.safe_put_bulk(url, bulk_json)
                    json_size = sys.getsizeof(bulk_json) / (1024 * 1024)
                    logger.debug("Added %i items to %s (%0.2f MB)", total, self.elastic.anonymize_url(url), json_size)
                except UnicodeEncodeError:
                    # Why is requests encoding the POST data as ascii?
                    logger.error("Unicode error in enriched items")
                    logger.debug(bulk_json)
                    safe_json = str(bulk_json.encode('ascii', 'ignore'), 'ascii')
                    total += self.elastic.safe_put_bulk(url, safe_json)
                bulk_json = ""
                current = 0

            rich_item = self.get_rich_item(item)
            data_json = json.dumps(rich_item)
            unique_field = self.get_field_unique_id()
            bulk_json += '{"index" : {"_id" : "%s" } }\n' % (rich_item[unique_field])
            bulk_json += data_json + "\n"  # Bulk document
            current += 1

            if self.pair_programming:
                # Multi author support
                if 'authors' in item['data']:
                    # First author already added in the above commit
                    authors = item['data']['authors']
                    for i in range(1, len(authors)):
                        # logger.debug('Adding a new commit for %s', authors[i])
                        item['data']['Author'] = authors[i]
                        item['data']['is_git_commit_multi_author'] = 1
                        rich_item = self.get_rich_item(item)
                        item['data']['is_git_commit_multi_author'] = 1
                        data_json = json.dumps(rich_item)
                        commit_id = item["uuid"] + "_" + str(i - 1)
                        rich_item['git_uuid'] = commit_id
                        bulk_json += '{"index" : {"_id" : "%s" } }\n' % rich_item['git_uuid']
                        bulk_json += data_json + "\n"  # Bulk document
                        current += 1
                        total_multi_author += 1

                if rich_item['Signed-off-by_number'] > 0:
                    nsg = 0
                    # Remove duplicates and the already added Author if exists
                    authors = list(set(item['data']['Signed-off-by']))
                    if item['data']['Author'] in authors:
                        authors.remove(item['data']['Author'])
                    for author in authors:
                        # logger.debug('Adding a new commit for %s', author)
                        # Change the Author in the original commit and generate
                        # a new enriched item with it
                        item['data']['Author'] = author
                        item['data']['is_git_commit_signed_off'] = 1
                        rich_item = self.get_rich_item(item)
                        commit_id = item["uuid"] + "_" + str(nsg)
                        rich_item['git_uuid'] = commit_id
                        data_json = json.dumps(rich_item)
                        bulk_json += '{"index" : {"_id" : "%s" } }\n' % rich_item['git_uuid']
                        bulk_json += data_json + "\n"  # Bulk document
                        current += 1
                        total_signed_off += 1
                        nsg += 1

        if current > 0:
            total += self.elastic.safe_put_bulk(url, bulk_json)

        if total == 0:
            # No items enriched, nothing to upload to ES
            return total

        if self.pair_programming:
            logger.info("Signed-off commits generated: %i", total_signed_off)
            logger.info("Multi author commits generated: %i", total_multi_author)

        return total
412530
private boolean isRelational(EntityMetadata m)
    {
        // if related via join table OR contains relations.
        return m.isRelationViaJoinTable() || (m.getRelationNames() != null && (!m.getRelationNames().isEmpty()));
    }
343735
def get_relationships(schema, model_field=False):
    
    relationships = [key for (key, value) in schema._declared_fields.items() if isinstance(value, Relationship)]

    if model_field is True:
        relationships = [get_model_field(schema, key) for key in relationships]

    return relationships
612987
public <T> List<Predicate> byExampleOnXToMany(ManagedType<T> mt, Root<T> mtPath, T mtValue, SearchParameters sp, CriteriaBuilder builder) {
        List<Predicate> predicates = newArrayList();
        for (PluralAttribute<? super T, ?, ?> pa : mt.getPluralAttributes()) {
            if (pa.getCollectionType() == PluralAttribute.CollectionType.LIST) {
                List<?> values = (List<?>) jpaUtil.getValue(mtValue, mt.getAttribute(pa.getName()));
                if (values != null && !values.isEmpty()) {
                    if (sp.getUseAndInXToMany()) {
                        if (values.size() > 3) {
                            log.warning("Please note that using AND restriction on an Many to Many relationship requires as many joins as values");
                        }
                        for (Object value : values) {
                            ListJoin<T, ?> join = mtPath.join(mt.getList(pa.getName()));
                            predicates.add(join.in(value));
                        }
                    } else {
                        ListJoin<T, ?> join = mtPath.join(mt.getList(pa.getName()));
                        predicates.add(join.in(values));
                    }
                }
            }
        }
        return predicates;
    }
1342417
def contribute_to_related_class(self, cls, related):
        
        super(VersionedManyToManyField, self). \
            contribute_to_related_class(cls, related)
        accessor_name = related.get_accessor_name()
        if accessor_name and hasattr(cls, accessor_name):
            descriptor = VersionedManyToManyDescriptor(related, accessor_name)
            setattr(cls, accessor_name, descriptor)
            if hasattr(cls._meta, 'many_to_many_related') and isinstance(
                    cls._meta.many_to_many_related, list):
                cls._meta.many_to_many_related.append(descriptor)
            else:
                cls._meta.many_to_many_related = [descriptor]
689188
def save_related(self, request, form, formsets, change):
        
        super(MenuItemAdmin, self).save_related(request, form, formsets, change)
        self.model.objects.rebuild()
361982
def published(self, for_user=UNSET, force_exchange=False):
        
        if for_user is not UNSET:
            return self.visible()

        queryset = super(PublishingUrlNodeQuerySet, self).published(
            for_user=for_user, force_exchange=force_exchange)

        # Exclude by publication date on the published version of items, *not*
        # the draft vesion, or we could get the wrong result.
        # Exclude fields of published copy of draft items, not draft itself...
        queryset = queryset.exclude(
            Q(publishing_is_draft=True) & Q(
                Q(publishing_linked__publication_date__gt=now())
                | Q(publishing_linked__publication_end_date__lte=now())))
        # ...and exclude fields directly on published items
        queryset = queryset.exclude(
            Q(publishing_is_draft=False) & Q(
                Q(publication_date__gt=now())
                | Q(publication_end_date__lte=now())))

        return queryset
874227
def get_m2m_widget(cls, field):
        
        return functools.partial(
            widgets.ManyToManyWidget,
            model=get_related_model(field))
395821
def lookups(self, request, model_admin):
        
        active_objects = self.model.published.all().annotate(
            count_entries_published=Count('entries')).order_by(
            '-count_entries_published', '-pk')
        for active_object in active_objects:
            yield (
                str(active_object.pk), ungettext_lazy(
                    '%(item)s (%(count)i entry)',
                    '%(item)s (%(count)i entries)',
                    active_object.count_entries_published) % {
                    'item': smart_text(active_object),
                    'count': active_object.count_entries_published})
1090486
def check_missing_references(client):
    """"""
    from renku.models.refs import LinkReference

    missing = [
        ref for ref in LinkReference.iter_items(client)
        if not ref.reference.exists()
    ]

    if not missing:
        return True

    click.secho(
        WARNING + 'There are missing references.'
        '\n  (use "git rm <name>" to clean them)\n\n\t' + '\n\t  '.join(
            click.style(str(ref.path), fg='yellow') + ' -> ' +
            click.style(str(ref.reference), fg='red') for ref in missing
        ) + '\n'
    )
    return False