Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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 ]
Ejemplo n.º 4
0
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]
Ejemplo n.º 5
0
def find(user_id=None,
         username=None,
         email=None,
         rank=None,
         lan=None,
         expand_rank=False,
         sorted_by='username',
         sort_ascending=True,
         only_one=False,
         my_rank=None,
         my_id=None):
    """
    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 user is None:
            return user

        if expand_rank:
            user['rank_name'] = { x['rank'] : x['name'] for x in model.ranks.find() }[user['rank']]

        # Data we want to show to our Soft Eng or the private user
        if isinstance(my_rank, int) and my_rank <= 70:
            return user

        # Data we want to show to our private user
        if str(my_id) == str(user["_id"]):
            return user

        # Data we want to show after sign in, to all
        user_to_show = {
            "_id" : user.get("_id", None),
            "rank": user.get("rank", None),
            "description": user.get("description", ""),
            "image": user.get("image", ""),
            "location": user.get("location", ""),
            "name": user.get("name", ""),
            "username": user.get("username", ""),
            "web": user.get("web", "")
        }
        return user_to_show

    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 = []

    if email:
        email = email.lower()
        conditions.append({'email': email})

    if rank:
        conditions.append({'rank': rank})

    if lan:
        conditions.append({'lan': lan})

    return db_engine(collection=db.users, 
                     item_id=user_id,
                     only_one=only_one, 
                     conditions=conditions,
                     sorted_by=sorted_by,
                     sort_ascending=sort_ascending,
                     denormalize=denormalize)
Ejemplo n.º 6
0
def find(user_id=None,
         username=None,
         email=None,
         rank=None,
         lan=None,
         expand_rank=False,
         sorted_by='username',
         sort_ascending=True,
         only_one=False,
         my_rank=None,
         my_id=None):
    """
    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 user is None:
            return user

        if expand_rank:
            user['rank_name'] = {
                x['rank']: x['name']
                for x in model.ranks.find()
            }[user['rank']]

        # Data we want to show to our Soft Eng or the private user
        if isinstance(my_rank, int) and my_rank <= 70:
            return user

        # Data we want to show to our private user
        if str(my_id) == str(user["_id"]):
            return user

        # Data we want to show after sign in, to all
        user_to_show = {
            "_id": user.get("_id", None),
            "rank": user.get("rank", None),
            "description": user.get("description", ""),
            "image": user.get("image", ""),
            "location": user.get("location", ""),
            "name": user.get("name", ""),
            "username": user.get("username", ""),
            "web": user.get("web", "")
        }
        return user_to_show

    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 = []

    if email:
        email = email.lower()
        conditions.append({'email': email})

    if rank:
        conditions.append({'rank': rank})

    if lan:
        conditions.append({'lan': lan})

    return db_engine(collection=db.users,
                     item_id=user_id,
                     only_one=only_one,
                     conditions=conditions,
                     sorted_by=sorted_by,
                     sort_ascending=sort_ascending,
                     denormalize=denormalize)