# Path: event_store/models.py
# class Organization(models.Model):
#     title = models.CharField(max_length=765)
#     slug = models.SlugField(max_length=128, db_index=True)
#     url = models.URLField(blank=True)
# 
#     facebook = models.CharField(max_length=128, null=True, blank=True)
#     twitter = models.CharField(max_length=128,
#                                null=True, blank=True,
#                                help_text="do not include @")
# 
#     logo_thumb = models.URLField(blank=True)
#     logo_big = models.URLField(blank=True)
#     privacy_policy = models.URLField(blank=True)
#     terms_and_conditions = models.URLField(blank=True)
# 
#     #default source id
#     osdi_source_id = models.CharField(max_length=128)
# 
#     group = models.ForeignKey(Group, db_index=True)
#     # for getting other folks' data; auto/re-generatable
#     api_key = models.CharField(max_length=765, editable=False)
# 
#     def __str__(self):
#         return self.title
# 
# EVENT_REVIEW_CHOICES = ((None, 'New'),
#                         ('reviewed', 'Claimed'),
#                         ('vetted', 'Vetted'),
#                         ('questionable', 'Questionable'),
#                         ('limbo', 'Limbo'))
# 
# EVENT_PREP_CHOICES = (('', 'Unclaimed'),
#                       ('claimed', 'Claimed'),
#                       ('partially_prepped', 'Partially prepped'),
#                       ('fully_prepped', 'Fully prepped'),
#                       ('nocontact', 'Unable to contact'))
# 
# Path: reviewer/models.py
# class ReviewGroup(models.Model):
#     """
#     many-to-many link of what groups can review for what organizations
#     (TODO) Eventually, we may want a workflow that allows someone
#     from one organization to vet events from another for the purposes
#     of copying them over.  In that case, the org would allow sharing (see event_exim app)
#     and wouldn't be added to the other org here, but a user might also be a member
#     of more than one org, and both orgs might be ok with 'sharing' the vetting
#     for the same event (assuming they have the same criteria).
#     """
#     organization = models.ForeignKey(Organization, db_index=True)
#     group = models.ForeignKey(Group, db_index=True)
#     visibility_level = models.IntegerField(
#         default=0,
#         help_text=("Think of it like an access hierarchy. "
#                    "0 is generally the lowest level. "
#                    "Anything higher is probably staff/etc. "
#                    "You can see Reviews/ReviewLogs with your own visibility and lower"))
# 
#     class Meta:
#         unique_together = ('organization', 'group')
# 
#     @classmethod
#     def org_groups(cls, organization_slug):
#         """
#         memo-ize orgs--it'll be a long time before we're too big for memory
#         """
#         if organization_slug not in _ORGANIZATIONS:
#             _ORGANIZATIONS[organization_slug] = list(
#                 ReviewGroup.objects.filter(
#                     organization__slug=organization_slug)
#                 .select_related('organization', 'group')
#                 # important for visibility options below
#                 .order_by('-visibility_level'))
#         return _ORGANIZATIONS.get(organization_slug)
# 
#     @classmethod
#     def user_allowed(cls, user, organization_slug):
#         allowed = ReviewGroup.org_groups(organization_slug)
#         allowed_groups = set([x.group_id for x in allowed])
#         group_ids = set(user.groups.values_list('id', flat=True))
#         return (group_ids.intersection(allowed_groups)
#                 or user.is_superuser)
# 
#     @classmethod
#     def user_visibility(cls, organization_slug, user):
#         org_groups = ReviewGroup.org_groups(organization_slug)
#         group_ids = set(user.groups.values_list('id', flat=True))
#         # ASSUMEs org_groups are ordered in descending value of visibility
#         # see org_groups()
#         for org_grp in org_groups:
#             if org_grp.group_id in group_ids:
#                 return org_grp.visibility_level
# 
#     @classmethod
#     def user_visibility_options(cls, organization_slug, user):
#         org_groups = ReviewGroup.org_groups(organization_slug)
#         group_ids = set(user.groups.values_list('id', flat=True))
# 
#         uservisibility = ReviewGroup.user_visibility(organization_slug, user)
#         options = {}
#         for org_grp in org_groups:
#             if org_grp.visibility_level <= uservisibility:
#                 if not options.get(org_grp.visibility_level) \
#                    or org_grp.group_id in group_ids:
#                     options[org_grp.visibility_level] = org_grp.group.name
#         return options
# 
#     @classmethod
#     def user_review_groups(cls, user):
#         return ReviewGroup.objects.filter(group__user=user)
# 
#     def __str__(self):
#         return '{} for {}'.format(self.group, self.organization)


