-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
154 lines (130 loc) · 5.27 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# importing extensions
import sentry_sdk
import psycopg2
from sentry_sdk.integrations.flask import FlaskIntegration
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
from flask_cors import CORS
from sqlalchemy.orm import Session
from flask import Flask, jsonify, make_response, request, g
from flask.cli import AppGroup
import click
# importing API stuff
from common.utils import unauthorized, headers, not_found
from config import DevelopmentConfig, ProdConfig
from resources.faqs import Faqs_RUD, Faqs_CR
from resources.announcements import Announcements_RUD, Announcements_CR
from resources.hardware import Hardware_RUD, Hardware_CR
from resources.sponsors import Sponsor_RD, Sponsor_CR
from resources.schedule import Schedule_RUD, Schedule_CR
from resources.applications import Applications_RU, Applications_CR
from resources.users import Users_RD, Users_CRU, Users_Change_Role, Users_Reset_Password_Token, Users_Reset_Password, Users_Change_Password
from resources.sessions import Sessions_C, Sessions_D
from resources.rsvps import RSVP_CR, RSVP_RD
from resources.checkin import Checkin_CR
# importing python stuff
from logging.config import dictConfig
import logging
import os
import sys
def register_extensions(app, api):
"""
Register Flask extensions.
"""
# initializing CORS object
CORS(app, origins=eval(os.getenv("CORS_ADDRESSES")))
# initializing api object
api.init_app(app)
def register_before_requests(app, Base, engine):
"""
Register before_request functions.
"""
def create_session():
"""
Before processing any request. Create a session by checking out a connection from the connection pool.
Also set global variables to be accessed for the life time of the request
"""
g.session = Session(engine)
g.Base = Base
app.before_request(create_session)
def register_teardown_requests(app):
"""
Register after_request functions.
"""
def close_session(err):
"""
After all the processing is done. Close the session to return the connection object back to the connection pool.
"""
g.session.close()
app.teardown_request(close_session)
def register_hello_world_route(app):
def helloworld():
"""
For flask app test and general info about the API.
Will also be used to check if the api is live or not on the slack hook
"""
metadata = {
"Organization": "SpartaHack",
"Backend Developer": "Yash",
"Frontend Developers": "Jarek",
"Contact": "hello@spartahack.com",
"Version": os.getenv("VERSION")
}
#app.logger.info('this is an INFO message')
#app.logger.warning('this is a WARNING message')
#app.logger.error('this is an ERROR messag')
#app.logger.info("Root url accessed", stack_info=True)
# app.logger.error(
# 'this is another error with breadcrumbs CRITICAL message')
return (jsonify(metadata), 200, headers)
app.add_url_rule('/', '/', helloworld)
def register_resources(api):
"""
Register resources on api object
"""
# adding resources. Just flask-restful things :)
api.add_resource(Faqs_RUD, "/faqs/<int:faq_id>")
api.add_resource(Faqs_CR, "/faqs")
api.add_resource(Announcements_RUD, "/announcements/<int:announcement_id>")
api.add_resource(Announcements_CR, "/announcements")
api.add_resource(Hardware_RUD, "/hardware/<int:hardware_id>")
api.add_resource(Hardware_CR, "/hardware")
api.add_resource(Sponsor_RD, "/sponsors/<int:sponsor_id>")
api.add_resource(Sponsor_CR, "/sponsors")
api.add_resource(Schedule_RUD, "/schedule/<int:schedule_id>")
api.add_resource(Schedule_CR, "/schedule")
api.add_resource(Applications_RU, "/applications/<int:application_id>")
api.add_resource(Applications_CR, "/applications")
api.add_resource(Users_RD, "/users/<int:user_id>")
api.add_resource(Users_CRU, "/users")
api.add_resource(Users_Change_Role, "/users/change_user_role")
api.add_resource(Users_Reset_Password_Token,
"/users/request_password_token")
api.add_resource(Users_Reset_Password, "/users/reset_password")
api.add_resource(Users_Change_Password, "/users/change_password")
api.add_resource(Sessions_D, "/sessions/<user_token>")
api.add_resource(Sessions_C, "/sessions")
api.add_resource(RSVP_RD, "/rsvps/<int:user_id>")
api.add_resource(RSVP_CR, "/rsvps")
api.add_resource(Checkin_CR, "/checkin")
def create_app(config):
"""
Flask application factory method
Sets up extentions and default routes
"""
# setting up sentry-sdk for logging exceptions and logs
sentry_sdk.init(
dsn=os.environ['SENTRY_DSN'],
integrations=[FlaskIntegration(), SqlalchemyIntegration()],
environment=os.getenv("FLASK_ENV"),
release=f"spartahackapi@{os.getenv('VERSION')}"
)
app = Flask(os.getenv("PROJECT"))
# loading config data into flask app from config object.
app.config.from_object(eval(config))
from extensions import api, Base, engine
register_before_requests(app, Base, engine)
register_teardown_requests(app)
register_resources(api)
register_hello_world_route(app)
register_extensions(app, api)
return app