def save_to_db(request): if request.method == 'POST': net = request.POST.get('net') net_name = request.POST.get('net_name') user_id = request.POST.get('user_id') next_layer_id = request.POST.get('nextLayerId') public_sharing = True user = None if net_name == '': net_name = 'Net' try: # making model sharing public by default for now # TODO: Prvilege on Sharing if user_id: user_id = int(user_id) user = User.objects.get(id=user_id) # create a new model on share event model = Network(name=net_name, public_sharing=public_sharing, author=user) model.save() # create first version of model model_version = NetworkVersion(network=model, network_def=net) model_version.save() # create initial update for nextLayerId model_update = NetworkUpdates(network_version=model_version, updated_data=json.dumps({'nextLayerId': next_layer_id}), tag='ModelShared') model_update.save() return JsonResponse({'result': 'success', 'id': model.id}) except: return JsonResponse({'result': 'error', 'error': str(sys.exc_info()[1])})
def fetch_network_version(netObj): network_version = NetworkVersion.objects.filter(network=netObj).order_by('-created_on')[0] updates_batch = NetworkUpdates.objects.filter(network_version=network_version) # Batching updates # Note - size of batch is 20 for now, optimization can be done if len(updates_batch) == 2: data = get_network_version(netObj) network_version = NetworkVersion(network=netObj, network_def=json.dumps(data['network'])) network_version.save() network_update = NetworkUpdates(network_version=network_version, updated_data=json.dumps({'nextLayerId': data['next_layer_id']}), tag='CheckpointCreated') network_update.save() return network_version
def test_load(self): u_1 = User(id=1, username='******') u_1.save() u_2 = User(id=2, username='******') u_2.save() model = Network(name='net') model.save() model_version = NetworkVersion(network=model, network_def={}) model_version.save() response = self.client.post( reverse('saveDB'), {'net': '{"net": "testnet"}', 'net_name': 'name'}) response = json.loads(response.content) self.assertEqual(response['result'], 'success') self.assertTrue('id' in response) proto_id = response['id'] response = self.client.post(reverse('loadDB'), {'proto_id': proto_id}) response = json.loads(response.content) self.assertEqual(response['result'], 'success') self.assertEqual(response['net_name'], 'name')
def test_load(self): u_1 = User(id=1, username='******') u_1.save() u_2 = User(id=2, username='******') u_2.save() model = Network(name='net') model.save() model_version = NetworkVersion(network=model, network_def={}) model_version.save() response = self.client.post(reverse('saveDB'), { 'net': '{"net": "testnet"}', 'net_name': 'name' }) response = json.loads(response.content) self.assertEqual(response['result'], 'success') self.assertTrue('id' in response) proto_id = response['id'] response = self.client.post(reverse('loadDB'), {'proto_id': proto_id}) response = json.loads(response.content) self.assertEqual(response['result'], 'success') self.assertEqual(response['net_name'], 'name')
def create_network_version(network, netObj): # creating a unique version of network to allow revert and view hitory network_version = NetworkVersion(network=netObj) network_version.network_def = network network_version.save() return network_version