def blog_load(title=None): # Get the post collection posts = get_db().moogloof.posts # Get the posts size post_size = posts.count() # Get load count count = int(request.args.get("c")) # Size of load load_size = 3 # Loaded array loaded = [] if not count * load_size > post_size: # Get loaded posts loaded = posts.find().sort("date", -1)[(count * load_size):((count + 1) * load_size)] timeago = util_processor()["timeago"] loaded = list(loaded) for d in loaded: d["date"] = timeago(d["date"]) response_data = json_util.dumps(loaded) # Return posts return app.response_class(response=response_data, status=200, mimetype='application/json')
def chats(hid=None): # Get the message collection messages = get_db().moogloof.messages if not hid: message_q = messages.find({ "head": { "$exists": False, "$ne": True } }).sort("date", -1) # Different handling depending on message id return render_template("chat/chats.html", messages=message_q) else: # Get chat with matchiing head id message = messages.find_one({"_id": int(hid)}) # Get replies replies = messages.find({"head": hid}).sort("date", -1) if not message: # No message with id abort(404) else: # Render message return render_template("chat/message.html", header="message", message=message, replies=replies)
def create_chat(): if request.method == "POST": # Clean the form message_content = request.form["content"].strip() message_author = request.form["name"].strip() # Get the database db = get_db() # Generate new id new_id = db.moogloof.messages.find().sort("date", -1).limit(1) if new_id.count() > 0: new_id = list(new_id)[0]["_id"] + 1 else: new_id = 0 # Create new message new_message = { "date": datetime.now(timezone.utc), "content": message_content, "_id": new_id, } # Set author of message if given if message_author != "": new_message["author"] = message_author # Check if message is reply or not if "head" in request.form.keys(): new_message["head"] = request.form["head"] # Validate message content if message_content == "": # Alert user that content cannot be empty or just whitespace flash("Actually put something in there.") else: # Insert message into collection db.moogloof.messages.insert_one(new_message) # Redirect to the message page return redirect(request.referrer) # Redirect to chat page if invalid return redirect(url_for("chats")) else: abort(403)
def create_blog(): # Check if user is logged in if "logged-id" in session and session["logged-id"] == LOGGED_ID: # Saved is the form data to keep in case of invalid inputs saved = {} # Check if the form was submitted if request.method == "POST": # Clean the form post_title = request.form["title"].strip() post_content = request.form["content"] # Get the database db = get_db() # Make content saved to form saved["content"] = post_content # Validate post title if post_title == "": # Alert user that title cannot be whitespace flash("Your title can't just be whitespace bro.") elif bool(db.moogloof.posts.find_one({"title": post_title})): # Alert user that post with the same title already exists flash("Post with the same title exists.") else: # Create a new post new_post = { "date": datetime.now(timezone.utc), "title": post_title, "content": post_content } # Insert post into collection db.moogloof.posts.insert_one(new_post) # Redirect to the page of the post return redirect(url_for("blog", title=post_title)) # Render the create page template return render_template("post_create.html", saved=saved) else: # Return with error if not logged in abort(403)
def blog(title=None): # Get the post collection posts = get_db().moogloof.posts # Different handling depending on title if not title: # Get the entire list of posts sorted by date post_q = posts.find().sort("date", -1)[:3] # Render all posts return render_template("blog.html", header="blog", posts=post_q) else: # Get post with matching title post = posts.find_one({ "title": title }) if not post: # No post with title exists abort(404) else: # Render post return render_template("post.html", header="post", post=post)