def test_selects_w_orm_joins(self): User, Address, Keyword, Order, Item = self.classes( "User", "Address", "Keyword", "Order", "Item" ) a1 = aliased(Address) self._run_cache_key_fixture( lambda: ( select(User).join(User.addresses), select(User).join(User.orders), select(User).join(User.addresses).join(User.orders), select(User).join(Address, User.addresses), select(User).join(a1, User.addresses), select(User).join(User.addresses.of_type(a1)), select(User) .join(Address, User.addresses) .join_from(User, Order), select(User) .join(Address, User.addresses) .join_from(User, User.orders), select(User.id, Order.id).select_from( orm_join(User, Order, User.orders) ), ), compare_values=True, )
def manage_album_viewers(session, gallery_name, album_name, *users): album = session.query(Album).select_from(orm_join(Album, Gallery)).\ filter(Gallery.name==gallery_name).\ filter(Album.gallery_id==Gallery.id).\ filter(Album.name==album_name).one() to_delete = [] to_add = [] for user in users: if user[0] == '-': assert user[1:] to_delete.append(user[1:]) elif user[0] == '+': assert user[1:] to_add.append(user[1:]) else: raise ValueError('Logins must be prefixed with ' '"+" or "-". Got: "%s"' % user) for login in to_add: user_id = session.query(User).filter_by(login=login).one().id is_already_viewer = session.query(AlbumViewer).filter_by( album_id=album.id, user_id=user_id).all() if is_already_viewer: logging.info('User "%s" was already a viewer.', login) else: session.add(AlbumViewer(album.id, user_id)) if to_delete: logins = ', '.join("'%s'" % login for login in to_delete) session.execute('DELETE FROM album_viewers ' 'WHERE album_id = :album_id AND user_id IN ( ' ' SELECT id FROM users ' " WHERE users.login IN (%s))" % logins, {'album_id': album.id})
def test_adapt_statement_cloned_traversal(self): User, Address = self.classes("User", "Address") statement = select(User).select_from( orm_join(User, Address, User.addresses)) @assert_cycles() def go(): cloned_traverse(statement, {}, {}) go()
def test_adapt_statement_replacement_traversal(self): User, Address = self.classes("User", "Address") statement = select(User).select_from( orm_join(User, Address, User.addresses)) @assert_cycles() def go(): replacement_traverse(statement, {}, lambda x: None) go()
def ajax_update(request): session = DBSession() pic_id = int(request.matchdict['picture_id']) picture = session.query(Picture).filter_by(id=pic_id).one() gallery = session.query(Gallery).select_from( orm_join(Gallery, Album)).\ filter(Album.id==picture.album_id).\ filter(Gallery.id==Album.gallery_id).one() if get_user_role(request, session, gallery) != ROLE_GALLERY_ADMIN: raise HTTPForbidden() picture.caption = request.POST['caption'] picture.location = request.POST['location'] return {'pic_info': picture.get_info}
def test_two_level(self): Company, Person, Paperwork = self.classes("Company", "Person", "Paperwork") stmt1 = select(Company).select_from( orm_join(Company, Person, Company.employees).join(Paperwork, Person.paperwork)) stmt2 = select(Company).join(Company.employees).join(Person.paperwork) stmt3 = (Session().query(Company).join(Company.employees).join( Person.paperwork).statement) self.assert_compile(stmt1, self.person_paperwork_expected) self.assert_compile( stmt2, self.person_paperwork_expected.replace("pjoin", "pjoin_1")) self.assert_compile( stmt3, self.person_paperwork_expected.replace("pjoin", "pjoin_1"))
def test_straight(self): Company, Person, Manager, Engineer = self.classes( "Company", "Person", "Manager", "Engineer") stmt1 = select(Company).select_from( orm_join(Company, Person, Company.employees)) stmt2 = select(Company).join(Company.employees) stmt3 = Session().query(Company).join(Company.employees).statement # TODO: can't get aliasing to not happen for .join() verion self.assert_compile( stmt1, self.straight_company_to_person_expected.replace( "pjoin_1", "pjoin"), ) self.assert_compile(stmt2, self.straight_company_to_person_expected) self.assert_compile(stmt3, self.straight_company_to_person_expected)
def test_wpoly_of_type(self): Company, Person, Manager, Engineer = self.classes( "Company", "Person", "Manager", "Engineer") p1 = with_polymorphic(Person, "*") stmt1 = select(Company).select_from( orm_join(Company, p1, Company.employees.of_type(p1))) stmt2 = select(Company).join(Company.employees.of_type(p1)) stmt3 = (Session().query(Company).join( Company.employees.of_type(p1)).statement) expected = ("SELECT companies.company_id, companies.name " "FROM companies JOIN %s" % self.default_pjoin) self.assert_compile(stmt1, expected) self.assert_compile(stmt2, expected) self.assert_compile(stmt3, expected)
def gallery_index(request): session = DBSession() gallery_name = request.matchdict['gallery_name'] try: gallery = session.query(Gallery).filter_by(name=gallery_name).one() except NoResultFound: raise HTTPNotFound(request.url) role = get_user_role(request, session, gallery) if role == ROLE_GALLERY_ADMIN: albums = sorted(gallery.albums, key=lambda a: a.title) else: user_id = get_user_metadata(request).get('id', None) albums = session.query(Album).select_from( orm_join(Album, AlbumViewer)).\ filter(Album.gallery_id==gallery.id).\ filter(Album.id==AlbumViewer.album_id).\ filter(AlbumViewer.user_id==user_id).order_by(Album.title).all() if not albums: raise HTTPForbidden() api = TemplateAPI(request, gallery.title) return {'api': api, 'gallery': gallery, 'albums': albums}
def go(): str(orm_join(User, Address, User.addresses).compile(testing.db))
def go(): orm_join(A, D, A.d)
def go(): orm_join(a1, B, a1.b)
def go(): orm_join(A, B, A.b)
def go(): orm_join(a1, D, a1.d)
def go(): str(orm_join(User, Address, User.addresses))
def remove_album(session, gallery_name, album_name): gallery = session.query(Gallery).filter_by(name=gallery_name).one() album = session.query(Album).select_from(orm_join(Album, Gallery)).\ filter(Album.gallery_id==gallery.id).\ filter(Album.name==album_name).one() session.delete(album)