def update_user_info(self):
     username = current_user.get_id()
     try:
         collection.update_one({'username': username},
                               {"$set": self.info_dict},
                               upsert=False)
         return svc_utils.get_sample_response(False, None, None,
                                              current_user.get_id())
     except pymongo.errors.PyMongoError as e:
         logger.error("Error while updating the user info")
         return svc_utils.get_sample_response(True,
                                              "User info update failed",
                                              "Update failed",
                                              current_user.get_id())
 def get(self):
     user_doc = collection.find_one({'username': current_user.get_id()})
     try:
         suggestion = user_doc["weekly_suggestion"]
     except KeyError as k:
         raise errors.SERVER_ERROR
     return svc_utils.get_sample_response(False, None, suggestion,
                                          current_user.get_id())
def get_comparison_with_firends():
    logger.info("Received request")
    try:
        response = FRIEND_COMPARISON.invoke_insert(request)
    except Exception as e:
        return svc_utils.get_response_from_dict(
            svc_utils.get_sample_response(True, e.message, "Failed",
                                          current_user.get_id()))
    return response
def add_friend():
    logger.info("Add friend request")
    try:
        response = ADD_FRIEND.invoke_insert(request)
    except Exception as e:
        return svc_utils.get_response_from_dict(
            svc_utils.get_sample_response(
                True, e.message, "Error while sending friend request",
                current_user.get_id()))
    return response
def get_users_list():
    logger.info("received request")

    try:
        response = GET_USERS_LIST.invoke_insert(request)
    except Exception as e:
        return svc_utils.get_response_from_dict(
            svc_utils.get_sample_response(True, e.message,
                                          "Error while fetching users list",
                                          current_user.get_id()))
    return response
def handle_activities():
    logger.info("Recieved request for fetching activities %s",
                current_user.get_id())
    try:
        response = GET_USER_FEED.invoke_insert(request)
    except Exception as e:
        return svc_utils.get_response_from_dict(
            svc_utils.get_sample_response(True, e.message,
                                          "Error while fetching activities",
                                          current_user.get_id()))
    return response
 def get(self):
     try:
         friend_to_add = collection.find_one({"username": self.add_friend})
         if None == friend_to_add:
             logger.error("Username to be added doesn't exist")
             return svc_utils.get_sample_response(
                 True, "Username to be added doesn't exist",
                 "Username to be added doesn't exist",
                 current_user.get_id())
         else:
             friend_gmc_auth = friend_to_add['credentials']['gmc'][
                 'auth_key']
         data = {
             "to": friend_gmc_auth,
             "notification": {
                 "title": "Friend request",
                 "message":
                 str(current_user.get_id()) + " sent you a friend request",
                 "sound": ""
             }
         }
         gcmessenger = GCMessage()
         if gcmessenger.post_request(data):
             collection.update_one(
                 {'username': current_user.get_id()},
                 {'$addToSet': {
                     'sent_friend_request': self.add_friend
                 }})
             return svc_utils.get_sample_response(
                 False, None, "Friend request sent successfully!",
                 current_user.get_id())
         else:
             return svc_utils.get_sample_response(
                 True, "Error while sending friend request",
                 "Error while sending friend request",
                 current_user.get_id())
     except Exception as e:
         logger.error("Error while sending friend request")
         return svc_utils.get_sample_response(
             True, "Error while sending friend request",
             "Error while sending friend request", current_user.get_id())
def take_action_friend_request():
    logger.info(
        "Received request for taking action on a friend request. request-%s",
        request)

    try:
        response = FRIEND_REQUEST_ACTION.invoke_insert(request)
    except Exception as e:
        return svc_utils.get_response_from_dict(
            svc_utils.get_sample_response(
                True, e.message, "Error while acting on friend request",
                current_user.get_id()))
    return response
 def get(self):
     try:
         users_list = []
         regx = re.compile(self.regex_str)
         users_list_doc = collection.find({'username': {"$regex": regx}})
         for doc in users_list_doc:
             user_inf = {
                 'username': doc.get('username', None),
                 'date_of_birth': doc.get('date_of_birth'),
                 'location': doc.get('location', None),
                 'name': doc.get('name', None),
                 'weight': doc.get('weight', None),
                 'height': doc.get('height', None)
             }
             users_list.append(user_inf)
         return svc_utils.get_sample_response(False, None,
                                              {'usersList': users_list},
                                              current_user.get_id())
     except Exception as e:
         return svc_utils.get_sample_response(True,
                                              "Error while fetching users",
                                              {'usersList': None},
                                              current_user.get_id())
