from __future__ import absolute_import, unicode_literals

from elephantblog.models import Category, Entry


def entry_list_lookup_related(entry_qs):
    entry_dict = dict((e.pk, e) for e in entry_qs)

    if hasattr(Entry, 'richtextcontent_set'):
        for content in Entry.richtextcontent_set.related.model.objects.filter(
                parent__in=entry_dict.keys()).reverse():
            entry_dict[content.parent_id].first_richtext = content

    if hasattr(Entry, 'mediafilecontent_set'):
        for content in Entry.mediafilecontent_set.related.model.objects.filter(
                parent__in=entry_dict.keys(),
                mediafile__type='image').reverse().select_related('mediafile'):
            entry_dict[content.parent_id].first_image = content

    m2mfield = Entry._meta.get_field('categories')
    categories = Category.objects.filter(
        blogentries__in=entry_dict.keys(),
    ).extra(
        select={
            'entry_id': '%s.%s' % (
                m2mfield.m2m_db_table(), m2mfield.m2m_column_name()),
        },
    )

    for category in categories:
        entry = entry_dict[category.entry_id]
        if not hasattr(entry, 'fetched_categories'):
            entry.fetched_categories = []
        entry.fetched_categories.append(category)


def same_category_entries(entry):
    """ @return: all entries that have at least one category in common """
    return Entry.objects.active().filter(
        categories__in=entry.categories.all(),
    ).exclude(pk=entry.pk).distinct()

from __future__ import absolute_import, unicode_literals

from elephantblog.models import Category, Entry

from feincms.content.richtext.models import RichTextContent
from feincms.content.medialibrary.models import MediaFileContent


def entry_list_lookup_related(entry_qs):
    entry_dict = dict((e.pk, e) for e in entry_qs)

    model = Entry.content_type_for(RichTextContent)
    if model:
        for content in model.objects.filter(
                parent__in=entry_dict.keys(),
        ).reverse():
            entry_dict[content.parent_id].first_richtext = content

    model = Entry.content_type_for(MediaFileContent)
    if model:
        for content in model.objects.filter(
                parent__in=entry_dict.keys(),
                mediafile__type='image',
        ).reverse().select_related('mediafile'):
            entry_dict[content.parent_id].first_image = content

    m2mfield = Entry._meta.get_field('categories')
    categories = Category.objects.filter(
        blogentries__in=entry_dict.keys(),
    ).extra(
        select={
            'entry_id': '%s.%s' % (
                m2mfield.m2m_db_table(), m2mfield.m2m_column_name()),
        },
    )

    for category in categories:
        entry = entry_dict[category.entry_id]
        if not hasattr(entry, 'fetched_categories'):
            entry.fetched_categories = []
        entry.fetched_categories.append(category)


def same_category_entries(entry):
    """ @return: all entries that have at least one category in common """
    return Entry.objects.active().filter(
        categories__in=entry.categories.all(),
    ).exclude(pk=entry.pk).distinct()

Fix entry_list_lookup_related with Django 1.8