import json
from django.conf import settings
from django.contrib.admin.filters import SimpleListFilter
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
from django.utils.html import format_html, conditional_escape
from event_store.models import Organization, EVENT_REVIEW_CHOICES, EVENT_PREP_CHOICES
from reviewer.models import ReviewGroup
        if not val and len(self.lookup_choices) == 1:
            val = self.lookup_choices[0][0]
        return val

    def visibility_options(self):
        val = self.value()
        if val:
            ReviewGroup.objects.filter(pk=val).values_list('group__name', 'visibility_level')

    def get_slug(self):
        val = self.value()
        if val:
            return Organization.objects.filter(id=val).first().slug

    def get_path(self):
        return reverse('reviewer_base')[:-1] #lop off trailing /

    def review_schema_json(self):
        return json.dumps([
            {'name': 'review_status',
             'choices': EVENT_REVIEW_CHOICES,
             'label': 'Review Status'},
            {'name': 'prep_status',
             'choices': EVENT_PREP_CHOICES,
             'label': 'Prep Status'},
        ])

    def lookups(self, request, model_admin):
        # we need this to save the right content type with the review api
        self.content_type = ContentType.objects.get_for_model(model_admin.model)

# Path: event_store/models.py
# class Organization(models.Model):
#     title = models.CharField(max_length=765)
#     slug = models.SlugField(max_length=128, db_index=True)
#     url = models.URLField(blank=True)
# 
#     facebook = models.CharField(max_length=128, null=True, blank=True)
#     twitter = models.CharField(max_length=128,
#                                null=True, blank=True,
#                                help_text="do not include @")
# 
#     logo_thumb = models.URLField(blank=True)
#     logo_big = models.URLField(blank=True)
#     privacy_policy = models.URLField(blank=True)
#     terms_and_conditions = models.URLField(blank=True)
# 
#     #default source id
#     osdi_source_id = models.CharField(max_length=128)
# 
#     group = models.ForeignKey(Group, db_index=True)
#     # for getting other folks' data; auto/re-generatable
#     api_key = models.CharField(max_length=765, editable=False)
# 
#     def __str__(self):
#         return self.title
# 
# EVENT_REVIEW_CHOICES = ((None, 'New'),
#                         ('reviewed', 'Claimed'),
#                         ('vetted', 'Vetted'),
#                         ('questionable', 'Questionable'),
#                         ('limbo', 'Limbo'))
# 
# EVENT_PREP_CHOICES = (('', 'Unclaimed'),
#                       ('claimed', 'Claimed'),
#                       ('partially_prepped', 'Partially prepped'),
#                       ('fully_prepped', 'Fully prepped'),
#                       ('nocontact', 'Unable to contact'))
# 
# Path: reviewer/models.py
# class ReviewGroup(models.Model):
#     """
#     many-to-many link of what groups can review for what organizations
#     (TODO) Eventually, we may want a workflow that allows someone
#     from one organization to vet events from another for the purposes
#     of copying them over.  In that case, the org would allow sharing (see event_exim app)
#     and wouldn't be added to the other org here, but a user might also be a member
#     of more than one org, and both orgs might be ok with 'sharing' the vetting
#     for the same event (assuming they have the same criteria).
#     """
#     organization = models.ForeignKey(Organization, db_index=True)
#     group = models.ForeignKey(Group, db_index=True)
#     visibility_level = models.IntegerField(
#         default=0,
#         help_text=("Think of it like an access hierarchy. "
#                    "0 is generally the lowest level. "
#                    "Anything higher is probably staff/etc. "
#                    "You can see Reviews/ReviewLogs with your own visibility and lower"))
# 
#     class Meta:
#         unique_together = ('organization', 'group')
# 
#     @classmethod
#     def org_groups(cls, organization_slug):
#         """
#         memo-ize orgs--it'll be a long time before we're too big for memory
#         """
#         if organization_slug not in _ORGANIZATIONS:
#             _ORGANIZATIONS[organization_slug] = list(
#                 ReviewGroup.objects.filter(
#                     organization__slug=organization_slug)
#                 .select_related('organization', 'group')
#                 # important for visibility options below
#                 .order_by('-visibility_level'))
#         return _ORGANIZATIONS.get(organization_slug)
# 
#     @classmethod
#     def user_allowed(cls, user, organization_slug):
#         allowed = ReviewGroup.org_groups(organization_slug)
#         allowed_groups = set([x.group_id for x in allowed])
#         group_ids = set(user.groups.values_list('id', flat=True))
#         return (group_ids.intersection(allowed_groups)
#                 or user.is_superuser)
# 
#     @classmethod
#     def user_visibility(cls, organization_slug, user):
#         org_groups = ReviewGroup.org_groups(organization_slug)
#         group_ids = set(user.groups.values_list('id', flat=True))
#         # ASSUMEs org_groups are ordered in descending value of visibility
#         # see org_groups()
#         for org_grp in org_groups:
#             if org_grp.group_id in group_ids:
#                 return org_grp.visibility_level
# 
#     @classmethod
#     def user_visibility_options(cls, organization_slug, user):
#         org_groups = ReviewGroup.org_groups(organization_slug)
#         group_ids = set(user.groups.values_list('id', flat=True))
# 
#         uservisibility = ReviewGroup.user_visibility(organization_slug, user)
#         options = {}
#         for org_grp in org_groups:
#             if org_grp.visibility_level <= uservisibility:
#                 if not options.get(org_grp.visibility_level) \
#                    or org_grp.group_id in group_ids:
#                     options[org_grp.visibility_level] = org_grp.group.name
#         return options
# 
#     @classmethod
#     def user_review_groups(cls, user):
#         return ReviewGroup.objects.filter(group__user=user)
# 
#     def __str__(self):
#         return '{} for {}'.format(self.group, self.organization)


