This repository has been archived by the owner on Jun 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
151 lines (125 loc) · 4.74 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
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
import os
from flask import Flask, request, make_response
app = Flask(__name__)
app.secret_key = 'aiosdjsaodjoidjioewnioewfnoeijfoisdjf'
import json
import urllib
import jinja2
from models import *
from base import *
FACEBOOK_KEY = "157028231131213"
FACEBOOK_SECRET = "0437ee70207dca46609219b990be0614"
jinja_environment = jinja2.Environment(
autoescape=True,
loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), "templates")))
@app.route("/")
@app.route("/(\d+)")
def main(*args):
template = jinja_environment.get_template("index.html")
return make_response(template.render({}))
@app.route("/markers")
@user_optional
def markers(methods=["GET", "POST"]):
if request.method == "GET":
ne_lat = float(request.values['ne_lat'])
ne_lng = float(request.values['ne_lng'])
sw_lat = float(request.values['sw_lat'])
sw_lng = float(request.values['sw_lng'])
zoom = int(request.values['zoom'])
min_zoom_level = 16
if zoom < min_zoom_level:
markers = []
else:
# query = Marker.all()
# if 'start_time' in request.values:
# query = query.filter("created >=", request.values["start_time"])
# if 'start_time' in request.values:
# query = query.filter("created <=", request.values["end_time"])
results = Marker.bounding_box_fetch(ne_lat, ne_lng, sw_lat, sw_lng)
markers = [marker.serialize() for marker in results.all()]
return make_response(json.dumps(markers))
else:
data = json.loads(self.request.body)
marker = Marker.parse(data)
marker.user = self.user
marker.update_location()
marker.put()
return make_response(json.dumps(marker.serialize(self.user)))
@app.route("/markers/(.*)", methods=["GET", "PUT", "DELETE"])
@user_required
def marker(self, key_name):
if request.method == "GET":
marker = Marker.get_by_key_name(key_name)
return make_response(json.dumps(marker.serialize(self.user)))
elif request.method == "PUT":
marker = Marker.get_by_key_name(key_name)
data = json.loads(self.request.body)
marker.update(data, self.user)
return make_response(json.dumps(marker.serialize(self.user)))
elif request.method == "DELETE":
marker = Marker.get_by_key_name(key_name)
marker.delete()
@app.route("/login", methods=["POST"])
@user_optional
def login():
user = get_user()
if user:
return make_response(json.dumps(user.serialize()))
if request.json:
facebook_data = request.json
user_id = facebook_data["userID"]
access_token = facebook_data["accessToken"]
user_details = json.loads(urllib.urlopen("https://graph.facebook.com/me?access_token=" + access_token).read())
# login successful
if user_details["id"] == user_id:
user = db_session.query(User).filter(User.email == user_details["email"]).scalar()
if not user:
user = User(
email = user_details["email"],
first_name = user_details["first_name"],
last_name = user_details["last_name"],
username = user_details["username"],
facebook_id = user_details["id"],
facebook_url = user_details["link"],
access_token = facebook_data["accessToken"]
)
else:
user.access_token = facebook_data["accessToken"]
db_session.add(user)
set_user(user)
return make_response(json.dumps(user.serialize()))
else:
raise Exception("Error in logging in.")
else:
raise Exception("No login data or user logged in.")
@app.route("/logout")
@user_required
def do_logout():
logout()
@app.route("/follow/(.*)")
@user_required
def follow(key_name):
marker = Marker.get_by_key_name(key_name)
follower = Follower.all().filter("marker", marker).filter("user", self.user).get()
if not follower:
Follower(parent = marker, marker = marker, user = self.user).put()
@app.route("/unfollow/(.*)")
@user_required
def unfollow(key_name):
marker = Marker.get_by_key_name(key_name)
follower = Follower.all().filter("marker", marker).filter("user", self.user).get()
if follower:
follower.delete()
@app.route("/make_admin")
def make_admin():
user = User.all().filter("email", request.get("email")).get()
if user:
user.is_admin = True
user.put()
return make_response("User with email %s is now admin" % user.email)
@app.route("/import")
def import_handler():
import process
process.import_to_datastore()
if __name__ == "__main__":
app.run(debug=True)