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


Python DBSession.add方法代码示例

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


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

示例1: file_create

# 需要导入模块: from pyrtos.models.meta import DBSession [as 别名]
# 或者: from pyrtos.models.meta.DBSession import add [as 别名]
    def file_create(self):
        """ New file view. Method for both post and get requests. """

        form = FileCreateForm(self.request.POST,
                              csrf_context=self.request.session)

        if self.request.method == 'POST' and form.validate():
            f = File()
            form.populate_obj(f)

            """ If file. Yes this method works without a file. """
            upload = self.request.POST.get('file')
            try:
                f.filename = f.make_filename(upload.filename)
                f.filemime = f.guess_mime(upload.filename)
                f.write_file(upload.file)
            except Exception:
                self.request\
                    .session.flash('File %s created but no file added' %
                                   (f.title), 'status')

            f.user_id = authenticated_userid(self.request)
            DBSession.add(f)
            self.request.session.flash('File %s created' %
                                       (f.title), 'success')
            return HTTPFound(location=self.request.route_url('files'))
        return {'title': 'New file',
                'form': form,
                'action': 'file_new'}
开发者ID:plastboks,项目名称:Pyrtos,代码行数:31,代码来源:file.py

示例2: creditor_create

# 需要导入模块: from pyrtos.models.meta import DBSession [as 别名]
# 或者: from pyrtos.models.meta.DBSession import add [as 别名]
    def creditor_create(self):
        """ New creditors. Method for both post and get request."""

        form = CreditorCreateForm(self.request.POST, csrf_context=self.request.session)

        if self.request.method == "POST" and form.validate():
            c = Creditor()
            form.populate_obj(c)
            c.user_id = authenticated_userid(self.request)
            DBSession.add(c)
            self.request.session.flash("Creditor %s created" % (c.title), "success")
            return HTTPFound(location=self.request.route_url("creditors"))
        return {"title": "New creditor", "form": form, "action": "creditor_new"}
开发者ID:plastboks,项目名称:Pyrtos,代码行数:15,代码来源:creditor.py

示例3: user_restore

# 需要导入模块: from pyrtos.models.meta import DBSession [as 别名]
# 或者: from pyrtos.models.meta.DBSession import add [as 别名]
    def user_restore(self):
        """ Restore user, returns redirect. """

        id = int(self.request.matchdict.get('id'))

        u = User.by_id(id)
        if not u:
            return HTTPNotFound()

        u.archived = False
        DBSession.add(u)
        self.request.session.flash('User %s restored' %
                                   (u.email), 'status')
        return HTTPFound(location=self.request.route_url('users_archived'))
开发者ID:plastboks,项目名称:Pyrtos,代码行数:16,代码来源:user.py

示例4: _initTestingDB

# 需要导入模块: from pyrtos.models.meta import DBSession [as 别名]
# 或者: from pyrtos.models.meta.DBSession import add [as 别名]
def _initTestingDB(makeuser=False):
    engine = create_engine('sqlite://')
    Base.metadata.create_all(engine)
    DBSession.configure(bind=engine)
    if makeuser:
        m = BPM()
        hashed = m.encode(u'1234567')
        with transaction.manager:
            user = User(email=u'[email protected]',
                        password=hashed,
                        group='admin',
                        )
            DBSession.add(user)
    return DBSession
开发者ID:plastboks,项目名称:Pyrtos,代码行数:16,代码来源:base.py

示例5: creditor_restore

# 需要导入模块: from pyrtos.models.meta import DBSession [as 别名]
# 或者: from pyrtos.models.meta.DBSession import add [as 别名]
    def creditor_restore(self):
        """ Restore creditor, returns redirect. """

        id = int(self.request.matchdict.get("id"))

        c = Creditor.by_id(id)
        if not c:
            return HTTPNotFound()
        """ Authorization check. """
        if c.private and c.user_id is not authenticated_userid(self.request):
            return HTTPForbidden()

        c.archived = False
        DBSession.add(c)
        self.request.session.flash("Creditor %s restored" % (c.title), "status")
        return HTTPFound(location=self.request.route_url("creditors_archived"))
开发者ID:plastboks,项目名称:Pyrtos,代码行数:18,代码来源:creditor.py

示例6: category_create

