import json
import requests
from Suchary.local_settings import GCM_API_KEY
from api.models import Device

URL = 'https://android.googleapis.com/gcm/send'
HEADER = {'Authorization': 'key=' + GCM_API_KEY, 'Content-Type': 'application/json'}


def get_reg_ids():
    reg_ids = [device.registration_id for device in Device.objects.filter(active=True)]
    return reg_ids


def send(data, collapse_key=None):
    reg_ids = get_reg_ids()
    payload = {'registration_ids': reg_ids, 'data': data}
    if collapse_key is not None:
        payload.update({'collapse_key': collapse_key})
    r = requests.post(URL, data=json.dumps(payload), headers=HEADER)


def edit_joke(key):
    data = {
        'type': 'edit',
        'key': key
    }
    send(data)


def new_jokes():
    data = {
        'type': 'new'
    }
    send(data, 'new')


def delete_joke(key):
    data = {
        'type': 'delete',
        'key': key
    }
    send(data)


def send_message(title, body):
    data = {
        'type': 'message',
        'title': title,
        'text': body
    }
    send(data)
import json

import requests

from Suchary.local_settings import GCM_API_KEY
from api.models import Device


URL = 'https://android.googleapis.com/gcm/send'
HEADER = {'Authorization': 'key=' + GCM_API_KEY, 'Content-Type': 'application/json'}


def get_reg_ids(alias):
    devices = Device.objects.filter(active=True)
    if alias:
        devices = devices.filter(alias=alias)
    reg_ids = [device.registration_id for device in devices]
    return reg_ids


def send(data, collapse_key=None, to=None):
    reg_ids = get_reg_ids(to)
    payload = {'registration_ids': reg_ids, 'data': data}
    if collapse_key is not None:
        payload.update({'collapse_key': collapse_key})
    r = requests.post(URL, data=json.dumps(payload), headers=HEADER)


def edit_joke(key):
    data = {
        'type': 'edit',
        'key': key
    }
    send(data)


def new_jokes():
    data = {
        'type': 'new'
    }
    send(data, 'new')


def delete_joke(key):
    data = {
        'type': 'delete',
        'key': key
    }
    send(data)


def send_message(title, body, alias=None):
    data = {
        'type': 'message',
        'title': title,
        'text': body
    }
    send(data, to=alias)
Send message only to one device if its alias was specified.
Send message only to one device if its alias was specified.

  import json
+ 
  import requests
+ 
  from Suchary.local_settings import GCM_API_KEY
  from api.models import Device
+ 
  
  URL = 'https://android.googleapis.com/gcm/send'
  HEADER = {'Authorization': 'key=' + GCM_API_KEY, 'Content-Type': 'application/json'}
  
  
- def get_reg_ids():
+ def get_reg_ids(alias):
+     devices = Device.objects.filter(active=True)
+     if alias:
+         devices = devices.filter(alias=alias)
-     reg_ids = [device.registration_id for device in Device.objects.filter(active=True)]
+     reg_ids = [device.registration_id for device in devices]
      return reg_ids
  
  
- def send(data, collapse_key=None):
+ def send(data, collapse_key=None, to=None):
-     reg_ids = get_reg_ids()
+     reg_ids = get_reg_ids(to)
      payload = {'registration_ids': reg_ids, 'data': data}
      if collapse_key is not None:
          payload.update({'collapse_key': collapse_key})
      r = requests.post(URL, data=json.dumps(payload), headers=HEADER)
  
  
  def edit_joke(key):
      data = {
          'type': 'edit',
          'key': key
      }
      send(data)
  
  
  def new_jokes():
      data = {
          'type': 'new'
      }
      send(data, 'new')
  
  
  def delete_joke(key):
      data = {
          'type': 'delete',
          'key': key
      }
      send(data)
  
  
- def send_message(title, body):
+ def send_message(title, body, alias=None):
      data = {
          'type': 'message',
          'title': title,
          'text': body
      }
-     send(data)
+     send(data, to=alias)
Send message only to one device if its alias was specified.
## Code Before:
import json
import requests
from Suchary.local_settings import GCM_API_KEY
from api.models import Device

URL = 'https://android.googleapis.com/gcm/send'
HEADER = {'Authorization': 'key=' + GCM_API_KEY, 'Content-Type': 'application/json'}


def get_reg_ids():
    reg_ids = [device.registration_id for device in Device.objects.filter(active=True)]
    return reg_ids


def send(data, collapse_key=None):
    reg_ids = get_reg_ids()
    payload = {'registration_ids': reg_ids, 'data': data}
    if collapse_key is not None:
        payload.update({'collapse_key': collapse_key})
    r = requests.post(URL, data=json.dumps(payload), headers=HEADER)


def edit_joke(key):
    data = {
        'type': 'edit',
        'key': key
    }
    send(data)


def new_jokes():
    data = {
        'type': 'new'
    }
    send(data, 'new')


def delete_joke(key):
    data = {
        'type': 'delete',
        'key': key
    }
    send(data)


def send_message(title, body):
    data = {
        'type': 'message',
        'title': title,
        'text': body
    }
    send(data)
## Instruction:
Send message only to one device if its alias was specified.
## Code After:
import json

import requests

from Suchary.local_settings import GCM_API_KEY
from api.models import Device


URL = 'https://android.googleapis.com/gcm/send'
HEADER = {'Authorization': 'key=' + GCM_API_KEY, 'Content-Type': 'application/json'}


def get_reg_ids(alias):
    devices = Device.objects.filter(active=True)
    if alias:
        devices = devices.filter(alias=alias)
    reg_ids = [device.registration_id for device in devices]
    return reg_ids


def send(data, collapse_key=None, to=None):
    reg_ids = get_reg_ids(to)
    payload = {'registration_ids': reg_ids, 'data': data}
    if collapse_key is not None:
        payload.update({'collapse_key': collapse_key})
    r = requests.post(URL, data=json.dumps(payload), headers=HEADER)


def edit_joke(key):
    data = {
        'type': 'edit',
        'key': key
    }
    send(data)


def new_jokes():
    data = {
        'type': 'new'
    }
    send(data, 'new')


def delete_joke(key):
    data = {
        'type': 'delete',
        'key': key
    }
    send(data)


def send_message(title, body, alias=None):
    data = {
        'type': 'message',
        'title': title,
        'text': body
    }
    send(data, to=alias)