<s> from __future__ import unicode_literals <EOL> from django . db import models <EOL> from django . utils . encoding import python_2_unicode_compatible <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from . fields import HexIntegerField <EOL> from . settings import PUSH_NOTIFICATIONS_SETTINGS as SETTINGS <EOL> @ python_2_unicode_compatible <EOL> class Device ( models . Model ) : <EOL> name = models . CharField ( max_length = <NUM_LIT:255> , verbose_name = _ ( "<STR_LIT:Name>" ) , blank = True , null = True ) <EOL> active = models . BooleanField ( verbose_name = _ ( "<STR_LIT>" ) , default = True , <EOL> help_text = _ ( "<STR_LIT>" ) ) <EOL> user = models . ForeignKey ( SETTINGS [ "<STR_LIT>" ] , blank = True , null = True ) <EOL> date_created = models . DateTimeField ( verbose_name = _ ( "<STR_LIT>" ) , auto_now_add = True , null = True ) <EOL> class Meta : <EOL> abstract = True <EOL> def __str__ ( self ) : <EOL> return self . name or str ( self . device_id or "<STR_LIT>" ) or "<STR_LIT>" % ( self . __class__ . __name__ , self . user or "<STR_LIT>" ) <EOL> class GCMDeviceManager ( models . Manager ) : <EOL> def get_queryset ( self ) : <EOL> return GCMDeviceQuerySet ( self . model ) <EOL> class GCMDeviceQuerySet ( models . query . QuerySet ) : <EOL> def send_message ( self , message , ** kwargs ) : <EOL> if self : <EOL> from . gcm import gcm_send_bulk_message <EOL> data = kwargs . pop ( "<STR_LIT>" , { } ) <EOL> if message is not None : <EOL> data [ "<STR_LIT:message>" ] = message <EOL> reg_ids = list ( self . filter ( active = True ) . values_list ( '<STR_LIT>' , flat = True ) ) <EOL> return gcm_send_bulk_message ( registration_ids = reg_ids , data = data , ** kwargs ) <EOL> class GCMDevice ( Device ) : <EOL> device_id = HexIntegerField ( verbose_name = _ ( "<STR_LIT>" ) , blank = True , null = True , db_index = True , <EOL> help_text = _ ( "<STR_LIT>" ) ) <EOL> registration_id = models . TextField ( verbose_name = _ ( "<STR_LIT>" ) ) <EOL> objects = GCMDeviceManager ( ) <EOL> class Meta : <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> def send_message ( self , message , ** kwargs ) : <EOL> from . gcm import gcm_send_message <EOL> data = kwargs . pop ( "<STR_LIT>" , { } ) <EOL> if message is not None : <EOL> data [ "<STR_LIT:message>" ] = message <EOL> return gcm_send_message ( registration_id = self . registration_id , data = data , ** kwargs ) <EOL> class APNSDeviceManager ( models . Manager ) : <EOL> def get_queryset ( self ) : <EOL> return APNSDeviceQuerySet ( self . model ) <EOL> class APNSDeviceQuerySet ( models . query . QuerySet ) : <EOL> def send_message ( self , message , ** kwargs ) : <EOL> if self : <EOL> from . apns import apns_send_bulk_message <EOL> reg_ids = list ( self . filter ( active = True ) . values_list ( '<STR_LIT>' , flat = True ) ) <EOL> return apns_send_bulk_message ( registration_ids = reg_ids , alert = message , ** kwargs ) <EOL> class APNSDevice ( Device ) : <EOL> device_id = models . UUIDField ( verbose_name = _ ( "<STR_LIT>" ) , blank = True , null = True , db_index = True , <EOL> help_text = "<STR_LIT>" ) <EOL> registration_id = models . CharField ( verbose_name = _ ( "<STR_LIT>" ) , max_length = <NUM_LIT:200> , unique = True ) <EOL> objects = APNSDeviceManager ( ) <EOL> class Meta : <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> def send_message ( self , message , ** kwargs ) : <EOL> from . apns import apns_send_message <EOL> return apns_send_message ( registration_id = self . registration_id , alert = message , ** kwargs ) <EOL> def get_expired_tokens ( ) : <EOL> from . apns import apns_fetch_inactive_ids <EOL> return apns_fetch_inactive_ids ( ) </s>