# 需要导入模块: from pyrtos.models.meta import DBSession [as 别名]
# 或者: from pyrtos.models.meta.DBSession import add [as 别名]
    def category_create(self):
        """ New category view. """

        form = CategoryCreateForm(self.request.POST,
                                  csrf_context=self.request.session)

        if self.request.method == 'POST' and form.validate():
            c = Category()
            form.populate_obj(c)
            c.user_id = authenticated_userid(self.request)
            DBSession.add(c)
            self.request.session.flash('Category %s created' %
                                       (c.title), 'success')
            return HTTPFound(location=self.request.route_url('categories'))
        return {'title': 'New category',
                'form': form,
                'action': 'category_new'}
开发者ID:plastboks,项目名称:Pyrtos,代码行数:19,代码来源:category.py

示例7: category_archive

# 需要导入模块: from pyrtos.models.meta import DBSession [as 别名]
# 或者: from pyrtos.models.meta.DBSession import add [as 别名]
    def category_archive(self):
        """ Archive category, returns redirect. """

        id = int(self.request.matchdict.get('id'))

        c = Category.by_id(id)
        if not c:
            return HTTPNotFound()

        """ Authorization check. """
        if c.private and c.user_id is not authenticated_userid(self.request):
            return HTTPForbidden()

        c.archived = True
        DBSession.add(c)
        self.request.session.flash('Category %s archived' %
                                   (c.title), 'status')
        return HTTPFound(location=self.request.route_url('categories'))
开发者ID:plastboks,项目名称:Pyrtos,代码行数:20,代码来源:category.py

示例8: expenditure_archive

# 需要导入模块: from pyrtos.models.meta import DBSession [as 别名]
# 或者: from pyrtos.models.meta.DBSession import add [as 别名]
    def expenditure_archive(self):
        """ Archive expenditure, returns redirect. """

        id = int(self.request.matchdict.get('id'))

        e = Expenditure.by_id(id)
        if not e:
            return HTTPNotFound()

        """ Authorization check. """
        if (e.category.private
           and e.category.user_id is not authenticated_userid(self.request)):
            return HTTPForbidden()

        e.archived = True
        DBSession.add(e)
        self.request.session.flash('Expenditure %s archived' %
                                   (e.title), 'status')
        return HTTPFound(location=self.request.route_url('expenditures'))
开发者ID:plastboks,项目名称:Pyrtos,代码行数:21,代码来源:expenditure.py

示例9: user_create

# 需要导入模块: from pyrtos.models.meta import DBSession [as 别名]
# 或者: from pyrtos.models.meta.DBSession import add [as 别名]
    def user_create(self):
        """ New user view. Method handles both post and get
        requests.
        """

        form = UserCreateForm(self.request.POST,
                              csrf_context=self.request.session)

        if self.request.method == 'POST' and form.validate():
            u = User()
            form.populate_obj(u)
            u.password = u.pm.encode(form.password.data)
            DBSession.add(u)
            self.request.session.flash('User %s created' %
                                       (u.email), 'success')
            return HTTPFound(location=self.request.route_url('users'))
        return {'title': 'New user',
                'form': form,
                'action': 'user_new'}
开发者ID:plastboks,项目名称:Pyrtos,代码行数:21,代码来源:user.py

示例10: user_archive

# 需要导入模块: from pyrtos.models.meta import DBSession [as 别名]
# 或者: from pyrtos.models.meta.DBSession import add [as 别名]
    def user_archive(self):
        """ Archive user, returns redirect. """

        a = authenticated_userid(self.request)
        id = int(self.request.matchdict.get('id'))

        """ User one (1) is a bit special..."""
        if id is 1:
            return HTTPNotFound()

        u = User.by_id(id)
        if not u:
            return HTTPNotFound()

        u.archived = True
        DBSession.add(u)
        self.request.session.flash('User %s archived' %
                                   (u.email), 'status')
        return HTTPFound(location=self.request.route_url('users'))
开发者ID:plastboks,项目名称:Pyrtos,代码行数:21,代码来源:user.py

示例11: expenditure_create

