def devicegroup_api(request, data, group, ident): command = data['command'] response = { } if command == 'get': try: pref = DeviceGroupManagedPreference.objects.get(group=group, identifier=ident) plist = pref.plist except: plist = '<plist><dict><key>Once</key><dict /><key>Often</key><dict /><key>Always</key><dict /></dict></plist>' response['status'] = 'OK' response['data'] = plist elif command == 'set': try: pref = DeviceGroupManagedPreference.objects.get(group=group, identifier=ident) except: pref = DeviceGroupManagedPreference(group=group, identifier=ident) pref.plist = data['plist'] pref.save() for device in group.recursive_devices(): mcx.build_system_configuration_profile(device, install=True) response['status'] = 'OK' else: response['status'] = 'ERROR' response['error'] = 'Unknown command' return HttpResponse(json.dumps(response))
def api(request, id): group = None try: group = DeviceGroup.objects.get(uuid=id) except DeviceGroup.DoesNotExist: raise Http404 if request.method == 'GET': data = request.GET else: data = request.POST result = {} if 'command' not in data: result['status'] = 'ERROR' result['error'] = 'Invalid request' elif data['command'] == 'set': attribute = data['attribute'] value = data['value'] if attribute == 'name': group.name = value group.save() result['status'] = 'OK' elif attribute == 'description': group.description = value group.save() result['status'] = 'OK' elif attribute == 'priority': result['status'] = 'OK' try: group.priority = value group.save() except: result['status'] = 'ERROR' result['error'] = 'Could not parse value into integer.' else: result['status'] = 'ERROR' result['error'] = 'Unknown attribute' elif data['command'] == 'addDevices': result['items'] = [] for id in data['id'].split(','): device = Device.objects.get(udid=id) if not device in group.devices.all(): group.devices.add(device) mcx.build_system_configuration_profile(device, install=True) result['items'].append({ 'name': device.name, 'id': device.udid }) group.save() result['status'] = 'OK' elif data['command'] == 'addGroups': result['items'] = [] for id in data['id'].split(','): g = DeviceGroup.objects.get(uuid=id) if not g in group.groups.all(): group.groups.add(g) result['items'].append({'name': g.name, 'id': g.uuid}) group.save() for device in group.devices.all(): mcx.build_system_configuration_profile(device, install=True) result['status'] = 'OK' elif data['command'] == 'addPreference': if DeviceGroupManagedPreference.objects.filter( group=group, identifier=data['identifier']).count() > 0: result['status'] = 'ERROR' result['error'] = 'Identifier exists.' return HttpResponse(json.dumps(result)) pref = DeviceGroupManagedPreference(group=group, identifier=data['identifier']) pref.plist = '<plist><dict><key>Once</key><dict /><key>Often</key><dict /><key>Always</key><dict /></dict></plist>' pref.save() result['status'] = 'OK' result['identifier'] = pref.identifier result['url'] = reverse('devicegroup_preference_edit', args=( group.uuid, pref.identifier, )) elif data['command'] == 'removeDevice': device = Device.objects.get(udid=data['id']) if device in group.devices.all(): group.devices.remove(device) group.save() mcx.build_system_configuration_profile(device, install=True) result['status'] = 'OK' elif data['command'] == 'removeGroup': g = DeviceGroup.objects.get(uuid=data['id']) if g in group.groups.all(): group.groups.remove(g) group.save() for device in group.devices.all(): mcx.build_system_configuration_profile(device, install=True) result['status'] = 'OK' elif data['command'] == 'removePreference': if DeviceGroupManagedPreference.objects.filter( group=group, identifier=data['identifier']).count() == 0: result['status'] = 'ERROR' result['error'] = 'Identifier not found.' return HttpResponse(json.dumps(result)) pref = DeviceGroupManagedPreference.objects.get( group=group, identifier=data['identifier']) pref.delete() result['status'] = 'OK' result['identifier'] = data['identifier'] elif data['command'] == 'availableDevices': devices = Device.objects.filter(name__startswith=data['query']) result['items'] = [] for device in devices: result['items'].append({'name': device.name, 'id': device.udid}) result['status'] = 'OK' elif data['command'] == 'availableGroups': groups = DeviceGroup.objects.filter(name__startswith=data['query']) result['items'] = [] for g in groups: result['items'].append({'name': g.name, 'id': g.uuid}) result['status'] = 'OK' else: result['status'] = 'ERROR' result['error'] = 'Unknown command' return HttpResponse(json.dumps(result))
def api(request, id): group = None try: group = DeviceGroup.objects.get(uuid=id) except DeviceGroup.DoesNotExist: raise Http404 if request.method == 'GET': data = request.GET else: data = request.POST result = { } if 'command' not in data: result['status'] = 'ERROR' result['error'] = 'Invalid request'; elif data['command'] == 'set': attribute = data['attribute'] value = data['value'] if attribute == 'name': group.name = value group.save() result['status'] = 'OK' elif attribute == 'description': group.description = value group.save() result['status'] = 'OK' elif attribute == 'priority': result['status'] = 'OK' try: group.priority = value group.save() except: result['status'] = 'ERROR'; result['error'] = 'Could not parse value into integer.' else: result['status'] = 'ERROR' result['error'] = 'Unknown attribute' elif data['command'] == 'addDevices': result['items'] = [ ] for id in data['id'].split(','): device = Device.objects.get(udid=id) if not device in group.devices.all(): group.devices.add(device) mcx.build_system_configuration_profile(device, install=True) result['items'].append({ 'name': device.name, 'id': device.udid }) group.save() result['status'] = 'OK' elif data['command'] == 'addGroups': result['items'] = [ ] for id in data['id'].split(','): g = DeviceGroup.objects.get(uuid=id) if not g in group.groups.all(): group.groups.add(g) result['items'].append({ 'name': g.name, 'id': g.uuid }) group.save() for device in group.devices.all(): mcx.build_system_configuration_profile(device, install=True) result['status'] = 'OK' elif data['command'] == 'addPreference': if DeviceGroupManagedPreference.objects.filter(group=group, identifier=data['identifier']).count() > 0: result['status'] = 'ERROR' result['error'] = 'Identifier exists.' return HttpResponse(json.dumps(result)) pref = DeviceGroupManagedPreference(group=group, identifier=data['identifier']) pref.plist = '<plist><dict><key>Once</key><dict /><key>Often</key><dict /><key>Always</key><dict /></dict></plist>' pref.save() result['status'] = 'OK' result['identifier'] = pref.identifier result['url'] = reverse('devicegroup_preference_edit', args=(group.uuid, pref.identifier,)) elif data['command'] == 'removeDevice': device = Device.objects.get(udid=data['id']) if device in group.devices.all(): group.devices.remove(device) group.save() mcx.build_system_configuration_profile(device, install=True) result['status'] = 'OK' elif data['command'] == 'removeGroup': g = DeviceGroup.objects.get(uuid=data['id']) if g in group.groups.all(): group.groups.remove(g) group.save() for device in group.devices.all(): mcx.build_system_configuration_profile(device, install=True) result['status'] = 'OK' elif data['command'] == 'removePreference': if DeviceGroupManagedPreference.objects.filter(group=group, identifier=data['identifier']).count() == 0: result['status'] = 'ERROR' result['error'] = 'Identifier not found.' return HttpResponse(json.dumps(result)) pref = DeviceGroupManagedPreference.objects.get(group=group, identifier=data['identifier']) pref.delete() result['status'] = 'OK' result['identifier'] = data['identifier'] elif data['command'] == 'availableDevices': devices = Device.objects.filter(name__startswith=data['query']) result['items'] = [ ] for device in devices: result['items'].append({ 'name': device.name, 'id': device.udid }) result['status'] = 'OK' elif data['command'] == 'availableGroups': groups = DeviceGroup.objects.filter(name__startswith=data['query']) result['items'] = [ ] for g in groups: result['items'].append({ 'name': g.name, 'id': g.uuid }) result['status'] = 'OK' else: result['status'] = 'ERROR' result['error'] = 'Unknown command' return HttpResponse(json.dumps(result))