Esempio n. 1
0
    def get(self):
        user = users.get_current_user()
        global sort_dist

        if (user):
            u_query = UserModel.query(UserModel.user==user).fetch()
            if (len(u_query)>0):
                u = u_query[0]
                current_location = u.last_location
                lat = u.last_latitude
                lg = u.last_longitude
                url = users.create_logout_url(self.request.url)
                url_linktext = 'Logout'
                restaurant_query = RestaurantModel.query().order(-RestaurantModel.TotalScore).fetch()
                restaurant_info = []
                if (len(restaurant_query) > 0):
                    for restaurant in restaurant_query:
                        a = restaurant.latitude
                        l = restaurant.longitude
                        tmp = "/view_picture/%s" % restaurant.Blob_key
                        part = urllib.urlencode({'name':restaurant.name})
                        tmp1 = "/order?%s" % part
                        score = restaurant.TotalScore
                        restaurant_info.append((restaurant.name, tmp, a, l, tmp1, score))

                if (sort_dist):
                    restaurant_info.sort(key=lambda tup: (tup[2]-lat)**2+(tup[3]-lg)**2)

                # # Initialize a cart for this user
                # cart_query = CartModel.query(CartModel.user==user).fetch()
                # if (len(cart_query)<1):
                #     cart = CartModel()
                #     cart.user = user
                #     cart.orders = []
                #     cart.total = 0.0
                #     cart.put()

                # current_location = 'UT-Austin'              #get the current location in database
                template_values = {
                    'user': user,
                    'url': url,
                    'url_linktext': url_linktext,
                    'current_location': current_location,
                    'restaurant_query_len': len(restaurant_query),
                    'restaurant_info': restaurant_info
                }
                template = JINJA_ENVIRONMENT.get_template('templates/main.html')
                self.response.write(template.render(template_values))
            else:
                self.redirect('/location')
        else:
            self.redirect('/')
Esempio n. 2
0
 def post(self):
     u = users.get_current_user()
     name = self.request.get('location')
     dis = self.request.get('distance')
     lat = float(self.request.get('latitude'))
     lg = float(self.request.get('longitude'))
     user_query = UserModel.query(UserModel.user==u).fetch()
     if (len(user_query)>0):
         user = user_query[0]
     else:
         user = UserModel()
         user.user = u
     user.last_latitude = lat
     user.last_longitude = lg
     user.last_location = name
     user.put()
     self.redirect('/main')