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()
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)
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()
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
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
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()])
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)