当前位置: 首页>>代码示例>>Python>>正文


Python Session.query方法代码示例

本文整理汇总了Python中core.people.person.Session.query方法的典型用法代码示例。如果您正苦于以下问题:Python Session.query方法的具体用法?Python Session.query怎么用?Python Session.query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在core.people.person.Session的用法示例。


在下文中一共展示了Session.query方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: run

# 需要导入模块: from core.people.person import Session [as 别名]
# 或者: from core.people.person.Session import query [as 别名]
    def run(self):
        """default method"""
        sess = Session()
        sender = self.req_obj.get('sender', '')
        uuid = self.req_obj.pop('uuid', '')
        hosts = None
        profile = None
        response = ''

        #exctract sender email
        if sender:
            email = sender.split('/')[0]
            #find user profile by primary email
            profile = sess.query(Profile).filter(
                Profile.email == email).one()

        if uuid:
            #find user profile by uuid
            profile = sess.query(Profile).filter(
                Profile.uuid == uuid).one()

        if profile:
            hosts = sess.query(ProfileLink).filter(
                ProfileLink.uuid == profile.uuid).all()

        code = -1
        if hosts:
            for h in hosts:
                logger.info('open http connection to %s ..' % h.url)
                try:
                    r = urllib2.urlopen(h.url)
                    code = r.code
                except Exception:
                    code = -1
                    #logger.exception(e)

                if code != 200:
                    response += "Could not connect %s \n" % (h.url)
        else:
            response = 'you have no urls that I can check, ' + \
                'add url by command: add url http://mysite.com'

        if self.req_from == 'jabber':
            if response == '':
                response  = 'looks like everything is ok.'
            todo = {'text': response, 'jmsg': response, 'type': 'response'}
            self.response = todo
        elif self.req_from == 'julius':
            bang()
            todo = {'say': response, 'text': response, 'type': 'response'}
            self.response = say(self.request.replace('say', '').upper())
        elif response:
            send(settings.MY_ACCOUNTS['gmail']['email'],
                 profile.email,
                 'check my sites report from Smarty',
                 response)
            todo = {'text': response, 'jmsg': response, 'type': 'response'}
            self.response = todo

        return self.response
开发者ID:vsilent,项目名称:smarty-bot,代码行数:62,代码来源:reaction.py

示例2: run

# 需要导入模块: from core.people.person import Session [as 别名]
# 或者: from core.people.person.Session import query [as 别名]
    def run(self):
        """default method"""

        #start sqlalchemy session
        sess = Session()
        sender = self.req_obj.get('sender', '')

        #exctract sender email
        email = sender.split('/')[0]

        #find user profile by primary email
        profile = sess.query(Profile).filter(Profile.email == email).one()

        #exstract url from request
        req = self.request.replace('add user', '', 1)
        name = req.strip().split()

        response = 'sorry you are not allowed to add new users'
        if profile.type == 'admin' and self.req_from == 'jabber':
            if name:
                logger.info('new user %s' % name)
                exists = sess.query(Profile).filter(
                    and_(Profile.first_name == name[0],
                         Profile.last_name == name[1])).all()
                user = {}

                if not exists:
                    try:
                        user['first_name'] = name[0]
                        user['last_name'] = name[1]
                        user['email'] = name[2]
                        add_profile(user)

                    except Exception as e:
                        sess.rollback()
                        logger.exception(e)

                    response = '%s added' % name
                else:
                    response = 'looks like %s is already exists' % name

            else:
                response = """
                Hmm  could not parse name,
                can you add it like this:
                    add user John Smith [email protected]"""

        if self.req_from == 'jabber':
            todo = {'text': response, 'jmsg': response, 'type': 'response'}
            self.response = todo

        if self.req_from == 'julius':
            bang()
            todo = {'say': response, 'text': response, 'type': 'response'}
            self.response = say(self.request.replace('say', '').upper())

        return self.response
开发者ID:vsilent,项目名称:smarty-bot,代码行数:59,代码来源:reaction.py

示例3: run

