Ejemplo n.º 1
0
    def normilize_field(self, field):
        """Convert field from date/db.Model/query to string value."""
        # pylint: disable=protected-access
        normilized_field = field

        if isinstance(normilized_field, datetime):
            normilized_field = self.normilize_field(normilized_field.date())
        elif isinstance(normilized_field, date):
            normilized_field = str(normilized_field)
        elif isinstance(normilized_field, db.Model):
            normilized_field = {
                "type": normilized_field.type,
                "id": normilized_field.id
            }
        elif isinstance(normilized_field,
                        dict) and "email" in normilized_field:
            user = user_generator.find_user_by_email(normilized_field["email"])
            normilized_field = self.normilize_field(user)
        elif isinstance(normilized_field, dict):
            normilized_field.pop("context_id", None)
            normilized_field.pop("href", None)
        elif isinstance(normilized_field, list):
            normilized_field = [
                self.normilize_field(i) for i in normilized_field
            ]
        elif isinstance(
                normilized_field,
            (associationproxy._AssociationList, collections.InstrumentedList)):
            normilized_field = [{
                "type": i.type,
                "id": i.id
            } for i in normilized_field]
        return normilized_field
Ejemplo n.º 2
0
  def normilize_field(self, field):
    """Convert field from date/db.Model/query to string value."""
    # pylint: disable=protected-access
    normilized_field = field

    if isinstance(normilized_field, datetime):
      normilized_field = self.normilize_field(normilized_field.date())
    elif isinstance(normilized_field, date):
      normilized_field = str(normilized_field)
    elif isinstance(normilized_field, db.Model):
      normilized_field = {
          "type": normilized_field.type,
          "id": normilized_field.id
      }
    elif isinstance(normilized_field, dict) and "email" in normilized_field:
      user = user_generator.find_user_by_email(normilized_field["email"])
      normilized_field = self.normilize_field(user)
    elif isinstance(normilized_field, dict):
      normilized_field.pop("context_id", None)
      normilized_field.pop("href", None)
    elif isinstance(normilized_field, list):
      normilized_field = [self.normilize_field(i) for i in normilized_field]
    elif isinstance(
        normilized_field,
        (associationproxy._AssociationList, collections.InstrumentedList)
    ):
      normilized_field = [
          {"type": i.type, "id": i.id} for i in normilized_field
      ]
    return normilized_field
Ejemplo n.º 3
0
    def deserialize(self, value):
        """Deserialize json representation to Person object.

    Creates non existing person if force_create attribute is True.
    """
        from ggrc.utils import user_generator

        if value.get("email"):
            email = value.get("email")
            name = value.get("name")
            if self.force_create:
                return user_generator.find_or_create_external_user(email, name)
            return user_generator.find_user_by_email(email)
        elif value.get("id"):
            return user_generator.find_user_by_id(value["id"])
        else:
            raise ValueError("Provided data are incorrect.")
Ejemplo n.º 4
0
  def deserialize(self, value):
    """Deserialize json representation to Person object.

    Creates non existing person if force_create attribute is True.
    """
    from ggrc.utils import user_generator

    email = value.get("email")
    name = value.get("name")

    if not email:
      raise ValueError("Missing mandatory \"email\" field in %s attribute" %
                       self.attr)

    if self.force_create:
      return user_generator.find_or_create_external_user(email, name)

    return user_generator.find_user_by_email(email)
Ejemplo n.º 5
0
    def deserialize(self, value):
        """Deserialize json representation to Person object.

    Creates non existing person if force_create attribute is True.
    """
        from ggrc.utils import user_generator

        email = value.get("email")
        name = value.get("name")

        if not email:
            raise ValueError(
                "Missing mandatory \"email\" field in %s attribute" %
                self.attr)

        if self.force_create:
            return user_generator.find_or_create_external_user(email, name)

        return user_generator.find_user_by_email(email)