# Path: baobab/utils/admin.py
# class ModelAdmin(DjangoModelAdmin):
# 
#     """
#     sub classes need to define a dict `list_display_dynamic` to have dynamic
#     columns with True/False value
#      - val_name key is a list of tuple:
#       - first elem will be used as the conditional value
#       - second elem will be used as the columns name
#      - filter: a string to define on which filed the obj should be filtered
#      - sub_obj: a string to define a sub object
#     """
# 
#     def __init__(self, *args, **kargs):
#         cls = self.__class__
#         if hasattr(cls, 'list_display_dynamic'):
#             val_name = cls.list_display_dynamic['val_name']
#             filter_ = cls.list_display_dynamic['filter']
#             if cls.list_display_dynamic['sub_obj']:
#                 sub_objs = cls.list_display_dynamic['sub_obj'].split('.')
#             else:
#                 sub_objs = []
#             for val, name in val_name:
# 
#                 def _columns(self, obj, val=val):
#                     for sub_obj in sub_objs:
#                         obj = getattr(obj, sub_obj)
#                     if obj.filter(**{filter_: val}).exists():
#                         return '<b>True</b>'
#                     return 'False'
#                 _columns.short_description = name
#                 _columns.allow_tags = True
# 
#                 setattr(cls, name, _columns)
#                 cls.list_display.append(name)
#         super(ModelAdmin, self).__init__(*args, **kargs)
# 
#     def get_form(self, request, *args, **kwargs):
#         form = super(ModelAdmin, self).get_form(request, *args, **kwargs)
#         form.request = request
#         return form
# 
#     def get_actions(self, request):
#         actions = super(ModelAdmin, self).get_actions(request)
#         if not self.has_delete_permission(request):
#             if 'delete_selected' in actions:
#                 del actions['delete_selected']
#         return actions
# 
# 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
# 
# class EventLog(BackOfficeEventLog):
# 
#     class Meta:
#         proxy = True
# 
# Path: baobab/translate/admininline.py
# class EventDataInline(TabularInline):
#     form = EventDataForm
#     model = models.EventData
#     fields = ['lang', 'title', 'summary']
#     extra = models.Lang.objects.count()
#     max_num = extra
# 
#     # XXX for safety
#     def has_delete_permission(self, request, obj=None):
#         return False
# 
# class EventLogDataInline(TabularInline):
#     form = EventLogDataForm
#     model = models.EventLogData
#     fields = ['lang', 'comment']
#     extra = models.Lang.objects.count()
#     max_num = extra
# 
#     # XXX for safety
#     def has_delete_permission(self, request, obj=None):
#         return False
# 
# Path: baobab/translate/adminfilter.py
# 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)
# 
# class EventLogCompleteFilter(SimpleListFilter):
#     title = 'CompleteLog'
#     parameter_name = 'completelog'
# 
#     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()
#         annotate = {'sum': Count('eventlogdatas__lang')}
#         queryset = queryset.annotate(**annotate)
#         if self.value() == 'true':
#             return queryset.filter(sum=nb_lang)
#         return queryset.filter(sum__lt=nb_lang)


from django.contrib import admin
from baobab.utils.admin import ModelAdmin
from baobab.translate.models import Lang, Event, EventLog
from baobab.translate.admininline import EventDataInline, EventLogDataInline
from baobab.translate.adminfilter import (EventCompleteFilter,
                                          EventLogCompleteFilter)
    def title_en(self, obj):
        return obj.eventdatas.get(lang__iso='en').title
    title_en.short_description = 'Title'

    def complete_event(self, obj):
        if obj.eventdatas.count() == Lang.objects.count():
            return '<b>True<b>'
        return 'False'
    complete_event.allow_tags = True

    def complete_log(self, obj):
        for eventlog in obj.eventlogs.all():
            if eventlog.eventlogdatas.count() != Lang.objects.count():
                return 'False'
        return '<b>True<b>'
    complete_log.allow_tags = True

    # XXX for safety
    def has_delete_permission(self, request, obj=None):
        return False

    def has_add_permission(self, request, obj=None):
        return False

    def has_change_permission(self, request, obj=None):
        if request.user.groups.filter(name='translate').exists():
            return True
        return super(EventAdmin, self).has_change_permission(request, obj=obj)



