def save_sheet(sheet, user_id): sheet["dateModified"] = datetime.now().isoformat() if "id" in sheet: existing = db.sheets.find_one({"id": sheet["id"]}) sheet["views"] = existing["views"] # prevent updating views existing.update(sheet) sheet = existing else: sheet["dateCreated"] = datetime.now().isoformat() lastId = sheets.find().sort([['id', -1]]).limit(1) if lastId.count(): sheet["id"] = lastId.next()["id"] + 1 else: sheet["id"] = 1 if "status" not in sheet: sheet["status"] = PRIVATE_SHEET sheet["owner"] = user_id sheet["views"] = 1 sheets.update({"id": sheet["id"]}, sheet, True, False) if sheet["status"] in LISTED_SHEETS: search.index_sheet(sheet["id"]) return sheet
def save_sheet(sheet, user_id): """ Saves sheet to the db, with user_id as owner. """ sheet["dateModified"] = datetime.now().isoformat() status_changed = False if "id" in sheet: existing = db.sheets.find_one({"id": sheet["id"]}) if sheet["lastModified"] != existing["dateModified"]: # Don't allow saving if the sheet has been modified since the time # that the user last received an update existing["error"] = "Sheet updated." existing["rebuild"] = True return existing del sheet["lastModified"] if sheet["status"] != existing["status"]: status_changed = True sheet["views"] = existing["views"] # prevent updating views existing.update(sheet) sheet = existing else: sheet["dateCreated"] = datetime.now().isoformat() lastId = db.sheets.find().sort([['id', -1]]).limit(1) if lastId.count(): sheet["id"] = lastId.next()["id"] + 1 else: sheet["id"] = 1 if "status" not in sheet: sheet["status"] = PRIVATE_SHEET sheet["owner"] = user_id sheet["views"] = 1 if status_changed: if sheet["status"] in LISTED_SHEETS and "datePublished" not in sheet: # PUBLISH sheet["datePublished"] = datetime.now().isoformat() record_sheet_publication(sheet["id"], user_id) broadcast_sheet_publication(user_id, sheet["id"]) if sheet["status"] not in LISTED_SHEETS: # UNPUBLISH delete_sheet_publication(sheet["id"], user_id) NotificationSet({ "type": "sheet publish", "content.publisher_id": user_id, "content.sheet_id": sheet["id"] }).delete() db.sheets.update({"id": sheet["id"]}, sheet, True, False) if sheet["status"] in LISTED_SHEETS and SEARCH_INDEX_ON_SAVE: search.index_sheet(sheet["id"]) global last_updated last_updated[sheet["id"]] = sheet["dateModified"] return sheet
def save_sheet(sheet, user_id): """ Saves sheet to the db, with user_id as owner. """ sheet["dateModified"] = datetime.now().isoformat() status_changed = False if "id" in sheet: existing = db.sheets.find_one({"id": sheet["id"]}) if sheet["lastModified"] != existing["dateModified"]: # Don't allow saving if the sheet has been modified since the time # that the user last received an update existing["error"] = "Sheet updated." existing["rebuild"] = True return existing del sheet["lastModified"] if sheet["status"] != existing["status"]: status_changed = True sheet["views"] = existing["views"] # prevent updating views existing.update(sheet) sheet = existing else: sheet["dateCreated"] = datetime.now().isoformat() lastId = db.sheets.find().sort([['id', -1]]).limit(1) if lastId.count(): sheet["id"] = lastId.next()["id"] + 1 else: sheet["id"] = 1 if "status" not in sheet: sheet["status"] = "unlisted" sheet["owner"] = user_id sheet["views"] = 1 if status_changed: if sheet["status"] is "public" and "datePublished" not in sheet: # PUBLISH sheet["datePublished"] = datetime.now().isoformat() record_sheet_publication(sheet["id"], user_id) broadcast_sheet_publication(user_id, sheet["id"]) if sheet["status"] is not "public": # UNPUBLISH delete_sheet_publication(sheet["id"], user_id) NotificationSet({"type": "sheet publish", "content.publisher_id": user_id, "content.sheet_id": sheet["id"] }).delete() db.sheets.update({"id": sheet["id"]}, sheet, True, False) if sheet["status"] is "public" and SEARCH_INDEX_ON_SAVE: search.index_sheet(sheet["id"]) global last_updated last_updated[sheet["id"]] = sheet["dateModified"] return sheet
def save_sheet(sheet, user_id): """ Saves sheet to the db, with user_id as owner. """ sheet["dateModified"] = datetime.now().isoformat() if "id" in sheet: existing = db.sheets.find_one({"id": sheet["id"]}) if sheet["lastModified"] != existing["dateModified"]: existing["error"] = "Sheet updated." existing["rebuild"] = True return existing del sheet["lastModified"] sheet["views"] = existing["views"] # prevent updating views existing.update(sheet) sheet = existing else: sheet["dateCreated"] = datetime.now().isoformat() lastId = db.sheets.find().sort([["id", -1]]).limit(1) if lastId.count(): sheet["id"] = lastId.next()["id"] + 1 else: sheet["id"] = 1 if "status" not in sheet: sheet["status"] = PRIVATE_SHEET sheet["owner"] = user_id sheet["views"] = 1 db.sheets.update({"id": sheet["id"]}, sheet, True, False) if sheet["status"] in LISTED_SHEETS and SEARCH_INDEX_ON_SAVE: search.index_sheet(sheet["id"]) global last_updated last_updated[sheet["id"]] = sheet["dateModified"] return sheet
def save_sheet(sheet, user_id, search_override=False): """ Saves sheet to the db, with user_id as owner. """ sheet["dateModified"] = datetime.now().isoformat() status_changed = False if "id" in sheet: existing = db.sheets.find_one({"id": sheet["id"]}) if sheet["lastModified"] != existing["dateModified"]: # Don't allow saving if the sheet has been modified since the time # that the user last received an update existing["error"] = "Sheet updated." existing["rebuild"] = True return existing del sheet["lastModified"] if sheet["status"] != existing["status"]: status_changed = True sheet["views"] = existing["views"] # prevent updating views sheet["owner"] = existing["owner"] # prevent updating owner sheet["likes"] = existing["likes"] if "likes" in existing else [ ] # prevent updating likes existing.update(sheet) sheet = existing else: sheet["dateCreated"] = datetime.now().isoformat() lastId = db.sheets.find().sort([['id', -1]]).limit(1) if lastId.count(): sheet["id"] = lastId.next()["id"] + 1 else: sheet["id"] = 1 if "status" not in sheet: sheet["status"] = "unlisted" sheet["owner"] = user_id sheet["views"] = 1 #ensure that sheet sources have nodes (primarily for sheets posted via API) nextNode = sheet.get("nextNode", 1) sheet["nextNode"] = nextNode checked_sources = [] for source in sheet["sources"]: if "node" not in source: source["node"] = nextNode nextNode += 1 checked_sources.append(source) sheet["sources"] = checked_sources if status_changed: if sheet["status"] == "public" and "datePublished" not in sheet: # PUBLISH sheet["datePublished"] = datetime.now().isoformat() record_sheet_publication(sheet["id"], user_id) broadcast_sheet_publication(user_id, sheet["id"]) if sheet["status"] != "public": # UNPUBLISH delete_sheet_publication(sheet["id"], user_id) NotificationSet({ "type": "sheet publish", "content.publisher_id": user_id, "content.sheet_id": sheet["id"] }).delete() db.sheets.update({"id": sheet["id"]}, sheet, True, False) if "tags" in sheet: update_sheet_tags(sheet["id"], sheet["tags"]) if sheet[ "status"] == "public" and SEARCH_INDEX_ON_SAVE and not search_override: try: index_name = search.get_new_and_current_index_names( "sheet")['current'] search.index_sheet(index_name, sheet["id"]) except: logger.error("Failed index on " + str(sheet["id"])) ''' global last_updated last_updated[sheet["id"]] = sheet["dateModified"] ''' return sheet
def save_sheet(sheet, user_id, search_override=False, rebuild_nodes=False): """ Saves sheet to the db, with user_id as owner. """ def next_sheet_id(): last_id = db.sheets.find().sort([['id', -1]]).limit(1) if last_id.count(): sheet_id = last_id.next()["id"] + 1 else: sheet_id = 1 return sheet_id sheet["dateModified"] = datetime.now().isoformat() status_changed = False if "id" in sheet: new_sheet = False existing = db.sheets.find_one({"id": sheet["id"]}) if sheet["lastModified"] != existing["dateModified"]: # Don't allow saving if the sheet has been modified since the time # that the user last received an update existing["error"] = "Sheet updated." existing["rebuild"] = True return existing del sheet["lastModified"] if sheet["status"] != existing["status"]: status_changed = True sheet["views"] = existing["views"] # prevent updating views sheet["owner"] = existing["owner"] # prevent updating owner sheet["likes"] = existing["likes"] if "likes" in existing else [ ] # prevent updating likes existing.update(sheet) sheet = existing else: new_sheet = True sheet["dateCreated"] = datetime.now().isoformat() if "status" not in sheet: sheet["status"] = "unlisted" sheet["owner"] = user_id sheet["views"] = 1 #ensure that sheet sources have nodes (primarily for sheets posted via API) nextNode = sheet.get("nextNode", 1) sheet["nextNode"] = nextNode checked_sources = [] for source in sheet["sources"]: if "node" not in source: source["node"] = nextNode nextNode += 1 checked_sources.append(source) sheet["sources"] = checked_sources if status_changed and not new_sheet: if sheet["status"] == "public" and "datePublished" not in sheet: # PUBLISH sheet["datePublished"] = datetime.now().isoformat() record_sheet_publication(sheet["id"], user_id) # record history broadcast_sheet_publication(user_id, sheet["id"]) if sheet["status"] != "public": # UNPUBLISH delete_sheet_publication(sheet["id"], user_id) # remove history UserStorySet({ "storyForm": "publishSheet", "data.publisher": user_id, "data.sheet_id": sheet["id"] }).delete() NotificationSet({ "type": "sheet publish", "content.publisher_id": user_id, "content.sheet_id": sheet["id"] }).delete() sheet["includedRefs"] = refs_in_sources(sheet.get("sources", [])) if rebuild_nodes: sheet = rebuild_sheet_nodes(sheet) if new_sheet: # mongo enforces a unique sheet id, get a new id until a unique one has been found while True: try: sheet["id"] = next_sheet_id() db.sheets.insert_one(sheet) break except DuplicateKeyError: pass else: db.sheets.find_one_and_replace({"id": sheet["id"]}, sheet) if "tags" in sheet: update_sheet_tags(sheet["id"], sheet["tags"]) if sheet[ "status"] == "public" and SEARCH_INDEX_ON_SAVE and not search_override: try: index_name = search.get_new_and_current_index_names( "sheet")['current'] search.index_sheet(index_name, sheet["id"]) except: logger.error(u"Failed index on " + str(sheet["id"])) ''' global last_updated last_updated[sheet["id"]] = sheet["dateModified"] ''' return sheet
def save_sheet(sheet, user_id, search_override=False): """ Saves sheet to the db, with user_id as owner. """ sheet["dateModified"] = datetime.now().isoformat() status_changed = False if "id" in sheet: existing = db.sheets.find_one({"id": sheet["id"]}) if sheet["lastModified"] != existing["dateModified"]: # Don't allow saving if the sheet has been modified since the time # that the user last received an update existing["error"] = "Sheet updated." existing["rebuild"] = True return existing del sheet["lastModified"] if sheet["status"] != existing["status"]: status_changed = True sheet["views"] = existing["views"] # prevent updating views sheet["owner"] = existing["owner"] # prevent updating owner sheet["likes"] = existing["likes"] if "likes" in existing else [] # prevent updating likes existing.update(sheet) sheet = existing else: sheet["dateCreated"] = datetime.now().isoformat() lastId = db.sheets.find().sort([['id', -1]]).limit(1) if lastId.count(): sheet["id"] = lastId.next()["id"] + 1 else: sheet["id"] = 1 if "status" not in sheet: sheet["status"] = "unlisted" sheet["owner"] = user_id sheet["views"] = 1 #ensure that sheet sources have nodes (primarily for sheets posted via API) nextNode = sheet.get("nextNode", 1) sheet["nextNode"] = nextNode checked_sources = [] for source in sheet["sources"]: if "node" not in source: source["node"] = nextNode nextNode += 1 checked_sources.append(source) sheet["sources"] = checked_sources if status_changed: if sheet["status"] == "public" and "datePublished" not in sheet: # PUBLISH sheet["datePublished"] = datetime.now().isoformat() record_sheet_publication(sheet["id"], user_id) broadcast_sheet_publication(user_id, sheet["id"]) if sheet["status"] != "public": # UNPUBLISH delete_sheet_publication(sheet["id"], user_id) NotificationSet({"type": "sheet publish", "content.publisher_id": user_id, "content.sheet_id": sheet["id"] }).delete() sheet["includedRefs"] = refs_in_sources(sheet.get("sources", [])) db.sheets.update({"id": sheet["id"]}, sheet, True, False) if "tags" in sheet: update_sheet_tags(sheet["id"], sheet["tags"]) if sheet["status"] == "public" and SEARCH_INDEX_ON_SAVE and not search_override: try: index_name = search.get_new_and_current_index_names("sheet")['current'] search.index_sheet(index_name, sheet["id"]) except: logger.error("Failed index on " + str(sheet["id"])) ''' global last_updated last_updated[sheet["id"]] = sheet["dateModified"] ''' return sheet