-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
96 lines (82 loc) · 3.41 KB
/
main.py
File metadata and controls
96 lines (82 loc) · 3.41 KB
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
import webapp2
import os
import jinja2
import time
from seed import seed_data
from seed_function import UserProfile, Item, Like
from google.appengine.api import users
import random
from google.appengine.ext import ndb
#remember, you can get this by searching for jinja2 google app engine
jinja_current_dir = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
cycle_number = 0
class Feed(webapp2.RequestHandler):
def get(self):
template = jinja_current_dir.get_template("feed.html")
global cycle_number
user = users.get_current_user()
potential_profiles = UserProfile.query(UserProfile.user_id == user.user_id()).fetch(1)
if not potential_profiles:
UserProfile(user_id=user.user_id(), first_name=user.nickname(), last_name=user.nickname(), email=user.email()).put()
time.sleep(0.1)
profile = UserProfile.query(UserProfile.user_id == user.user_id()).fetch(1)[0]
item_display = Item.query().filter(Item.owner != profile.key).fetch()[cycle_number]
cycle_number = cycle_number + 1
if cycle_number == 5:
cycle_number = 0
# maybe use random integer for random picture (need to update seed if yes)
my_feed_dict = {
"image_url": item_display.img_url,
"item_description": item_display.description,
"item_id": item_display.key.urlsafe(),
}
self.response.write(template.render(my_feed_dict))
# item gets the item liked
# liker gets "who is the liker?"
# Like saves the like info
class LikeHandler(webapp2.RequestHandler):
def post(self):
user = users.get_current_user()
item = ndb.Key(urlsafe=self.request.get('item_id')).get()
liker = UserProfile.query(UserProfile.user_id == user.user_id()).fetch(1)[0]
item_owner_key = item.owner
Like(item=item.key, owner=item_owner_key, liker=liker.key).put()
mutual_likes = Like.query().filter(Like.liker == item_owner_key and Like.owner == liker.key).fetch()
if mutual_likes:
self.redirect('/match?matched_user_id=' + item_owner_key.urlsafe())
else:
self.redirect('/')
class MatchHandler(webapp2.RequestHandler):
def get(self):
template = jinja_current_dir.get_template("match.html")
matched_user_id = self.request.get('matched_user_id')
matched_user = ndb.Key(urlsafe=matched_user_id).get()
user = users.get_current_user()
liker = UserProfile.query(UserProfile.user_id == user.user_id()).fetch(1)[0]
my_match_dict = {
"match_email": str(matched_user.email)
}
self.response.write(template.render(my_match_dict))
# self.response.write("You had a match! You matched with " + str(matched_user.email))
class LoadDataHandler(webapp2.RequestHandler):
def get(self):
seed_data()
#
# class Profile(webapp2.RequestHandler):
# def get(self):
# item = ndb.Key(urlsafe=self.request.get('item_id')).get()
# item_owner_key = item.owner
# user = users.get_current_user()
# user_id = user.user_id()
# if user_id ==item_owner_key:
# print item
app = webapp2.WSGIApplication([
('/', Feed),
('/seed-data', LoadDataHandler),
('/like', LikeHandler),
('/match', MatchHandler),
# ('/profile', Profile)
], debug=True)