示例#1
0
 def get(self, component_id):
     """ Gets the info about a component or
     gets a filtered list of the components stored in the system
     Keyword arguments: 
     self -- info about the request build by webapp2
     component_id -- path url directory corresponding to the component id
     """
     # Get the cookie in the request
     cookie_value = self.request.cookies.get("session")
     # Format is an optional param
     format = self.request.get("format", default_value="reduced")
     if not cookie_value == None:
         user_id = self.getUserInfo(cookie_value)
         if not user_id == None and format == "reduced" or format == "complete":
             format_flag = True if format == "complete" else False
             component = ndb_pb.getComponent(user_id, component_id, format_flag)
             if not component == None:
                 self.response.content_type = "application/json"
                 self.response.write(component)
                 self.response.set_status(200)
             else:
                 response = \
                 {"error": "Component not found in the system"}
                 self.response.content_type = "application/json"
                 self.response.write(json.dumps(response))
                 self.response.set_status(404)            
         else:
             response = \
                 {"error": "The cookie session or the format param provided are incorrect"}
             self.response.content_type = "application/json"
             self.response.write(json.dumps(response))
             self.response.set_status(400)
     else:
         response = {"error": "You must provide a session cookie"}
         self.response.content_type = "application/json"
         self.response.write(json.dumps(response))
         self.response.set_status(401)
示例#2
0
  def post(self, user_id):
    cookie_value = self.request.cookies.get("session")
    if not cookie_value == None:
      user_logged_key = self.getUserInfo(cookie_value)
      if not user_logged_key == None:
        user_logged_id = ndb_pb.getUserId(user_logged_key)
        user_info = ndb_pb.getUser(user_id)
        # Checks if the user active is the owner of the resource (if exists)
        if user_info == None:
          self.response.content_type = "application/json"
          self.response.write(json.dumps({"error": "The user requested does not exist"}))
          self.response.set_status(404)
        elif not user_info == None and user_logged_id==user_id: 
          values = self.request.POST
          # Dict that contains the user values and fields to be updated
          update_data = {}

          # We parse the data received in the request
          if values.has_key("description"):
            update_data["description"] = values.get("description")
          if values.has_key("website"):
            update_data["website"] = values.get("website")
          if values.has_key("image"):
            update_data["image"] = values.get("image")
          if values.has_key("phone"):
            update_data["phone"] = int(values.get("phone"))
          if values.has_key("email"):
            update_data["email"] = values.get("email")
          if values.has_key("private_phone"):
            # Checks if private_phone has a proper value
            if values.get("private_phone") in ["True", "true"]:
              private_phone = True
              update_data["private_phone"] = private_phone
            elif values.get("private_phone") in ["False", "false"]:
              private_phone = False
              update_data["private_phone"] = private_phone
          if values.has_key("private_email"):
             # Checks if private_email has a proper value
            if values.get("private_email") in ["True", "true"]:
              private_email = True
              update_data["private_email"] = private_email
            elif values.get("private_email")in ["False", "false"]:
              private_email = False
              update_data["private_email"] = private_email
          if values.has_key("component"):
            component_id = values.get("component")      
            component = ndb_pb.getComponent(user_logged_key, component_id)
            # If the component_id provided in the request exists in the system and the user has not added it previously,
            # we add the component_id provided to the list of user's data to be updated
            if not component == None:
              update_data["component"] = component_id
          
          # Updates the resource 
          if not len(update_data) == 0:
            updated_info = ndb_pb.updateUser(user_logged_key, update_data)
            if not len(updated_info) == 0:
              self.response.content_type = "application/json"
              self.response.write(json.dumps({"details": "The update has been successfully executed", "status": "Updated", "updated": update_data.keys()}))
              self.response.set_status(200)
            else:
              self.response.content_type = "application/json"
              self.response.write(json.dumps({"details": "Resource not modified (check parameters and values provided)", "status": "Not Modified"}))
              self.response.set_status(304)   
          else:
            self.response.content_type = "application/json"
            self.response.write(json.dumps({"details": "Resource not modified (check parameters and values provided)", "status": "Not Modified"}))
            self.response.set_status(304) 
        else:
          self.response.content_type = "application/json"
          self.response.write(json.dumps({"error": "You don\"t have the proper rights to modify this resource" +
            " (The cookie session header does not match with the resource requested)"}))
          self.response.set_status(401)
      else:
        self.response.content_type = "application/json"
        self.response.write(json.dumps({"error": "The session cookie header does not belong to an active user in the system"}))
        self.response.set_status(400)
    else:
      self.response.content_type = "application/json"
      self.response.write(json.dumps({"error": "The user is not authenticated"}))
      self.response.set_status(401)
