Permalink
Browse files

Tweak dbutils.User class a little

Move static makeAnonymous() function down to the other static functions.

Don't load all preferences when setting one, and update the cached or
loaded preference value when setting the preference.

Raise base.ImplementationError exception instead of Exception when
attempting to read a preference that doesn't exist in the database.

Assert against using str() or int() on anonymous user.  They normally
return the user name or id, and anonymous users have neither.  (And
Python doesn't allow returning None from __str__() or __int__().)

Use isinstance(X, basestring) instead of isinstance(X, str).
  • Loading branch information...
jensl committed Nov 28, 2012
1 parent aa544c7 commit 1dd1be50d01818e08994553c5becabab43292bd7
Showing with 17 additions and 8 deletions.
  1. +3 −0 base.py
  2. +14 −8 dbutils.py
View
@@ -16,3 +16,6 @@
class Error(Exception):
pass
+
+class ImplementationError(Error):
+ pass
View
@@ -183,7 +183,7 @@ def __eq__(self, other):
else: return self.id == other.id
elif isinstance(other, int):
return self.id == other
- elif isinstance(other, str):
+ elif isinstance(other, basestring):
return self.name == other
else:
raise base.Error, "invalid comparison"
@@ -192,18 +192,19 @@ def __ne__(self, other):
return not (self == other)
def __int__(self):
+ assert not self.isAnonymous()
return self.id
+ def __str__(self):
+ assert not self.isAnonymous()
+ return self.name
+
def __repr__(self):
return "User(%r, %r, %r, %r)" % (self.id, self.name, self.email, self.fullname)
def __hash__(self):
return hash(self.id)
- @staticmethod
- def makeAnonymous():
- return User(None, None, None, None, 'anonymous')
-
def isAnonymous(self):
return self.status == 'anonymous'
@@ -239,7 +240,7 @@ def getPreference(self, db, item):
WHERE preferences.item=%s""", (self.id, item))
row = cursor.fetchone()
- if not row: raise Exception, "invalid preference: %s" % item
+ if not row: raise base.ImplementationError("invalid preference: %s" % item)
preference_type, integer, string = row
@@ -253,8 +254,7 @@ def getPreference(self, db, item):
return self.preferences[item]
def setPreference(self, db, item, value):
- self.loadPreferences(db)
- if self.preferences[item] != value:
+ if self.getPreference(db, item) != value:
cursor = db.cursor()
cursor.execute("DELETE FROM userpreferences WHERE uid=%s AND item=%s", [self.id, item])
cursor.execute("SELECT type FROM preferences WHERE item=%s", [item])
@@ -266,6 +266,8 @@ def setPreference(self, db, item, value):
else:
cursor.execute("INSERT INTO userpreferences (uid, item, string) VALUES (%s, %s, %s)", [self.id, item, str(value)])
+ self.preferences[item] = value
+
def getDefaultRepository(self, db):
default_repo = self.getPreference(db, "defaultRepository")
if not default_repo:
@@ -348,6 +350,10 @@ def cache(db, user):
if user.email: storage["e:" + user.email] = user
return user
+ @staticmethod
+ def makeAnonymous():
+ return User(None, None, None, None, 'anonymous')
+
@staticmethod
def fromId(db, user_id):
cached_user = db.storage["User"].get(user_id)

0 comments on commit 1dd1be5

Please sign in to comment.