# 需要导入模块: from core.people.person import Session [as 别名]
# 或者: from core.people.person.Session import query [as 别名]
    def run(self):
        """default method"""

        #start sqlalchemy session
        sess = Session()

        sender = self.req_obj.get('sender', '')

        #exctract sender email
        email = sender.split('/')[0]

        #find user profile by primary email
        profile = sess.query(Profile).filter(Profile.email == email).one()

        #exstract url from request
        req = self.request.replace('add url', '', 1).strip()
        url = re.search("(?P<url>https?://[^\s]+)", req).group("url")
        logger.info('got %s' % url)

        if url:
            exists = sess.query(ProfileLink).filter(and_(ProfileLink.url == url, ProfileLink.uuid == profile.uuid)).all()
            link = {}

            if not exists:
                try:
                    link['url'] = url
                    link['type'] = 'external'
                    link['uuid'] = profile.uuid

                    l = ProfileLink(**link)
                    sess.add(l)
                    sess.commit()

                except Exception as e:
                    sess.rollback()
                    logger.exception(e)

                response = '%s added to your collection' % url
            else:
                response = 'looks like %s is already in collection' % url

        else:
            response = 'seems like %s is not valid url' % url

        if self.req_from == 'jabber':
            todo = { 'text' : response, 'jmsg' : response, 'type': 'response' }
            self.response = todo

        if self.req_from == 'julius':
            bang()
            todo = { 'say': response , 'text' : response ,'type': 'response' }
            self.response = say(self.request.replace('say', '').upper())

        return self.response
开发者ID:vsilent,项目名称:smarty-bot,代码行数:56,代码来源:reaction.py

示例4: search_users_request

# 需要导入模块: from core.people.person import Session [as 别名]
# 或者: from core.people.person.Session import query [as 别名]
def search_users_request(request, uuid):
        logger.info('Search for a request')

        prefix = 'client-request-'
        response = ''

        #need cache results for an hour because a lot of users
        #can have the same request
        #hour = datetime.now().hour
        minute = datetime.now().minute
        key = prefix + datetime.now().strftime("%Y-%m-%d-") + str(minute)
        old = prefix + datetime.now().strftime("%Y-%m-%d-") + str(minute - 1)

        #use unix socket
        r = redis.Redis(password=REDIS['password'],
                        unix_socket_path=REDIS['socket'])

        existing = r.get(key)

        sess = Session()

        #find user profile by uuid
        profile = sess.query(
            Profile).filter(Profile.uuid == uuid).one()

        #read for all for now
        allowed_to_read_all = True

        #here I need to know request to be public or not
        if profile.type == 'admin':
            allowed_to_read_all = True

        if existing:
            return existing
        else:
            if allowed_to_read_all:
                requests = sess.query(ProfileRequest).filter(
                    ProfileRequest.request.like('%' + request + '%')).all()
            else:
                requests = sess.query(ProfileRequest).filter(
                    and_(
                        ProfileRequest.uuid == uuid,
                        ProfileRequest.request.like('%' + request + '%')
                    )
                ).all()

            response = [req.request for req in requests]
            response = "\n".join(response)

            if r.set(key, response):
                r.delete(old)
                return response
开发者ID:vsilent,项目名称:smarty-bot,代码行数:54,代码来源:requests.py

示例5: run

# 需要导入模块: from core.people.person import Session [as 别名]
# 或者: from core.people.person.Session import query [as 别名]
    def run(self):
        """default method"""

        """default method that will be executed by /core/brain/main.py and receives request string"""

        response = 'You are not allowed to send broadcast messages.'
        email = None
        sender = self.req_obj.get('sender', '')

        if sender:
            email = sender.split('/')[0]
            sess = Session()
            #exctract uuid
            uuid = self.req_obj.pop('uuid', '')

            if email:
                #find user profile by primary email
                profile = sess.query(Profile).filter(Profile.email == email).one()
            elif uuid:
                #find user profile by uuid
                profile = sess.query(Profile).filter(Profile.uuid == uuid).one()
            else:
                response = 'Can not authenticate your id, are you registered ?'
                return { 'text' : response, 'jmsg' : response, 'type': 'response' }

        if profile.type  != 'admin':
            return { 'text' : response, 'jmsg' : response, 'type': 'response' }

        logger.info('Start video broadcasting...')
        proc = subprocess.Popen(
            ["motion"]
            #,shell=False
            #,stdin=subprocess.PIPE
            #,stdout=subprocess.PIPE
            #,stderr=subprocess.PIPE
        )
        #output, err = proc.communicate()
        #logger.info("%s ", output)
        #logger.info("%s", err)

        response  = 'Camera on.'

        if self.req_from == 'jabber':
            todo = { 'text' : response, 'jmsg' : response, 'type': 'response' }
            self.response = todo

        if self.req_from == 'julius':
            bang()
            todo = { 'say': response , 'text' : response ,'type': 'response' }
            self.response = say(self.request.replace('say', '').upper())

        return self.response
开发者ID:vsilent,项目名称:smarty-bot,代码行数:54,代码来源:reaction.py

示例6: run

