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


Python Session.save方法代码示例

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


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

示例1: Manager

# 需要导入模块: from meta import Session [as 别名]
# 或者: from meta.Session import save [as 别名]
class Manager(object):
    """
    Feeds Manager.
    This is the main object to control Leolo. No other classes should be used
    instead.
    """

    def __init__(self):
        """
        Database loading.
        """
        self.sites = []  # list with all sites objects
        self.logger = Logger("Manager")
        self.session = Session()
        self.sites = self.session.query(Site).all()  # loading all sites

    def activate_site(self, opt):
        """
        Makes a site active.
        User can pass as 'opt' a site number or feed url.
        """
        self.deactivate_site(opt, False)

    def add_site(self, feedurl, mark_as_read=False):
        """
        Adds a new site.
        'feedurl' is the feed's url.
        If 'mark_as_read' is True, then old entries will be ignored in next
        update. Only new entries from now will be indexed.
        Returns feed's title if everything it's ok, otherwise, None.
        """
        # Check valid url
        if not url_re.search(feedurl):
            s = "Site: '%s' is not a valid url!" % (feedurl)
            self.logger.info(s)
            raise ValueError(s)

        # Check if site already exists in DB, and if it doesn't, a new Site
        # instance is created and saved.
        if not self.session.query(Site, Feed).filter(Site.id == Feed.siteid).filter(Feed.url == feedurl).first():
            fm = FeedManager()
            result = fm.check_feed(feedurl, True)
            if result:
                site = Site(feedurl, result[0], result[1])
                if mark_as_read:
                    FeedManager.mark_as_read(site.feed)
                self.session.save(site)
                self.session.commit()
                self.sites.append(site)
                return result[0]

    def deactivate_site(self, opt, deactivate=True):
        """
        Makes a site inactive.
        User can pass as 'opt' a site number or feed url.
        """
        # By option number
        if isinstance(opt, int):
            try:
                site = self.session.query(Site).filter(Site.id == opt).one()
                feed = self.session.query(Feed).filter(Feed.siteid == opt).one()
                i = 0
                while True:
                    if self.sites[i].feed.url == site.feed.url:
                        if deactivate:
                            self.sites[i].inactive = True
                        else:
                            self.sites[i].inactive = False
                        self.session.commit()
                        break
                    i += 1
            except IndexError:
                act_deact = None
                if deactivate:
                    act_deact = "deactivate"
                else:
                    act_deact = "activate"
                s = "Cannot find to %s site #%s." % (act_deact, str(opt))
                self.logger.error(s)
                return
        # By feed url
        elif isinstance(opt, str):
            i = 0
            while True:
                try:
                    site = self.sites[i]
                    if site.feed.url == opt:
                        if deactivate:
                            self.sites[i].inactive = True
                        else:
                            self.sites[i].inactive = False
                        self.session.commit()
                        break
                    i += 1
                except IndexError:
                    act_deact = None
                    if deactivate:
                        act_deact = "deactivate"
                    else:
                        act_deact = "activate"
#.........这里部分代码省略.........
开发者ID:Menda,项目名称:Leolo,代码行数:103,代码来源:manager.py


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