Ejemplo n.º 1
0
      def post(self):
        cookies = h.get_default_cookies(self)
        if '_pvk' not in cookies or (cookies['_pvk'] == None or cookies['_pvk'] == ''):
            new_session_key = str(datetime.datetime.utcnow()).replace(' ', '') + '-' + str(random.getrandbits(50))
            cookies['_pvk'] = new_session_key

        previous_page_view_for_session = db.GqlQuery("SELECT * FROM PageView WHERE session_id = :1 ORDER BY created_at DESC", cookies['_pvk']).get()

        page_view = PageView()
        current_user = h.get_current_user(cookies)
        
        if current_user != None:
              page_view.user = current_user

        page_view.url = self.request.get('u')
        page_view.normalized_url = self.request.get('u')[:500] # TODO actually normalize URLs according to some logic (e.g. stripping out fb params)
        page_view.referrer = self.request.get('referrer')
        page_view.session_id = cookies['_pvk']
        page_view.ip_address = self.request.remote_addr
        
        
        try: 
          page_view.user_agent = self.request.headers['User-Agent'][:500]
        except:
          page_view.user_agent = None
        
        if previous_page_view_for_session:
            page_view.session_order = previous_page_view_for_session.session_order + 1
        else:
            page_view.session_order = 0
        
        page_view.put()

        self.response.out.write('{status: \'ok\'}')
Ejemplo n.º 2
0
 def get_answers(self):
     query = Answer.all()
     cookies = h.get_default_cookies(self)
     current_user = h.get_current_user(cookies)
     query.filter("user =", current_user)
     answers = query.fetch(1000)
     return answers
Ejemplo n.º 3
0
 def post(self):
     cookies = h.get_default_cookies(self)
     current_user = h.get_current_user(cookies)
     
   	if current_user != None and '_pvk' in cookies and cookies['_pvk'] != None and cookies['_pvk'] != '':
         unlinked_result_views = ResultView.gql("WHERE session_id = :1 AND user = NULL", cookies['_pvk']).fetch(5)
         for result_view in unlinked_result_views:
             result_view.user = current_user
             if current_user.fb_user_id != None:
                 result_view.fb_user_id = current_user.fb_user_id
             
             # Create a "backdated" version for this query, because the user probably made the query while 
             # logged out, and it's worthwile knowing that that happened
             if result_view.query_string != None and result_view.query_string != '':
                 query = Query(query_string = result_view.query_string, referrer = self.request.referer, url = "__ARTIFICIALLY_CREATED_FOR_USER__")
                 
                 query.user = current_user
                 if current_user.fb_user_id != None:
                     query.fb_user_id = current_user.fb_user_id
                 if '_pvk' in cookies and cookies['_pvk'] != None and cookies['_pvk'] != '':
                     query.session_id = cookies['_pvk']
                 
                 query.put()
             
                 result_view.query = query
                 result_view.put()
                 fb_post_id = QueryFacebookWallpostHelper.postToWallIfNecessary(current_user, result_view, query, result_view.image_url)
             
             else:
                 result_view.put()
     
     self.response.out.write('{status: \'ok\'}')
Ejemplo n.º 4
0
    def get(self):
        c = h.context()
        logging.info("***Auth2")
        cookies = h.get_default_cookies(self)
        c['cookies'] = cookies
        logging.info("***cookies: "+str(cookies))
        c['access_token'] = self.request.get("access_token")
        
        if self.request.get("access_token") == '':
            logging.error("No access token set, but we expected on %s" % str(self.request.url))
        else:
            logging.warning("Got access token %s" % self.request.get("access_token"))
        
        user = User.gql("WHERE fb_oauth_access_token = :1", self.request.get("access_token")).get()
        if user != None:
            # If the user currently exists in the DB
            logging.info("***SCU1")
            h.set_current_user(cookies, user)
            logging.info("***cookies: "+str(cookies))
            c['current_user'] = user
        else: 
            # Look user up in graph, and either find them in the DB or create them if they don't exist
            try:
                graph = facebook.GraphAPI(self.request.get("access_token"))
                me = graph.get_object('me')
            except Exception, e:
                logging.error(
                    "ERROR!  Failed to access Facebook Graph for access_token=" + 
                    self.request.get("access_token") +
                    "::" + str(e)) 
                me = None
                c['error'] = e

            if me != None:
                og_user = User.gql("WHERE fb_user_id = :1", str(me['id'])).get()
                
                if og_user == None:
                    new_user = User(
                        fb_user_id = str(me['id']),
                        fb_oauth_access_token = self.request.get("access_token"),
                        fb_oauth_access_token_stored_at = datetime.datetime.utcnow(),
                        first_name = me['first_name'], 
                        last_name = me['last_name'])
                    if 'email' in me:
                        new_user.email = me['email']
                    new_user.put()
                    logging.info("***SCU2")
                    h.set_current_user(cookies, new_user)
                    logging.info("***cookies: "+str(cookies))
                    c['current_user'] = new_user
                    
                else: # Update auth token for user because it's out of date
                    og_user.fb_oauth_access_token = self.request.get("access_token")
                    og_user.fb_oauth_access_token_stored_at = datetime.datetime.utcnow()
                    og_user.put()
                    logging.info("***SCU3")
                    h.set_current_user(cookies, og_user)
                    logging.info("***cookies: "+str(cookies))
                    c['current_user'] = og_user
