def test_find_one(self): doc = JsonObject() doc.xyz = "xyzvalue" updated_doc = self.test_db.update_doc(doc.to_json_map()) logging.debug(updated_doc) found_doc = self.test_db.find_one(find_filter={"xyz": "xyzvalue"}) self.assertTrue(JsonObject.make_from_dict(updated_doc).equals_ignore_id(JsonObject.make_from_dict(found_doc)))
def from_path(cls, path, db_interface): book_portion_dict = db_interface.find_one(find_filter={"path": path}) if book_portion_dict is None: return None else: book_portion = JsonObject.make_from_dict(book_portion_dict) return book_portion
def from_path(cls, path, my_collection): book_portion_dict = my_collection.find_one(find_filter={"path": path}) if book_portion_dict is None: return None else: book_portion = JsonObject.make_from_dict(book_portion_dict) return book_portion
def is_user_admin(): user = JsonObject.make_from_dict(session.get('user', None)) logging.debug(session.get('user', None)) logging.debug(session) logging.debug(user) if user is None or not user.check_permission(service="users", action="admin"): return False else: return True
def get(self): """ Get current user details. PS: Login with <a href="v1/oauth_login/google" target="new">google oauth in a new tab</a>. """ session_user = JsonObject.make_from_dict(session.get('user', None)) if session_user is None: return {"message": "No user found, not authorized!"}, 401 else: return [session_user.to_json_map()], 200
def delete(self, id): """Delete a user. PS: Login with <a href="v1/oauth_login/google" target="new">google oauth in a new tab</a>. """ matching_user = get_db().find_by_id(id=id) if matching_user is None: return {"message": "User not found!"}, 404 session_user = JsonObject.make_from_dict(session.get('user', None)) logging.info(str(request.json)) if not is_user_admin() and (session_user is None or session_user._id != matching_user._id): return {"message": "Unauthorized!"}, 401 matching_user.delete_in_collection(db_interface=get_db()) return {}, 200
def post(self, id): """Modify a user. PS: Login with <a href="v1/oauth_login/google" target="new">google oauth in a new tab</a>. """ matching_user = get_db().find_by_id(id=id) if matching_user is None: return {"message": "User not found!"}, 404 session_user = JsonObject.make_from_dict(session.get('user', None)) logging.info(str(request.json)) if not is_user_admin() and (session_user is None or session_user._id != matching_user._id): return {"message": "Unauthorized!"}, 401 user = common_data_containers.JsonObject.make_from_dict(request.json) if not isinstance(user, User): return { "message": "Input JSON object does not conform to User.schema: " + User.schema }, 417 # Check to see if there are other entries in the database with identical authentication info. matching_users = get_db().get_matching_users_by_auth_infos(user=user) if len(matching_users) > 1: logging.warning(str(matching_users)) return { "message": "Another object with matching info already exists. Please delete it first.", "another_matching_user": str(matching_users) }, 409 try: user.update_collection(db_interface=get_db()) except ValidationError as e: import traceback message = { "message": "Some input object does not fit the schema.", "exception_dump": (traceback.format_exc()) } return message, 417 return user.to_json_map(), 200
def get(self, id): """Just get the user info. PS: Login with <a href="v1/oauth_login/google" target="new">google oauth in a new tab</a>. :param id: String :return: A User object. """ matching_user = get_db().find_by_id(id=id) if matching_user is None: return {"message": "User not found!"}, 404 session_user = JsonObject.make_from_dict(session.get('user', None)) if not is_user_admin() and (session_user is None or session_user._id != matching_user._id): return {"message": "User is not an admin!"}, 401 return matching_user, 200
def post(self, id): """Modify a user. PS: Login with <a href="v1/oauth_login/google" target="new">google oauth in a new tab</a>. """ matching_user = get_db().find_by_id(id=id) if matching_user is None: return {"message": "User not found!"}, 404 session_user = JsonObject.make_from_dict(session.get('user', None)) logging.info(str(request.json)) if not is_user_admin() and (session_user is None or session_user._id != matching_user._id): return {"message": "Unauthorized!"}, 401 user = common_data_containers.JsonObject.make_from_dict(request.json) if not isinstance(user, User): return {"message": "Input JSON object does not conform to User.schema: " + User.schema}, 417 # Check to see if there are other entries in the database with identical authentication info. matching_users = get_db().get_matching_users_by_auth_infos(user=user) if len(matching_users) > 1: logging.warning(str(matching_users)) return {"message": "Another object with matching info already exists. Please delete it first.", "another_matching_user": str(matching_users) }, 409 try: user.update_collection(db_interface=get_db()) except ValidationError as e: import traceback message = { "message": "Some input object does not fit the schema.", "exception_dump": (traceback.format_exc()) } return message, 417 return user.to_json_map(), 200
def main(argv): Parms = DotDict({ 'reset' : False, 'dbgFlag' : True, 'server_baseurl' : '', 'auth' : DotDict({'user' : 'vedavaapiAdmin', 'passwd' : '@utoDump1'}), 'dbname' : 'ullekhanam_test' }) try: opts, args = getopt.getopt(argv, "hru:d:s:", ["url="]) except getopt.GetoptError: logging.info("Error in command line: ", getopt.GetoptError) usage() for opt, arg in opts: if opt == '-h': usage() elif opt in ("-r", "--reset"): Parms.reset = True elif opt in ("-u", "--auth"): Parms.auth = DotDict(dict(zip(('user', 'passwd'), arg.split(':')))) print Parms.auth elif opt in ("-d", "--db"): Parms.dbname = arg elif opt in ("-s", "--serverurl"): logging.info("server url = ", arg) Parms.server_baseurl = arg else: logging.info("Unknown parameter: ", opt) usage() if not Parms.server_baseurl: logging.info("Error: Supply server URL via -s.") usage() if not args: logging.info("Error: Missing book path to import...") usage() vvclient = VedavaapiClient(Parms.server_baseurl) if not vvclient.authenticate(Parms.auth): sys.exit(1) for path in args: for book in import_books(path): pages = [] for page in book.children: pages.append(('in_files', \ open(os.path.join(book.content.path, page.content.path), 'rb'))) #print_dict(book.content.to_json_map()) r = vvclient.post("ullekhanam/v1/dbs/{}/books".format(Parms.dbname), parms={'book_json' : json.dumps(book.content.to_json_map())}, files=pages) if not r: sys.exit(1) book_json = json.loads(r.text) book = JsonObject.make_from_dict(book_json["content"]) #print_dict(book_json['content']) url = "ullekhanam/v1/dbs/{}/entities/{}".format(Parms.dbname, book._id) r = vvclient.get(url, {'depth' : 1}) if not r: logging.error("Error: invoking {}".format(url)) continue book_info = r.json() for p in book_info['children']: logging.info("page id " + p['content']['title']) page_id = p['content']['_id'] url = "ullekhanam/v1/dbs/{}/pages/{}/annotations".format(Parms.dbname, page_id) r = vvclient.get(url) if not r: sys.exit(1) print_dict(r.json())
def main(argv): parms = DotDict({ 'reset': False, 'dbgFlag': True, 'server_baseurl': '', 'auth': DotDict({ 'user': '******', 'passwd': '@utoDump1' }), 'repo_id': 'vedavaapi_test' }) try: opts, args = getopt.getopt(argv, "hru:i:s:", ["url="]) except getopt.GetoptError: logging.info("Error in command line: ", getopt.GetoptError) usage() for opt, arg in opts: if opt == '-h': usage() elif opt in ("-r", "--reset"): parms.reset = True elif opt in ("-u", "--auth"): parms.auth = DotDict(dict(zip(('user', 'passwd'), arg.split(':')))) print(parms.auth) elif opt in ("-i", "--repo_id"): parms.repo_id = arg elif opt in ("-s", "--serverurl"): logging.info("server url = ", arg) parms.server_baseurl = arg else: logging.info("Unknown parameter: ", opt) usage() if not parms.server_baseurl: logging.info("Error: Supply server URL via -s.") usage() if not args: logging.info("Error: Missing book path to import...") usage() vvclient = VedavaapiClient(parms.server_baseurl, parms.repo_id) if not vvclient.authenticate(parms.auth): sys.exit(1) for path in args: for book in import_books(path): pages = [] for page in book.children: pages.append( ('in_files', open(os.path.join(book.content.path, page.content.path), 'rb'))) #print_dict(book.content.to_json_map()) r = vvclient.post( "ullekhanam/v1/books", parms={'book_json': json.dumps(book.content.to_json_map())}, files=pages) if not r: sys.exit(1) book_json = json.loads(r.text) book = JsonObject.make_from_dict(book_json["content"]) #print_dict(book_json['content']) url = "ullekhanam/v1/entities/{}".format(book._id) r = vvclient.get(url, {'depth': 1}) if not r: logging.error("Error: invoking {}".format(url)) continue book_info = r.json() for p in book_info['children']: logging.info("page id " + p['content']['title']) page_id = p['content']['_id'] url = "ullekhanam/v1/pages/{}/annotations".format(page_id) r = vvclient.get(url) if not r: sys.exit(1) print_dict(r.json())
def find(self, find_filter): for index, key in enumerate(self.db): if JsonObject.make_from_dict( self.db[key]).match_filter(find_filter=find_filter): yield self.db[key]
def get_user(): from flask import session return JsonObject.make_from_dict(session.get('user', None))
def get_user(): from flask import session from sanskrit_data.schema.common import JsonObject return JsonObject.make_from_dict(session.get('user', None))