# 需要导入模块: from core.people.person import Session [as 别名]
# 或者: from core.people.person.Session import query [as 别名]
    def run(self):
        """default method"""

        sender = self.req_obj.get('sender', '')
        uuid = self.req_obj.get('uuid', '')

        #######################################################################
        #                                                                     #
        #                        get user profile                             #
        #                                                                     #
        #######################################################################

        sess = Session()

        #exctract sender email
        if sender:
            email = sender.split('/')[0]
            if email:
                #find user profile by primary email
                self.profile = sess.query(Profile).filter(
                    Profile.email == email).one()
        elif uuid:
            #find user profile by uuid
            self.profile = sess.query(
                Profile).filter(Profile.uuid == uuid).one()

        logger.info(self.profile.email)

        #######################################################################
        #                                                                     #
        #                        prepare response                             #
        #                                                                     #
        #######################################################################

        response = self.profile.home_postcode
        if not self.profile.home_postcode:
            request_to_user = "I don't have your zip" +\
                    ", but you can add it later with command: my zip is ..."

        else:
            todo = {'text': response, 'jmsg': response, 'type': 'response'}

        if self.req_from == 'jabber':
            self.response = todo

        if self.req_from == 'julius':
            bang()
            todo = {'say': response, 'text': response, 'type': 'response'}
            self.response = say(self.request.replace('say', '').upper())

        return self.response
开发者ID:vsilent,项目名称:smarty-bot,代码行数:53,代码来源:reaction.py

示例7: run

# 需要导入模块: from core.people.person import Session [as 别名]
# 或者: from core.people.person.Session import query [as 别名]
    def run(self):
        """default method"""

        email = None
        sess = Session()
        sender = self.req_obj.get('sender', '')

        #exctract sender email
        if sender:
            email = sender.split('/')[0]

        uuid = self.req_obj.pop('uuid', '')

        if email:
            #find user profile by primary email
            profile = sess.query(Profile).filter(Profile.email == email).one()
        elif uuid:
            #find user profile by uuid
            profile = sess.query(Profile).filter(Profile.uuid == uuid).one()

        if profile.type == 'admin' and self.req_from == 'jabber':
            users = sess.query(Profile).filter().all()
            user_list = ['%s %s %s %s' % (
                u.first_name, u.last_name, u.nickname, u.email
            ) for u in users]

            if user_list:
                response = "\n".join(user_list)
            else:
                response = 'No users found'
        else:
            response = 'Sorry, only admins can see that'

        #########################################
        # If reaction executed by jabber client #
        #########################################

        if self.req_from == 'jabber':
            todo = {'text': response, 'jmsg': response, 'type': 'response'}
            self.response = todo

        #########################################
        # If reaction executed by julius client #
        #########################################

        if self.req_from == 'julius':
            bang()
            todo = {'say': response, 'text': response, 'type': 'response'}
            self.response = say(self.request.replace('say', '').upper())

        return self.response
开发者ID:vsilent,项目名称:smarty-bot,代码行数:53,代码来源:reaction.py

示例8: run

# 需要导入模块: from core.people.person import Session [as 别名]
# 或者: from core.people.person.Session import query [as 别名]
    def run(self):
        """default method"""

        sender = self.req_obj.get('sender', '')
        uuid = self.req_obj.get('uuid', '')

        #######################################################################
        #                                                                     #
        #                        get user profile                             #
        #                                                                     #
        #######################################################################

        sess = Session()

        #exctract sender email
        if sender:
            email = sender.split('/')[0]
            if email:
                #find user profile by primary email
                self.profile = sess.query(Profile).filter(
                    Profile.email == email).one()
        elif uuid:
            #find user profile by uuid
            self.profile = sess.query(
                Profile).filter(Profile.uuid == uuid).one()

        logger.info(self.profile.email)

        #######################################################################
        #                                                                     #
        #                        prepare response                             #
        #                                                                     #
        #######################################################################

        myrg = random.SystemRandom()
        length = 10
        # If you want non-English characters, remove the [0:52]
        alphabet = string.letters[0:52] + string.digits
        pw = str().join(myrg.choice(alphabet) for _ in range(length))
        response = pw

        if self.req_from == 'jabber':
            todo = {'text': response, 'jmsg': response, 'type': 'response'}
            self.response = todo

        if self.req_from == 'julius':
            bang()
            todo = {'say': response, 'text': response, 'type': 'response'}
            self.response = say(self.request.replace('say', '').upper())

        return self.response
开发者ID:vsilent,项目名称:smarty-bot,代码行数:53,代码来源:reaction.py

示例9: run

