def index(self, categories=None, status=None ): identity = request.environ.get( 'repoze.who.identity' ) user = identity['user'] me_contact = Contact.find_by_user( user ) if not me_contact: raise ValueError( "Unable to retrieve user-to-contact mapping. For some reason the contact mapping the current user to a contact_id has vanished." ) return dict( page='index', user=user, me_contact = me_contact, categories=DBSession.query(Category) )
def view(self, contact_id): identity = request.environ.get( 'repoze.who.identity' ) user = identity['user'] me_contact = Contact.find_by_user(user) contact_query = DBSession.query( Contact ) contact_query = contact_query.filter_by( user_id=user.user_id ) contact_query = contact_query.filter_by( contact_id=contact_id ) contact = contact_query.first() your_items_query = DBSession.query( Item ) your_items_query = your_items_query.filter_by( holder_id=contact.contact_id ) your_items_query = your_items_query.filter_by( owner_id=me_contact.contact_id ) his_items_query = DBSession.query( Item ) his_items_query = his_items_query.filter_by( holder_id=me_contact.contact_id ) his_items_query = his_items_query.filter_by( owner_id=contact.contact_id ) return dict( page='index', user=user, contact=contact, his_items_query=his_items_query, your_items_query=your_items_query )
def view(self, item_id): identity = request.environ.get( 'repoze.who.identity' ) user = identity['user'] me_contact = Contact.find_by_user( user ) item = DBSession.query(Item)\ .filter_by( item_id=item_id )\ .first() if not item: raise ValueError( "Item not found!" ) if item.holder_id != me_contact.contact_id and item.owner_id != me_contact.contact_id: raise ValueError( "You neither own nor hold this item! Access Denied!" ) return dict( item=item )
def setup(self): try: new_attrs = {} new_attrs.update(self.attrs) new_attrs.update(self.do_get_dependencies()) self.obj = self.klass(**new_attrs) DBSession.add(self.obj) DBSession.flush() return self.obj except: DBSession.rollback() raise
def find_by_user(self, user): the_contact = DBSession.query( self )\ .filter_by( user_id=user.user_id )\ .filter_by( other_user_id=user.user_id)\ .first() return the_contact
def test_query_obj(self): obj = DBSession.query(self.klass).one() for key, value in self.attrs.iteritems(): assert_equals(getattr(obj, key), value)
def tearDown(self): DBSession.rollback()
def by_user_name(cls, username): """Return the user object whose user name is ``username``.""" return DBSession.query(cls).filter(cls.user_name==username).first()
def by_email_address(cls, email): """Return the user object whose email address is ``email``.""" return DBSession.query(cls).filter(cls.email_address==email).first()