Permalink
Browse files

Add import list api call and garden

  • Loading branch information...
1 parent d75a068 commit 39e6402ddd10ea96359ded72069e27260ee4c675 @mitechie committed Aug 28, 2012
View
@@ -64,6 +64,16 @@ def size():
ImportQueue.status != ERROR))
return qry.count()
+ @staticmethod
+ def get_list():
+ """Searching for records and all that.
+
+ """
+ qry = ImportQueue.query
+ qry = qry.order_by(ImportQueue.id)
+ return qry.all()
+
+
class ImportQueue(Base):
"""Track imports we need to do"""
__tablename__ = 'import_queue'
View
@@ -161,6 +161,11 @@ def build_routes(config):
"api_admin_accounts_invites",
"/api/v1/a/accounts/invites",
request_method="GET")
+ config.add_route(
+ "api_admin_imports_list",
+ "/api/v1/a/imports/list",
+ request_method="GET")
+
# these are single word matching, they must be after /recent /popular etc
config.add_route("user_home", "{username}")
@@ -8,6 +8,8 @@
from pyramid import testing
from bookie.models import DBSession
+from bookie.models.queue import ImportQueue
+from bookie.models.queue import ImportQueueMgr
from bookie.tests import BOOKIE_TEST_INI
from bookie.tests import empty_db
@@ -39,6 +41,17 @@ def tearDown(self):
testing.tearDown()
empty_db()
+ def _add_demo_import(self):
+ """DB Needs some imports to be able to query."""
+ # add out completed one
+ q = ImportQueue(
+ username='admin',
+ file_path='testing.txt'
+ )
+ DBSession.add(q)
+ transaction.commit()
+ return
+
def test_list_inactive_users(self):
"""Test that we can fetch the inactive users."""
# for now just make sure we can get a 200 call on it.
@@ -94,3 +107,23 @@ def test_set_invite_ct(self):
res = self.testapp.post('/api/v1/a/accounts/invites/admin/0',
params=params,
status=200)
+
+ def test_import_info(self):
+ """Test that we can get a count of the imports in the system."""
+ self._add_demo_import()
+ params = {
+ 'api_key': self.api_key
+ }
+ res = self.testapp.get('/api/v1/a/imports/list',
+ params=params,
+ status=200)
+
+ # we should get back tuples of username/count
+ data = json.loads(res.body)
+
+ eq_(1, data.get('count'), "There are none by default. " + res.body)
+
+ eq_('admin', data.get('imports')[0]['username'],
+ "The first import is from rharding " + res.body)
+ eq_(0, data.get('imports')[0]['status'],
+ "And it has a status of 0 " + res.body)
@@ -12,7 +12,6 @@
from bookie.models import DBSession
from bookie.models import Bmark
-from bookie.models import Tag, bmarks_tags
from bookie.models.queue import ImportQueue
from bookie.models.queue import ImportQueueMgr
from bookie.lib.urlhash import generate_hash
@@ -267,7 +266,7 @@ def test_skip_running(self):
# now verify that we've got our record
imp = ImportQueueMgr.get_ready()
imp = imp[0]
- imp.status=2
+ imp.status = 2
DBSession.flush()
imp = ImportQueueMgr.get_ready()
@@ -280,8 +279,10 @@ def test_one_import(self):
# Prep the db with 2 other imports ahead of this user's.
# We have to commit these since the request takes place in a new
# session/transaction.
- DBSession.add(ImportQueue(username='testing', file_path='testing.txt'))
- DBSession.add(ImportQueue(username='testing2', file_path='testing2.txt'))
+ DBSession.add(ImportQueue(username='testing',
+ file_path='testing.txt'))
+ DBSession.add(ImportQueue(username='testing2',
+ file_path='testing2.txt'))
DBSession.flush()
transaction.commit()
@@ -293,8 +294,10 @@ def test_one_import(self):
res = self.app.get('/admin/import')
ok_('<form' not in res.body, "We shouldn't have a form")
- ok_('waiting in the queue' in res.body, "We want to display a waiting message.")
- ok_('2 other imports' in res.body, "We want to display a count message." + res.body)
+ ok_('waiting in the queue' in res.body,
+ "We want to display a waiting message.")
+ ok_('2 other imports' in res.body,
+ "We want to display a count message." + res.body)
def test_completed_dont_count(self):
"""Once completed, we should get the form again"""
@@ -315,4 +318,3 @@ def test_completed_dont_count(self):
res = self.app.get('/admin/import')
ok_('<form' in res.body, "We should have a form")
-
View
@@ -23,6 +23,7 @@
from bookie.models import TagMgr
from bookie.models.auth import ActivationMgr
from bookie.models.auth import UserMgr
+from bookie.models.queue import ImportQueueMgr
from bookie.models.fulltext import get_fulltext_handler
@@ -927,3 +928,15 @@ def accounts_invites_add(request):
request.response.status_int = 400
ret = {'error': "Bad request, missing parameters"}
return ret
+
+
+@view_config(route_name="api_admin_imports_list", renderer="json")
+@api_auth('api_key', UserMgr.get, admin_only=True)
+def import_list(request):
+ """Provide some import related data."""
+ import_list = ImportQueueMgr.get_list()
+ ret = {
+ 'count': len(import_list),
+ 'imports': [dict(h) for h in import_list],
+ }
+ return ret
@@ -108,10 +108,10 @@ def fetch_content(i, q):
ct = 0
all = False
- while(not all):
- # start the queue up we'll use to thread the url fetching
- enclosure_queue = Queue()
+ # start the queue up we'll use to thread the url fetching
+ enclosure_queue = Queue()
+ while(not all):
if args.new_only:
# we take off the offset because each time we run, we should
# have new ones to process. The query should return the 10
@@ -147,7 +147,6 @@ def fetch_content(i, q):
# the queue.
worker = threading.Thread(target=fetch_content,
args=(i, enclosure_queue,))
- worker.setDaemon(True)
worker.start()
for hash_id, url in urls.iteritems():

0 comments on commit 39e6402

Please sign in to comment.