Пример #1
0
def remove_user_collection(args):
    """Remove the Users from the Collection"""
    is_parameter_exists([constants.ID, constants.USER_IDS], args)

    collection_id = int(args[constants.ID])
    user_ids = json.loads(args[constants.USER_IDS])

    request_user = args[constants.USER]

    # Check Collection Id
    if not Collection.objects.filter(id=collection_id).exists():
        raise ApiError(constants.NOT_EXIST_OBJECT)

    # if request_user is not owner, then raise AUTH_ERROR
    collection_user = CollectionUser.objects.get(collection_id=collection_id,
                                                 user_id=request_user.id)
    if collection_user.type != COLLECTION_USER_TYPE[0]:
        raise ApiError(constants.AUTH_ERROR)

    # the owner cannot delete himself or herself
    # if the owner want to leave a collection, he or she must transfer it to other user
    # or deleting the collection would be a solution
    if request_user.id in user_ids:
        raise ApiError(constants.UNPROCESSABLE_ENTITY)

    CollectionUser.objects.filter(user_id__in=user_ids,
                                  collection_id=collection_id).delete()

    # Get the number of Members(including owner) Of Collections
    user_counts = __get_collection_user_count([collection_id], 'collection_id')
    return {
        constants.USERS:
        user_counts[collection_id] if collection_id in user_counts else 0
    }
Пример #2
0
def remove_user_collection_self(args):
    """Leave the collection (member)"""
    is_parameter_exists([constants.ID], args)

    collection_id = int(args[constants.ID])

    request_user = args[constants.USER]

    # Check Collection Id
    if not Collection.objects.filter(id=collection_id).exists():
        raise ApiError(constants.NOT_EXIST_OBJECT)

    try:
        collection_user = CollectionUser.objects.get(
            collection_id=collection_id, user_id=request_user.id)
    except ObjectDoesNotExist:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    # if request_user is not member, then raise AUTH_ERROR
    # the owner cannot delete himself or herself
    # if the owner want to leave a collection, he or she must transfer it to other user
    # or deleting the collection would be a solution
    if collection_user.type != COLLECTION_USER_TYPE[1]:
        raise ApiError(constants.AUTH_ERROR)

    collection_user.delete()

    # Get the number of Members(including owner) Of Collections
    user_counts = __get_collection_user_count([collection_id], 'collection_id')
    return {
        constants.USERS:
        user_counts[collection_id] if collection_id in user_counts else 0
    }
Пример #3
0
def remove_review(args):
    """Remove Review"""
    is_parameter_exists([constants.ID], args)

    # Review ID
    review_id = args[constants.ID]

    # Request User
    request_user = args[constants.USER]

    try:
        review = Review.objects.get(id=review_id)
    except ObjectDoesNotExist:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    # Auth
    if review.user_id != request_user.id:
        raise ApiError(constants.AUTH_ERROR)

    paper_id = review.paper_id

    review.delete()

    obj = UserAction.objects.get(user_id=request_user.id,
                                 paper_id=paper_id,
                                 type=USER_ACTION_TYPE[2])
    obj.count = obj.count - 1
    obj.save()
Пример #4
0
def update_user_collection(args):
    """Transfer ownership of the Collection to the User"""
    is_parameter_exists([constants.ID, constants.USER_ID], args)

    collection_id = int(args[constants.ID])
    user_id = args[constants.USER_ID]

    request_user = args[constants.USER]

    # Revoke ownership from request_user
    try:
        collection_user = CollectionUser.objects.get(
            collection_id=collection_id, user_id=request_user.id)
    except ObjectDoesNotExist:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    # if request_user is not owner, then raise AUTH_ERROR
    if collection_user.type != COLLECTION_USER_TYPE[0]:
        raise ApiError(constants.AUTH_ERROR)

    collection_user.type = COLLECTION_USER_TYPE[1]  # change to member
    collection_user.save()

    # Grant ownership to the user whose id is user_id
    try:
        collection_user = CollectionUser.objects.get(
            collection_id=collection_id, user_id=user_id)
    except ObjectDoesNotExist:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    collection_user.type = COLLECTION_USER_TYPE[0]  # change to owner
    collection_user.save()
