Commits

ronald martinez  committed 319bf31

ok

  • Participants
  • Parent commits 2482b58
  • Branches dev, dev2

Comments (0)

Files changed (4)

File controllers/gift.py

 import logging
 import settings
-import datetime
 
 from models import Gift, UserGift
 from tornado.web import authenticated
 from controllers import BaseHandler, Memcached, Session
 
 
-#friend_id = '500226517'
-#Memcached.delete(friend_id)
-
 AGES = (
     ('13-17', '13-17'),
     ('18-24', '18-24'),
     ('55-0', '+55'),
 )
 
-GENDER = (
+GENDERS = (
     ('male', 'Masculino'),
     ('female', 'Femenino')
 )
 )
 
 
-def get_age(date_of_birth):
-
-    if date_of_birth > datetime.date.today().replace(
-            year=date_of_birth.year):
-        return datetime.date.today().year - date_of_birth.year - 1
-    else:
-        return datetime.date.today().year - date_of_birth.year
-
-
 def search_btw(field, value):
 
     left, right = [int(x) for x in value.split('-')]
     @authenticated
     def get(self, friend_id):
 
-        print "xxxxxxxxxxx"
-
         user = self.current_user
         items = Session.query(Gift).all()
 
         if not skus:
             self.redirect(self.reverse_url('find_gift', friend_id))
 
-        friend_data = self.load_data_url(
-            url='%s/%s' % (settings.FACEBOOK_GRAPH, friend_id),
-            params={'access_token': user.access_token,
-                'fields': 'gender,birthday'},
-            json=True
-        )
-
-        logging.info(friend_data)
-
-        if 'birthday' in friend_data:
-            has_birthday = False
-        else:
-            has_birthday = True
-
-        if 'gender' in friend_data:
-            has_gender = False
-        else:
-            has_gender = True
-
         self.render('detail.html',
+            user=user,
             ages=AGES,
             prices=PRICES,
-            gender=GENDER,
+            genders=GENDERS,
             categories=CATEGORIES,
-            has_birthday=has_birthday,
-            has_gender=has_gender,
             friend_id=friend_id,
             **kwargs
         )
         user = self.current_user
 
         price = self.get_argument('price', None)
-        gender = self.get_argument('gender', None)
         category = self.get_argument('category', None)
         age = self.get_argument('age', None)
-
-        friend_data = self.load_data_url(
-            url='%s/%s' % (settings.FACEBOOK_GRAPH, friend_id),
-            params={'access_token': user.access_token,
-                'fields': 'gender,birthday'},
-            json=True
-        )
-
-        if 'birthday' in friend_data:
-            has_birthday = False
-        else:
-            has_birthday = True
-
-        if 'gender' in friend_data:
-            gender = friend_data['gender']
-            has_gender = False
-        else:
-            has_gender = True
+        gender = self.get_argument('gender', None)
 
         logging.info('price: %s' % price)
         logging.info('gender: %s' % gender)
         logging.info('category: %s' % category)
         logging.info('age: %s' % age)
-        logging.info('has_birthday: %s' % has_birthday)
-        logging.info('has_gender: %s' % has_gender)
 
         skus = Memcached.get(str(friend_id)).split(',')
 
         filters = [Gift.sku.in_(skus)]
 
-        if age:
-            filters.append(search_btw(Gift.age, age))
+        if user.age:
+            filters.append(Gift.age == user.age)
+        else:
+            if age:
+                filters.append(search_btw(Gift.age, age))
 
         if price:
             filters.append(search_btw(Gift.price, price))
         if category:
             filters.append(Gift.category == category)
 
-        if gender:
-            filters.append(Gift.gender == gender)
+        if user.gender:
+            filters.append(Gift.gender == user.gender)
+        else:
+            if gender:
+                filters.append(Gift.gender == gender)
 
         items = Session.query(Gift).filter(*filters).all()
 
-        self.get(friend_id, items=items)
+        data_post = dict(
+            price=price,
+            category=category,
+            age=age,
+            gender=gender
+        )
+
+        self.get(friend_id, items=items, **data_post)
 
 
 class List(BaseHandler):

