incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brond...@apache.org
Subject [13/14] git commit: [#4785] ticket:163 improve ShortUrl model
Date Tue, 25 Sep 2012 02:13:10 GMT
[#4785] ticket:163 improve ShortUrl model

* add upsert method
* change index to unique_index
* fix typo in db collection name


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/22c09c56
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/22c09c56
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/22c09c56

Branch: refs/heads/master
Commit: 22c09c565edd37f08924959bc660ec80e5802b3d
Parents: 81be3ee
Author: Igor Bondarenko <jetmind2@gmail.com>
Authored: Fri Sep 7 15:04:09 2012 +0300
Committer: Cory Johns <johnsca@geek.net>
Committed: Fri Sep 21 20:06:16 2012 +0000

----------------------------------------------------------------------
 ForgeShortUrl/forgeshorturl/model/__init__.py |    1 +
 ForgeShortUrl/forgeshorturl/model/shorturl.py |   20 +++++++++++++++++---
 2 files changed, 18 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/22c09c56/ForgeShortUrl/forgeshorturl/model/__init__.py
----------------------------------------------------------------------
diff --git a/ForgeShortUrl/forgeshorturl/model/__init__.py b/ForgeShortUrl/forgeshorturl/model/__init__.py
index e69de29..1fa43b3 100644
--- a/ForgeShortUrl/forgeshorturl/model/__init__.py
+++ b/ForgeShortUrl/forgeshorturl/model/__init__.py
@@ -0,0 +1 @@
+from .shorturl import ShortUrl

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/22c09c56/ForgeShortUrl/forgeshorturl/model/shorturl.py
----------------------------------------------------------------------
diff --git a/ForgeShortUrl/forgeshorturl/model/shorturl.py b/ForgeShortUrl/forgeshorturl/model/shorturl.py
index 3fefe3d..d20987c 100644
--- a/ForgeShortUrl/forgeshorturl/model/shorturl.py
+++ b/ForgeShortUrl/forgeshorturl/model/shorturl.py
@@ -1,6 +1,7 @@
+import pymongo
 from ming.orm.declarative import MappedClass
 from allura.model import project_orm_session
-from ming.orm import FieldProperty, ForeignIdProperty
+from ming.orm import FieldProperty, ForeignIdProperty, session
 from datetime import datetime
 from ming import schema
 from allura.model.auth import User
@@ -11,9 +12,9 @@ from allura.model.timeline import ActivityNode, ActivityObject
 class ShortUrl(MappedClass, ActivityNode, ActivityObject):
 
     class __mongometa__:
-        name = 'short_urs'
+        name = 'short_urls'
         session = project_orm_session
-        indexes = ['short_name']
+        unique_indexes = ['short_name']
 
     _id = FieldProperty(schema.ObjectId)
     url = FieldProperty(str)
@@ -28,3 +29,16 @@ class ShortUrl(MappedClass, ActivityNode, ActivityObject):
     @property
     def user(self):
         return User.query.get(_id=self.create_user)
+
+    @classmethod
+    def upsert(cls, shortname):
+        u = cls.query.get(short_name=shortname)
+        if u is not None:
+            return u
+        try:
+            u = cls(short_name=shortname)
+            session(u).flush(u)
+        except pymongo.errors.DuplicateKeyError:
+            session(u).expunge(u)
+            u = cls.query.get(short_name=shortname)
+        return u


Mime
View raw message