Пример #5
0
def select_session(args):
    """Sign In"""
    is_parameter_exists([constants.EMAIL, constants.PASSWORD], args)

    # Email
    email = args[constants.EMAIL]

    # Password
    password = args[constants.PASSWORD]

    # Request
    request = args[constants.REQUEST]

    try:
        user = User.objects.get(email=email)
    except ObjectDoesNotExist:
        raise ApiError(constants.NOT_EXIST_OBJECT)
    else:
        if not __is_correct_password(password, user):
            raise ApiError(constants.AUTH_ERROR)

        # Set Session Id
        request.session[constants.ID] = user.id

    users, _, _ = __get_users(Q(id=user.id), user, None)
    if not users:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    return users[0]
Пример #6
0
def update_user_collection_pending(args):
    """'pending' user accepts invitation"""
    is_parameter_exists([constants.ID], args)

    collection_id = int(args[constants.ID])

    request_user = args[constants.USER]

    # Check CollectionUser
    try:
        collection_user = CollectionUser.objects.get(
            collection_id=collection_id, user_id=request_user.id)
    except ObjectDoesNotExist:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    # Check Collection Id
    if not Collection.objects.filter(id=collection_id).exists():
        raise ApiError(constants.NOT_EXIST_OBJECT)

    # if request_user is not 'pending', then raise AUTH_ERROR
    if collection_user.type != COLLECTION_USER_TYPE[2]:
        raise ApiError(constants.AUTH_ERROR)

    collection_user.type = COLLECTION_USER_TYPE[1]  # change to member
    collection_user.save()

    # Get the number of Members(including owner) Of Collections
    user_counts = __get_collection_user_count([collection_id], 'collection_id')
    return {
        constants.USERS:
        user_counts[collection_id] if collection_id in user_counts else 0
    }
Пример #7
0
def insert_collection(args):
    """Insert Collection"""
    is_parameter_exists([constants.TITLE, constants.TEXT], args)

    # Request User
    request_user = args[constants.USER]

    # User Id
    user_id = request_user.id

    # Title
    title = args[constants.TITLE]

    # Text
    text = args[constants.TEXT]

    request_user = args[constants.USER]

    # Check Valid
    if not title or not text:
        raise ApiError(constants.PARAMETER_ERROR)

    # default type is 'public'
    collection_share_type = args[
        constants.TYPE] if constants.TYPE in args else COLLECTION_SHARE_TYPE[0]
    if collection_share_type not in COLLECTION_SHARE_TYPE:
        raise ApiError(constants.PARAMETER_ERROR)

    # Create New Collection
    collection = Collection.objects.create(title=title,
                                           text=text,
                                           type=collection_share_type)

    # store an action for subscription feed
    Subscription.objects.create(
        actor=request_user,
        verb="created",
        action_object=collection,
    )

    # Add User To Collection
    CollectionUser.objects.create(collection_id=collection.id,
                                  user_id=user_id,
                                  type=COLLECTION_USER_TYPE[0])

    collection_id = collection.id

    collections, _, _, _ = __get_collections(Q(id=collection_id), request_user,
                                             None)

    # Does Not Exist
    if not collections:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    collection = collections[0]

    return collection
Пример #8
0
def insert_reply_collection(args):
    """Insert Reply Collection"""
    is_parameter_exists([constants.ID, constants.TEXT], args)

    # Collection Id
    collection_id = args[constants.ID]

    # Request User
    request_user = args[constants.USER]

    # Text
    text = args[constants.TEXT]

    # Check parameter
    if not text:
        raise ApiError(constants.PARAMETER_ERROR)

    # Check collection id
    if not Collection.objects.filter(id=collection_id).exists():
        raise ApiError(constants.NOT_EXIST_OBJECT)

    # Create

    reply = Reply.objects.create(
        user_id=request_user.id,
        text=text,
    )

    ReplyCollection.objects.create(collection_id=collection_id,
                                   reply_id=reply.id)

    # send notifications to members of the collection
    collection = Collection.objects.get(id=collection_id)

    collection_members = CollectionUser.objects.filter(
        Q(collection_id=collection_id))
    member_ids = [
        collection_member.user_id for collection_member in collection_members
    ]
    member_ids = list(set(member_ids))

    members = User.objects.filter(Q(id__in=member_ids))

    notify.send(request_user,
                recipient=members,
                verb='replied to',
                action_object=reply,
                target=collection)

    replies, _ = __get_replies(Q(id=reply.id), request_user, None)

    if not replies:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    reply = replies[0]

    return reply