def update_user_info():
    logger.info("Recieved request for updating user's info by user- %s",
                current_user.get_id())
    start = time.time()
    try:
        response = UPDATE_INFO_SERV.invoke_insert(request)
    except Exception as e:
        return svc_utils.get_response_from_dict(
            svc_utils.get_sample_response(True, e.message,
                                          "Error while updating info",
                                          current_user.get_id()))
    logger.info("Request evaluation time in seconds: %s",
                str(time.time() - start))
    logger.info("Sending a reponse for single request for raw text")
    return response
    def get(self):

        if self.filter != None:
            try:
                query = {
                    'username':
                    current_user.get_id().encode('ascii', 'ignore')
                }
                if self.filter.get('endTime') != None:
                    query['endTime'] = {'$lt': self.filter.get('endTime')}
                if self.filter.get('startTime') != None:
                    query['startTime'] = {'$gt': self.filter.get('startTime')}
                if self.filter.get('activity') != None:
                    query['activity'] = self.filter.get('activity')
                activity_list_doc = activity_collection.find(query)
                activity_list = []
                for doc in activity_list_doc:
                    doc_copy = doc.copy()
                    doc_copy['_id'] = str(doc_copy.get("_id"))
                    activity_list.append(doc_copy)
                return svc_utils.get_sample_response(
                    False, None, {'activity_list': activity_list},
                    current_user.get_id())

            except Exception as e:
                return svc_utils.get_sample_response(
                    True, "Error while fetching Activities1",
                    {'usersList': None}, current_user.get_id())
        if self.activity != None:
            if self.activity.get('_id') == None:
                activity_collection.insert(self.activity)
                activity_copy = self.activity.copy()
                activity_copy['_id'] = str(activity_copy.get("_id"))
                return svc_utils.get_sample_response(
                    False, None, {'activity': activity_copy},
                    current_user.get_id())
 def get(self):
     try:
         friend_request_sendor = collection.find_one(
             {'username': self.add_friend})
         print self.action == 'ACCEPT', current_user.get_id()
         if None == friend_request_sendor:
             logger.error("Given username doesn't exist")
             return svc_utils.get_sample_response(
                 True, "Given username doesn't exist", "No such user found",
                 current_user.get_id())
         elif self.action == "ACCEPT":
             sent_friend_request = friend_request_sendor[
                 'sent_friend_request']
             if current_user.get_id() in sent_friend_request:
                 collection.update_one({'username': self.add_friend}, {
                     '$pull': {
                         'sent_friend_request': current_user.get_id()
                     },
                     '$push': {
                         'friends': current_user.get_id()
                     }
                 })
                 collection.update_one(
                     {'username': current_user.get_id()},
                     {'$push': {
                         'friends': self.add_friend
                     }})
                 logger.info("Friend request accepted successfully")
                 gmc_auth = friend_request_sendor['credentials']['gmc'][
                     'auth_key']
                 data = {
                     "to": gmc_auth,
                     "notification": {
                         "title": "Friend request accepted",
                         "username": str(current_user.get_id()),
                         "message": str(current_user.get_id()) +
                         "accepted your friend request",
                         "sound": ""
                     }
                 }
                 gcmessenger = GCMessage()
                 if not gcmessenger.post_request(data):
                     logger.error("Friend request notification not sent")
                 return svc_utils.get_sample_response(
                     False, None, "Added to friends", current_user.get_id())
             else:
                 print "testing checkpoint"
                 logger.error("Invalid username passed")
                 return svc_utils.get_sample_response(
                     True, "Request not found", "Request not found",
                     current_user.get_id())
         elif self.action == "REJECT":
             collection.update_one(
                 {'username': self.add_friend},
                 {'$pull': {
                     'sent_friend_request': current_user.get_id()
                 }})
             return svc_utils.get_sample_response(False, None,
                                                  "Request deleted!")
     except Exception as e:
         logger.error("Error while responding to friend request")
         return svc_utils.get_sample_response(True, "Failed", "Failed",
                                              current_user.get_id())
     return svc_utils.get_sample_response(True, "Failed", "Failed",
                                          current_user.get_id())