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 create_network_update(network_version, updated_data, tag): network_update = NetworkUpdates(network_version=network_version, updated_data=updated_data, tag=tag) return network_update