From orm-devel@mailman.tux4web.de Wed Aug 27 03:00:39 2003 From: orm-devel@mailman.tux4web.de (Michael Watkins) Date: Wed, 27 Aug 2003 02:00:39 +0000 Subject: [orm-devel] dbclass - __getattr__ Message-ID: <1061949639.25435.24.camel@frog.mikewatkins.net> Greetings ORM folks. A minor patch request to allow ORM objects to be pickled. Note, I have not considered the implications of this at all, but thought I would put this out there to spark a discussion. Why pickle? I have a web Session object which has a user attribute which is an object containing other objects and collections of objects. (Person, Subscriptions, Groups, etc). It would be handy to save this Session object in the db to restore state between web requests without re-querying the database. I suppose the biggest issue is that when unpickled, both the database connection, cursor and associated objects may have changed radically in the interim. The database may have been stopped. Connections broken. Etc. And how would the objects know this without requerying... So, the change is not required, in the end, to address the dead end I was walking down. But, this very quick process of trying something and deciding it was foolish did raise one thing - because __getattr__ has been overridden, some standard python library code (like pickle) may fail because it expects the presence or absence of an attribute (and thus tests for AttributeError) rather than a key. Hope everyone is having a good summer... Mike in dbclass, change def __getattr__(self, name): """ The __getattr__ method is overwriten to call each column's get method when an attribute is requested. Attributes that start with a _ are ignored. """ if name[0] == "_": self.__dict__[name] to: def __getattr__(self, name): """ The __getattr__ method is overwriten to call each column's get method when an attribute is requested. Attributes that start with a _ are ignored. """ if name[0] == "_": try: return self.__dict__[name] except KeyError: # remapped the error so that getattr (was failing on __getstate__) works in pickle and other standard Python library functions raise AttributeError From orm-devel@mailman.tux4web.de Wed Aug 27 16:17:54 2003 From: orm-devel@mailman.tux4web.de (Michael Watkins) Date: Wed, 27 Aug 2003 08:17:54 -0700 Subject: [orm-devel] dbclass - __getattr__ In-Reply-To: <1061949639.25435.24.camel@frog.mikewatkins.net> References: <1061949639.25435.24.camel@frog.mikewatkins.net> Message-ID: <1061997474.46174.61.camel@frog.mikewatkins.net> On Tue, 2003-08-26 at 19:00, Michael Watkins wrote: > I suppose the biggest issue is that when unpickled, both the database > connection, cursor and associated objects may have changed radically in > the interim. The database may have been stopped. Connections broken. > Etc. And how would the objects know this without requerying... Out of interest I picked an ORM object and then called it after killing the db and restarting. I was able to modify an attribute and then commit the change after unpickling it. Your mileage may vary. From orm-devel@mailman.tux4web.de Wed Aug 27 17:58:49 2003 From: orm-devel@mailman.tux4web.de (Diedrich Vorberg) Date: Wed, 27 Aug 2003 18:58:49 +0200 Subject: [orm-devel] dbclass - __getattr__ In-Reply-To: <1061949639.25435.24.camel@frog.mikewatkins.net> References: <1061949639.25435.24.camel@frog.mikewatkins.net> Message-ID: <16204.58185.470291.280460@lisa.local.> Hi Mike, > if name[0] == "_": > try: > return self.__dict__[name] > except KeyError: > # remapped the error so that getattr (was failing on >__getstate__) works in pickle and other standard Python library >functions > raise AttributeError I've changed this as you suggested, it's in the CVS version. Diedrich -- _..._ Diedrich Vorberg .' '. / _ _ \ http://www.tux4web.de | (o)_(o) | info@tux4web.de \( ) / .---. //'._.'\ \ / \ Internet Dienstleistungen // . \ \ \.@-@./ und 'Consulting'. || . \ \ /`\_/`\ |\ : / | // _ \\ Linux Rules! \ `) ' (` /_ | \ )|_ _)``".____,.'"` (_ /`\_`> <_/ \ ) )'--'( ( \__/'---'\__/ '---` `---` From orm-devel@mailman.tux4web.de Thu Aug 28 23:07:01 2003 From: orm-devel@mailman.tux4web.de (Shaw Residential ISP Postmaster) Date: Thu, 28 Aug 2003 16:07:01 -0600 (MDT) Subject: [orm-devel] Delivery Notification: Delivery has failed Message-ID: <0HKC0015WO3PNT@l-daemon> --Boundary_(ID_vJKrRo7qrmMxhDm2lTy9ow) Content-type: text/plain; charset=us-ascii Content-language: en-US Content-transfer-encoding: 7BIT This report relates to a message you sent with the following header fields: Return-path: Received: from mail.islandnet.com (mail.islandnet.com [199.175.106.4]) by l-daemon (iPlanet Messaging Server 5.2 HotFix 1.16 (built May 14 2003)) with ESMTP id <0HKC0015SO3ONT@l-daemon> (original mail from orm-devel@mailman.tux4web.de); Thu, 28 Aug 2003 16:07:01 -0600 (MDT) Received: from jodster by mail.islandnet.com with LOCAL id 19sUuy-000ApY-00 for westcoasticons@shaw.ca; Thu, 28 Aug 2003 15:07:00 -0700 Received: from adsl213217.vnet.hu ([62.77.213.217] helo=DIGI) by mail.islandnet.com with ESMTP id 19sUux-000AoC-00 for icons@bywestcoast.com; Thu, 28 Aug 2003 15:07:00 -0700 Date: Fri, 29 Aug 2003 0:07:03 +0200 From: Subject: Re: Wicked screensaver To: Message-id: MIME-version: 1.0 X-Mailer: Microsoft Outlook Express 6.00.2600.0000 Content-type: multipart/mixed; boundary="_NextPart_000_0005A204" Importance: Normal X-Priority: 3 (Normal) X-MSMail-priority: Normal X-MailScanner: Found to be clean X-PEP: build Aug 1 2003 13:32:16 Your message cannot be delivered to the following recipients: Recipient address: westcoasticons@shaw.ca Reason: Rejected by filter processing The delivery of this message has been rejected. This message appears to have the sobig virus. If you have received this message in error, please change the subject line and resend the message --Boundary_(ID_vJKrRo7qrmMxhDm2lTy9ow) Content-type: message/delivery-status Reporting-MTA: dns;l-daemon (shaw_internet-daemon) Original-recipient: rfc822;westcoasticons@shaw.ca Final-recipient: rfc822;westcoasticons@shaw.ca Action: failed Status: 5.0.0 (Rejected by filter processing) --Boundary_(ID_vJKrRo7qrmMxhDm2lTy9ow) Content-type: TEXT/RFC822-HEADERS Return-path: Received: from mail.islandnet.com (mail.islandnet.com [199.175.106.4]) by l-daemon (iPlanet Messaging Server 5.2 HotFix 1.16 (built May 14 2003)) with ESMTP id <0HKC0015SO3ONT@l-daemon> (original mail from orm-devel@mailman.tux4web.de); Thu, 28 Aug 2003 16:07:01 -0600 (MDT) Received: from jodster by mail.islandnet.com with LOCAL id 19sUuy-000ApY-00 for westcoasticons@shaw.ca; Thu, 28 Aug 2003 15:07:00 -0700 Received: from adsl213217.vnet.hu ([62.77.213.217] helo=DIGI) by mail.islandnet.com with ESMTP id 19sUux-000AoC-00 for icons@bywestcoast.com; Thu, 28 Aug 2003 15:07:00 -0700 Date: Fri, 29 Aug 2003 0:07:03 +0200 From: Subject: Re: Wicked screensaver To: Message-id: MIME-version: 1.0 X-Mailer: Microsoft Outlook Express 6.00.2600.0000 Content-type: TEXT/PLAIN Content-transfer-encoding: QUOTED-PRINTABLE Importance: Normal X-Priority: 3 (Normal) X-MSMail-priority: Normal X-MailScanner: Found to be clean X-PEP: build Aug 1 2003 13:32:16 --Boundary_(ID_vJKrRo7qrmMxhDm2lTy9ow)-- From orm-devel@mailman.tux4web.de Sun Aug 31 16:56:01 2003 From: orm-devel@mailman.tux4web.de (Michael Watkins) Date: Sun, 31 Aug 2003 08:56:01 -0700 Subject: [orm-devel] Question: setting an object's many2one attribute to a "None" value In-Reply-To: <1061997474.46174.61.camel@frog.mikewatkins.net> References: <1061949639.25435.24.camel@frog.mikewatkins.net> <1061997474.46174.61.camel@frog.mikewatkins.net> Message-ID: <1062345361.71087.23.camel@frog.mikewatkins.net> Scenario: - Product.type_id has once upon a time been set to a Type object instance (which is a many2one type relationship) - The business requirement is to remove the relationship, such that Product.type_id is effectively a None Simply setting product_instance.type_id = None Will fail of couse, since the class expects a dbobj on the right hand side, not None. I've been puzzling through how to do this and perhaps have not had enough coffee. I'm surprised I've gone this long without having to do this, but... here I am! Mike From orm-devel@mailman.tux4web.de Sun Aug 31 19:25:00 2003 From: orm-devel@mailman.tux4web.de (Diedrich Vorberg) Date: Sun, 31 Aug 2003 20:25:00 +0200 Subject: [orm-devel] Question: setting an object's many2one attribute to a "None" value In-Reply-To: <1062345361.71087.23.camel@frog.mikewatkins.net> References: <1061949639.25435.24.camel@frog.mikewatkins.net> <1061997474.46174.61.camel@frog.mikewatkins.net> <1062345361.71087.23.camel@frog.mikewatkins.net> Message-ID: <16210.15740.310981.684865@lisa.local.> Hi Mike, >Simply setting > > product_instance.type_id = None > >Will fail of couse, since the class expects a dbobj on the right hand >side, not None. Actually this is not an "of course", it's a bug in ORM! The case of a many2one relationship being set to None (which should set the corresponding forign key column in your product table to NULL) is not handled by ORM! Here's a new version of many2oneColumn's set() method. I didn't test this, sorry... Diedrich ---------------------------------------------------------------------- class many2oneColumn(_2oneColumn): def set(self, data): """ You can only assign objects to a many2one-managed attribute which are of the right type and have already been inserted into the database. """ if data is None: self.ds().update(self._dbObj, self._columnName, "NULL") if self._columnName != self._attributeName and \ self._dbObj.columns.has_key(self._columnName): self._dbObj.__setattr__(self._columnName, None) else: if not isinstance(data, self._childClass): raise ObjectMustBeInserted( "You can only assign objects of %s to this column" \ % self._childClass.__name__) if not data.oid(): raise ObjectMustBeInserted( "You must insert a dbobj before you assign it to a " + \ "one2one relationship") if self._dbObj.oid(): fmt = data.primaryKeyValue().format() self.ds().update(self._dbObj, self._columnName, fmt) # if there is a column Object representing the foreign key column # set it to the value of the newly asigned object if self._columnName != self._attributeName and \ self._dbObj.columns.has_key(self._columnName): self._dbObj.__setattr__(self._columnName, data.primaryKeyValue().get()) self._retrieved = 1 self._data = data -- _..._ Diedrich Vorberg .' '. / _ _ \ http://www.tux4web.de | (o)_(o) | info@tux4web.de \( ) / .---. //'._.'\ \ / \ Internet Dienstleistungen // . \ \ \.@-@./ und 'Consulting'. || . \ \ /`\_/`\ |\ : / | // _ \\ Linux Rules! \ `) ' (` /_ | \ )|_ _)``".____,.'"` (_ /`\_`> <_/ \ ) )'--'( ( \__/'---'\__/ '---` `---`