From 77671f444acb640ac19ac8f7f21771d216352879 Mon Sep 17 00:00:00 2001 From: ben Date: Fri, 30 Oct 2009 15:06:59 +0000 Subject: StoreProxy ready for review --- diff --git a/setup.py b/setup.py index 9362dc7..c963d4b 100755 --- a/setup.py +++ b/setup.py @@ -93,6 +93,8 @@ setup(name='Tutorius', 'sugar.tutorius', 'sugar.tutorius.uam', 'sugar.tutorius.addons', + 'sugar.tutorius.apilib', + 'sugar.tutorius.apilib.httplib2', ], package_dir={ 'sugar.tutorius': 'tutorius', diff --git a/tests/storetests.py b/tests/storetests.py index da20c00..73ac2f7 100644 --- a/tests/storetests.py +++ b/tests/storetests.py @@ -18,7 +18,7 @@ import unittest from sugar.tutorius.store import * -g_tutorial_id = '114db454-b2a1-11de-8cfc-001f5bf747dc' +g_tutorial_id = '4075' g_other_id = '47efc6ee-b2a3-11de-8cfc-001f5bf747dc' class StoreProxyTest(unittest.TestCase): @@ -36,11 +36,6 @@ class StoreProxyTest(unittest.TestCase): def test_get_tutorials(self): self.store.get_tutorials() - def test_get_tutorial_collection(self): - collection_list = self.store.get_tutorial_collection('top5_rating') - - assert isinstance(collection_list, list), "get_tutorial_collection should return a list" - def test_get_latest_version(self): version_dict = self.store.get_latest_version([]) @@ -52,14 +47,13 @@ class StoreProxyTest(unittest.TestCase): assert tutorial is not None def test_login(self): - assert self.store.login("unknown_user", "random_password") + assert self.store.login("benoit.tremblay1@gmail.com", "tutorius12") def test_register_new_user(self): user_info = { - 'name' : "Albert", - 'last_name' : "The Tester", - 'location' : 'Mozambique', - 'email' : 'albertthetester@mozambique.org' + 'nickname' : "Albert2", + 'password' : "tutorius12", + 'email' : 'albertthetester2@mozambique.org' } assert self.store.register_new_user(user_info) @@ -68,7 +62,7 @@ class StoreProxyTest(unittest.TestCase): class StoreProxyLoginTest(unittest.TestCase): def setUp(self): self.store = StoreProxy() - self.store.login("unknown_user", "random_password") + self.store.login("benoit.tremblay1@gmail.com", "tutorius12") def tearDown(self): session_id = self.store.get_session_id() @@ -90,18 +84,39 @@ class StoreProxyLoginTest(unittest.TestCase): def test_publish(self): # TODO : We need to send in a real tutorial loaded from # the Vault - assert self.store.publish(['This should be a real tutorial...']) + tutorial_info = { + 'name': 'newtut', + 'summary': 'This is a tutorial', + 'filename': 'test.xml', + 'homepage': 'http://google.com', + 'version': '1', + 'cat1': '17', + 'cat2': '18', + 'cat3': '' + } + assert self.store.publish('This should be a real tutorial...', tutorial_info) def test_unpublish(self): # TODO : We need to send in a real tutorial loaded from # the Vault - self.store.publish([g_tutorial_id, 'Fake tutorial']) + #self.store.publish([g_tutorial_id, 'Fake tutorial']) assert self.store.unpublish(g_tutorial_id) def test_update_published_tutorial(self): # TODO : Run these tests with files from the Vault - self.store.publish([g_tutorial_id, 'Fake tutorial']) + #self.store.publish([g_tutorial_id, 'Fake tutorial']) + + tutorial_info = { + 'name': 'newtut', + 'summary': 'This is a tutorial', + 'filename': 'test.xml', + 'homepage': 'http://google.com', + 'version': '1', + 'cat1': '17', + 'cat2': '18', + 'cat3': '' + } - assert self.store.update_published_tutorial(g_tutorial_id, [g_tutorial_id, 'This is an updated tutorial']) + assert self.store.update_published_tutorial(g_tutorial_id, 'This is an updated tutorial', tutorial_info) diff --git a/tutorius/apilib/__init__.pyc b/tutorius/apilib/__init__.pyc index 5ccf259..bd4346b 100644 --- a/tutorius/apilib/__init__.pyc +++ b/tutorius/apilib/__init__.pyc Binary files differ diff --git a/tutorius/apilib/httplib2/__init__.pyc b/tutorius/apilib/httplib2/__init__.pyc index f4d8036..e5f8ebe 100644 --- a/tutorius/apilib/httplib2/__init__.pyc +++ b/tutorius/apilib/httplib2/__init__.pyc Binary files differ diff --git a/tutorius/apilib/httplib2/iri2uri.pyc b/tutorius/apilib/httplib2/iri2uri.pyc index 670ac45..879e719 100644 --- a/tutorius/apilib/httplib2/iri2uri.pyc +++ b/tutorius/apilib/httplib2/iri2uri.pyc Binary files differ diff --git a/tutorius/apilib/mimeTypes.pyc b/tutorius/apilib/mimeTypes.pyc index e9bd261..35ef2b2 100644 --- a/tutorius/apilib/mimeTypes.pyc +++ b/tutorius/apilib/mimeTypes.pyc Binary files differ diff --git a/tutorius/apilib/restful_lib.pyc b/tutorius/apilib/restful_lib.pyc index 9cf5277..5b06765 100644 --- a/tutorius/apilib/restful_lib.pyc +++ b/tutorius/apilib/restful_lib.pyc Binary files differ diff --git a/tutorius/store.py b/tutorius/store.py index 9c57ce9..d678729 100644 --- a/tutorius/store.py +++ b/tutorius/store.py @@ -18,6 +18,7 @@ import urllib import urllib2 from xml.dom import minidom from apilib.restful_lib import Connection +from array import array class StoreProxy(object): """ @@ -27,14 +28,14 @@ class StoreProxy(object): """ def __init__(self): - + # Base Urls for the api - self.base_url = "http://tutorius.dev/en-US/tutorius" + self.base_url = "http://bobthebuilder.mine.nu/tutorius/en-US/tutorius" self.remora_api = "api/1.4" self.tutorius_api = "TutoriusApi" self.bandwagon_api = "api/1.4/sharing" - self.api_auth_key = "" + self.api_auth_key = None # Prepares the connection with the api self.conn = Connection(self.base_url) @@ -67,22 +68,48 @@ class StoreProxy(object): # Loop through the categories and create the list to be returned for xml_category in xml_categories: category = {} - + category['id'] = xml_category.getElementsByTagName('id')[0].firstChild.nodeValue category['name'] = xml_category.getElementsByTagName('name')[0].firstChild.nodeValue categories.append(category) return categories + + def search(self, keywords, category='all', page=1, numResults=10, sortBy='name'): + """ + Returns a list of tutorials that correspond to the given search criteria. + + @param keywords The keywords to search for + @param page The page in the result set from which to return results. This is + used to allow applications to fetch results one set at a time. + @param numResults The max number of results that can be returned in a page + @param sortBy The field on which to sort the results + @return A list of tutorial meta-data that corresponds to the query + """ + request_url = "/%s/search/%s/%s/%d/%d/%s" % (self.tutorius_api, keywords, category, page, numResults, sortBy) + + response = self.conn.request_get(request_url) + + if (self.helper.iserror(response)): + return False + + xml_response = minidom.parseString(response['body']) + + xml_tutorials = xml_response.getElementsByTagName('tutorial') + + tutorials = list() + + for xml_tutorial in xml_tutorials: + tutorial = self.helper.parse_tutorial(xml_tutorial) + tutorials.append(tutorial) + return tutorials def get_tutorials(self, category='all', page=1, numResults=10, sortBy='name'): """ Returns the list of tutorials that correspond to the given search criteria. - @param keywords The list of keywords that should be matched inside the tutorial title - or description. If None, the search will not filter the results - according to the keywords. @param category The category in which to restrict the search. @param page The page in the result set from which to return results. This is used to allow applications to fetch results one set at a time. @@ -138,17 +165,6 @@ class StoreProxy(object): return tutorials - def get_tutorial_collection(self, collection_name): - """ - Returns a list of tutorials corresponding to the given collection name. - Collections can be groups like '5 most downloaded' or 'Top 10 ratings'. - - @param collection_name The name of the collection from which we want the - meta-data - @return A list of tutorial meta-data corresponding to the given group - """ - raise NotImplementedError("get_tutorial_collection() not implemented... yet!") - def get_latest_version(self, tutorial_id_list): """ Returns the latest version number on the server, for each tutorial ID @@ -325,7 +341,7 @@ class StoreProxy(object): return True - + def unpublish(self, tutorial_store_id): """ Removes a tutorial from the server. The user in the current session @@ -433,12 +449,22 @@ class StoreProxyHelper(object): @return A dictionnary containing the metadata """ tutorial = {} - - tutorial['name'] = xml_tutorial.getElementsByTagName('name')[0].firstChild.nodeValue - tutorial['summary'] = xml_tutorial.getElementsByTagName('summary')[0].firstChild.nodeValue - tutorial['version'] = xml_tutorial.getElementsByTagName('version')[0].firstChild.nodeValue - tutorial['description'] ="" - tutorial['author'] = "" - tutorial['rating'] = "" + + params = [ + 'name', + 'summary', + 'version', + 'description', + 'author', + 'rating' + ] + + for param in params: + xml_node = xml_tutorial.getElementsByTagName(param)[0].firstChild + + if xml_node != None: + tutorial[param] = xml_node.nodeValue + else: + tutorial[param] = '' return tutorial \ No newline at end of file -- cgit v0.9.1