Пример #9
0
def insert_like_reply(args):
    """Insert Like of Reply"""
    is_parameter_exists([constants.ID], args)

    # Reply Id
    reply_id = int(args[constants.ID])

    # Request User
    request_user = args[constants.USER]

    # Check Reply Id
    if not Reply.objects.filter(id=reply_id).exists():
        raise ApiError(constants.NOT_EXIST_OBJECT)

    # If Not Already Like, Create One
    if not ReplyLike.objects.filter(reply_id=reply_id,
                                    user_id=request_user.id).exists():
        ReplyLike.objects.create(
            reply_id=reply_id,
            user_id=request_user.id,
        )

        # send notifications to the author of the reply
        reply = Reply.objects.get(id=reply_id)
        reply_author = User.objects.get(id=reply.user_id)

        # consider target as the object which has the reply, instead of reply object
        try:  # assume it is the reply of a collection
            collection_id = ReplyCollection.objects.get(
                reply_id=reply_id).collection_id
            target = Collection.objects.get(id=collection_id)
        except ObjectDoesNotExist:  # maybe it is the reply of a review
            try:
                review_id = ReplyReview.objects.get(
                    reply_id=reply_id).review_id
                target = Review.objects.get(id=review_id)
            except ObjectDoesNotExist:
                raise ApiError(constants.NOT_EXIST_OBJECT)

        notify.send(
            request_user,
            recipient=[reply_author],
            verb='liked your reply of',
            action_object=reply,  # reply instead of reply_like
            target=target,
        )

    like_counts = __get_reply_like_count([reply_id], 'reply_id')
    return {
        constants.LIKES:
        like_counts[reply_id] if reply_id in like_counts else 0
    }
Пример #10
0
def update_review(args):
    """Update Review"""
    is_parameter_exists([constants.ID], args)

    # Review ID
    review_id = args[constants.ID]

    # Request User
    request_user = args[constants.USER]

    try:
        review = Review.objects.get(id=review_id)
    except ObjectDoesNotExist:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    # Auth
    if review.user_id != request_user.id:
        raise ApiError(constants.AUTH_ERROR)

    # Title
    title = args[constants.TITLE] if constants.TITLE in args else None

    # Text
    text = args[constants.TEXT] if constants.TEXT in args else None

    # Anonymous
    is_anonymous = args[
        constants.IS_ANONYMOUS] if constants.IS_ANONYMOUS in args else None

    # Update Title
    if title:
        review.title = title

    # Update Text
    if text:
        review.text = text

    # Update Anonymous
    if is_anonymous:
        review.anonymous = is_anonymous

    review.save()

    reviews, _, _, _ = __get_reviews(Q(id=review.id), request_user, None)

    if not reviews:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    review = reviews[0]

    return review
Пример #11
0
def remove_like_paper(args):
    """Remove Like of Paper"""
    is_parameter_exists([constants.ID], args)

    # Paper ID
    paper_id = int(args[constants.ID])

    # Request User
    request_user = args[constants.USER]

    try:
        paper_like = PaperLike.objects.get(paper_id=paper_id,
                                           user_id=request_user.id)
    except ObjectDoesNotExist:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    paper_like.delete()

    # Update action count for recommendation
    obj = UserAction.objects.get(user_id=request_user.id,
                                 paper_id=paper_id,
                                 type=USER_ACTION_TYPE[1])
    obj.count = obj.count - 1
    obj.save()

    like_counts = __get_paper_like_count([paper_id], 'paper_id')
    return {
        constants.LIKES:
        like_counts[paper_id] if paper_id in like_counts else 0
    }
