Ejemplo n.º 1
0
    def post(self, request, format=None):
        try:
            tkn = request.META["HTTP_AUTHORIZATION"]
        except:
            return Response({'status': 'error','errorCode':'101', 'errorMessage':'Invalid Token'})

        logger.debug("Token = " + tkn)
        ret,user = get_user_by_token(tkn)
        if ret == False:
            return Response({'status': 'error','errorCode':'102', 'errorMessage':'Invalid Token'})

        serializer = ConfigurationSerializer(data=request.data)
        if serializer.is_valid():
            config = json.dumps(serializer.data['construct_list'])
            name = serializer.data['name']
            config_obj = Configuration()
            config_obj.name = name
            config_obj.construct_list = config
            config_obj.last_modified_by = user
            config_obj.submit = serializer.data['submit']
            config_obj.save()
            logger.debug("Configuration created successfully")
            serializer = ConfigurationGetSerializer(config_obj)
            config_details = serializer.data
            config_details['construct_list'] =  json.loads(config_details['construct_list'])
            return Response(config_details, status=status.HTTP_201_CREATED)
        logger.error("Invalid Json  ")
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 2
0
def configuration_upload(request):
    if request.method == 'POST':
        # script= request.POST.get('script')
        # print type(script)
        print request
        myFile = request.FILES.get('script', None)
        name = request.POST.get('name')
        describe = request.POST.get('describe')
        print name, describe
        if name:
            save_name = name
        else:
            save_name = myFile
        # print(myFile._size)  # 文件大小字节数
        if Configuration.objects.filter(configuration_name=myFile).exists():
            return render_to_response('400.html', {'info': '脚本已经存在'})
        data = myFile.read()
        job_obj = Configuration()

        job_obj.configuration_name = save_name
        job_obj.info = data
        job_obj.describe = describe
        job_obj.save()
        return render_to_response('configuration_upload.html',
                                  {'username': request.user.username})

    else:

        return render_to_response('configuration_upload.html',
                                  {'username': request.user.username})
Ejemplo n.º 3
0
def register_new_computer(name, uid, distribution, site, configuration):
    """Register a new computer with the admin system - after registration, the
    computer will be submitted for approval."""

    try:
        new_pc = PC.objects.get(uid=uid)
        package_list = new_pc.package_list
        custom_packages = new_pc.custom_packages
    except PC.DoesNotExist:
        new_pc = PC(name=name, uid=uid)
        new_pc.site = Site.objects.get(uid=site)
        # TODO: Better to enforce existence of package list in AfterSave
        # signal.
        package_list = PackageList(name=name)
        custom_packages = CustomPackages(name=name)

    new_pc.distribution = Distribution.objects.get(uid=distribution)
    new_pc.is_active = False
    # Create new configuration, populate with data from computer's config.
    # If a configuration with the same ID is hanging, reuse.
    config_name = '_'.join([site, name, uid])
    try:
        my_config = Configuration.objects.get(name=config_name)
    except Configuration.DoesNotExist:
        my_config = Configuration()
        my_config.name = config_name
    finally:
        # Delete pre-existing entries
        entries = ConfigurationEntry.objects.filter(
            owner_configuration=my_config)
        for e in entries:
            e.delete()
    my_config.save()
    # And load configuration
    for k, v in configuration.items():
        entry = ConfigurationEntry(key=k, value=v,
                                   owner_configuration=my_config)
        entry.save()
    # Tell us about yourself
    new_pc.do_send_package_info = True
    # Set and save PmC
    new_pc.configuration = my_config
    package_list.save()
    new_pc.package_list = package_list
    custom_packages.save()
    new_pc.custom_packages = custom_packages
    new_pc.save()
    return 0
Ejemplo n.º 4
0
    def upload_configuration(self, request):
        if request.GET.get('source') == 'master':
            data = request.DATA
            myFile = request.FILES.get('script', None)

            name = data.get('name')
            describe = data.get('describe')

            if name:
                save_name = name
            else:
                save_name = myFile.name
            # print(myFile._size)  # 文件大小字节数
            if Configuration.objects.filter(
                    configuration_name=save_name).exists():
                return Response('脚本已经存在', status=status.HTTP_400_BAD_REQUEST)
            data = myFile.read()
            job_obj = Configuration()

            job_obj.configuration_name = save_name
            job_obj.info = data
            job_obj.describe = describe
            job_obj.save()
            return Response("success", status=status.HTTP_200_OK)