示例#1
0
 def getFullUserRecord(self, user, now=None):
     try:
         places_id2json = {}
         vote_list = []
         if settings.config['all_are_friends']:
             q = User.gql('')
         else:
             # start with me
             q = [user]
             # then get my friends
             for f in user.get_friends_key_list():
                 q.append(f.get())
         place_json = None
         place_id = None
         for u in q:
             if u:
                 user_votes = models.Vote.query(
                     models.Vote.voter == u.key.integer_id()).fetch()
                 for vote in user_votes:
                     try:
                         place_id = vote.item.id()
                         vote_list.append(vote.get_json())
                         if not place_id in places_id2json:
                             place_json = models.Item.id_to_json(place_id)
                             if "cuisineName" in place_json:
                                 places_id2json[place_id] = place_json
                     except Exception, e:
                         if place_json:
                             logging_ext.error(
                                 "** getFullUserRecord Exception 1 %s" %
                                 place_json['place_name'],
                                 exc_info=True)
                         else:
                             logging_ext.error(
                                 "** getFullUserRecord Exception %s" %
                                 place_id,
                                 exc_info=True)
         return vote_list, places_id2json
示例#2
0
    def get(self):
        """ get the user record, including friends' places """
        try:
            if self.user.blocked:
                raise Exception('Blocked')
            my_id = self.user_id

        except:
            logging_ext.error('** getFullUserRecord: User Exception')
            json.dump({'result': 'FAIL'},
                      self.response.out,
                      default=views.json_serial)
            return

        if my_id:
            try:
                # logged in
                #check if the client version is allowed
                good_version = False
                if 'version' in self.request.params:
                    version = float(self.request.params['version'])
                    min_version = Config.min_server_version_allowed()
                    if version >= float(min_version):
                        good_version = True
                if not good_version:
                    logging_ext.error(
                        "getUserRecordFastViaWorkers BAD VERSION")
                    self.response.out.write("BAD_VERSION")
                    return

                #good client version if we got here
                result = {
                    "id": my_id,
                    "admin": self.user.profile().is_admin,
                    "version": settings.config["version"],
                    "min_version": settings.config['min_version']
                }
                since = None
                now = datetime.now()
                if 'since' in self.request.params:
                    try:
                        # move since back in time to allow for error
                        since = datetime.strptime(
                          self.request.params['since'],
                          views.config['DATETIME_FORMAT']) - \
                                views.config['TIMING_DELTA']
                        vote_list, place_id2json = self.getIncrement(
                            my_id, since)
                    except OverflowError, ex:
                        logging_ext.error(
                            "** getFullUserRecord Time error with %s" % since,
                            exc_info=True)
                        #full update
                        vote_list, place_id2json = self.getFullUserRecord(
                            self.user)
                else:
                    #full update
                    vote_list, place_id2json = self.getFullUserRecord(
                        self.user)

                friends_list = []
                if views.config['all_are_friends']:
                    q = User.gql('')
                    for u in q:
                        user_str = {
                            "id": u.get_id(),
                            # todo is it first_name?
                            'name': u.screen_name
                        }
                        friends_list.append(user_str)
                else:
                    friends = self.user.get_friends_key_list()
                    for f in friends:
                        friend = f.get()
                        try:
                            user_str = {
                                "id": f.id(),
                                # todo is it first_name?
                                'name': friend.screen_name
                            }
                        except:
                            logging.error("getFullUserRecord Friends error")
                        friends_list.append(user_str)

                sentInvites = models.InviteInternal.query(
                    models.InviteInternal.inviter == my_id)
                recdInvites = models.InviteInternal.query(
                    models.InviteInternal.invitee == my_id)
                sent = []
                for i in sentInvites:
                    sent.append(i.to_json())
                recd = []
                for i in recdInvites:
                    recd.append(i.to_json())
                result["sentInvites"] = sent
                result["receivedInvites"] = recd
                result['votes'] = vote_list
                result["places"] = place_id2json
                result["friendsData"] = friends_list
                json_str = json.dumps(result, default=views.json_serial)
                try:
                    since_str = str(since) if since else ""
                    logging.debug(
                        "GetFullUserRecord for %s %s P:%d, V:%d, F:%d" %
                        (self.user.screen_name, since_str, len(place_id2json),
                         len(vote_list), len(friends_list)))
                except:
                    pass
                try:
                    #logging
                    logging.debug("getUserRecordFastViaWorkers done ")
                except:
                    pass
                self.response.out.write(json_str)
                #profile_out("getFullUserRecord")
                return
