def delete(self, entity_id):

        entity = self.id_entity()

        if entity.role == Role.client:
            pair = Pair.query(Pair.client == entity.key).get()
        else:
            pair = Pair.query(Pair.coach == entity.key).get()

        if pair:
            logging.info('deleting pair object: {}'.format(entity.name))
            pair.key.delete()

        CRUDService.do_delete(self, entity_id)
    def post(self, entity_id):

        client_key = ndb.Key('User', self.json['client']['key_id'])
        coach_key = ndb.Key('User', self.json['coach']['key_id'])

        client = client_key.get()
        coach = coach_key.get()
        if client.role == coach.role:
            raise ResponseException('Cannot pair users with the same role.')

        pair = Pair(
            id='{}_{}'.format(self.json['client']['key_id'], self.json['coach']['key_id']),
            client=client_key,
            coach=coach_key
        )

        pair.put()

        self.resp = pair.to_json()

        self.format_resp()
Example #3
0
    def get_coach(self, client_id):

        pair = Pair.query(Pair.client == ndb.Key(urlsafe=client_id)).get()
        if not pair:
            return {}
        try:
            client = pair.client.get()
            coach = pair.coach.get()
        except Exception as e:
            # one of the key properties do not exist. remove pair as it is no longer valid
            pair.key.delete()

        return pair.to_json()
Example #4
0
    def get_clients(self, coach_id):

        pairs = Pair.query(Pair.coach == ndb.Key(urlsafe=coach_id)).fetch()

        clean_pairs = []
        for pair in pairs:
            if not pair:
                continue
            try:
                client = pair.client.get()
                coach = pair.coach.get()
                clean_pairs.append(pair)
            except Exception as e:
                # one of the key properties do not exist. remove pair as it is no longer valid
                pair.key.delete()
                raise ResponseException("Invalid pair user keys", no_error=True)

        return self.json_list(clean_pairs)
    def get_clients(self):

        pairs = Pair.query(Pair.coach == self.user.key).fetch()
        return [pair.client for pair in pairs if pair]
    def get_coach(self):
        pair = Pair.query(Pair.client == self.user.key).get()
        if pair is None:
            return None

        return pair.coach