# Path: baobab/translate/models.py
# class Lang(models.Model):
# 
#     name = models.CharField(max_length=200, unique=True)
#     iso = models.CharField(max_length=5, unique=True)
# 
#     def __unicode__(self):
#         return self.name
# 
# class Event(BackOfficeEvent):
# 
#     class Meta:
#         proxy = True


from django.db.models import Q, F, Count
from django.contrib.admin import SimpleListFilter
from baobab.translate.models import Lang, Event
"""




class EventCompleteFilter(SimpleListFilter):
    title = 'Complete'
    parameter_name = 'complete'

    def lookups(self, request, model_admin):
        return (
            ('true', 'True'),
            ('false', 'False'),
        )

    def queryset(self, request, queryset):
        if not self.value():
            return queryset
        nb_lang = Lang.objects.count()
        queryset = queryset.annotate(sum=Count('eventdatas'))
        events = queryset.filter(sum=nb_lang)
        ids = []
        for event in events:
            annotate = {'sum': Count('eventlogdatas__lang')}
            nb_logdatas = event.eventlogs.annotate(**annotate).values('sum')
            if not all(map(lambda e: e['sum'] == nb_lang, nb_logdatas)):
                continue
            ids.append(event.id)
        if self.value() == 'true':
            return Event.objects.filter(id__in=ids)

# Path: baobab/translate/models.py
# class Lang(models.Model):
# 
#     name = models.CharField(max_length=200, unique=True)
#     iso = models.CharField(max_length=5, unique=True)
# 
#     def __unicode__(self):
#         return self.name
# 
# class Event(BackOfficeEvent):
# 
#     class Meta:
#         proxy = True


# Path: baobab/translate/adminfilter.py
from django.db.models import Q, F, Count
from django.contrib.admin import SimpleListFilter
from baobab.translate.models import Lang, Event

"""




class EventCompleteFilter(SimpleListFilter):
    title = 'Complete'
    parameter_name = 'complete'

    def lookups(self, request, model_admin):
        return (
            ('true', 'True'),
            ('false', 'False'),
        )

    def queryset(self, request, queryset):
        if not self.value():
            return queryset
        nb_lang = Lang.objects.count()
        queryset = queryset.annotate(sum=Count('eventdatas'))
        events = queryset.filter(sum=nb_lang)
        ids = []
        for event in events:
            annotate = {'sum': Count('eventlogdatas__lang')}
            nb_logdatas = event.eventlogs.annotate(**annotate).values('sum')
            if not all(map(lambda e: e['sum'] == nb_lang, nb_logdatas)):
                continue
            ids.append(event.id)
        if self.value() == 'true':
            return Event.objects.filter(id__in=ids)

        return Event.objects.exclude(id__in=ids)