def update(user_id=None, email=None, lan=None, language=None, image=None, password=None, status=None, unset=None, pull=None, addToSet=None, user=None): """ Update one or more users """ user_id = ensure_objectid(user_id) if user_id is None: return False if user: db.users.update({"_id": user_id}, user) return True if pull: db.users.update({"_id": user_id}, {"$pull": pull }) return True if addToSet: db.users.update({"_id": user_id}, {"$addToSet": addToSet }) return True # First, builds the filter conditions list dict_set = {} local = locals() for item in ["email", "lan", "language", "image", "password", "status"]: if not local[item] is None: dict_set[item] = local[item] if not unset is None: dict_unset = {} for item in unset: dict_unset[item] = 1 if is_iterable(user_id): for _id in user_id: if ensure_objectid(_id): if unset: db.users.update({"_id": _id}, {"$unset": dict_unset}, False) db.users.update({"_id": _id}, {"$set": dict_set}) else: if unset: db.users.update({"_id": user_id}, {"$unset": dict_unset}, False) db.users.update({"_id": user_id}, {"$set": dict_set}) db.users.ensure_index('username') return True
def update(rank_id=None, name=None, rank=None): """ """ error_code = None if ensure_objectid(rank_id) is None: error_code = ('rank_msg', 'error_0') elif name is None or len(name) == 0: error_code = ('rank_msg', 'error_1') elif rank is None: error_code = ('rank_msg', 'error_2') elif check.is_integer(rank) == False: error_code = ('rank_msg', 'error_3') else: rank = int(rank) result = model.ranks.update(rank_id=rank_id, name=name, rank=rank) if result: data = { "success": True, } return data error_code = ('rank_msg', 'error_4') data = { "success": False, "errors": [{ "code": error_code }] } return data
def remove(page_id=None): """ """ if page_id is None: return False else: db.pages.remove({'_id' : ensure_objectid(page_id)}) return True
def update(page_id=None, page=None): """ """ if page_id is None: return False if page: db.pages.update({'_id': ensure_objectid(page_id)}, page) return page_id
def remove(page_id=None): """ """ if page_id is None: return False else: db.pages.remove({'_id': ensure_objectid(page_id)}) return True
def get(): """ """ _id = request.args.get("_id", None) page = g.db.pages.find_one({ "_id": ensure_objectid(_id) }) data = { "success": True, "page": page } return jsonify(data)
def __request_second_block(self): """ """ form = self.params old_url = self.page['url'] self.page['url'] = {} self.page['title'] = {} self.page['description'] = {} for i in range(10): key = 'url_%s' % i if key in self.page: del(self.page[key]) self.page['url'] = form['url'] self.page['title'] = form['title'] self.page['description'] = form['description'] # Get URL, Title and Description in any languages for code in self.languages: if self.message is None: error_in = ' ( ' + code + ' )' # If the url is changed if old_url[code] != self.page['url'][code]: url_list = self.__get_url_list(code) num_urls = len(url_list) for code_two in self.languages: field = "url_{}.{}".format(num_urls, code_two) page_id = ensure_objectid(self.page["_id"]) if "_id" in self.page else None available_url = g.db.pages.find_one({ field: url_list, "_id": { "$ne": page_id } }) print available_url # Check that the url is a maximum of 200 characters if not check.length(self.page['url'][code], 0, 200): self.message = g.pages_msg('error_b_2') + error_in # Verify that the format of the url is correct elif len(self.page['url'][code]) and not check.url_two(self.page['url'][code]): print 3, self.page['url'][code] self.message = g.pages_msg('error_b_3') + error_in # Raises an error message if url is not available. elif not available_url is None: self.message = g.pages_msg('error_b_4') + error_in else: url_list = self.__get_url_list(code) num_urls = len(url_list) if not self.message: kind_of_url = 'url_{}'.format(num_urls) if not kind_of_url in self.page: self.page[kind_of_url] = {} self.page[kind_of_url][code] = url_list
def remove(self): """ Remove page from the database """ # It checks page _id exist and that # you have permission to remove that page if g.my['rank'] < 15: try: g.db.pages.remove({ '_id' : ensure_objectid(self.page["_id"]) }) return 'ok' except PyMongoError: return 'nada' return 'nada'
def check_verify_remember(verify): """ """ str_verify = verify.split('_') if len(str_verify) != 3: return None _id = str_verify[2] user = g.db.users.find_one({ '_id' : ensure_objectid(_id) }) if user and user['remember_verify'] == verify: return user else: return None
def check_verify_remember(verify): """ """ str_verify = verify.split('_') if len(str_verify) != 3: return None _id = str_verify[2] user = g.db.users.find_one({'_id': ensure_objectid(_id)}) if user and user['remember_verify'] == verify: return user else: return None
def update(language_id=None, value=None, check=None): """ """ if language_id is None: return False dict_set = {} if value: dict_set["value"] = value if check: dict_set["check"] = check set_language = {'$set': dict_set} db.languages.update({'_id': ensure_objectid(language_id)}, set_language) return language_id
def get(page_id=None): """ """ if page_id is None: errors = [{"message": "Page id required"}] elif not ensure_objectid(page_id): errors = [{"message": "Bad page id"}] else: page = model.pages.find(page_id=page_id) if page: return dict(success=True, page=page) else: errors = [{"message": "Bad page id"}] return dict(success=False, errors=errors)
def update(rank_id=None, name=None, rank=None): """ """ if rank_id is None: return False dict_set = {} if name: dict_set["name"] = name if rank: dict_set["rank"] = rank set_rank = {'$set': dict_set} db.ranks.update({'_id': ensure_objectid(rank_id)}, set_rank) return rank_id
def get(page_id=None): """ """ if page_id is None: errors = [{ "message": "Page id required" }] elif not ensure_objectid(page_id): errors = [{ "message": "Bad page id" }] else: page = model.pages.find(page_id=page_id) if page: return dict(success=True, page=page) else: errors = [{ "message": "Bad page id" }] return dict(success=False, errors=errors)
def remove(rank_id=None, my_rank=None): """ """ if my_rank is None or my_rank > 10: return False if rank_id is None: return False else: rank = model.ranks.find(rank_id=rank_id, expand_number=True) if rank is None: return False if rank["number_user"] != 0: return False db.ranks.remove({'_id' : ensure_objectid(rank_id)}) return True
def remove(rank_id=None, my_rank=None): """ """ if my_rank is None or my_rank > 10: return False if rank_id is None: return False else: rank = model.ranks.find(rank_id=rank_id, expand_number=True) if rank is None: return False if rank["number_user"] != 0: return False db.ranks.remove({'_id': ensure_objectid(rank_id)}) return True
def change_email(check): """ """ user = check_verify_email(check) if user and "new_email" in user: user['email'] = user['new_email'] del(user['new_email']) del(user['email_verify']) g.db.users.update({ '_id' : ensure_objectid(user['_id']) }, user) message = g.settings_msg('success_update_email') status = 'msg msg-success' else: message = g.settings_msg('error_update_email') status = 'msg msg-error' return render_template('{}/change_email.html'.format(MODULE_DIR), **locals())
def update(rank_id=None, name=None, rank=None): """ """ if rank_id is None: return False dict_set = {} if name: dict_set["name"] = name if rank: dict_set["rank"] = rank set_rank = { '$set': dict_set } db.ranks.update({'_id': ensure_objectid(rank_id)}, set_rank) return rank_id
def update(language_id=None, value=None, check=None): """ """ if language_id is None: return False dict_set = {} if value: dict_set["value"] = value if check: dict_set["check"] = check set_language = { '$set': dict_set } db.languages.update({'_id': ensure_objectid(language_id)}, set_language) return language_id
def check_verify_email(verify, is_email=False): """ """ str_verify = verify.split('_') if len(str_verify) != 2: return None if is_email: email = str_verify[1] user = g.db.users.find_one({'email': email}) else: _id = str_verify[1] user = g.db.users.find_one({'_id': ensure_objectid(_id)}) if user and "email_verify" in user and user['email_verify'] == verify: return user else: return None
def check_verify_email(verify, is_email=False): """ """ str_verify = verify.split('_') if len(str_verify) != 2: return None if is_email: email = str_verify[1] user = g.db.users.find_one({ 'email' : email }) else: _id = str_verify[1] user = g.db.users.find_one({ '_id' : ensure_objectid(_id) }) if user and "email_verify" in user and user['email_verify'] == verify: return user else: return None
def remove(self): """ Remove hash map """ list_main_id = [ '4f2b3e3918429f1b86000016', # '4f2b3e3918429f1b86000018', # '4f2b3e3918429f1b86000017', # '4f2b3e3918429f1b8600001a', # '4f44fd7218429f265b000000', # '4f37da6b18429f3ec3000000', # '4f2b3e3918429f1b86000019' #users ] if str(self.hash_map["_id"]) in list_main_id: return False else: try: g.db.hash_table.remove({ '_id' : ensure_objectid(self.hash_map["_id"]) }) return True except PyMongoError: return False
def get(user_id=None, my_rank=None, my_id=None): """ By passing a user id, return an object with the user info. That object could be different in base of different rank permission. :param user_id: user id :returns: an object with all the user information """ if user_id is None: errors = [{ "message": "User id required" }] elif not ensure_objectid(user_id): errors = [{ "message": "Bad user id" }] else: user = model.users.find(user_id=user_id, my_rank=my_rank, my_id=my_id) if user: return dict(success=True, user=user) else: errors = [{ "message": "Bad user id" }] return dict(success=False, errors=errors)
def get(user_id=None, my_rank=None, my_id=None): """ By passing a user id, return an object with the user info. That object could be different in base of different rank permission. :param user_id: user id :returns: an object with all the user information """ if user_id is None: errors = [{"message": "User id required"}] elif not ensure_objectid(user_id): errors = [{"message": "Bad user id"}] else: user = model.users.find(user_id=user_id, my_rank=my_rank, my_id=my_id) if user: return dict(success=True, user=user) else: errors = [{"message": "Bad user id"}] return dict(success=False, errors=errors)
def update(rank_id=None, name=None, rank=None): """ """ error_code = None if ensure_objectid(rank_id) is None: error_code = ('rank_msg', 'error_0') elif name is None or len(name) == 0: error_code = ('rank_msg', 'error_1') elif rank is None: error_code = ('rank_msg', 'error_2') elif check.is_integer(rank) == False: error_code = ('rank_msg', 'error_3') else: rank = int(rank) result = model.ranks.update(rank_id=rank_id, name=name, rank=rank) if result: data = { "success": True, } return data error_code = ('rank_msg', 'error_4') data = {"success": False, "errors": [{"code": error_code}]} return data
def find(page_id=None, name=None, url=None, field=None, field_value=None, page_id_ne=None, sorted_by=None, sort_ascending=True, limit=None, only_one=False, skip=None, count=None): """ """ conditions = [] if url: conditions.append({'url': url}) if name: conditions.append({'name': name}) if field: conditions.append({field: field_value}) if page_id_ne: conditions.append({'_id': {"$ne": ensure_objectid(page_id_ne)}}) return db_engine(collection=db.pages, item_id=page_id, conditions=conditions, only_one=only_one, sorted_by=sorted_by, sort_ascending=sort_ascending, limit=limit, skip=skip, count=count)
def find(page_id=None, name=None, url=None, field=None, field_value=None, page_id_ne=None, sorted_by=None, sort_ascending=True, limit=None, only_one=False, skip=None, count=None): """ """ conditions = [] if url: conditions.append({'url': url}) if name: conditions.append({'name': name}) if field: conditions.append({field: field_value}) if page_id_ne: conditions.append({'_id': { "$ne": ensure_objectid(page_id_ne) }}) return db_engine(collection=db.pages, item_id=page_id, conditions=conditions, only_one=only_one, sorted_by=sorted_by, sort_ascending=sort_ascending, limit=limit, skip=skip, count=count)
def update(hash_table_id=None, hash_map=None): """ """ db.hash_table.update({"_id": ensure_objectid(hash_table_id)}, hash_map)
def __request_second_block(self): """ """ form = self.params old_url = self.page['url'] self.page['url'] = {} self.page['title'] = {} self.page['description'] = {} for i in range(10): key = 'url_%s' % i if key in self.page: del(self.page[key]) self.page['url'] = form['url'] self.page['title'] = form['title'] self.page['description'] = form['description'] # Get URL, Title and Description in any languages for code in self.languages: self.page['url'][code] = self.page['url'].get(code, '') self.page['title'][code] = self.page['url'].get(code, '') self.page['description'][code] = self.page['url'].get(code, '') if self.message is None: error_in = ' ( ' + code + ' )' # If the url is changed if old_url.get(code) != self.page['url'][code]: url_list = self.__get_url_list(code) num_urls = len(url_list) for code_two in self.languages: field = "url_{}.{}".format(num_urls, code_two) page_id = ensure_objectid(self.page["_id"]) if "_id" in self.page else None available_url = model.pages.find(field=field, field_value=url_list, page_id_ne=page_id, only_one=True) print available_url # Check that the url is a maximum of 200 characters if not check.length(self.page['url'][code], 0, 200): self.message = g.pages_msg('error_b_2') + error_in # Verify that the format of the url is correct elif len(self.page['url'][code]) and not check.url_two(self.page['url'][code]): print 3, self.page['url'][code] self.message = g.pages_msg('error_b_3') + error_in # Raises an error message if url is not available. elif not available_url is None: name_page = available_url['name'] error_where = '{0} in the "{1}" page'.format(error_in, name_page) self.message = g.pages_msg('error_b_4') + error_where else: url_list = self.__get_url_list(code) num_urls = len(url_list) if self.message is None: kind_of_url = 'url_{}'.format(num_urls) if not kind_of_url in self.page: self.page[kind_of_url] = {} self.page[kind_of_url][code] = url_list
def remove(hash_table_id=None): """ """ db.hash_table.remove({'_id': ensure_objectid(hash_map_id)})
def db_engine(collection=None, item_id=None, only_one=False, conditions=None, sorted_by=None, sort_lang=None, sort_ascending=True, skip=None, limit=None, count=None, denormalize=denormalize): """ MongoDB Engine of Bombolone There are several main steps that every model need it. - Looking specifically for one or more items? No further filtering needed! - Looking for one user only or more - Queries the collection conditions - Sorts the filtered query results, if they're more than one - The skip() expression allows to implementing "paging" - Limit the maximum number of results to return. For best performance, use limit() whenever possible. Otherwise, the database may return more objects than are required for processing. - Count the items :return a number, when count is True :return a dictionary, when only_one is True :return a list, when only_one is False """ if item_id: if is_iterable(item_id): list_items = collection.find( {"_id": { "$in": [ensure_objectid(x) for x in item_id] }}) list_items = sort_if_you_must(list_items, only_one, sorted_by, sort_lang, sort_ascending) return [denormalize(item) for item in list_items] else: return denormalize( collection.find_one({"_id": ensure_objectid(item_id)})) if only_one: f = collection.find_one else: f = collection.find if conditions: items = f({'$and': conditions}) else: items = f() items = sort_if_you_must(items, only_one, sorted_by, sort_lang, sort_ascending) if skip: items = items.skip(skip * limit) if only_one == False and limit: items = items.limit(limit) if count: return items.count() if only_one: return denormalize(items) else: list_items = list(items) return [denormalize(item) for item in list_items]
def hash_table_get(_id=None): """ """ hash_map = g.db.hash_table.find_one({"_id": ensure_objectid(_id)}) return hash_map
def remove(hash_table_id=None): """ """ db.hash_table.remove({"_id": ensure_objectid(hash_map_id)})
def __request_second_block(self): """ """ form = self.params old_url = self.page['url'] self.page['url'] = {} self.page['title'] = {} self.page['description'] = {} for i in range(10): key = 'url_%s' % i if key in self.page: del (self.page[key]) self.page['url'] = form['url'] self.page['title'] = form['title'] self.page['description'] = form['description'] # Get URL, Title and Description in any languages for code in self.languages: self.page['url'][code] = self.page['url'].get(code, '') self.page['title'][code] = self.page['url'].get(code, '') self.page['description'][code] = self.page['url'].get(code, '') if self.message is None: error_in = ' ( ' + code + ' )' # If the url is changed if old_url.get(code) != self.page['url'][code]: url_list = self.__get_url_list(code) num_urls = len(url_list) for code_two in self.languages: field = "url_{}.{}".format(num_urls, code_two) page_id = ensure_objectid( self.page["_id"]) if "_id" in self.page else None available_url = model.pages.find(field=field, field_value=url_list, page_id_ne=page_id, only_one=True) print available_url # Check that the url is a maximum of 200 characters if not check.length(self.page['url'][code], 0, 200): self.message = g.pages_msg('error_b_2') + error_in # Verify that the format of the url is correct elif len(self.page['url'][code]) and not check.url_two( self.page['url'][code]): print 3, self.page['url'][code] self.message = g.pages_msg('error_b_3') + error_in # Raises an error message if url is not available. elif not available_url is None: name_page = available_url['name'] error_where = '{0} in the "{1}" page'.format( error_in, name_page) self.message = g.pages_msg('error_b_4') + error_where else: url_list = self.__get_url_list(code) num_urls = len(url_list) if self.message is None: kind_of_url = 'url_{}'.format(num_urls) if not kind_of_url in self.page: self.page[kind_of_url] = {} self.page[kind_of_url][code] = url_list
def find(user_id=None, username=None, sort_ascending=True, only_one=False): """ Returns a list of users or a single user, if user_id or only_one are specified. user_id: a single user identifier (a string or an ObjectId) or a list of them username: the unique user's name sort_ascending: if True, sorts the results from first to last, if False sorts them the other way only_one: if True, returns one tag at most """ def denormalize(user): if not user: return user if len(user["image"]): user["image_show"] = "/static/avatars/{}/{}".format( user["_id"], user["image"][2]) else: user["image_show"] = "/static/avatars/default.jpg" return user # Looking specifically for one or more users? # No further filtering needed! if user_id: if is_iterable(user_id): list_users = list( db.users.find( {"_id": { "$in": [ensure_objectid(x) for x in user_id] }})) return [denormalize(u) for u in list_users] else: return denormalize( db.users.find_one({"_id": ensure_objectid(user_id)})) if username: if is_iterable(username): list_users = list( db.users.find({"username": { "$in": list(username) }})) return [denormalize(u) for u in list_users] else: regex = re.compile('^' + username + '$', re.IGNORECASE) return denormalize(db.users.find_one({"username": regex})) # First, builds the filter conditions list conditions = [{'rank': 80}] # TODO: what is rank 80? # Looking for one user only or more? if only_one: f = db.users.find_one else: f = db.users.find # Queries the users if conditions: users = f({'$and': conditions}) # And, by default else: users = f() # Sorts the filtered query results, if they're more than one if not only_one: users = users.sort('username', sort_ascending and ASCENDING or DESCENDING) if only_one: return denormalize(users) # A dictionary else: list_users = list(users) return [denormalize(u) for u in list_users] # A list
def update(hash_table_id=None, hash_map=None): """ """ db.hash_table.update({'_id': ensure_objectid(hash_table_id)}, hash_map)
def get_user(self, _id): """ Get the user document from Database """ _id = ensure_objectid(_id) self.user = model.users.find(user_id=_id, my_id=_id)
def find(user_id=None, username=None, sort_ascending=True, only_one=False): """ Returns a list of users or a single user, if user_id or only_one are specified. user_id: a single user identifier (a string or an ObjectId) or a list of them username: the unique user's name sort_ascending: if True, sorts the results from first to last, if False sorts them the other way only_one: if True, returns one tag at most """ def denormalize(user): if not user: return user if len(user["image"]): user["image_show"] = "/static/avatars/{}/{}".format(user["_id"], user["image"][2]) else: user["image_show"] = "/static/avatars/default.jpg" return user # Looking specifically for one or more users? # No further filtering needed! if user_id: if is_iterable(user_id): list_users = list(db.users.find({"_id" : {"$in": [ensure_objectid(x) for x in user_id]}})) return [ denormalize(u) for u in list_users ] else: return denormalize(db.users.find_one({"_id" : ensure_objectid(user_id)})) if username: if is_iterable(username): list_users = list(db.users.find({"username" : {"$in": list(username)}})) return [ denormalize(u) for u in list_users ] else: regex = re.compile('^'+username+'$', re.IGNORECASE) return denormalize(db.users.find_one({"username" : regex})) # First, builds the filter conditions list conditions = [{'rank': 80}] # TODO: what is rank 80? # Looking for one user only or more? if only_one: f = db.users.find_one else: f = db.users.find # Queries the users if conditions: users = f({'$and': conditions}) # And, by default else: users = f() # Sorts the filtered query results, if they're more than one if not only_one: users = users.sort('username', sort_ascending and ASCENDING or DESCENDING) if only_one: return denormalize(users) # A dictionary else: list_users = list(users) return [ denormalize(u) for u in list_users ] # A list
def hash_table_get(_id=None): """ """ hash_map = g.db.hash_table.find_one({ "_id": ensure_objectid(_id) }) return hash_map
def get(): """ """ _id = request.args.get("_id", None) page = g.db.pages.find_one({"_id": ensure_objectid(_id)}) data = {"success": True, "page": page} return jsonify(data)
def db_engine(collection=None, item_id=None, only_one=False, conditions=None, sorted_by=None, sort_lang=None, sort_ascending=True, skip=None, limit=None, count=None, denormalize=denormalize): """ MongoDB Engine of Bombolone There are several main steps that every model need it. - Looking specifically for one or more items? No further filtering needed! - Looking for one user only or more - Queries the collection conditions - Sorts the filtered query results, if they're more than one - The skip() expression allows to implementing "paging" - Limit the maximum number of results to return. For best performance, use limit() whenever possible. Otherwise, the database may return more objects than are required for processing. - Count the items :return a number, when count is True :return a dictionary, when only_one is True :return a list, when only_one is False """ if item_id: if is_iterable(item_id): list_items = collection.find({"_id" : {"$in": [ensure_objectid(x) for x in item_id]}}) list_items = sort_if_you_must(list_items, only_one, sorted_by, sort_lang, sort_ascending) return [ denormalize(item) for item in list_items] else: return denormalize(collection.find_one({"_id" : ensure_objectid(item_id)})) if only_one: f = collection.find_one else: f = collection.find if conditions: items = f({'$and': conditions}) else: items = f() items = sort_if_you_must(items, only_one, sorted_by, sort_lang, sort_ascending) if skip: items = items.skip(skip * limit) if only_one == False and limit: items = items.limit(limit) if count: return items.count() if only_one: return denormalize(items) else: list_items = list(items) return [ denormalize(item) for item in list_items ]