示例#3
0
    def post(self, user_id):
        cookie_value = self.request.cookies.get("session")
        if not cookie_value == None:
            user_logged_key = self.getUserInfo(cookie_value)
            if not user_logged_key == None:
                user_logged_id = ndb_pb.getUserId(user_logged_key)
                user_info = ndb_pb.getUser(user_id)
                # Checks if the user active is the owner of the resource (if exists)
                if user_info == None:
                    self.response.content_type = "application/json"
                    self.response.write(
                        json.dumps(
                            {"error": "The user requested does not exist"}))
                    self.response.set_status(404)
                elif not user_info == None and user_logged_id == user_id:
                    values = self.request.POST
                    # Dict that contains the user values and fields to be updated
                    update_data = {}

                    # We parse the data received in the request
                    if values.has_key("description"):
                        update_data["description"] = values.get("description")
                    if values.has_key("website"):
                        update_data["website"] = values.get("website")
                    if values.has_key("image"):
                        update_data["image"] = values.get("image")
                    if values.has_key("phone"):
                        update_data["phone"] = int(values.get("phone"))
                    if values.has_key("email"):
                        update_data["email"] = values.get("email")
                    if values.has_key("private_phone"):
                        # Checks if private_phone has a proper value
                        if values.get("private_phone") in ["True", "true"]:
                            private_phone = True
                            update_data["private_phone"] = private_phone
                        elif values.get("private_phone") in ["False", "false"]:
                            private_phone = False
                            update_data["private_phone"] = private_phone
                    if values.has_key("private_email"):
                        # Checks if private_email has a proper value
                        if values.get("private_email") in ["True", "true"]:
                            private_email = True
                            update_data["private_email"] = private_email
                        elif values.get("private_email") in ["False", "false"]:
                            private_email = False
                            update_data["private_email"] = private_email
                    if values.has_key("component"):
                        component_id = values.get("component")
                        component = ndb_pb.getComponent(
                            user_logged_key, component_id)
                        # If the component_id provided in the request exists in the system and the user has not added it previously,
                        # we add the component_id provided to the list of user's data to be updated
                        if not component == None:
                            update_data["component"] = component_id

                    # Updates the resource and return the proper response to the client
                    if not len(update_data) == 0:
                        updated_info = ndb_pb.updateUser(
                            user_logged_key, update_data)
                        if not len(updated_info) == 0:
                            self.response.content_type = "application/json"
                            self.response.write(
                                json.dumps({
                                    "details":
                                    "The update has been successfully executed",
                                    "status": "Updated",
                                    "updated": update_data.keys()
                                }))
                            self.response.set_status(200)
                        # We return a custom error message if the request had as purpose adding a component to the user's dashboard
                        elif len(updated_info) == 0:
                            self.response.content_type = "application/json"
                            self.response.set_status(304)
                            if update_data.has_key("component_id"):
                                self.response.write(
                                    json.dumps({
                                        "details":
                                        "Resource not modified (The component specified does not exists"
                                        +
                                        "or the user has not added to its account the social networks that consumes the component)",
                                        "status":
                                        "Not Modified"
                                    }))
                            else:
                                self.response.write(
                                    json.dumps({
                                        "details":
                                        "Resource not modified (check parameters and values provided)",
                                        "status": "Not Modified"
                                    }))
                    else:
                        self.response.content_type = "application/json"
                        self.response.write(
                            json.dumps({
                                "details":
                                "Resource not modified (It hasn't been specified any valid parameter for this method)",
                                "status": "Not Modified"
                            }))
                        self.response.set_status(304)

                # Status errors related to permission and user authentication
                else:
                    self.response.content_type = "application/json"
                    self.response.write(
                        json.dumps({
                            "error":
                            "You don\"t have the proper rights to modify this resource"
                            +
                            " (The cookie session header does not match with the resource requested)"
                        }))
                    self.response.set_status(401)
            else:
                # We invalidate the session cookies received
                expire_date = datetime.datetime(1970, 1, 1, 0, 0, 0)
                self.response.set_cookie("session",
                                         "",
                                         path="/",
                                         domain=domain,
                                         secure=True,
                                         expires=expire_date)
                # We delete and invalidate other cookies received, like the user logged nickname
                # and social network in which the user performed the login
                if not self.request.cookies.get("social_network") == None:
                    self.response.set_cookie("social_network",
                                             "",
                                             path="/",
                                             domain=domain,
                                             secure=True,
                                             expires=expire_date)
                if not self.request.cookies.get("user") == None:
                    self.response.set_cookie("user",
                                             "",
                                             path="/",
                                             domain=domain,
                                             secure=True,
                                             expires=expire_date)

                # Builds the response
                self.response.content_type = "application/json"
                self.response.write(
                    json.dumps({
                        "error":
                        "The session cookie header does not belong to an active user in the system"
                    }))
                self.response.set_status(400)
        else:
            self.response.content_type = "application/json"
            self.response.write(
                json.dumps({"error": "The user is not authenticated"}))
            self.response.set_status(401)