# 需要导入模块: from core.people.person import Session [as 别名]
# 或者: from core.people.person.Session import query [as 别名]
    def run(self):
        """default method"""
        email = None
        sender = self.req_obj.get('sender', '')
        if sender:
            email = sender.split('/')[0]

        sess = Session()
        #exctract uuid
        uuid = self.req_obj.pop('uuid', '')

        response = 'Sorry, you are not allowed to do that.'

        if email:
            #find user profile by primary email
            profile = sess.query(Profile).filter(Profile.email == email).one()
        elif uuid:
            #find user profile by uuid
            profile = sess.query(Profile).filter(Profile.uuid == uuid).one()
        else:
            todo = { 'text' : response, 'jmsg' : response, 'type': 'response' }
            self.response = todo
            return self.response

        if profile.type  == 'admin' and self.req_from == 'jabber':
            nm = nmap.PortScanner()                             # instantiate nmap.PortScanner object
            nm.scan(hosts='192.168.1.0/24', arguments='-sP')
            nm.command_line()                                   # get command line used for the scan : nmap -oX - -p 22-443 127.0.0.1
            nm.scaninfo()                                       # get nmap scan informations {'tcp': {'services': '22-443', 'method': 'connect'}}
            hosts_list = [(x, nm[x]['status']['state'], nm[x]['hostname']) for x in nm.all_hosts()]
            #for host, status, name in hosts_list:
                #print('{0}:{1}:{2}'.format(host, status, name))
            response = hosts_list


        #########################################
        # If executed by jabber client          #
        #########################################

        if self.req_from == 'jabber':
            todo = { 'text' : response, 'jmsg' : response, 'type': 'response' }
            self.response = todo

        if self.req_from == 'julius':
            response = 'see information in the log file'
            todo = { 'text' : response, 'jmsg' : response, 'type': 'response' }
            self.response = todo

        return self.response
开发者ID:vsilent,项目名称:smarty-bot,代码行数:51,代码来源:reaction.py

示例10: run

# 需要导入模块: from core.people.person import Session [as 别名]
# 或者: from core.people.person.Session import query [as 别名]
    def run(self):
        """default method"""

        response = ''
        #start sqlalchemy session
        sess = Session()

        sender = self.req_obj.get('sender', '')

        #exctract sender email
        email = sender.split('/')[0]

        #find user profile by primary email
        profile = sess.query(Profile).filter(Profile.email == email).one()

        #exstract url from request
        url = re.search("(?P<url>https?://[^\s]+)", self.request).group("url")

        if url:
            exists = sess.query(
                ProfileLink).filter(and_(
                    ProfileLink.url == url,
                    ProfileLink.uuid == profile.uuid)).all()
            if exists:
                response = 'Can not remove links yet'
                #pass
                # @todo pass control to delete link
            else:
                response = 'could not find %s' % url

        else:
            #check client's existing data, if request is something else
            pass

        if self.req_from == 'jabber':
            todo = {'text': response,
                    'jmsg': response,
                    'type': 'response'}
            self.response = todo

        if self.req_from == 'julius':
            bang()
            todo = {'say': response,
                    'text': response,
                    'type': 'response'}
            self.response = say(self.request.replace('say', '').upper())

        return self.response
开发者ID:vsilent,项目名称:smarty-bot,代码行数:50,代码来源:reaction.py

示例11: save_users_request

# 需要导入模块: from core.people.person import Session [as 别名]
# 或者: from core.people.person.Session import query [as 别名]
def save_users_request(sender, text):
    """ should be in separate file"""
    #start sqlalchemy session
    sess = Session()
    profile = None

    #exctract sender email
    if sender:
        email = sender.split('/')[0]
        if email:
            update_list_from_jabber({email})
            #find user profile by primary email
            try:
                profile = sess.query(Profile).filter(
                    Profile.email == email).one()
            except Exception as e:
                logger.exception(e)
                return False

    if not profile:
        return False

    req = {}
    logger.info('save new request %s' % text)
    try:
        req['request'] = text
        req['type'] = 'command'
        req['uuid'] = profile.uuid
        request = ProfileRequest(**req)
        sess.add(request)
        sess.commit()
    except Exception as e:
        sess.rollback()
        logger.exception(e)
开发者ID:vsilent,项目名称:smarty-bot,代码行数:36,代码来源:requests.py

示例12: run