Пример #12
0
def insert_like_collection(args):
    """Insert Like of Collection"""
    is_parameter_exists([constants.ID], args)

    # Collection Id
    collection_id = int(args[constants.ID])

    # Request User
    request_user = args[constants.USER]

    # Check Collection Id
    if not Collection.objects.filter(id=collection_id).exists():
        raise ApiError(constants.NOT_EXIST_OBJECT)

    # If Not Already Like, Create One
    if not CollectionLike.objects.filter(collection_id=collection_id,
                                         user_id=request_user.id).exists():
        collection_like = CollectionLike(
            collection_id=collection_id,
            user_id=request_user.id,
        )
        collection_like.save()

        collection = Collection.objects.get(id=collection_id)

        collection_members = CollectionUser.objects.filter(
            Q(collection_id=collection_id))
        member_ids = [
            collection_member.user_id
            for collection_member in collection_members
        ]
        member_ids = list(set(member_ids))

        members = User.objects.filter(Q(id__in=member_ids))

        # store an action for subscription feed
        try:
            subscription = Subscription.objects.get(
                actor=request_user,
                verb="liked",
                action_object_object_id=collection_id)
            subscription.save()  # for updating time
        except Subscription.DoesNotExist:
            Subscription.objects.create(actor=request_user,
                                        verb="liked",
                                        action_object=collection)

        notify.send(
            request_user,
            recipient=members,
            verb='liked',
            action_object=collection_like,
            target=collection,
        )

    like_counts = __get_collection_like_count([collection_id], 'collection_id')
    return {
        constants.LIKES:
        like_counts[collection_id] if collection_id in like_counts else 0
    }
Пример #13
0
def insert_user(args):
    """Insert New User"""
    is_parameter_exists(
        [constants.EMAIL, constants.USERNAME, constants.PASSWORD], args)

    # # Request User
    # request_user = args[constants.USER]

    # Email
    email = args[constants.EMAIL]

    # User Name
    username = args[constants.USERNAME]

    # Password
    password = args[constants.PASSWORD]

    # Check parameters are valid
    if not email or not username or not password:
        raise ApiError(constants.PARAMETER_ERROR)

    # Check username
    if User.objects.filter(username=username).exists():
        raise ApiError(constants.USERNAME_ALREADY_EXISTS)

    # Check email
    if User.objects.filter(email=email).exists():
        raise ApiError(constants.EMAIL_ALREADY_EXISTS)

    hashed, salt = __hash_password(password)

    try:
        user = User.objects.create_user(description=None,
                                        email=email,
                                        username=username,
                                        password=hashed,
                                        salt=salt)
    except IntegrityError:
        raise ApiError(constants.USERNAME_ALREADY_EXISTS)

    users, _, _ = __get_users(Q(id=user.id), user, None)

    if not users:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    return users[0]
Пример #14
0
def __is_correct_password(password, user):
    if user.salt is None:
        raise ApiError(constants.AUTH_ERROR)

    hashed_password = hashlib.sha256(user.salt.encode() +
                                     password.encode()).hexdigest()

    return hashed_password == user.password
Пример #15
0
def update_collection_type(args):
    """Update Collection Type"""
    is_parameter_exists([constants.ID, constants.TYPE], args)

    # Request User
    request_user = args[constants.USER]

    # Collection Share Type
    share_type = args[constants.TYPE]

    # Collection Id
    collection_id = args[constants.ID]

    # Check CollectionUser
    try:
        collection_user = CollectionUser.objects.get(
            collection_id=collection_id, user_id=request_user.id)
    except ObjectDoesNotExist:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    # if request_user is not owner, then raise AUTH_ERROR
    if collection_user.type != COLLECTION_USER_TYPE[0]:
        raise ApiError(constants.AUTH_ERROR)

    # Get Collection
    try:
        collection = Collection.objects.get(id=collection_id)
    except ObjectDoesNotExist:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    if share_type == COLLECTION_SHARE_TYPE[0]:
        collection.type = COLLECTION_SHARE_TYPE[0]
    elif share_type == COLLECTION_SHARE_TYPE[1]:
        collection.type = COLLECTION_SHARE_TYPE[1]
    else:
        raise ApiError(constants.PARAMETER_ERROR)

    collection.save()

    collections, _, _, _ = __get_collections(Q(id=collection.id), request_user,
                                             None)

    if not collections:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    return collections[0]