# Path: reviewer/filters.py
import json
from django.conf import settings
from django.contrib.admin.filters import SimpleListFilter
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
from django.utils.html import format_html, conditional_escape
from event_store.models import Organization, EVENT_REVIEW_CHOICES, EVENT_PREP_CHOICES
from reviewer.models import ReviewGroup

        if not val and len(self.lookup_choices) == 1:
            val = self.lookup_choices[0][0]
        return val

    def visibility_options(self):
        val = self.value()
        if val:
            ReviewGroup.objects.filter(pk=val).values_list('group__name', 'visibility_level')

    def get_slug(self):
        val = self.value()
        if val:
            return Organization.objects.filter(id=val).first().slug

    def get_path(self):
        return reverse('reviewer_base')[:-1] #lop off trailing /

    def review_schema_json(self):
        return json.dumps([
            {'name': 'review_status',
             'choices': EVENT_REVIEW_CHOICES,
             'label': 'Review Status'},
            {'name': 'prep_status',
             'choices': EVENT_PREP_CHOICES,
             'label': 'Prep Status'},
        ])

    def lookups(self, request, model_admin):
        # we need this to save the right content type with the review api
        self.content_type = ContentType.objects.get_for_model(model_admin.model)

        return list(set(ReviewGroup.user_review_groups(request.user)