# 需要导入模块: from core.people.person import Session [as 别名]
# 或者: from core.people.person.Session import query [as 别名]
    def run(self):
        """default method"""

        sess = Session()
        sender = self.req_obj.get('sender', '')
        #exctract sender email
        email = sender.split('/')[0]
        #find user profile by primary email
        profile = sess.query(Profile).filter(Profile.email == email).one()

        cron = CronTab(getuser())
        msg = ''
        job = cron.new(command='/usr/bin/python %s/core/cron/cronjob.py --uuid=%s --cmd="check my sites" --arguments="%s"' % (ROBOT_DIR, profile.uuid, msg.replace('"', '')))

        #every hour
        job.minute.on(0)
        cron.write()
        response = 'ok, cronjob added %s' % job.render()

        if self.req_from == 'jabber':
            todo = {'text': response, 'jmsg': response, 'type': 'response'}
            self.response = todo

        if self.req_from == 'julius':
            bang()
            todo = {'say': response, 'text': response, 'type': 'response'}
            self.response = say(self.request.replace('say', '').upper())

        return self.response
开发者ID:vsilent,项目名称:smarty-bot,代码行数:31,代码来源:reaction.py

示例13: run

# 需要导入模块: from core.people.person import Session [as 别名]
# 或者: from core.people.person.Session import query [as 别名]
    def run(self):
        """default method"""

        #start sqlalchemy session

        sess = Session()

        sender = self.req_obj.get('sender', '')

        #exctract sender email
        email = sender.split('/')[0]

        #find user profile by primary email
        profile = sess.query(Profile).filter(Profile.email == email).one()

        if profile:
            pass

        response = 'Sorry, you are not allowed to execute this action'
        if profile.type == 'admin' and self.req_from == 'jabber':
            upgrade()
            response = 'Upgrade started.'

        if self.req_from == 'jabber':
            todo = {'text': response, 'jmsg': response, 'type': 'response'}
            self.response = todo

        if self.req_from == 'julius':
            bang()
            todo = {'say': response, 'text': response, 'type': 'response'}
            self.response = say(self.request.replace('say', '').upper())

        return self.response
开发者ID:vsilent,项目名称:smarty-bot,代码行数:35,代码来源:reaction.py

示例14: run

# 需要导入模块: from core.people.person import Session [as 别名]
# 或者: from core.people.person.Session import query [as 别名]
    def run(self):
        """default method"""

        sess = Session()
        sender = self.req_obj.get('sender', '')
        subdir = ROBOT_DIR.child('core', 'brain', 'remind', 'me')
        logger.info(subdir)

        #exctract sender email
        email = sender.split('/')[0]

        #find user profile by primary email
        profile = sess.query(Profile).filter(Profile.email == email).one()
        response = ''
        if profile:
            response = profile.first_name + ', '
            pass

        response += "how do you want to be reminded ?\nHere are some examples of how you can set a reminder:\n"
        l = [" ".join(p.format().split('/')[2:]) for p in subdir.relative().listdir() if p.isdir()]
        response += "\n".join(l)

        if self.req_from == 'jabber':
            todo = {'text': response, 'jmsg': response, 'type': 'response'}
            self.response = todo

        if self.req_from == 'julius':
            from core.broadcast import say, bang
            bang()
            todo = {'say': response, 'text': response, 'type': 'response'}
            self.response = say(self.request.replace('say', '').upper())

        return self.response
开发者ID:vsilent,项目名称:smarty-bot,代码行数:35,代码来源:reaction.py

示例15: run

# 需要导入模块: from core.people.person import Session [as 别名]
# 或者: from core.people.person.Session import query [as 别名]
    def run(self):
        """default method"""

        sender = self.req_obj.get('sender', '')
        uuid = self.req_obj.get('uuid', '')

        #######################################################################
        #                                                                     #
        #                        get user profile                             #
        #                                                                     #
        #######################################################################

        sess = Session()

        #exctract sender email
        if sender:
            email = sender.split('/')[0]
            if email:
                #find user profile by primary email
                self.profile = sess.query(Profile).filter(
                    Profile.email == email).one()
        elif uuid:
            #find user profile by uuid
            self.profile = sess.query(
                Profile).filter(Profile.uuid == uuid).one()

        logger.info(self.profile.email)

        #######################################################################
        #                                                                     #
        #                        prepare response                             #
        #                                                                     #
        #######################################################################

        response = 'sudo dd if=/dev/sdb of=/media/vs/b3021b4e-c482-4e8a-8653'\
            '-23d35adbeb3a/vs/smarty-raspberry.img'

        if self.req_from == 'jabber':
            todo = {'text': response, 'jmsg': response, 'type': 'response'}
            self.response = todo

        if self.req_from == 'julius':
            bang()
            todo = {'say': response, 'text': response, 'type': 'response'}
            self.response = say(self.request.replace('say', '').upper())

        return self.response
开发者ID:vsilent,项目名称:smarty-bot,代码行数:49,代码来源:reaction.py


注:本文中的core.people.person.Session.query方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。