Пример #16
0
def insert_reply_review(args):
    """Insert Reply Review"""
    is_parameter_exists([constants.ID, constants.TEXT], args)

    # Review Id
    review_id = args[constants.ID]

    # Request User
    request_user = args[constants.USER]

    # Text
    text = args[constants.TEXT]

    # Check parameter
    if not text:
        raise ApiError(constants.PARAMETER_ERROR)

    # Check review id
    if not Review.objects.filter(id=review_id).exists():
        raise ApiError(constants.NOT_EXIST_OBJECT)

    # Create
    reply = Reply.objects.create(user_id=request_user.id, text=text)

    ReplyReview.objects.create(review_id=review_id, reply_id=reply.id)

    # send notifications to the author of the review
    review = Review.objects.get(id=review_id)
    review_author = User.objects.get(id=review.user_id)

    notify.send(request_user,
                recipient=[review_author],
                verb='replied to',
                action_object=reply,
                target=review)

    replies, _ = __get_replies(Q(id=reply.id), request_user, None)

    if not replies:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    reply = replies[0]

    return reply
Пример #17
0
def update_collection(args):
    """Update Collection"""
    is_parameter_exists([constants.ID], args)

    # Request User
    request_user = args[constants.USER]

    # Collection Id
    collection_id = args[constants.ID]

    # Get Collection
    try:
        collection = Collection.objects.get(id=collection_id)
    except ObjectDoesNotExist:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    # Check Collection User Id
    if not request_user or not CollectionUser.objects.filter(
            collection_id=collection_id, user_id=request_user.id).exists():
        raise ApiError(constants.AUTH_ERROR)

    # Title
    title = args[constants.TITLE]

    # Text
    text = args[constants.TEXT]

    # Update Title
    if title:
        collection.title = title

    # Update Text
    if text:
        collection.text = text

    collection.save()

    collections, _, _, _ = __get_collections(Q(id=collection.id), request_user,
                                             None)

    if not collections:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    return collections[0]
Пример #18
0
def insert_like_paper(args):
    """Insert Like of Paper"""
    is_parameter_exists([constants.ID], args)

    # Paper Id
    paper_id = int(args[constants.ID])

    # Request User
    request_user = args[constants.USER]

    # Check Paper Id
    if not Paper.objects.filter(id=paper_id).exists():
        raise ApiError(constants.NOT_EXIST_OBJECT)

    # If Not Already Like, Create One
    if not PaperLike.objects.filter(paper_id=paper_id,
                                    user_id=request_user.id).exists():
        PaperLike.objects.create(
            paper_id=paper_id,
            user_id=request_user.id,
        )

        # store an action for subscription feed
        try:
            subscription = Subscription.objects.get(
                actor=request_user,
                verb="liked",
                action_object_object_id=paper_id)
            subscription.save()  # for updating time
        except Subscription.DoesNotExist:
            paper = Paper.objects.get(id=paper_id)
            Subscription.objects.create(actor=request_user,
                                        verb="liked",
                                        action_object=paper)

        # Create action for recommendation
        try:
            obj = UserAction.objects.get(user_id=request_user.id,
                                         paper_id=paper_id,
                                         type=USER_ACTION_TYPE[1])
            obj.count = obj.count + 1
            obj.save()
        except ObjectDoesNotExist:
            UserAction.objects.create(
                user_id=request_user.id,
                paper_id=paper_id,
                type=USER_ACTION_TYPE[1],
                count=1,
            )

    like_counts = __get_paper_like_count([paper_id], 'paper_id')
    return {
        constants.LIKES:
        like_counts[paper_id] if paper_id in like_counts else 0
    }
Пример #19
0
def remove_reply(args):
    """Remove Reply"""
    is_parameter_exists([constants.ID], args)

    # Reply ID
    reply_id = args[constants.ID]

    # Request User
    request_user = args[constants.USER]

    try:
        reply = Reply.objects.get(id=reply_id)
    except ObjectDoesNotExist:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    # Auth
    if reply.user_id != request_user.id:
        raise ApiError(constants.AUTH_ERROR)

    reply.delete()
