# Path: invenio_communities/communities/records/api.py
# class Community(Record):
#     """Community API."""
# 
#     pid = PIDField('id', provider=CommunitiesIdProvider, create=False)
#     schema = ConstantField(
#         '$schema', 'local://communities/communities-v1.0.0.json')
# 
#     model_cls = models.CommunityMetadata
# 
#     index = IndexField(
#         "communities-communities-v1.0.0",
#         search_alias="communities"
#     )
# 
#     access = CommunityAccessField()
# 
#     bucket_id = ModelField(dump=False)
#     bucket = ModelField(dump=False)
#     files = FilesField(
#         store=False,
#         file_cls=CommunityFile,
#         # Don't delete, we'll manage in the service
#         delete=False,
#     )


from invenio_db import db
from invenio_records.api import Record
from invenio_communities.communities.records.api import Community
        ids = db.session.query(
            self._m2m_model_cls.community_id
        ).filter(
            self._m2m_model_cls.record_id == self._record_id
        ).all()

        # Set internal list
        self._communities_ids = set([str(x[0]) for x in ids])

        # Unset default if no longer available
        if self._default_id and self._default_id not in self._communities_ids:
            self._default_id = None

    def __len__(self):
        """Get number of communities"""
        return len(self._communities_ids)

    def __contains__(self, community_or_id):
        """Check record is in community."""
        id_ = self._to_id(community_or_id)
        return id_ in self._communities_ids

    def __iter__(self):
        """Iterate over a communities."""
        # Determine community ids not already cached.
        nocache_ids = self._communities_ids - set(
            self._communities_cache.keys())

        # Fetch and cache missing community records
        if nocache_ids:

# Path: invenio_communities/communities/records/api.py
# class Community(Record):
#     """Community API."""
# 
#     pid = PIDField('id', provider=CommunitiesIdProvider, create=False)
#     schema = ConstantField(
#         '$schema', 'local://communities/communities-v1.0.0.json')
# 
#     model_cls = models.CommunityMetadata
# 
#     index = IndexField(
#         "communities-communities-v1.0.0",
#         search_alias="communities"
#     )
# 
#     access = CommunityAccessField()
# 
#     bucket_id = ModelField(dump=False)
#     bucket = ModelField(dump=False)
#     files = FilesField(
#         store=False,
#         file_cls=CommunityFile,
#         # Don't delete, we'll manage in the service
#         delete=False,
#     )


# Path: invenio_communities/records/records/systemfields/communities/manager.py
from invenio_db import db
from invenio_records.api import Record
from invenio_communities.communities.records.api import Community

        ids = db.session.query(
            self._m2m_model_cls.community_id
        ).filter(
            self._m2m_model_cls.record_id == self._record_id
        ).all()

        # Set internal list
        self._communities_ids = set([str(x[0]) for x in ids])

        # Unset default if no longer available
        if self._default_id and self._default_id not in self._communities_ids:
            self._default_id = None

    def __len__(self):
        """Get number of communities"""
        return len(self._communities_ids)

    def __contains__(self, community_or_id):
        """Check record is in community."""
        id_ = self._to_id(community_or_id)
        return id_ in self._communities_ids

    def __iter__(self):
        """Iterate over a communities."""
        # Determine community ids not already cached.
        nocache_ids = self._communities_ids - set(
            self._communities_cache.keys())

        # Fetch and cache missing community records
        if nocache_ids:

            communities = Community.get_records(nocache_ids)