예제 #1
0
파일: news.py 프로젝트: sprymak/yolo-news
def delete_post(val):
    """ Delete news post.

        :param val: ``Post`` object instance or primary key
    """
    post = val
    if isinstance(post, (int, long, basestring)):
        post = get_object_or_none(models.Post, pk=post)
    if isinstance(post, models.Post):
        post.delete()
예제 #2
0
파일: news.py 프로젝트: sprymak/yolo-news
def get_post_data(val):
    """ Construct the composite Transfer Object for ``Post`` model object.

        :param val: ``Post`` object instance or primary key
    """
    post = val
    if isinstance(post, (int, long, basestring)):
        post = get_object_or_none(models.Post, pk=post)
    from utils.django import get_object_dto

    return get_object_dto(post)
예제 #3
0
파일: news.py 프로젝트: sprymak/yolo-news
def recall_post(val):
    """ Recall published news post.
        Note: ``save`` method will be called first if post has not been
            saved yet.
        :param val: ``Post`` object instance or primary key
    """
    post = val
    if isinstance(post, (int, long, basestring)):
        post = get_object_or_none(models.Post, pk=post)
    if isinstance(post, models.Post):
        post.recall()
예제 #4
0
파일: news.py 프로젝트: sprymak/yolo-news
def update_post(val, data):
    """ Update existing ``Post`` object with data passed in Transfer Object and
        validate instance values.
        This function does not call ``save`` method on the object.

        :param val: ``Post`` object instance or primary key
    """
    from utils.django import update_object_from_dto

    post = val
    if isinstance(post, (int, long, basestring)):
        post = get_object_or_none(models.Post, pk=post)
    if isinstance(post, models.Post):
        update_object_from_dto(post, data, partial=True)
        # TODO(sprymak): validate new instance values
        return post
예제 #5
0
def get_user_roles(user, obj):
    """ Return collection of active user roles associated with object or
        [anonymous] if user is not authenticated or is not User class instance.

        :param user: user id or User class instance.
        :param obj: target object. It can be a Model instance class or its
            descendant.
    """
    roles = [Roles.ALL]
    if isinstance(user, (int, long, basestring)):
        user = get_object_or_none(User, pk=user)
    if isinstance(user, User) and not user.is_anonymous():
        roles.extend(rbac.get_active_roles(obj,
            get_user_security_subjects(user)))
    else:
        roles.extend([Roles.ANONYMOUS])
    return roles
예제 #6
0
def get_user_security_subjects(user):
    """ Return collection of security subjects associated with user.
        Currently, this collection includes the user, as well as the groups
        to which he belongs and locations and companies associated with
        the user.

        Subject representing an authenticated user or entity (both referred
        to as "subject").

        :param user: user id or User class instance.
    """
    if isinstance(user, (int, long, basestring)):
        user = get_object_or_none(User, pk=user)
    if not isinstance(user, User) or user.is_anonymous():
        return []
    profile = user.get_profile()
    return ([user] +
        [g for g in user.groups.all()] +
        [c for c in profile.companies.all()] +
        [l for l in profile.cities.all()])
예제 #7
0
파일: news.py 프로젝트: sprymak/yolo-news
def get_post_by_id(val):
    """ Get news post by its id. """
    if isinstance(val, (int, long, basestring)):
        return get_object_or_none(models.Post, pk=val)