Ejemplo n.º 5
0
 def add_answer(self, atext, qkey):
     answer = Answer()
     cookies = h.get_default_cookies(self)
     current_user = h.get_current_user(cookies)
     if current_user:
         answer.user = current_user
     answer.atext = atext
     answer.qkey = db.Key(qkey)
     answer.put()
     return answer
Ejemplo n.º 6
0
def add_question(qtype, qtext, hint, img, handler):
    question = Question()
    cookies = h.get_default_cookies(handler)
    current_user = h.get_current_user(cookies)
    if current_user:
        question.user = current_user
    question.qtype = int(qtype)
    question.qtext = qtext
    question.hint = hint
    question.img = img
    question.status = 1
    question.put()
    return question
Ejemplo n.º 7
0
    def post(self):
        logging.info("***rvlh")
        cookies = h.get_default_cookies(self)
        current_user = h.get_current_user(cookies)
        result_view = ResultView()
      	result_view.source = self.request.get('src')
      	result_view.referrer = self.request.get('referrer')
      	result_view.url = self.request.get('u')
        result_view.ip_address = self.request.remote_addr
        result_view.image_url = self.request.get('image_url')
        result_view.query_string = self.request.get('q')
      	#logging.info(" ".join((result_view.source, result_view.referrer, result_view.url, result_view.ip_address,
        #                       result_view.image_url, result_view.query_string)))
      	if '_pvk' in cookies and cookies['_pvk'] != None and cookies['_pvk'] != '':
            result_view.session_id = cookies['_pvk']
        
        if current_user != None:
            result_view.user = current_user
            if current_user.fb_user_id != None:
                result_view.fb_user_id = current_user.fb_user_id
                
            query = Query.gql(
                "WHERE user = :1 AND query_string = :2 ORDER BY created_at DESC", 
                current_user.key(), 
                self.request.get('q')).get()

            if query != None:
                result_view.query = query

        result_view.put()
        
        # Post to facebook wall for the query if the query hasn't already posted to wall
        if current_user != None and query != None and query.fb_wall_post_id == None:
            logging.info("***Posting")
            image_url = self.request.get('image_url')
            if not image_url:
                from google.appengine.api import urlfetch
                from django.utils import simplejson
                q = 'site:'+result_view.url+" "+result_view.query_string
                ue_query = urllib.quote_plus(q)
                url = 'http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q='\
                      +ue_query+'&key='\
                      +h.cfg['gs_api_key']
                result = urlfetch.fetch(url)
                o = simplejson.loads(result.content)
                image_url = o['responseData']['results'][0]['tbUrl']
            fb_post_id = QueryFacebookWallpostHelper.postToWallIfNecessary(current_user, result_view, query, image_url)
            self.response.out.write('{status: \'ok\', fb_wall_post_id: \'%s\', result_view_key: \'%s\'}' % (fb_post_id, str(result_view.key())))
        else:
            # logging.info("***NoPost : "+current_user+" "+query+" "+query.fb_wall_post_id)
            self.response.out.write('{status: \'ok\', result_view_key: \'%s\', result_view_has_user: \'%s\'}' % (str(result_view.key()), str(current_user != None)))
Ejemplo n.º 8
0
    def get(self):
        c = h.context()
        s = self.request.get("s")
        c["s"] = s
        c["data"] = xrange(6)
        r = list()
        r += [[0, 0]]
        r += [[253, 0]]
        r += [[506, 0]]
        r += [[0, 253]]
        r += [[253, 253]]
        r += [[506, 253]]
        c["rects"] = r
        c["stash_items"] = xrange(7)
        cookies = h.get_default_cookies(self)
        u = h.get_current_user(cookies)
        if u:
            c["uid"] = u.fb_user_id

        # get some appropriate questions
        query = Question.all()
        query.filter("qtype =", 1).filter("status =", 1)
        questions = query.fetch(1000)
        building = list()
        for o in questions:
            d = dict()
            # d['qtype'] = o.qtype
            d["key"] = str(o.key())
            d["qtext"] = o.qtext
            d["hint"] = o.hint
            d["img"] = str(o.img.key())
            # d['status'] = o.status
            # d['created_at'] = str(o.created_at)
            building += [d]
        c["questions"] = simplejson.dumps(building)

        answers = self.get_answers()
        building = list()
        for o in answers:
            d = dict()
            d["key"] = str(o.key())
            d["atext"] = o.atext
            d["qkey"] = str(o.qkey)
            building += [d]
        c["answers"] = simplejson.dumps(building)

        h.render_out(self, "questions.tplt", c)
Ejemplo n.º 9
0
    def post(self):
        cookies = h.get_default_cookies(self)
        current_user = h.get_current_user(cookies)
        query = Query(query_string = self.request.get('q'))
        
      	query.referrer = self.request.get('referrer')
      	query.url = self.request.get('u')
        query.ip_address = self.request.remote_addr
        if current_user != None:
            query.user = current_user
            if current_user.fb_user_id != None:
                query.fb_user_id = current_user.fb_user_id

        if '_pvk' in cookies and cookies['_pvk'] != None and cookies['_pvk'] != '':
            query.session_id = cookies['_pvk']
        
        query.put()
        self.response.out.write('{status: \'ok\'}')