-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
95 lines (85 loc) · 2.96 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from chalice import Chalice, Response
from pymongo import MongoClient
import os
import jinja2
import urllib
import slugify
import datetime
app = Chalice(app_name='helloworld')
client = MongoClient("mongodb+srv://username:password@blogs-3khlw.mongodb.net/test?retryWrites=true")
db = client.chaliceblog
posts = db.blog_posts
def render(tpl_path, context):
path, filename = os.path.split(tpl_path)
return jinja2.Environment(loader=jinja2.FileSystemLoader(path or "./")).get_template(filename).render(context)
@app.route("/")
def index():
current_blog = {
"title": "No Posts Yet",
"content": "",
"create_date": ""
}
blog_posts = db.blog_posts.find({}).sort([("create_date", -1)])
if blog_posts.count() != 0:
current_blog = blog_posts[0]
context = {
"current_blog": current_blog,
"blog_posts": blog_posts
}
template = render("chalicelib/templates/index.html", context)
return Response(template, status_code=200, headers={"Content-Type": "text/html", "Access-Control-Allow-Origin": "*"})
@app.route("/blog/{blog_slug}")
def blog_view(blog_slug):
blog_posts = db.blog_posts.find({}).sort([("create_date", -1)])
current_blog = db.blog_posts.find_one({"slug": blog_slug})
context = {
"current_blog": current_blog,
"blog_posts": blog_posts
}
template = render("chalicelib/templates/index.html", context)
return Response(template, status_code=200, headers={
"Content-Type": "text/html",
"Access-Control-Allow-Origin": "*"
})
@app.route("/admin")
def create_blog():
template = render("chalicelib/templates/admin.html", {})
return Response(template, status_code=200, headers={
"Content-Type": "text/html",
"Access-Control-Allow-Origin": "*"
})
@app.route("/post-blog", methods=["POST"], content_types=["application/x-www-form-urlencoded"])
def post_blog():
post_json = urllib.parse.parse_qs(app.current_request.__dict__.get("_body"))
title = post_json["title"][0]
content = post_json["content"][0]
slug = slugify.slugify(title)
blog_post = {
"title": title,
"content": content,
"slug": slug,
"create_date": datetime.datetime.now()
}
try:
result = db.blog_posts.insert_one(blog_post)
except Exception as e:
context = {
"notification": f"Something Went Wrong: {e}",
"notification_color": "#f74435",
"notification_border": "#88261e",
"title": title,
"content": content
}
else:
context = {
"notification": "Blog Post Successful",
"notification_color": "#37da61",
"notification_border": "#2ba74a",
"title": "",
"content": ""
}
template = render("chalicelib/templates/admin.html", context)
return Response(template, status_code=200, headers={
"Content-Type": "text/html",
"Access-Control-Allow-Origin": "*"
})