# Path: app/models/startup.py
# class Startup(models.Model):
#     class Meta:
#         app_label = 'app'
# 
#     name = models.CharField(max_length=255,db_index=True,unique=True)
#     url = models.URLField(max_length=255,unique=True,blank=True,null=True)
# 
#     def __unicode__(self):
#         return self.name
# 
#     @staticmethod
#     def get_from_mailbox(mailbox):
#         if mailbox.startup_id: return mailbox.startup
# 
#         obj, created = Startup.objects.get_or_create(name=mailbox.username.capitalize())
#         return obj


from django.conf import settings
from django.contrib.auth import get_user_model
from django.db import models
from django.core.cache import cache
from app.models.startup import Startup
        from app.models.userprofile import UserProfile
        from app.models.userprofile import UserProfile
    class Meta:
        unique_together = (("user", "startup"),)

    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    startup = models.ForeignKey('Startup')

    @staticmethod
    def has_user_permission(user, startup):
        cache_key = u"has_user_permission|{0}|{1}".format(user.id,startup.id)
        if cache.get(cache_key): return cache.get(cache_key)

        userprofile = UserProfile.objects.get_or_create(user=user)[0]

        if userprofile.permission_mode == UserProfile.PERMISSION_ALLOW_DEFAULT:
            has_user_permission = (not StartupPermission.objects.filter(user=user, startup=startup).exists())
        else:
            has_user_permission = (StartupPermission.objects.filter(user=user, startup=startup).exists())

        cache.set(cache_key, has_user_permission, 10) # 10 secs cache
        return has_user_permission

    @staticmethod
    def startups_allowed(user):
        userprofile = UserProfile.objects.get_or_create(user=user)[0]

        startup_permission_ids = StartupPermission.objects.filter(user=user).values_list('startup_id', flat=True)

        if userprofile.permission_mode == UserProfile.PERMISSION_ALLOW_DEFAULT:
            return Startup.objects.exclude(id__in=startup_permission_ids)
        else:

# Path: app/models/startup.py
# class Startup(models.Model):
#     class Meta:
#         app_label = 'app'
# 
#     name = models.CharField(max_length=255,db_index=True,unique=True)
#     url = models.URLField(max_length=255,unique=True,blank=True,null=True)
# 
#     def __unicode__(self):
#         return self.name
# 
#     @staticmethod
#     def get_from_mailbox(mailbox):
#         if mailbox.startup_id: return mailbox.startup
# 
#         obj, created = Startup.objects.get_or_create(name=mailbox.username.capitalize())
#         return obj


# Path: app/models/startuppermission.py
from django.conf import settings
from django.contrib.auth import get_user_model
from django.db import models
from django.core.cache import cache
from app.models.startup import Startup
        from app.models.userprofile import UserProfile
        from app.models.userprofile import UserProfile

    class Meta:
        unique_together = (("user", "startup"),)

    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    startup = models.ForeignKey('Startup')

    @staticmethod
    def has_user_permission(user, startup):
        cache_key = u"has_user_permission|{0}|{1}".format(user.id,startup.id)
        if cache.get(cache_key): return cache.get(cache_key)

        userprofile = UserProfile.objects.get_or_create(user=user)[0]

        if userprofile.permission_mode == UserProfile.PERMISSION_ALLOW_DEFAULT:
            has_user_permission = (not StartupPermission.objects.filter(user=user, startup=startup).exists())
        else:
            has_user_permission = (StartupPermission.objects.filter(user=user, startup=startup).exists())

        cache.set(cache_key, has_user_permission, 10) # 10 secs cache
        return has_user_permission

    @staticmethod
    def startups_allowed(user):
        userprofile = UserProfile.objects.get_or_create(user=user)[0]

        startup_permission_ids = StartupPermission.objects.filter(user=user).values_list('startup_id', flat=True)

        if userprofile.permission_mode == UserProfile.PERMISSION_ALLOW_DEFAULT:
            return Startup.objects.exclude(id__in=startup_permission_ids)
        else:

            return Startup.objects.filter(id__in=startup_permission_ids)