Вопрос

I'm creating an iOS client for App.net and I'm attempting to setup a push notification server. Currently my app can add a user's App.net account id (a string of numbers) and a APNS device token to a MySQL database on my server. It can also remove this data. I've adapted code from these two tutorials:

In addition, I've adapted this awesome python script to listen in to App.net's App Stream API.

My python is horrendous, as is my MySQL knowledge. What I'm trying to do is access the APNS device token for the accounts I need to notify. My database table has two fields/columns for each entry, one for user_id and a one for device_token. I'm not sure of the terminology, please let me know if I can clarify this.

I've been trying to use peewee to read from the database but I'm in way over my head. This is a test script with placeholder user_id:

import logging
from pprint import pprint

import peewee
from peewee import *

db = peewee.MySQLDatabase("...", host="localhost", user="...", passwd="...")

class MySQLModel(peewee.Model):
    class Meta:
        database = db

class Active_Users(MySQLModel):
    user_id = peewee.CharField(primary_key=True)
    device_token = peewee.CharField()

db.connect()

# This is the placeholder user_id
userID = '1234' 

token = Active_Users.select().where(Active_Users.user_id == userID)
pprint(token)

This then prints out:

<class '__main__.User'> SELECT t1.`id`, t1.`user_id`, t1.`device_token` FROM `user` AS t1 WHERE (t1.`user_id` = %s) [u'1234']

If the code didn't make it clear, I'm trying to query the database for the row with the user_id of '1234' and I want to store the device_token of the same row (again, probably the wrong terminology) into a variable that I can use when I send the push notification later on in the script.

How do I correctly return the device_token? Also, would it be easier to forgo peewee and simply query the database using python-mysqldb? If that is the case, how would I go about doing that?

Это было полезно?

Решение

The call User.select().where(User.user_id == userID) returns a User object but you are assigning it to a variable called token as you're expecting just the device_token.

Your assignment should be this:

matching_users = Active_Users.select().where(Active_Users.user_id == userID) # returns an array of matching users even if there's just one
if matching_users is not None:
    token = matching_users[0].device_token
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top