示例#3
0
文件: views.py 项目: wgilpin/shout
  def get(self):
    """ get the entire user record, including friends' places """
    try:
      if self.user.blocked:
        raise Exception('Blocked')
      my_id = self.user_id

    except:
      logging_ext.error('getFullUserRecord: User Exception')
      json.dump({'result':'FAIL'},
                  self.response.out,
                  default=json_serial)
      return

    if my_id:
      #profile_in("getFullUserRecord")
      user = User.get_by_id(my_id)
      if user:
        # logged in
        since = None
        if 'since' in self.request.params:
          # move since back in time to allow for error
          since = datetime.datetime.strptime(
            self.request.params['since'],
            config['DATETIME_FORMAT']) - \
                  config['TIMING_DELTA']
        # is it for a specific user?
        if "forUser" in self.request.params:
          for_1_user = long(self.request.get("forUser"))
        else:
          for_1_user = None

        # either the first lookup is for me, plus everyone,
        # or it is for a specified user
        result = {
          "id": my_id,
          "admin": self.user.profile().is_admin }
        if for_1_user:
          logging.info("getFullUserRecord: 1 user")
          first_user = for_1_user
          result["for_1_user"] = for_1_user
        else:
          logging.info("getFullUserRecord: 1+ user")
          first_user = my_id
        dict_id_place = {}
        # load the data for the 1 user  - me or specified
        friends_data = [
          serialize_user_details(
            first_user,
            dict_id_place,
            my_id,
            self.request,
            since)]
        # was it for all users? If so we've only done ourselves
        if not for_1_user:
          # for all users
          prof = user['p']
          if config['all_are_friends']:
            q = User.gql('')
            logging.info("getFullUserRecord: %d friends"%q.count())
            for user in q:
            # for userProf in UserProfile().all():
              if user.get_id() == my_id:
                continue  # don't add myself again
              data = serialize_user_details(
                user.get_id(), dict_id_place, my_id, self.request, since)
              logging.info("getFullUserRecord: record %s"%data)
              friends_data.append(data)
          else:
            for friend in prof.friends:
              friends_data.append(serialize_user_details(
                friend, dict_id_place, my_id, self.request, since))
          result["friendsData"] = friends_data
          logging.debug('getFullUserRecord: return %d places'%len(dict_id_place))
        result["places"] = dict_id_place
        # encode using a custom encoder for datetime

        json_str = json.dumps(
          result,
          default=json_serial)
        self.response.out.write(json_str)
        #profile_out("getFullUserRecord")
        return
    self.error(401)
示例#4
0
文件: views.py 项目: wgilpin/shout
  def get(self):
    """ get the user record, including friends' places """
    try:
      if self.user.blocked:
        raise Exception('Blocked')
      my_id = self.user_id

    except:
      logging_ext.error('getFullUserRecord: User Exception')
      json.dump({'result':'FAIL'},
                  self.response.out,
                  default=json_serial)
      return

    if my_id:
      user = User.get_by_id(my_id)
      if user:
        # logged in
        result = {
          "id": my_id,
          "admin": self.user.profile().is_admin }
        since = None
        if 'since' in self.request.params:
          # move since back in time to allow for error
          since = datetime.datetime.strptime(
            self.request.params['since'],
            config['DATETIME_FORMAT']) - \
                  config['TIMING_DELTA']
        user_list = []
        user_results = []
        # is it for a specific user?
        if "forUser" in self.request.params:
          user_list.append(user.get(self.request.params['forUser']))
        else:
          if config['all_are_friends']:
            q = User.gql('')
            for user in q:
              user_list.append(user)
        places = {}
        my_votes = Vote.query(Vote.voter==my_id)
        for u in user_list:
          user_id = u.get_id()
          if user_id == my_id:
            votes = my_votes
          else:
            votes = Vote.query(Vote.voter==u.get_id())
          for v in votes:
            #add to the list if it's not there, or overwrite if this is my version
            if not v in places or user_id == my_id:
              places [v] = Item.id_to_json(v)

          user_profile = u.profile()
          if getProp(user_profile, 'last_write'):
            last_write = user_profile.last_write
          else:
            last_write = None
          user_str = {"votes": votes,
              "id": u.get_id(),
              # todo is it first_name?
              'name': u.screen_name,
              'last_write': last_write}
          user_results.append(user_str)

        result["places"] = places
        result["friendsData"] = user_results
        json_str = json.dumps(
          result,
          default=json_serial)
        self.response.out.write(json_str)
        #profile_out("getFullUserRecord")
        return
    self.error(401)