def subscribe(self): if not self.verify_token: self.verify_token = make_id() if not self.id: self.lease_expires = datetime.now() + timedelta(seconds=LEASE_SECONDS) self.id = self.save().id self._ping_hub('subscribe')
def create(cls, user, object_type, object_id): # 5 bytes of random data for id = 2**(8*5) ~= 1 billion ids # Shouldn't get (m)any collisions at that size but we try to # handle it anyway. link = cls(user=user, object_type=object_type, object_id=object_id) session = cls.query.session for i in xrange(3): link.id = make_id(length=5) session.add(link) try: session.commit() except IntegrityError: session.rollback() if i < 2: continue else: # Automatically increase id length? raise Exception('Unable to select new link id') else: break return link