# 需要导入模块: from pyrtos.models.meta import DBSession [as 别名]
# 或者: from pyrtos.models.meta.DBSession import add [as 别名]
    def expenditure_create(self):
        """ New expenditure. Method for both post and get request. """

        form = ExpenditureCreateForm(self.request.POST,
                                     csrf_context=self.request.session)

        private = self.request.params.get('private')
        if private:
            """ Check if there exists any private categories. """
            if not Category.first_private(self.request):
                self.request.session.flash(self.missing_priv_cat, 'error')
                return HTTPFound(location=self.request
                                              .route_url('expenditures'))
            form.category_id.query = Category.all_private(self.request)
        else:
            """ Check if there exists any categories. """
            if not Category.first_active():
                self.request.session.flash(self.missing_shared_cat, 'error')
                return HTTPFound(location=self.request
                                              .route_url('expenditures'))
            form.category_id.query = Category.all_active(self.request)

        if self.request.method == 'POST' and form.validate():
            e = Expenditure()
            form.populate_obj(e)
            e.user_id = authenticated_userid(self.request)
            e.category_id = form.category_id.data.id
            DBSession.add(e)
            self.request.session.flash('Expenditure %s created' %
                                       (e.title), 'success')
            """ A bit ugly, but redirect the user based on private or not. """
            if private:
                return HTTPFound(location=
                                 self.request
                                     .route_url('expenditures',
                                                _query={'private': 1}))
            return HTTPFound(location=self.request.route_url('expenditures'))
        return {'title': 'New private expenditure' if private
                         else 'New expenditure',
                'form': form,
                'action': 'expenditure_new',
                'private': private}
开发者ID:plastboks,项目名称:Pyrtos,代码行数:44,代码来源:expenditure.py

示例12: invoice_restore

# 需要导入模块: from pyrtos.models.meta import DBSession [as 别名]
# 或者: from pyrtos.models.meta.DBSession import add [as 别名]
    def invoice_restore(self):
        """ Restore invoice, returns redirect. """
        id = int(self.request.matchdict.get('id'))
        i = Invoice.by_id(id)

        if not i:
            return HTTPNotFound()
        """ Authorization check. """
        if (i.category.private
           and i.category.user_id is not authenticated_userid(self.request)):
            return HTTPForbidden()
        """ Authorization check. """
        if (i.creditor.private
           and i.creditor.user_id is not authenticated_userid(self.request)):
            return HTTPForbidden()

        i.archived = False
        DBSession.add(i)
        self.request.session.flash('Invoice %s restored' % (i.title), 'status')
        return HTTPFound(location=self.request.route_url('invoices_archived'))
开发者ID:plastboks,项目名称:Pyrtos,代码行数:22,代码来源:invoice.py

示例13: main

# 需要导入模块: from pyrtos.models.meta import DBSession [as 别名]
# 或者: from pyrtos.models.meta.DBSession import add [as 别名]
def main(argv=sys.argv):
    if len(argv) != 2:
        usage(argv)
    config_uri = argv[1]
    setup_logging(config_uri)
    settings = get_appsettings(config_uri)
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.create_all(engine)

    m = BPM()
    a_email = raw_input('Enter email for admin account: ')
    a_pw = getpass('Enter password for admin account: ')
    a_hashed = m.encode(a_pw)

    with transaction.manager:
        admin = User(
                        email=a_email,
                        password=a_hashed,
                        group='admin',
                    )
        DBSession.add(admin)
开发者ID:plastboks,项目名称:Pyrtos,代码行数:24,代码来源:initializedb.py

示例14: invoice_edit

# 需要导入模块: from pyrtos.models.meta import DBSession [as 别名]
# 或者: from pyrtos.models.meta.DBSession import add [as 别名]
    def invoice_edit(self):
        """ Edit invoice view. This method handles both post,
        and get requests. """

        id = int(self.request.matchdict.get('id'))
        i = Invoice.by_id(id)

        if not i:
            return HTTPNotFound()
        """ Authorization check. """
        if (i.category.private
           and i.category.user_id is not authenticated_userid(self.request)):
            return HTTPForbidden()
        """ Authorization check. """
        if (i.creditor.private
           and i.creditor.user_id is not authenticated_userid(self.request)):
            return HTTPForbidden()

        form = InvoiceEditForm(self.request.POST, i,
                               csrf_context=self.request.session)

        if not i.files:
            del form.files
        else:
            form.files.query = i.files

        private = self.request.params.get('private')
        if private:
            """ Check if the necessary object exists. """
            if not Category.first_private(self.request):
                self.request.session.flash(self.missing_priv_cat, 'error')
                return HTTPFound(location=self.request.route_url('invoices'))
            if not Creditor.first_private(self.request):
                self.request.session.flash(self.missing_priv_cred, 'error')
                return HTTPFound(location=self.request.route_url('invoices'))
            form.category_id.query = Category.all_private(self.request)
            form.creditor_id.query = Creditor.all_private(self.request)
        else:
            """ Check if the necessary object exists. """
            if not Category.first_active():
                self.request.session.flash(self.missing_shared_cat, 'error')
                return HTTPFound(location=self.request.route_url('invoices'))
            if not Creditor.first_active():
                self.request.session.flash(self.missing_shared_cred, 'error')
                return HTTPFound(location=self.request.route_url('invoices'))
            form.category_id.query = Category.all_shared()
            form.creditor_id.query = Creditor.all_shared()

        if self.request.method == 'POST' and form.validate():
            form.populate_obj(i)
            i.category_id = form.category_id.data.id
            i.creditor_id = form.creditor_id.data.id

            if form.files:
                i.files = form.files.data

            """ If file, make file object and save/create file. """
            upload = self.request.POST.get('attachment')
            try:
                f = File()
                f.filename = f.make_filename(upload.filename)
                f.filemime = f.guess_mime(upload.filename)
                f.write_file(upload.file)
                f.title = 'Invoice.' +\
                          form.title.data + '.' +\
                          self.randomstr(6) + '.' +\
                          form.category_id.data.title + '.' +\
                          form.creditor_id.data.title + '.' +\
                          str(i.due)
                if private:
                    f.private = True
                f.user_id = authenticated_userid(self.request)
                DBSession.add(f)
                i.files.append(f)
            except Exception:
                self.request.session.flash('No file added.',
                                           'status')

            self.request.session.flash('Invoice %s updated' %
                                       (i.title), 'status')
            self.update_flash()
            if private:
                return HTTPFound(location=
                                 self.request
                                     .route_url('invoices',
                                                _query={'private': 1}))
            return HTTPFound(location=self.request.route_url('invoices'))

        form.category_id.data = i.category
        form.creditor_id.data = i.creditor
        return {'title': 'Edit private invoice' if private else 'Edit invoice',
                'form': form,
                'id': id,
                'action': 'invoice_edit',
                'private': private,
                'invoice': i}
