# Path: ngnotifier/models.py
# class DeviceSession(models.Model):
#     session_key = models.CharField(max_length=60)
#     ANDROID = 'AN'
#     IOS = 'IO'
#     SERVICE_CHOICES = (
#         (ANDROID, 'Android'),
#         (IOS, 'iOS'),
#     )
#     service = models.CharField(max_length=2, choices=SERVICE_CHOICES)
#     gcm_device = models.ForeignKey(to='push_notifications.GCMDevice', null=True)
#     apns_device = models.ForeignKey(to='push_notifications.APNSDevice', null=True)
# 
#     def save(self, *args, **kwargs):
#         if not self.pk:
#             self.session_key = sha224(uuid4().hex.encode('utf-8')).hexdigest()
#         return super(DeviceSession, self).save(*args, **kwargs)
# 
#     def delete(self, *args, **kwargs):
#         if self.service == 'AN':
#             self.gcm_device.delete()
#         else:
#             self.apns_device.delete()
#         return super(DeviceSession, self).delete(*args, **kwargs)
# 
#     def get_name(self):
#         return self.gcm_device.name if self.service == 'AN'\
#             else self.apns_device.name
# 
#     def get_registration_id(self):
#         return self.gcm_device.registration_id if self.service == 'AN' \
#             else self.apns_device.registration_id
# 
#     def get_user(self):
#         return self.gcm_device.user if self.service == 'AN'\
#             else self.apns_device.user
# 
#     def is_active(self):
#         return self.gcm_device.active if self.service == 'AN'\
#             else self.apns_device.active
# 
#     def switch_active(self):
#         if self.service == 'AN':
#             self.gcm_device.active = not self.is_active()
#             self.gcm_device.save()
#         else:
#             self.apns_device.active = not self.is_active()
#             self.apns_device.save()


from django.http import HttpResponse
from ngnotifier.models import DeviceSession
from ngnotifier.settings import API_KEY


def api_key_required(func):
    def decorator(request, *args, **kwargs):
        if API_KEY == '':
            return HttpResponse(status=500)
        api_key = request.META.get('HTTP_KEY', '')
        if api_key != API_KEY:
            return HttpResponse(status=403)
        return func(request, *args, **kwargs)
    return decorator

def device_login_required(func):
    def decorator(request, *args, **kwargs):
        session_key = request.META.get('HTTP_SESSION', '')
        try:

# Path: ngnotifier/models.py
# class DeviceSession(models.Model):
#     session_key = models.CharField(max_length=60)
#     ANDROID = 'AN'
#     IOS = 'IO'
#     SERVICE_CHOICES = (
#         (ANDROID, 'Android'),
#         (IOS, 'iOS'),
#     )
#     service = models.CharField(max_length=2, choices=SERVICE_CHOICES)
#     gcm_device = models.ForeignKey(to='push_notifications.GCMDevice', null=True)
#     apns_device = models.ForeignKey(to='push_notifications.APNSDevice', null=True)
# 
#     def save(self, *args, **kwargs):
#         if not self.pk:
#             self.session_key = sha224(uuid4().hex.encode('utf-8')).hexdigest()
#         return super(DeviceSession, self).save(*args, **kwargs)
# 
#     def delete(self, *args, **kwargs):
#         if self.service == 'AN':
#             self.gcm_device.delete()
#         else:
#             self.apns_device.delete()
#         return super(DeviceSession, self).delete(*args, **kwargs)
# 
#     def get_name(self):
#         return self.gcm_device.name if self.service == 'AN'\
#             else self.apns_device.name
# 
#     def get_registration_id(self):
#         return self.gcm_device.registration_id if self.service == 'AN' \
#             else self.apns_device.registration_id
# 
#     def get_user(self):
#         return self.gcm_device.user if self.service == 'AN'\
#             else self.apns_device.user
# 
#     def is_active(self):
#         return self.gcm_device.active if self.service == 'AN'\
#             else self.apns_device.active
# 
#     def switch_active(self):
#         if self.service == 'AN':
#             self.gcm_device.active = not self.is_active()
#             self.gcm_device.save()
#         else:
#             self.apns_device.active = not self.is_active()
#             self.apns_device.save()


# Path: ngnotifier/decorators.py
from django.http import HttpResponse
from ngnotifier.models import DeviceSession
from ngnotifier.settings import API_KEY



def api_key_required(func):
    def decorator(request, *args, **kwargs):
        if API_KEY == '':
            return HttpResponse(status=500)
        api_key = request.META.get('HTTP_KEY', '')
        if api_key != API_KEY:
            return HttpResponse(status=403)
        return func(request, *args, **kwargs)
    return decorator

def device_login_required(func):
    def decorator(request, *args, **kwargs):
        session_key = request.META.get('HTTP_SESSION', '')
        try:

            device_session = DeviceSession.objects.get(session_key=session_key)