def user_sheet_list_api(request, user_id): """ API for listing the sheets that belong to user_id. """ if int(user_id) != request.user.id: return jsonResponse({"error": "You are not authorized to view that."}) return jsonResponse(sheet_list(user_id))
def sheet_list_api(request): """ API for listing available sheets """ if request.method == "GET": return jsonResponse(sheet_list()) # Save a sheet if request.method == "POST": if not request.user.is_authenticated(): return jsonResponse({"error": "You must be logged in to save."}) j = request.POST.get("json") if not j: return jsonResponse({"error": "No JSON given in post data."}) sheet = json.loads(j) if "id" in sheet: existing = get_sheet(sheet["id"]) if "error" not in existing and \ not can_edit(request.user, existing) and \ not can_add(request.user, existing): return jsonResponse({"error": "You don't have permission to edit this sheet."}) responseSheet = save_sheet(sheet, request.user.id) if "rebuild" in responseSheet and responseSheet["rebuild"]: # Don't bother adding user links if this data won't be used to rebuild the sheet responseSheet["sources"] = annotate_user_links(responseSheet["sources"]) return jsonResponse(responseSheet)
def add_ref_to_sheet_api(request, sheet_id): """ API to add a source to a sheet using only a ref. """ ref = request.POST.get("ref") if not ref: return jsonResponse({"error": "No ref given in post data."}) return jsonResponse(add_ref_to_sheet(int(sheet_id), ref))
def add_source_to_sheet_api(request, sheet_id): """ API to add a fully formed source (posted as JSON) to sheet_id. """ source = json.loads(request.POST.get("source")) if not source: return jsonResponse({"error": "No source to copy given."}) return jsonResponse(add_source_to_sheet(int(sheet_id), source))
def copy_source_to_sheet_api(request, sheet_id): """ API to copy a source from one sheet to another. """ copy_sheet = request.POST.get("sheet") copy_source = request.POST.get("source") if not copy_sheet and copy_source: return jsonResponse({"error": "Need both a sheet and source number to copy."}) return jsonResponse(copy_source_to_sheet(int(sheet_id), int(copy_sheet), int(copy_source)))
def sheet_api(request, sheet_id): """ API for accessing and individual sheet. """ if request.method == "GET": sheet = get_sheet(int(sheet_id)) return jsonResponse(sheet) if request.method == "POST": return jsonResponse({"error": "TODO - save to sheet by id"})
def unlike_sheet_api(request, sheet_id): """ API to unlike sheet_id. """ if not request.user.is_authenticated(): return jsonResponse({"error": "You must be logged in to like sheets."}) if request.method != "POST": return jsonResponse({"error": "Unsupported HTTP method."}) remove_like_from_sheet(int(sheet_id), request.user.id) return jsonResponse({"status": "ok"})
def delete_sheet_api(request, sheet_id): """ Deletes sheet with id, only if the requester is the sheet owner. """ id = int(sheet_id) sheet = db.sheets.find_one({"id": id}) if not sheet: return jsonResponse({"error": "Sheet %d not found." % id}) if request.user.id != sheet["owner"]: return jsonResponse({"error": "Only the sheet owner may delete a sheet."}) db.sheets.remove({"id": id}) return jsonResponse({"status": "ok"})
def check_sheet_modified_api(request, sheet_id, timestamp): """ Check if sheet_id has been modified since timestamp. If modified, return the new sheet. """ sheet_id = int(sheet_id) last_mod = get_last_updated_time(sheet_id) if not last_mod: return jsonResponse({"error": "Couldn't find last modified time."}) if timestamp >= last_mod: return jsonResponse({"modified": False}) sheet = get_sheet(sheet_id) if "error" in sheet: return jsonResponse(sheet) sheet["modified"] = True sheet["sources"] = annotate_user_links(sheet["sources"]) return jsonResponse(sheet)
def sheet_likers_api(request, sheet_id): """ API to retrieve the list of peopke who like sheet_id. """ response = {"likers": likers_list_for_sheet(sheet_id)} return jsonResponse(response)
def update_sheet_tags_api(request, sheet_id): """ API to update tags for sheet_id. """ tags = json.loads(request.POST.get("tags")) return jsonResponse(update_sheet_tags(int(sheet_id), tags))