开发者ID:plastboks,项目名称:Pyrtos,代码行数:98,代码来源:invoice.py

示例15: invoice_create

# 需要导入模块: from pyrtos.models.meta import DBSession [as 别名]
# 或者: from pyrtos.models.meta.DBSession import add [as 别名]
    def invoice_create(self):
        """ New invoice view. This method handles both post,
        and get requests.
        """

        form = InvoiceCreateForm(self.request.POST,
                                 csrf_context=self.request.session)

        private = self.request.params.get('private')
        if private:
            """ Check if the necessary object exists. """
            if not Category.first_private(self.request):
                self.request.session.flash(self.missing_priv_cat)
                return HTTPFound(location=self.request.route_url('invoices'))
            if not Creditor.first_private(self.request):
                self.request.session.flash(self.missing_priv_cred)
                return HTTPFound(location=self.request.route_url('invoices'))
            form.category_id.query = Category.all_private(self.request)
            form.creditor_id.query = Creditor.all_private(self.request)
        else:
            """ Check if the necessary object exists. """
            if not Category.first_active():
                self.request.session.flash(self.missing_shared_cat, 'error')
                return HTTPFound(location=self.request.route_url('invoices'))
            if not Creditor.first_active():
                self.request.session.flash(self.missing_shared_cred, 'error')
                return HTTPFound(location=self.request.route_url('invoices'))
            form.category_id.query = Category.all_shared()
            form.creditor_id.query = Creditor.all_shared()

        if self.request.method == 'POST' and form.validate():
            i = Invoice()
            form.populate_obj(i)
            i.user_id = authenticated_userid(self.request)
            i.category_id = form.category_id.data.id
            i.creditor_id = form.creditor_id.data.id

            """ If file, make file object and save/create file. """
            upload = self.request.POST.get('attachment')
            try:
                f = File()
                f.filename = f.make_filename(upload.filename)
                f.filemime = f.guess_mime(upload.filename)
                f.write_file(upload.file)
                f.title = 'Invoice.' +\
                          form.title.data + '.' +\
                          self.randomstr(6) + '.' +\
                          form.category_id.data.title + '.' +\
                          form.creditor_id.data.title + '.' +\
                          str(i.due)
                if private:
                    f.private = True
                f.user_id = authenticated_userid(self.request)
                DBSession.add(f)
                i.files = [f]
            except Exception:
                self.request.session.flash('No file added.',
                                           'status')

            DBSession.add(i)
            self.request.session.flash('Invoice %s created' %
                                       (i.title), 'success')
            self.update_flash()
            if private:
                return HTTPFound(location=
                                 self.request
                                     .route_url('invoices',
                                                _query={'private': 1}))
            return HTTPFound(location=self.request.route_url('invoices'))
        return {'title': 'New private invoice' if private else 'New invoice',
                'form': form,
                'action': 'invoice_new',
                'private': private,
                'invoice': False}
开发者ID:plastboks,项目名称:Pyrtos,代码行数:76,代码来源:invoice.py


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