# Path: baobab/utils/admin.py
# class ModelAdmin(DjangoModelAdmin):
# 
#     """
#     sub classes need to define a dict `list_display_dynamic` to have dynamic
#     columns with True/False value
#      - val_name key is a list of tuple:
#       - first elem will be used as the conditional value
#       - second elem will be used as the columns name
#      - filter: a string to define on which filed the obj should be filtered
#      - sub_obj: a string to define a sub object
#     """
# 
#     def __init__(self, *args, **kargs):
#         cls = self.__class__
#         if hasattr(cls, 'list_display_dynamic'):
#             val_name = cls.list_display_dynamic['val_name']
#             filter_ = cls.list_display_dynamic['filter']
#             if cls.list_display_dynamic['sub_obj']:
#                 sub_objs = cls.list_display_dynamic['sub_obj'].split('.')
#             else:
#                 sub_objs = []
#             for val, name in val_name:
# 
#                 def _columns(self, obj, val=val):
#                     for sub_obj in sub_objs:
#                         obj = getattr(obj, sub_obj)
#                     if obj.filter(**{filter_: val}).exists():
#                         return '<b>True</b>'
#                     return 'False'
#                 _columns.short_description = name
#                 _columns.allow_tags = True
# 
#                 setattr(cls, name, _columns)
#                 cls.list_display.append(name)
#         super(ModelAdmin, self).__init__(*args, **kargs)
# 
#     def get_form(self, request, *args, **kwargs):
#         form = super(ModelAdmin, self).get_form(request, *args, **kwargs)
#         form.request = request
#         return form
# 
#     def get_actions(self, request):
#         actions = super(ModelAdmin, self).get_actions(request)
#         if not self.has_delete_permission(request):
#             if 'delete_selected' in actions:
#                 del actions['delete_selected']
#         return actions
# 
# 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
# 
# class EventLog(BackOfficeEventLog):
# 
#     class Meta:
#         proxy = True
# 
# Path: baobab/translate/admininline.py
# class EventDataInline(TabularInline):
#     form = EventDataForm
#     model = models.EventData
#     fields = ['lang', 'title', 'summary']
#     extra = models.Lang.objects.count()
#     max_num = extra
# 
#     # XXX for safety
#     def has_delete_permission(self, request, obj=None):
#         return False
# 
# class EventLogDataInline(TabularInline):
#     form = EventLogDataForm
#     model = models.EventLogData
#     fields = ['lang', 'comment']
#     extra = models.Lang.objects.count()
#     max_num = extra
# 
#     # XXX for safety
#     def has_delete_permission(self, request, obj=None):
#         return False
# 
# Path: baobab/translate/adminfilter.py
# 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)
# 
# class EventLogCompleteFilter(SimpleListFilter):
#     title = 'CompleteLog'
#     parameter_name = 'completelog'
# 
#     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()
#         annotate = {'sum': Count('eventlogdatas__lang')}
#         queryset = queryset.annotate(**annotate)
#         if self.value() == 'true':
#             return queryset.filter(sum=nb_lang)
#         return queryset.filter(sum__lt=nb_lang)


# Path: baobab/translate/admin.py
from django.contrib import admin
from baobab.utils.admin import ModelAdmin
from baobab.translate.models import Lang, Event, EventLog
from baobab.translate.admininline import EventDataInline, EventLogDataInline
from baobab.translate.adminfilter import (EventCompleteFilter,
                                          EventLogCompleteFilter)

    def title_en(self, obj):
        return obj.eventdatas.get(lang__iso='en').title
    title_en.short_description = 'Title'

    def complete_event(self, obj):
        if obj.eventdatas.count() == Lang.objects.count():
            return '<b>True<b>'
        return 'False'
    complete_event.allow_tags = True

    def complete_log(self, obj):
        for eventlog in obj.eventlogs.all():
            if eventlog.eventlogdatas.count() != Lang.objects.count():
                return 'False'
        return '<b>True<b>'
    complete_log.allow_tags = True

    # XXX for safety
    def has_delete_permission(self, request, obj=None):
        return False

    def has_add_permission(self, request, obj=None):
        return False

    def has_change_permission(self, request, obj=None):
        if request.user.groups.filter(name='translate').exists():
            return True
        return super(EventAdmin, self).has_change_permission(request, obj=obj)



class EventLogAdmin(ModelAdmin):