File controllers/user.py

 
 from tornado.web import HTTPError, authenticated
 
+import datetime
+
+
+def get_age(date_of_birth):
+
+    tmp = None
+
+    try:
+
+        date_of_birth = datetime.datetime.strptime(
+            date_of_birth, '%d/%m/%Y').date()
+
+        print 'c', date_of_birth
+
+        if date_of_birth > datetime.date.today().replace(
+                year=date_of_birth.year):
+            tmp = datetime.date.today().year - date_of_birth.year - 1
+        else:
+            tmp = datetime.date.today().year - date_of_birth.year
+
+    except Exception as exc:
+
+        logging.error(exc)
+
+    return tmp
+
 
 def base64_url_decode(inp):
 
                 'friends_interests',
                 'friends_birthday',
                 'read_stream',
+                'user_birthday'
             )
 
             params = dict(
 
                 params = dict(
                     access_token=user_data_facebook.get('oauth_token'),
-                    fields='email'
+                    fields='email,birthday,gender,name'
                 )
 
                 user_data_facebook_graph = self.load_data_url(
                 )
 
                 user = User()
+                user.name = user_data_facebook_graph.get('name')
                 user.fbid = user_data_facebook.get('user_id')
                 user.email = user_data_facebook_graph.get('email')
                 user.access_token = user_data_facebook.get('oauth_token')
 
+                if 'gender' in user_data_facebook_graph:
+                    user.gender = user_data_facebook_graph.get('gender')
+
+                if 'birthday' in user_data_facebook_graph:
+
+                    user_age = get_age(
+                        user_data_facebook_graph.get('birthday'))
+
+                    if user_age:
+                        user.age = user_age
+
                 Session.add(user)
 
                 try:
     __table_args__ = {'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8'}
 
     id = Column(Integer, primary_key=True)
+    name = Column(Unicode(200), nullable=False)
     email = Column(Unicode(50), unique=True, nullable=False)
     fbid = Column(Unicode(100), unique=True, nullable=False)
-    gifts = relationship('UserGift', backref='user')
     created_at = Column(DateTime, default=datetime.now)
     access_token = Column(Unicode(200), nullable=False)
+    age = Column(Integer, nullable=True)
+    gender = Column(Unicode(10), nullable=True)
+    gifts = relationship('UserGift', backref='user')
 
 class Gift(Base):
 

File templates/detail.html

                 <td>
                     <select name="price">
                         {% for c in prices %}
-                        <option value="{{ c[0] }}">{{ c[1] }}
+                        <option value="{{ c[0] }}" {% if c[0]==price %} selected {% endif %} >{{ c[1] }}
                         {% endfor %}
                     </select>
                 </td>
             </tr>
 
-            {% if has_birthday %}
+            {% if not user.age %}
             <tr>
                 <td> SELECCIONA EL RANGO DE EDAD DE TU AMIGO</td>
                 <td>
                     <select name="age">
                         {% for c in ages %}
-                        <option value="{{ c[0] }}">{{ c[1] }}
+                        <option value="{{ c[0] }}" {% if c[0]==age %} selected {% endif %}  >{{ c[1] }}
                         {% endfor %}
                     </select>
                 </td>
             </tr>
             {% endif %}
 
-            {% if has_gender %}
+            {% if not user.gender %}
             <tr>
                 <td> SELECCIONA EL G&EACUTE;NERO DE TU AMIGO</td>
                 <td>
                     <select name="gender">
-                        {% for c in gender %}
-                        <option value="{{ c[0] }}">{{ c[1] }}
+                        {% for c in genders %}
+                        <option value="{{ c[0] }}" {% if c[0]==gender %} selected {% endif %}  >{{ c[1] }}
                         {% endfor %}
                     </select>
                 </td>
                 <td>
                     <select name="category">
                         {% for c in categories %}
-                        <option value="{{ c[0] }}">{{ c[1] }}
+                        <option value="{{ c[0] }}" {% if c[0]==category %} selected {% endif %}  >{{ c[1] }}
                         {% endfor %}
                     </select>
                 </td>