# Path: webapp/main/models.py
# class Organization(TimeStampedModel):
#     name = models.CharField(max_length=50, unique=True)
#     city = models.CharField(max_length=25, null=True, blank=True)
#     state = models.CharField(max_length=25, null=True, blank=True)
#     slug = models.SlugField(max_length=15, unique=True)
#     member_count = models.IntegerField(null=True, blank=True)
#     session_length = models.IntegerField(default=90)
# 
#     class Meta:
#         db_table = 'organization'
# 
#     def __unicode__(self):
#         return "%s - %s, %s" % (self.name, self.city, self.state)
# 
#     def metrics_list(self):
#         return OrganizationMetrics.objects.filter(
#             organization=self).values_list('metric__system_name', flat=True)
# 
# class Membership(TimeStampedModel):
#     ROLES = Choices(
#         ('member', 'member'),
#         ('staff', 'staff'),
#         ('admin', 'admin'),
#     )
#     user = models.ForeignKey(User)
#     organization = models.ForeignKey(Organization)
#     role = models.CharField(choices=ROLES, default=ROLES.member, max_length=15)
# 
#     class Meta:
#         db_table = 'membership'
#         unique_together = ('user', 'organization')
# 
#     def __unicode__(self):
#         return "%s - %s - %s" % (self.user, self.organization.name, self.role)


import pytz
from django.utils import timezone
from django.conf import settings
from .models import Organization, Membership


class TimezoneMiddleware(object):
    def process_request(self, request):
        # TODO - set timezone at user/org level
        timezone.activate(pytz.timezone('US/Eastern'))

        # If set at a session scope
        #tzname = request.session.get('django_timezone')
        #if tzname:
        #    timezone.activate(pytz.timezone(tzname))
        #else:
        #    timezone.deactivate()


class RequireMembershipMiddleware(object):

    def process_view(self, request, view_func, view_args, view_kwargs):
        if not request.user.is_authenticated():
            return
        try:
            slug = view_kwargs['slug']
            m = Membership.objects.get(user=request.user, organization__slug=slug)
            request.current_org = slug
        except KeyError:
            try:

# Path: webapp/main/models.py
# class Organization(TimeStampedModel):
#     name = models.CharField(max_length=50, unique=True)
#     city = models.CharField(max_length=25, null=True, blank=True)
#     state = models.CharField(max_length=25, null=True, blank=True)
#     slug = models.SlugField(max_length=15, unique=True)
#     member_count = models.IntegerField(null=True, blank=True)
#     session_length = models.IntegerField(default=90)
# 
#     class Meta:
#         db_table = 'organization'
# 
#     def __unicode__(self):
#         return "%s - %s, %s" % (self.name, self.city, self.state)
# 
#     def metrics_list(self):
#         return OrganizationMetrics.objects.filter(
#             organization=self).values_list('metric__system_name', flat=True)
# 
# class Membership(TimeStampedModel):
#     ROLES = Choices(
#         ('member', 'member'),
#         ('staff', 'staff'),
#         ('admin', 'admin'),
#     )
#     user = models.ForeignKey(User)
#     organization = models.ForeignKey(Organization)
#     role = models.CharField(choices=ROLES, default=ROLES.member, max_length=15)
# 
#     class Meta:
#         db_table = 'membership'
#         unique_together = ('user', 'organization')
# 
#     def __unicode__(self):
#         return "%s - %s - %s" % (self.user, self.organization.name, self.role)


# Path: webapp/main/middleware.py
import pytz
from django.utils import timezone
from django.conf import settings
from .models import Organization, Membership



class TimezoneMiddleware(object):
    def process_request(self, request):
        # TODO - set timezone at user/org level
        timezone.activate(pytz.timezone('US/Eastern'))

        # If set at a session scope
        #tzname = request.session.get('django_timezone')
        #if tzname:
        #    timezone.activate(pytz.timezone(tzname))
        #else:
        #    timezone.deactivate()


class RequireMembershipMiddleware(object):

    def process_view(self, request, view_func, view_args, view_kwargs):
        if not request.user.is_authenticated():
            return
        try:
            slug = view_kwargs['slug']
            m = Membership.objects.get(user=request.user, organization__slug=slug)
            request.current_org = slug
        except KeyError:
            try:

                m = Membership.objects.filter(user=request.user)[0]