Пример #20
0
def remove_collection(args):
    """Remove Collection"""
    is_parameter_exists([constants.ID], args)

    # Request User
    request_user = args[constants.USER]

    # Collection Id
    collection_id = args[constants.ID]

    # Get Collection
    try:
        collection = Collection.objects.get(id=collection_id)
    except ObjectDoesNotExist:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    # Check Collection User Id
    if not request_user or not CollectionUser.objects.filter(
            collection_id=collection_id, user_id=request_user.id).exists():
        raise ApiError(constants.AUTH_ERROR)

    collection.delete()
Пример #21
0
def insert_user_collection(args):
    """Add Users(members) of the given Collection"""
    is_parameter_exists([constants.ID, constants.USER_IDS], args)

    collection_id = int(args[constants.ID])
    user_ids = args[constants.USER_IDS]

    request_user = args[constants.USER]

    # Check Collection Id
    try:
        collection = Collection.objects.get(id=collection_id)
    except ObjectDoesNotExist:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    # Self Add
    if request_user.id in user_ids:
        raise ApiError(constants.UNPROCESSABLE_ENTITY)

    # Add Users to Collection
    for user_id in user_ids:
        CollectionUser.objects.update_or_create(
            collection_id=collection_id,
            user_id=user_id,
            type=COLLECTION_USER_TYPE[2],  # pending until invitees accept
        )

    invited_users = User.objects.filter(Q(id__in=user_ids))
    notify.send(request_user,
                recipient=invited_users,
                verb='invited you to',
                target=collection)

    # Get the number of Members(including owner) Of Collections
    user_counts = __get_collection_user_count([collection_id], 'collection_id')
    return {
        constants.USERS:
        user_counts[collection_id] if collection_id in user_counts else 0
    }
Пример #22
0
def insert_like_review(args):
    """Insert Like of Review"""
    is_parameter_exists([constants.ID], args)

    # Review Id
    review_id = int(args[constants.ID])

    # Request User
    request_user = args[constants.USER]

    # Check Review Id
    if not Review.objects.filter(id=review_id).exists():
        raise ApiError(constants.NOT_EXIST_OBJECT)

    # If Not Already Like, Create One
    if not ReviewLike.objects.filter(review_id=review_id,
                                     user_id=request_user.id).exists():
        review_like = ReviewLike(
            review_id=review_id,
            user_id=request_user.id,
        )
        review_like.save()

        review = Review.objects.get(id=review_id)
        review_author = User.objects.get(id=review.user_id)

        # store an action for subscription feed
        try:
            subscription = Subscription.objects.get(
                actor=request_user,
                verb="liked",
                action_object_object_id=review_id)
            subscription.save()  # for updating time
        except Subscription.DoesNotExist:
            Subscription.objects.create(actor=request_user,
                                        verb="liked",
                                        action_object=review)

        notify.send(
            request_user,
            recipient=[review_author],
            verb='liked',
            action_object=review_like,
            target=review,
        )

    like_counts = __get_review_like_count([review_id], 'review_id')
    return {
        constants.LIKES:
        like_counts[review_id] if review_id in like_counts else 0
    }
Пример #23
0
def select_user_collection(args):
    """Get Users of the given Collection"""
    is_parameter_exists([constants.ID], args)

    collection_id = args[constants.ID]

    request_user = args[constants.USER]

    # Decide if results include request_user if she or he is one of members
    includes_me = True if constants.INCLUDES_ME not in args else json.loads(
        args[constants.INCLUDES_ME])

    # Page Number
    page_number = 1 if constants.PAGE_NUMBER not in args else int(
        args[constants.PAGE_NUMBER])

    # Check Collection Id
    if not Collection.objects.filter(id=collection_id).exists():
        raise ApiError(constants.NOT_EXIST_OBJECT)

    # Members QuerySet (except 'pending')
    if includes_me:
        query = Q(
            collection_id=collection_id) & ~Q(type=COLLECTION_USER_TYPE[2])
    else:
        query = Q(collection_id=collection_id) & ~Q(
            user_id=request_user.id) & ~Q(type=COLLECTION_USER_TYPE[2])

    queryset = CollectionUser.objects.filter(query)

    # Members(including owner) Of Collections
    collection_members = get_results_from_queryset(queryset, 10, page_number)

    # is_finished
    is_finished = not collection_members.has_next()

    # Member Ids
    member_ids = [
        collection_member.user_id for collection_member in collection_members
    ]
    member_ids = list(set(member_ids))

    # Get Members
    params = {constants.COLLECTION_ID: collection_id}
    members, _, _ = __get_users(Q(id__in=member_ids),
                                request_user,
                                10,
                                params=params)

    return members, page_number, is_finished
