Example #1
0
    def post(self):
        """
        ---
        description: Post new taxonomy
        tags:
          - taxonomies
        requestBody:
          content:
            application/json:
              schema:
                type: object
                properties:
                  name:
                    type: string
                    description: |
                      Short string to make this taxonomy memorable
                      to end users.
                  hierarchy:
                    type: object
                    description: |
                       Nested JSON describing the taxonomy
                       which should be validated against
                       a schema before entry
                  group_ids:
                    type: array
                    items:
                      type: integer
                    description: |
                      List of group IDs corresponding to which groups should be
                      able to view comment. Defaults to all of requesting
                      user's groups.
                  version:
                    type: string
                    description: |
                      Semantic version of this taxonomy name
                  provenance:
                    type: string
                    description: |
                      Identifier (e.g., URL or git hash) that
                      uniquely ties this taxonomy back
                      to an origin or place of record
                  isLatest:
                    type: boolean
                    description: |
                      Consider this version of the taxonomy with this
                      name the latest? Defaults to True.
                required:
                  - name
                  - hierarchy
                  - version

        responses:
          200:
            content:
              application/json:
                schema:
                  allOf:
                    - $ref: '#/components/schemas/Success'
                    - type: object
                      properties:
                        data:
                          type: object
                          properties:
                            taxonomy_id:
                              type: integer
                              description: New taxonomy ID
        """
        data = self.get_json()
        name = data.get('name', None)
        if name is None:
            return self.error("A name must be provided for a taxonomy")

        version = data.get('version', None)
        if version is None:
            return self.error("A version string must be provided for a taxonomy")

        existing_matches = (
            Taxonomy.query.filter(Taxonomy.name == name)
            .filter(Taxonomy.version == version)
            .all()
        )
        if len(existing_matches) != 0:
            return self.error(
                "That version/name combination is already "
                "present. If you really want to replace this "
                "then delete the appropriate entry."
            )

        # Ensure a valid taxonomy
        hierarchy = data.get('hierarchy', None)
        if hierarchy is None:
            return self.error("A JSON of the taxonomy must be given")

        try:
            validate(hierarchy, schema)
        except JSONValidationError:
            return self.error("Hierarchy does not validate against the schema.")

        # establish the groups to use
        user_group_ids = [g.id for g in self.current_user.groups]
        user_accessible_group_ids = [g.id for g in self.current_user.accessible_groups]
        group_ids = data.pop("group_ids", user_group_ids)
        if group_ids == []:
            group_ids = user_group_ids
        group_ids = [gid for gid in group_ids if gid in user_accessible_group_ids]
        if not group_ids:
            return self.error(
                f"Invalid group IDs field ({group_ids}): "
                "You must provide one or more valid group IDs."
            )
        groups = Group.query.filter(Group.id.in_(group_ids)).all()

        provenance = data.get('provenance', None)

        # update others with this name
        # TODO: deal with the same name but different groups?
        isLatest = data.get('isLatest', True)
        if isLatest:
            DBSession().query(Taxonomy).filter(Taxonomy.name == name).update(
                {'isLatest': False}
            )

        taxonomy = Taxonomy(
            name=name,
            hierarchy=hierarchy,
            provenance=provenance,
            version=version,
            isLatest=isLatest,
            groups=groups,
        )

        DBSession().add(taxonomy)
        self.verify_and_commit()

        return self.success(data={'taxonomy_id': taxonomy.id})
Example #2
0
                                {"admin": member.get('admin', False)},
                                tokens[group["token"]]
                            )
            if src.get("taxonomies") is not None:
                with status("Creating Taxonomies"):

                    for tax in src.get('taxonomies', []):
                        name = tax["name"]
                        provenance = tax.get("provenance")

                        if tax["tdtax"]:
                            hierarchy = tdtax.taxonomy
                            version = tdtax.__version__
                        else:
                            hierarchy = tax["hierarchy"]
                            tdtax.validate(hierarchy, tdtax.schema)
                            version = tax["version"]

                        group_ids = [group_dict[g] for g in tax["groups"]]
                        payload = {
                            "name": name,
                            "hierarchy": hierarchy,
                            "group_ids": group_ids,
                            "provenance": provenance,
                            "version": version,
                        }
                        data = assert_post(
                            "taxonomy",
                            payload,
                            tokens[tax["token"]]
                        )
def test():
    tdtax.validate(tdtax.taxonomy, tdtax.taxonomy)