def tearDown(self):
     meta_schema = actions.get_meta_schema_name(self.test_schema)
     if actions.has_table(
             dict(table=self.test_table, schema=self.test_schema)):
         actions.perform_sql(
             "DROP TABLE IF EXISTS {schema}.{table} CASCADE".format(
                 schema=meta_schema,
                 table=actions.get_insert_table_name(
                     self.test_schema, self.test_table),
             ))
         actions.perform_sql(
             "DROP TABLE IF EXISTS {schema}.{table} CASCADE".format(
                 schema=meta_schema,
                 table=actions.get_edit_table_name(self.test_schema,
                                                   self.test_table),
             ))
         actions.perform_sql(
             "DROP TABLE IF EXISTS {schema}.{table} CASCADE".format(
                 schema=meta_schema,
                 table=actions.get_delete_table_name(
                     self.test_schema, self.test_table),
             ))
         actions.perform_sql(
             "DROP TABLE IF EXISTS {schema}.{table} CASCADE".format(
                 schema=self.test_schema, table=self.test_table))
Exemple #2
0
    def put(self, request, schema, table):
        """
        Every request to unsave http methods have to contain a "csrftoken".
        This token is used to deny cross site reference forwarding.
        In every request the header had to contain "X-CSRFToken" with the actual csrftoken.
        The token can be requested at / and will be returned as cookie.

        :param request:
        :return:
        """
        if schema not in PLAYGROUNDS and schema not in UNVERSIONED_SCHEMAS:
            raise PermissionDenied
        if schema.startswith("_"):
            raise PermissionDenied
        if request.user.is_anonymous:
            raise PermissionDenied
        if actions.has_table(dict(schema=schema, table=table), {}):
            raise APIError("Table already exists")
        json_data = request.data["query"]
        constraint_definitions = []
        column_definitions = []

        for constraint_definiton in json_data.get("constraints", []):
            constraint_definiton.update({
                "action": "ADD",
                "c_table": table,
                "c_schema": schema
            })
            constraint_definitions.append(constraint_definiton)

        if "columns" not in json_data:
            raise actions.APIError("Table contains no columns")
        for column_definition in json_data["columns"]:
            column_definition.update({"c_table": table, "c_schema": schema})
            column_definitions.append(column_definition)
        metadata = json_data.get("metadata")

        result = self.__create_table(request,
                                     schema,
                                     table,
                                     column_definitions,
                                     constraint_definitions,
                                     metadata=metadata)

        perm, _ = login_models.UserPermission.objects.get_or_create(
            table=DBTable.load(schema, table), holder=request.user)
        perm.level = login_models.ADMIN_PERM
        perm.save()
        request.user.save()
        return JsonResponse({}, status=status.HTTP_201_CREATED)
Exemple #3
0
    def put(self, request, schema, table):
        """
        Every request to unsave http methods have to contain a "csrftoken".
        This token is used to deny cross site reference forwarding.
        In every request the header had to contain "X-CSRFToken" with the actual csrftoken.
        The token can be requested at / and will be returned as cookie.

        :param request:
        :return:
        """
        if schema not in ['model_draft', 'sandbox', 'test']:
            raise PermissionDenied
        if schema.startswith('_'):
            raise PermissionDenied
        if request.user.is_anonymous():
            raise PermissionDenied
        if actions.has_table(dict(schema=schema, table=table), {}):
            raise APIError('Table already exists')
        json_data = request.data['query']
        constraint_definitions = []
        column_definitions = []

        for constraint_definiton in json_data.get('constraints', []):
            constraint_definiton.update({
                "action": "ADD",
                "c_table": table,
                "c_schema": schema
            })
            constraint_definitions.append(constraint_definiton)

        if 'columns' not in json_data:
            raise actions.APIError("Table contains no columns")
        for column_definition in json_data['columns']:
            column_definition.update({"c_table": table, "c_schema": schema})
            column_definitions.append(column_definition)

        result = actions.table_create(schema, table, column_definitions,
                                      constraint_definitions)

        perm, _ = login_models.UserPermission.objects.get_or_create(
            table=DBTable.load(schema, table), holder=request.user)
        perm.level = login_models.ADMIN_PERM
        perm.save()
        request.user.save()
        return JsonResponse(result, status=status.HTTP_201_CREATED)