def update_image(entry_id): timestamp = datetime.now() form = EntryForm() image = request.files[form.image.name] # The new image is uploaded to cloudinary and its url and id are retrieved uploaded_image = cloudinary.uploader.upload( image, width=800, quality='auto' ) image_url = uploaded_image.get('secure_url') new_image_id = uploaded_image.get('public_id') the_entry = entries.find_one({"_id": ObjectId(entry_id)}) # The previous image in is deleted from cloudinary to not crowd it with # unused images cloudinary.uploader.destroy(the_entry["image_id"]) # Image fields are updated in the database if the_entry["user_id"] == playground_id or the_entry["user_id"] == current_user.id: update_field( {"image": image_url, "image_id": new_image_id, "updated_on": timestamp}, entry_id) return update_success_msg("Image", timestamp, image_url) else: return render_template('pages/403.html', title="Forbidden")
def update_tags(entry_id): timestamp = datetime.now() form = EntryForm() the_entry = entries.find_one({"_id": ObjectId(entry_id)}) if the_entry["user_id"] == playground_id or the_entry["user_id"] == current_user.id: # If the tag list sent to python is empty, remove field from db if len(form.tags.data) == 0: entries.update_one( {"_id": ObjectId(entry_id)}, {"$unset": { "tags": "", "updated_on": timestamp } } ) return update_success_msg("Tags", timestamp) else: # If there is a list of tags, turn tags to a lowercase list and # remove any duplicates lowercase_tags = form.tags.data.lower().split(',') final_tags = [] for tag in lowercase_tags: if tag not in final_tags: final_tags.append(tag) update_field( {"tags": final_tags, "updated_on": timestamp}, entry_id) return update_success_msg("Tags", timestamp) else: return render_template('pages/403.html', title="Forbidden")
def update_rating(entry_id): timestamp = datetime.now() form = EntryForm() the_entry = entries.find_one({"_id": ObjectId(entry_id)}) if the_entry["user_id"] == playground_id or the_entry["user_id"] == current_user.id: update_field( {"rating": int(form.rating.data), "updated_on": timestamp}, entry_id) return update_success_msg("Rating", timestamp) else: return render_template('pages/403.html', title="Forbidden")
def update_fav(entry_id): the_entry = entries.find_one({"_id": ObjectId(entry_id)}) form = EntryForm() timestamp = datetime.now() if the_entry["user_id"] == current_user.id: update_field({ "is_fav": form.is_fav.data, "updated_on": timestamp }, entry_id) return update_success_msg("is_fav", timestamp) else: return render_template('pages/403.html', title="Forbidden")
def update_name(entry_id): form = EntryForm() new_name = form.name.data the_entry = entries.find_one({"_id": ObjectId(entry_id)}) timestamp = datetime.now() if the_entry["user_id"] == current_user.id: if (len(new_name) > 0 and len(new_name) <= 30 and text_regex.match(new_name)): update_field({ "name": form.name.data, "updated_on": timestamp }, entry_id) return update_success_msg("Name", timestamp) else: return update_failure_msg("Name must be betwen 1 and 30 " "characters, and cannot start with" " a space.") else: return render_template('pages/403.html', title="Forbidden")
def update_description(entry_id): timestamp = datetime.now() form = EntryForm() new_description = form.description.data the_entry = entries.find_one({"_id": ObjectId(entry_id)}) if the_entry["user_id"] == playground_id or the_entry["user_id"] == current_user.id: if (len(new_description) > 0 and len(new_description) <= 2000 and text_regex.match(new_description)): update_field( {"description": form.description.data, "updated_on": timestamp}, entry_id) return update_success_msg("Description", timestamp) else: return update_failure_msg("Description must be betwen 1 and 2000 " "characters, and cannot start with a " "space or line break.") else: return render_template('pages/403.html', title="Forbidden")