Exemplo n.º 1
0
 def post(request, pk):
     data = request.POST
     form = NewCompanyForm(data)
     if form.is_valid():
         current_company = form.save()
     else:
         return HttpResponseBadRequest(form.errors.as_json())
     return HttpJson(ujson.dumps({"name": current_company.name}))
Exemplo n.º 2
0
    def put(request, pk):
        try:
            current_company = Company.objects.get(pk=pk)
            current_company.name = request.data["name"]
            current_company.save()
        except Exception:
            return HttpResponseBadRequest("Error: %s" % sys.exc_info()[1])

        return HttpJson(ujson.dumps({"name": current_company.name}))
Exemplo n.º 3
0
 def get(request):
     data = [{
         "name": x[0],
         "data": x[1],
         "primary_columns": x[2],
         "id": x[3]
     }
             for x in MapSettings.objects.values_list(
                 "name", "data", "primary_columns", "id")]
     return HttpJson(ujson.dumps(data))
Exemplo n.º 4
0
 def put(self, request, pk):
     data = request.PUT
     try:
         instance = MLData.objects.get(pk=pk)
     except Exception:
         return HttpResponseBadRequest(ujson.dumps(['Data set with `pk` %d not found' % pk]))
     row_index = int(data["row_index"])
     for x in instance.data:
         if x["row_index"] == row_index:
             x[data['col_name']] = data['value']
     instance.save()
     return HttpJson(ujson.dumps(["ok"]))
Exemplo n.º 5
0
 def save_form(self, form, data):
     if form.is_valid():
         data_mapping = form.save(commit=False)
         data_mapping.data = ujson.loads(data["data"])
         data_mapping.primary_columns = ujson.loads(data["primary_columns"])
         data_mapping.save()
     else:
         return HttpResponseBadRequest(form.errors.as_json())
     return HttpJson(
         ujson.dumps({
             "id": data_mapping.id,
             "name": data_mapping.name,
             "data": data_mapping.data,
             "primary_columns": data_mapping.primary_columns
         }))
Exemplo n.º 6
0
 def get(self, request, pk):
     context = {}
     try:
         # todo: rewrite for one query
         ml_data_q = MLData.objects.filter(id=pk).values("id", "name", "company_id", "company__name", "key_data",
                                                         "data")
         ml_data = ml_data_q[0]
     except Exception:
         return HttpResponseBadRequest(ujson.dumps(['Data set with `pk` %d not found' % pk]))
     context["id"] = ml_data["id"]
     context["name"] = ml_data["name"]
     context["company"] = {"id": ml_data["company_id"], "name": ml_data["company__name"]}
     context["key_data"] = {"name": ml_data["key_data"]}
     context["data"] = ml_data["data"]
     return HttpJson(ujson.dumps(context))
Exemplo n.º 7
0
    def _create_update_data_set(file, req_data, pk=0):

        if pk:
            try:
                instance = MLData.objects.get(pk=pk)
                data_set = NewDataSetForm(req_data, instance=instance)
            except Exception:
                return HttpResponseBadRequest(ujson.dumps(['Data set with `pk` %d not found' % pk]))
        else:
            data_set = NewDataSetForm(req_data)

        if not data_set.is_valid():
            return HttpResponseBadRequest(data_set.errors.as_json())
        ml_data = data_set.save(commit=False)
        if file:
            ml_data.data = csv_to_json(file)
        ml_data.save()
        return HttpJson(ujson.dumps({"data": ml_data.data, "id": ml_data.id}))
Exemplo n.º 8
0
    def post(self, request, pk):
        try:
            instance = MLData.objects.get(pk=pk)
        except Exception:
            return HttpResponseBadRequest(ujson.dumps(['Data set with `pk` %d not found' % pk]))
        mapped_columns = ujson.loads(request.POST["mapped_columns"])
        primary_mapping = ujson.loads(request.POST["primary_columns"])
        origin_column = primary_mapping['origin_column']
        new_set_column = primary_mapping['new_set_column']
        file = request.FILES.get('file', '')
        new_data_set = csv_to_json(file)
        new_data_set_dict = {}
        for x in new_data_set:
            if new_set_column not in x:
                return HttpResponseBadRequest(
                    ujson.dumps(['In new row %s  column %s was not found' % (x, new_set_column)]))
            new_data_set_dict[x[new_set_column]] = x

        origin_data_set = instance.data
        key_data = instance.key_data
        for row in origin_data_set:
            if origin_column not in row:
                return HttpResponseBadRequest(ujson.dumps(['In row %s  column %s was not found' % (row, origin_column)]))
            pk = row[origin_column]
            if pk in new_data_set_dict:
                new_data_set_row = new_data_set_dict[pk]
                for columns in mapped_columns:
                    if columns['origin_column'] == key_data:
                        continue
                    if columns['origin_column'] not in row:
                        return HttpResponseBadRequest(
                            ujson.dumps(['In row %s  column %s was not found' % (row, columns['origin_column'])]))
                    if columns['new_set_column'] not in new_data_set_row:
                        return HttpResponseBadRequest(
                            ujson.dumps(['In new row %s  column %s was not found' % (new_data_set_row,
                                         columns['new_set_column'])]))
                    row[columns['origin_column']] = new_data_set_row[columns['new_set_column']]

        instance.data = origin_data_set
        instance.save()
        return HttpJson(ujson.dumps(origin_data_set))
Exemplo n.º 9
0
    def get(request):
        res = []
        data_sets = list(MLData.objects.values_list("company_id", "name", "id"))
        companies = Company.objects.values_list("name", "id").order_by("name")

        tree = {x[1]: {"name": x[0], "children": []} for x in companies}

        for data_set in data_sets:
            company_id = data_set[0]
            ds_name = data_set[1]
            id_data_set = data_set[2]
            tree[company_id]["children"].append({
                "label": ds_name,
                "data": id_data_set,
            })

        for tree_key, value in tree.items():
            res.append({
                "label": value["name"],
                "data": tree_key,
                "children": value["children"]
            })
        return HttpJson(ujson.dumps(res))
Exemplo n.º 10
0
 def get(request):
     companies = Company.objects.all()
     return HttpJson(ujson.dumps([{"name": x.name, "id": x.id} for x in companies]))
Exemplo n.º 11
0
 def delete(request, pk):
     Company.objects.filter(pk=pk).delete()
     return HttpJson(ujson.dumps(["ok"]))
Exemplo n.º 12
0
 def get(request, pk):
     current_company = Company.objects.filter(pk=pk)
     if not len(current_company):
         return HttpResponseBadRequest("Company with id: %d  not found" % pk)
     return HttpJson(ujson.dumps({"name": current_company[0].name, "id": current_company[0].id}))
Exemplo n.º 13
0
 def delete(self, request, pk):
     MapSettings.objects.filter(pk=pk).delete()
     return HttpJson(ujson.dumps(["ok"]))