예제 #1
0
    def post(self, request, app, model):
        model = apps.get_model(app, model)
        org = json.loads(request.body.decode('utf8'))
        try:
            d = request.data
            print("###################3 bahar", d)
            if not d:
                print("#########################3333", d)
                REQ_LOGS.error("In valid Json provided for POST")
                raise json.JSONDecodeError("InValid JSON provided",
                                           doc=request.data,
                                           pos=0)
        except json.JSONDecodeError:
            REQ_LOGS.error("In valid Json provided for POST")
            return Response("Invalid JSON for the POST")

        validators_d = {}
        # validators_d['validators'] = [CharOnly(), UpperCaseOnly()]
        serialize = getGenericSerializer(model)
        serial_data = serialize(data=d)
        if serial_data.is_valid():
            # with transaction.atomic(), reversion.create_revision():
            #     reversion.set_user(request.user)
            #     reversion.set_comment(request.method)
            #     reversion.set_date_created(date_created=datetime.datetime.now())
            serial_data.save()
            # audit_log_list(org, request, app, model)

            return Response(serial_data.data, status=status.HTTP_201_CREATED)
        return Response(serial_data.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #2
0
 def get(self, request, app, model):
     model = apps.get_model(app, model)
     field_list = [field.name for field in model._meta.get_fields()]
     foreign_keys = {}
     related_fields = {}
     simple_fields = []
     objs = model.objects.all()
     for field in model._meta.get_fields():
         if isinstance(field, models.ForeignKey):
             serial = ReverseNestedSerializer(field.related_model)
             foreign_keys[field.name] = serial(read_only=True)
             continue
         elif field.is_relation:
             serial = GenericTrackSerializer(field.related_model)
             related_fields[field.name] = serial(many=True)
             continue
         else:
             simple_fields.append(field.name)
             continue
     if foreign_keys:
         serializer_uniq = ReverseStringSerializer(model, foreign_keys)
         serializer = serializer_uniq(objs, many=True)
         return Response(serializer.data)
     elif related_fields:
         fields_to_fetch = [
             field.name for field in model._meta.get_fields()
         ]
         GenericSzl = NestedSerializer(model, related_fields,
                                       fields_to_fetch)
         serializer = GenericSzl(objs, many=True)
         return Response(serializer.data)
     else:
         serializer = getGenericSerializer(model)
         serial = serializer(objs, many=True)
         return Response(serial.data)
예제 #3
0
 def delete(self, request, app, model, id):
     parser_classes = [parsers.JSONParser]
     if id:
         model = apps.get_model(app, model)
         try:
             obj = model.objects.get(id=id)
             if not obj:
                 raise ObjectDoesNotExist
             with transaction.atomic(), reversion.create_revision():
                 obj = model.objects.get(id=id)
                 obj.save()
                 reversion.set_user(request.user)
                 reversion.set_comment(request.method)
                 reversion.set_date_created(
                     date_created=datetime.datetime.now())
             GenericSzl = getGenericSerializer(model)
             serializer = GenericSzl(obj)
             org = serializer.data
             obj.delete()
             updated = None
             audit_log_id(org, updated, request, app, model, id)
             return Response(
                 {
                     "Deleted successfully": "Deleted Successfully!!!",
                     "data": serializer.data
                 },
                 status=status.HTTP_204_NO_CONTENT)
         except ObjectDoesNotExist:
             REQ_LOGS.error("Object Doesnt Exist for DELETE " + " " +
                            str(id) + " " + " by " + str(request.user))
             return Response("The Given Object Doesnt Exist!")
예제 #4
0
    def put(self, request, app, model, id):
        try:

            d = request.data
            # if not d:
            #     raise json.JSONDecodeError("InValid JSON provided",doc=,pos=0)
        except json.JSONDecodeError:
            REQ_LOGS.error("In valid Json provided for POST")
            # return Response("Invalid JSON for the POST")

        model = apps.get_model(app, model)
        obj = model.objects.get(id=id)
        validators_d = {}
        validators_d['validators'] = [CharOnly(), UpperCaseOnly()]
        serializer_org = getGenericSerializer(model)
        serial_org_data = serializer_org(obj)
        serializer = GenericSerializerField(model, validators_d)
        serial_data = serializer(obj, data=d, partial=True)
        if serial_data.is_valid():
            with transaction.atomic(), reversion.create_revision():
                reversion.set_user(request.user)
                reversion.set_comment(request.method)
                reversion.set_date_created(
                    date_created=datetime.datetime.now())
                serial_data.save()
                audit_log_id(serial_org_data.data, serial_data.data, request,
                             app, model, id)

            return Response(serial_data.data)
        return Response(serial_data.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #5
0
    def delete(self, request, app, model):
        if request.data:
            dict_mapp = request.data
            model = apps.get_model(app, model)
            objs = model.objects.filter(**dict_mapp)
            with transaction.atomic(), reversion.create_revision():
                [objs[i].save() for i in range(len(objs))]
                reversion.set_user(request.user)
                reversion.set_comment(request.method)
                reversion.set_date_created(
                    date_created=datetime.datetime.now())
            GenericSzl = getGenericSerializer(model)
            serializer = GenericSzl(objs, many=True)
            org = serializer.data
            objs.delete()
            audit_log_list(org, request, app, model)
            return Response({'Received data': request.data})

        else:
            return Response({"return none instead": "return none instead"})
예제 #6
0
 def get(self, request, app, model, id):
     model = apps.get_model(app, model)
     if id:
         dataset = model.objects.all()
         try:
             dept_name = get_object_or_404(dataset, id=id)
         except ObjectDoesNotExist:
             REQ_LOGS.error("Requested Object Doesnt Exist" +
                            str(ObjectDoesNotExist))
         foreign_keys = {}
         related_fields = {}
         simple_fields = []
         for field in model._meta.get_fields():
             if isinstance(field, models.ForeignKey):
                 serial = ReverseNestedSerializer(field.related_model)
                 foreign_keys[field.name] = serial(read_only=True)
                 continue
             elif field.is_relation:
                 serial = GenericTrackSerializer(field.related_model)
                 related_fields[field.name] = serial(many=True)
                 continue
             else:
                 simple_fields.append(field.name)
                 continue
         if foreign_keys:
             serializer_uniq = ReverseStringSerializer(model, foreign_keys)
             serializer = serializer_uniq(dept_name)
             return Response(serializer.data)
         elif related_fields:
             fields_to_fetch = [
                 field.name for field in model._meta.get_fields()
             ]
             GenericSzl = NestedSerializer(model, related_fields,
                                           fields_to_fetch)
             serializer = GenericSzl(dept_name)
             return Response(serializer.data)
         else:
             serializer = getGenericSerializer(model)
             serial = serializer(dept_name)
             return Response(serial.data)