示例#4
0
    def get(self, component_id):
        """ Gets the info about a component or
        gets a filtered list of the components stored in the system
        Keyword arguments:
        self -- info about the request build by webapp2
        component_id -- path url directory corresponding to the component id
        """
        # Get the cookie in the request
        cookie_value = self.request.cookies.get("session")
        # Format is an optional param
        format = self.request.get("format", default_value="reduced")
        if not cookie_value == None:
            user_id = self.getUserInfo(cookie_value)
            if not user_id == None:
                if format == "reduced" or format == "complete":
                    format_flag = True if format == "complete" else False
                    component = ndb_pb.getComponent(user_id, component_id,
                                                    format_flag)
                    if not component == None:
                        comp_aux = json.load(component)
                        comp_aux[
                            "ref"] = "centauro.ls.fi.upm.es/bower_components/" + comp_aux[
                                "component_id"] + "-" + comp_aux["version"]
                        component = json.dumps(comp_aux)
                        self.response.content_type = "application/json"
                        self.response.write(component)
                        self.response.set_status(200)
                    else:
                        response = \
                        {"error": "Component not found in the system"}
                        self.response.content_type = "application/json"
                        self.response.write(json.dumps(response))
                        self.response.set_status(404)
                else:
                    response = \
                    {"error": "The format param provided is incorrect"}
                    self.response.content_type = "application/json"
                    self.response.write(json.dumps(response))
                    self.response.set_status(400)
            else:
                # We invalidate the session cookie received
                expire_date = datetime.datetime(1970, 1, 1, 0, 0, 0)
                self.response.set_cookie("session",
                                         "",
                                         path="/",
                                         domain=domain,
                                         secure=True,
                                         expires=expire_date)
                # We delete and invalidate other cookies received, like the user logged nickname
                # and social network in which performed the login
                if not self.request.cookies.get("social_network") == None:
                    self.response.set_cookie("social_network",
                                             "",
                                             path="/",
                                             domain=domain,
                                             secure=True,
                                             expires=expire_date)
                if not self.request.cookies.get("user") == None:
                    self.response.set_cookie("user",
                                             "",
                                             path="/",
                                             domain=domain,
                                             secure=True,
                                             expires=expire_date)

                # We write the response providing details about the error
                response = \
                    {"error": "The session cookie provided is incorrect"}
                self.response.content_type = "application/json"
                self.response.write(json.dumps(response))
                self.response.set_status(400)
        else:
            response = {"error": "You must provide a session cookie"}
            self.response.content_type = "application/json"
            self.response.write(json.dumps(response))
            self.response.set_status(401)