forked from Team-Tomato/QuestionPaper_Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
122 lines (107 loc) · 3.37 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from flask import Flask, request, jsonify, render_template
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import or_, func
from dotenv import load_dotenv
from flask_mail import Mail, Message
import os
app = Flask(__name__)
APP_ROOT = os.path.dirname(__file__) # refers to application_top
dotenv_path = os.path.join(APP_ROOT, '.env')
load_dotenv(dotenv_path)
app.config.from_object(os.getenv('APP_SETTINGS'))
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
# Mail settings
mail_settings = {
"MAIL_SERVER": 'smtp.gmail.com',
"MAIL_PORT": 465,
"MAIL_USE_TLS": False,
"MAIL_USE_SSL": True,
"MAIL_USERNAME": os.getenv('EMAIL_USER'),
"MAIL_PASSWORD": os.getenv('EMAIL_PASSWORD')
}
print(os.getenv('EMAIL_PASSWORD'))
print(os.getenv('EMAIL_USER'))
app.config.update(mail_settings)
mail = Mail(app)
from models import Question
@app.route("/", methods=['GET'])
def get():
return "<h1>Team Tomato welcome you</h1>"
@app.route("/api/v1/question/add", methods=['POST'])
def add_question():
question_data = request.get_json()['question']
subjectName = question_data['subjectName']
shortForm = question_data['shortForm']
staff = question_data['staff']
year = question_data['year']
url = question_data['url']
try:
question=Question(
subjectName = subjectName,
shortForm = shortForm,
staff = staff,
year = year,
url = url
)
db.session.add(question)
db.session.commit()
res = {
'id': question.id,
'subjectName': question.subjectName,
'shortForm': question.shortForm,
'staff': question.staff,
'year': question.year,
'url': question.url
}
return jsonify(res)
except Exception as e:
return(str(e))
@app.route("/api/v1/question", methods=['GET'])
def get_all_questions():
try:
questions = Question.query.all()
return jsonify([e.serialize() for e in questions])
except Exception as e:
return(str(e))
@app.route("/api/v1/question/<id_>", methods=['GET'])
def get_question_by_id(id_):
try:
question = Question.query.filter_by(id=id_).first()
return jsonify(question.serialize())
except Exception as e:
return(str(e))
@app.route("/api/v1/question/search", methods=['GET'])
def search_question():
try:
search_str = "%"+request.args.get('search_str')+"%"
questions = Question.query.filter(or_(Question.subjectName.ilike(search_str), Question.staff.ilike(search_str), Question.shortForm.ilike(search_str)))
print(questions)
return jsonify([e.serialize() for e in questions])
except Exception as e:
return(str(e))
@app.route("/api/v1/contactus", methods=['POST'])
def contact_us():
try:
contact_data = request.get_json()['contact']
name = contact_data['name']
email = contact_data['email']
message = contact_data['message']
__send_email(os.getenv('EMAIL_SUB'), [email])
res = {
'status': "Submission successful",
'name': name,
'email': email,
'message': message
}
return jsonify(res)
except Exception as e:
return (str(e))
def __send_email(sub, recipient_list):
msg = Message(subject=sub,
sender = (os.getenv('MAIL_SENDER_NAME'), app.config.get("MAIL_USERNAME")),
recipients = recipient_list,
body = "This is a test email I sent with Gmail and Python!")
mail.send(msg)
if __name__ == '__main__':
app.run()