Пример #24
0
def read_notification(args):
    """Mark the given Notification as Read"""
    is_parameter_exists([
        constants.ID
    ], args)

    # Request User
    request_user = args[constants.USER]

    # Notification Id
    notification_id = args[constants.ID]

    # Get Notification
    try:
        notification = Notification.objects.get(id=notification_id)
    except ObjectDoesNotExist:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    # Check Collection User Id
    if not request_user or notification.recipient.id != request_user.id:
        raise ApiError(constants.AUTH_ERROR)

    # Mark as Read
    notification.mark_as_read()
Пример #25
0
def update_reply(args):
    """Update Reply"""
    is_parameter_exists([constants.ID], args)

    # Reply Id
    reply_id = args[constants.ID]

    # Request User
    request_user = args[constants.USER]

    try:
        reply = Reply.objects.get(id=reply_id)
    except ObjectDoesNotExist:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    # Auth
    if reply.user_id != request_user.id:
        raise ApiError(constants.AUTH_ERROR)

    # Text
    text = args[constants.TEXT] if constants.TEXT in args else None

    # Update Text
    if text:
        reply.text = text

    reply.save()

    replies, _ = __get_replies(Q(id=reply.id), request_user, None)

    if not replies:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    reply = replies[0]

    return reply
Пример #26
0
def select_me(args):
    """Get Current User"""

    # Request User-
    request_user = args[constants.USER] if constants.USER in args else None

    # User ID
    user_id = request_user.id

    users, _, _ = __get_users(Q(id=user_id), request_user, None)

    if not users:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    return users[0]
Пример #27
0
def remove_user_collection_pending(args):
    """'pending' user dismisses invitation"""
    is_parameter_exists([constants.ID], args)

    collection_id = int(args[constants.ID])

    request_user = args[constants.USER]

    # Check Collection Id
    if not Collection.objects.filter(id=collection_id).exists():
        raise ApiError(constants.NOT_EXIST_OBJECT)

    # Check CollectionUser
    try:
        collection_user = CollectionUser.objects.get(
            collection_id=collection_id, user_id=request_user.id)
    except ObjectDoesNotExist:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    # if request_user is not 'pending', then raise AUTH_ERROR
    if collection_user.type != COLLECTION_USER_TYPE[2]:
        raise ApiError(constants.AUTH_ERROR)

    collection_user.delete()
Пример #28
0
def select_user(args):
    """Get Single User"""
    is_parameter_exists([constants.ID], args)

    # Request User
    request_user = args[constants.USER] if constants.USER in args else None

    # User ID
    user_id = args[constants.ID]

    users, _, _ = __get_users(Q(id=user_id), request_user, None)

    if not users:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    return users[0]
Пример #29
0
def remove_paper_collection(args):
    """Remove paper from the collection"""
    is_parameter_exists([
        constants.ID, constants.PAPER_ID
    ], args)

    collection_id = int(args[constants.ID])
    paper_id = int(args[constants.PAPER_ID])

    if not CollectionPaper.objects.filter(collection_id=collection_id, paper_id=paper_id).exists():
        raise ApiError(constants.NOT_EXIST_OBJECT)

    CollectionPaper.objects.filter(collection_id=collection_id, paper_id=paper_id).delete()

    # Get the number of papers in the collection
    paper_counts = get_collection_paper_count([collection_id], 'collection_id')
    return {constants.PAPERS: paper_counts[collection_id] if collection_id in paper_counts else 0}
Пример #30
0
def select_review(args):
    """Select Review"""
    is_parameter_exists([constants.ID], args)

    # Review Id
    review_id = args[constants.ID]

    # Request User
    request_user = args[constants.USER]

    # Reviews
    reviews, _, _, _ = __get_reviews(Q(id=review_id), request_user, None)

    # Not Exists
    if not reviews:
        raise ApiError(constants.NOT_EXIST_OBJECT)

    review = reviews[0]

    return review