Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRogelio Mita <rogeliomita@activitycentral.com>2013-03-22 20:12:04 (GMT)
committer Rogelio Mita <rogeliomita@activitycentral.com>2013-03-25 21:51:48 (GMT)
commitce733264e00cb470112149943c430e2494fd9e54 (patch)
tree4438c4afec4c38d648de10dbdae4462a68636d13
parent60fe611a2860998fe7e4193592a56c1a60ce73aa (diff)
Ensure mongodb indexes; Index: unique name for Poll
-rw-r--r--webapp/deploy/fabfile.py4
-rw-r--r--webapp/polls/exceptions.py4
-rw-r--r--webapp/polls/management/__init__.py0
-rw-r--r--webapp/polls/management/commands/__init__.py0
-rw-r--r--webapp/polls/management/commands/syncindex.py18
-rw-r--r--webapp/polls/models.py11
-rw-r--r--webapp/polls/tests.py16
7 files changed, 52 insertions, 1 deletions
diff --git a/webapp/deploy/fabfile.py b/webapp/deploy/fabfile.py
index 5e23765..2f7c552 100644
--- a/webapp/deploy/fabfile.py
+++ b/webapp/deploy/fabfile.py
@@ -123,6 +123,10 @@ class deploy(object):
with prefix(activate):
run('python webapp/manage.py syncdb')
+ # Sync index for mongo db
+ with prefix(activate):
+ run('python webapp/manage.py syncindex')
+
with settings(warn_only=True):
sudo("chown -R www-data:ceibal webapp/webapp/media/")
diff --git a/webapp/polls/exceptions.py b/webapp/polls/exceptions.py
index 15e676c..76e1fa0 100644
--- a/webapp/polls/exceptions.py
+++ b/webapp/polls/exceptions.py
@@ -1,2 +1,6 @@
class ValidationError(Exception):
pass
+
+
+class UniqueNameError(Exception):
+ pass
diff --git a/webapp/polls/management/__init__.py b/webapp/polls/management/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/webapp/polls/management/__init__.py
diff --git a/webapp/polls/management/commands/__init__.py b/webapp/polls/management/commands/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/webapp/polls/management/commands/__init__.py
diff --git a/webapp/polls/management/commands/syncindex.py b/webapp/polls/management/commands/syncindex.py
new file mode 100644
index 0000000..36c21c7
--- /dev/null
+++ b/webapp/polls/management/commands/syncindex.py
@@ -0,0 +1,18 @@
+from django.core.management.base import BaseCommand
+
+from utils.mongo_connection import get_db, ConnectionError
+
+
+class Command(BaseCommand):
+
+ def handle(self, *args, **options):
+ try:
+ print "Getting db connection..."
+ db = get_db()
+ print "Db connection sucess: %s" % db.name
+ except ConnectionError, e:
+ if "[Errno 61]" in str(e):
+ print "Connection refused: Ensure that mongod is running"
+ else:
+ print " - Ensure unique name key in poll collection"
+ db.polls.ensure_index('name', unique=True)
diff --git a/webapp/polls/models.py b/webapp/polls/models.py
index 05104f7..d323aec 100644
--- a/webapp/polls/models.py
+++ b/webapp/polls/models.py
@@ -3,6 +3,7 @@ import time
import json
import os
import shutil
+import re
from exceptions import *
from bson import ObjectId, DBRef
@@ -46,6 +47,7 @@ class AbstracErrorObject(object):
class Poll(AbstracErrorObject):
collection_name = 'polls'
+ UniqueNameError = UniqueNameError
def __init__(self, data={}):
super(Poll, self).__init__()
@@ -76,9 +78,16 @@ class Poll(AbstracErrorObject):
if not self.name:
msg = "Necesita ingresar un nombre de encuesta."
self.errors.append(msg)
+ else:
+ # Check unique name key, Important !!!
+ existing = get_db().polls.find_one(
+ {'name': re.compile(self.name, re.IGNORECASE)})
+ if existing and existing.get("_id", None) != self.id:
+ raise Poll.ValidationError(
+ u"Poll name '%s' already in use." % self.name)
if len(self.errors):
- raise Group.ValidationError(str(self.errors))
+ raise Poll.ValidationError(str(self.errors))
def save(self):
self.validate()
diff --git a/webapp/polls/tests.py b/webapp/polls/tests.py
index 74bf10d..9ab4348 100644
--- a/webapp/polls/tests.py
+++ b/webapp/polls/tests.py
@@ -43,6 +43,22 @@ class PollTests(MongoTestCase):
self.assertEqual(1, self.db.polls.count())
+ def test_unique_name(self):
+
+ name = "unique name"
+ data = {'name': name}
+
+ poll = Poll(data=data)
+ poll.save()
+
+ poll = Poll(data=data)
+
+ self.assertRaisesRegexp(
+ Poll.ValidationError,
+ "Poll name '%s' already in use." % poll.name,
+ poll.validate
+ )
+
def test_get(self):
self.assertIsNone(Poll.get(id=None))