Exemple #1
0
def filter_users(context: "micro_context", term="", offset=0, limit=10, id=None):
    with context.session as session:
        if id is not None:
            user = session.query(model.User).get(id)
            result = serialize(user)
            del user["password"]
            subs = active_subscriptions_for(session, user_id=id)
            result.subscriptions = [sub_to_json(o) for o in subs]
            return result
        term = "{}%".format(term)
        users = session.query(model.User).filter(or_(model.User.name.like(term), model.User.email.like(term)))

        result = []
        for user in users:
            item = serialize(user)
            del item["password"]
            result.append(item)
        return result
Exemple #2
0
    def flushed(self, request=None):
        BaseContext.flushed(self)
        sync_user_subs = set()
        with self.session as session:
            for signal, message in self.broadcasts:
                if signal in ["user-added", "user-changed"]:
                    user = session.query(model.User).get(message["id"])
                    IOLoop.instance().add_future(
                        sync_auth("save_user.js", **{
                            'user_id': user.id,
                            'name': user.name,
                            'email': user.email,
                            'password': user.password
                        }),
                        self.synced)
                elif signal in ["service-added", "service-changed"]:
                    service = session.query(model.Service).get(message["id"])
                    IOLoop.instance().add_future(
                        sync_auth("save_service.js", **{
                            "service_id": service.id,
                            "name": service.name,
                            "cookie_url": service.token_url,
                            "cors": service.cors
                        }),
                        self.synced)
                elif signal == "subscription-active":
                    sync_user_subs.add(message.get("user_id"))

                for user_id in sync_user_subs:
                    subs = {
                        "user_id": user_id,
                        "service_ids": [s.service_id
                                        for s in active_subscriptions_for(
                                            session,
                                            user_id,
                                            loaded=False)]
                    }
                    IOLoop.instance().add_future(
                        sync_auth("set_subscriptions.js", **subs),
                        self.synced)