-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
74 lines (62 loc) · 2.32 KB
/
main.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
from flask import Flask, request, jsonify, redirect
from dotenv import load_dotenv
from markupsafe import escape
from flask_cors import CORS, cross_origin
from lib import *
# Create flask app
app = Flask(__name__)
CORS(app)
false = False
true = True
load_dotenv() # take environment variables from .env.
# interacts with contact post from frontend
@app.route("/contact" , methods=["GET", "POST"])
@cross_origin()
def send():
if request.method == "POST":
data = request.get_json()
try:
send_email(name=data['name'], subject=data['subject'], email=data['email'], msg=data['msg'])
return jsonify({"success":true, "message": "Mail Sent!"})
except Exception as e:
return jsonify({"success":false, "message": f"Error: {e}"})
if request.method == "GET":
return "<h1>You should not use GET, use POST</h1>"
# redirect to frontend instead of plain text
@app.route("/")
@cross_origin()
def home():
return redirect("https://wwf-jr.netlify.app/", code=302)
# fetches a list of continents
@app.route("/fetch_cont")
@cross_origin()
def fetch_cont():
try:
return jsonify({"success":true, "conts":get_conts()})
except Exception as e:
return jsonify({"success":false, "error":e})
# fetches info of a continent
@app.route("/<string:cont>")
@cross_origin()
def fetch_cont_info(cont):
cont = cont.lower()
try:
if cont in get_conts():
return jsonify({"success":true, "cont":escape(cont), "info":get_cont_info(escape(cont)), "animals":get_animals_of_cont(escape(cont))})
else:
return jsonify({"success":false, "error": "No such continent!!"})
except Exception as e:
return jsonify({"success":false, "error":e})
# fetches info of a animal
@app.route("/<string:cont>/info/<string:animal>")
@cross_origin()
def fetch_cont_animal_info(cont, animal):
cont = cont.lower()
animal = animal.lower()
try:
if cont in get_conts() and animal in get_animals_of_cont(escape(cont)):
return jsonify({"success":true, "cont":escape(cont), "animal":escape(animal), "info":get_animal_info(escape(cont), escape(animal))})
else:
return jsonify({"success":false, "error": "No such continent or animal!!"})
except Exception as e:
return jsonify({"success":false, "error":e})