Commits

ronald martinez  committed e37a920

ok

  • Participants
  • Parent commits 319bf31
  • Branches dev, dev2

Comments (0)

Files changed (4)

File controllers/gift.py

         return (between(field, left, right))
 
 
-def multiquery_kwords(friend_id, items):
+def sql_multiquery_kwords(friend_id, items):
 
     tmp_queries = []
 
 class Find(BaseHandler):
 
     @authenticated
-    def get(self, friend_id):
+    def get(self, friend_id, **kwargs):
 
         user = self.current_user
-        items = Session.query(Gift).all()
-
-        params = dict(
-            q=multiquery_kwords(friend_id, items),
-            access_token=user.access_token
-        )
-
-        data = self.load_data_url(
-            url='%s/fql' % settings.FACEBOOK_GRAPH,
-            params=params,
-            json=True
-        )
 
-        skus = []
-
-        for x in data.get('data'):
-            if x.get('fql_result_set'):
-                skus.append(x.get('name'))
-
-        Memcached.set(str(friend_id), ','.join([str(x) for x in skus]))
-
-        self.redirect(self.reverse_url('search_gift', friend_id))
+        skus = Memcached.get(str(friend_id))
 
+        if not skus:
 
-class Search(BaseHandler):
+            q = sql_multiquery_kwords(
+                friend_id,
+                Session.query(Gift.sku, Gift.keywords).filter_by(
+                    enabled=True).all()
+            )
 
-    @authenticated
-    def get(self, friend_id, **kwargs):
+            params = dict(
+                q=q,
+                access_token=user.access_token
+            )
 
-        user = self.current_user
+            data = self.load_data_url(
+                url='%s/fql' % settings.FACEBOOK_GRAPH,
+                params=params,
+                json=True
+            )
 
-        skus = Memcached.get(str(friend_id))
+            skus = [str(x.get('name')) for x in data.get('data') \
+                if x.get('fql_result_set')]
 
-        if not skus:
-            self.redirect(self.reverse_url('find_gift', friend_id))
+            Memcached.set(str(friend_id), ','.join(skus))
 
         self.render('detail.html',
             user=user,
         logging.info('gender: %s' % gender)
         logging.info('category: %s' % category)
         logging.info('age: %s' % age)
+        logging.info('user_age: %s' % user.age)
+        logging.info('user_gender: %s' % user.gender)
 
         skus = Memcached.get(str(friend_id)).split(',')
 
-        filters = [Gift.sku.in_(skus)]
+        filters = [Gift.enabled == True, Gift.sku.in_(skus)]
 
         if user.age:
             filters.append(Gift.age == user.age)
             if gender:
                 filters.append(Gift.gender == gender)
 
-        items = Session.query(Gift).filter(*filters).all()
+        items = Session.query(
+            Gift.id,
+            Gift.title,
+            Gift.price,
+            Gift.stores,
+            Gift.category
+        ).filter(*filters).all()
 
         data_post = dict(
             price=price,

File controllers/user.py

             self.set_secure_cookie('user', data)
 
             user = Session.query(User).filter_by(
-                fbid=user_data_facebook.get('user_id')).first()
+                fbid=user_data_facebook.get('user_id'))
 
-            if not user:
+            if not user.first():
 
                 params = dict(
                     access_token=user_data_facebook.get('oauth_token'),
 
             else:
 
-                user = Session.query(User).filter_by(
-                    fbid=user_data_facebook.get('user_id'))
-
                 try:
                     user.update({'access_token': user_data_facebook.get(
                         'oauth_token')})
     gender = Column(Unicode(10), nullable=True)
     gifts = relationship('UserGift', backref='user')
 
+
 class Gift(Base):
 
     __tablename__ = "gifts"
         name='find_gift'
     ),
     url(
-        '/searchgift/(.*)',
-        gift.Search,
-        name='search_gift'
-    ),
-    url(
         '/',
         user.Index,
         name='home'