예제 #1
0
def collections_invite_api(request, slug, uid_or_email, uninvite=False):
    """
    API for adding or removing collection members, or collection invitations
    """
    if request.method != "POST":
        return jsonResponse({"error": "Unsupported HTTP method."})
    collection = Collection().load({"slug": slug})
    if not collection:
        return jsonResponse(
            {"error": "No collection with slug {}.".format(slug)})
    if request.user.id not in collection.admins:
        return jsonResponse(
            {"error": "You must be a collection owner to invite new members."})

    user = UserProfile(email=uid_or_email)
    if not user.exists():
        if uninvite:
            collection.remove_invitation(uid_or_email)
            message = "Invitation removed."
        else:
            collection.invite_member(uid_or_email, request.user.id)
            message = "Invitation sent."
    else:
        is_new_member = not collection.is_member(user.id)

        if is_new_member:
            collection.add_member(user.id)
            from sefaria.model.notification import Notification
            notification = Notification({"uid": user.id})
            notification.make_collection_add(adder_id=request.user.id,
                                             collection_slug=collection.slug)
            notification.save()
            message = "Collection editor added."
        else:
            message = "%s is already a editor of this collection." % user.full_name

    collection_content = collection.contents(with_content=True,
                                             authenticated=True)
    del collection_content["lastModified"]
    return jsonResponse({"collection": collection_content, "message": message})