From f8efc2f178eeb51bb0f50f15d7b80bb2ed49f34e Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Wed, 4 Jun 2008 19:46:08 -0700 Subject: [PATCH] Added a new group_storage_bytes member to model.User and some SQL to calculate it. --- NEWS | 5 ++++- model/User.py | 35 ++++++++++++++++++++++++++++++++++- model/test/Test_user.py | 10 ++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 0302a63..51c0f2b 100644 --- a/NEWS +++ b/NEWS @@ -1,9 +1,12 @@ 1.4.0: ?? - * Implemented some basic user administration features. + * Implemented some basic user administration features, allowing you to create + Luminotes users yourself. * Added new rate plans with support for user administration. * Wrote a tool for manually updating a user's rate plan: tools/set_plan.py * Refactored some of the client-side form-handling code to cut down on special-case hacks. + * NOTE: After upgrading to this release, you must restart memcached to clear + the cache. Failing to do so will cause errors with the User object. 1.3.40: May 27, 2008 * Added some minor product page tweaks like meta description tags. diff --git a/model/User.py b/model/User.py index fa1fea1..1c65269 100644 --- a/model/User.py +++ b/model/User.py @@ -41,6 +41,7 @@ class User( Persistent ): self.__password_hash = password_hash self.__email_address = email_address self.__storage_bytes = storage_bytes or 0 + self.__group_storage_bytes = 0 self.__rate_plan = rate_plan or 0 @staticmethod @@ -323,11 +324,37 @@ class User( Persistent ): ) as file_storage; """ % ( quote( self.object_id ), quote( self.object_id ) ) + def sql_calculate_group_storage( self ): + """ + Return a SQL string to calculate the total bytes of storage usage for all groups that this user + is a member of. This includes the cumulative storage of all users in these groups. + """ + return \ + """ + select + coalesce( sum( storage_bytes ), 0 ) + from + user_group, luminotes_user_current + where + group_id in ( + select + group_id + from + user_group + where + user_id = %s + ) and + user_id = luminotes_user_current.id + group by + user_id, storage_bytes; + """ % quote( self.object_id ) + def to_dict( self ): d = Persistent.to_dict( self ) d.update( dict( username = self.username, storage_bytes = self.__storage_bytes, + group_storage_bytes = self.__group_storage_bytes, rate_plan = self.__rate_plan, ) ) @@ -346,6 +373,11 @@ class User( Persistent ): self.update_revision() self.__storage_bytes = storage_bytes + def __set_group_storage_bytes( self, group_storage_bytes ): + # The group_storage_bytes member isn't actually saved to the database, so setting it doesn't + # need to call update_revision(). + self.__group_storage_bytes = group_storage_bytes + def __set_rate_plan( self, rate_plan ): self.update_revision() self.__rate_plan = rate_plan @@ -353,5 +385,6 @@ class User( Persistent ): username = property( lambda self: self.__username ) email_address = property( lambda self: self.__email_address, __set_email_address ) password = property( None, __set_password ) - storage_bytes = property( lambda self: self.__storage_bytes, __set_storage_bytes ) + storage_bytes = property( lambda self: self.__group_storage_bytes or self.__storage_bytes, __set_storage_bytes ) + group_storage_bytes = property( lambda self: self.__group_storage_bytes, __set_group_storage_bytes ) rate_plan = property( lambda self: self.__rate_plan, __set_rate_plan ) diff --git a/model/test/Test_user.py b/model/test/Test_user.py index 24eb8d5..618b07d 100644 --- a/model/test/Test_user.py +++ b/model/test/Test_user.py @@ -19,6 +19,7 @@ class Test_user( object ): assert self.user.username == self.username assert self.user.email_address == self.email_address assert self.user.storage_bytes == 0 + assert self.user.group_storage_bytes == 0 assert self.user.rate_plan == 0 def test_check_correct_password( self ): @@ -78,6 +79,14 @@ class Test_user( object ): assert self.user.storage_bytes == storage_bytes assert self.user.revision > previous_revision + def test_set_storage_bytes( self ): + original_revision = self.user.revision + group_storage_bytes = 44 + self.user.group_storage_bytes = group_storage_bytes + + assert self.user.group_storage_bytes == group_storage_bytes + assert self.user.revision == original_revision + def test_set_rate_plan( self ): previous_revision = self.user.revision rate_plan = 2 @@ -91,4 +100,5 @@ class Test_user( object ): assert d.get( "username" ) == self.username assert d.get( "storage_bytes" ) == self.user.storage_bytes + assert d.get( "group_storage_bytes" ) == self.user.group_storage_bytes assert d.get( "rate_plan" ) == self.user.rate_plan