Make Instance actually stand alone.
This commit is contained in:
4
api.py
4
api.py
@@ -32,7 +32,7 @@ class InvalidInstanceID(Exception):
|
||||
|
||||
|
||||
def CreateChannel(google_user, client, instance_id, args):
|
||||
models.Instance.FindOrCreate(instance_id, client)
|
||||
models.Instance.FindOrCreate(instance_id)
|
||||
|
||||
token = channel.create_channel(
|
||||
client_id=instance_id,
|
||||
@@ -95,7 +95,7 @@ def Subscribe(google_user, client, instance_id, args):
|
||||
ret = {
|
||||
'result': 'ok',
|
||||
'events': models.Subscription.FindOrCreate(
|
||||
subject, instance, messages, last_id),
|
||||
subject, client, instance, messages, last_id),
|
||||
}
|
||||
except models.AccessDenied:
|
||||
logging.exception('Subscribe access denied')
|
||||
|
||||
14
channel.py
14
channel.py
@@ -33,17 +33,23 @@ class OnChannelConnect(webapp2.RequestHandler):
|
||||
|
||||
|
||||
class OnChannelDisconnect(webapp2.RequestHandler):
|
||||
@staticmethod
|
||||
@db.transactional()
|
||||
def DeleteInstance(instance_id):
|
||||
instance = models.Instance.FromID(instance_id)
|
||||
ret = instance.key()
|
||||
instance.delete()
|
||||
return ret
|
||||
|
||||
@utils.local_namespace
|
||||
def post(self):
|
||||
instance_id = self.request.get('from')
|
||||
instance = models.Instance.FromID(instance_id)
|
||||
instance_key = self.DeleteInstance(instance_id)
|
||||
|
||||
subscriptions = models.Subscription.all().filter('instance =', instance)
|
||||
subscriptions = models.Subscription.all().filter('instance =', instance_key)
|
||||
for subscription in subscriptions:
|
||||
subscription.delete()
|
||||
|
||||
instance.delete()
|
||||
|
||||
|
||||
app = webapp2.WSGIApplication([
|
||||
('/_ah/channel/connected/', OnChannelConnect),
|
||||
|
||||
@@ -87,24 +87,21 @@ class Client(db.Model):
|
||||
|
||||
|
||||
class Instance(db.Model):
|
||||
client = db.ReferenceProperty(required=True)
|
||||
active = db.BooleanProperty(required=True, default=False)
|
||||
|
||||
@classmethod
|
||||
@db.transactional()
|
||||
def FromID(cls, instance_id):
|
||||
# TODO: assert client equality here if possible
|
||||
return cls.get_by_key_name(instance_id)
|
||||
|
||||
@classmethod
|
||||
@db.transactional()
|
||||
def FindOrCreate(cls, instance_id, client):
|
||||
def FindOrCreate(cls, instance_id):
|
||||
instance = cls.get_by_key_name(instance_id)
|
||||
if instance:
|
||||
# TODO: assert client equality here
|
||||
return instance
|
||||
else:
|
||||
return cls(key_name=instance_id, client=client).put()
|
||||
return cls(key_name=instance_id).put()
|
||||
|
||||
|
||||
class Subject(db.Model):
|
||||
@@ -242,13 +239,11 @@ class Subscription(db.Model):
|
||||
|
||||
@classmethod
|
||||
@db.transactional()
|
||||
def FindOrCreate(cls, subject, instance, messages=0, last_id=None):
|
||||
def FindOrCreate(cls, subject, client, instance, messages=0, last_id=None):
|
||||
readable_only_by = (
|
||||
Subject.readable_only_by.get_value_for_datastore(subject))
|
||||
client_key = (
|
||||
Instance.client.get_value_for_datastore(instance))
|
||||
if (readable_only_by and
|
||||
readable_only_by != client_key.parent()):
|
||||
readable_only_by != client.parent_key()):
|
||||
raise AccessDenied
|
||||
|
||||
subscriptions = (
|
||||
|
||||
Reference in New Issue
Block a user