layers = [] timeseries = False # should we display timeseries controls? # we convert the channels to layers here """ # AB Note: I decided it would be better to get all channels than just the default # channel. But it is up for discussion. if channels is None: # assume default channel, single layer called by the token # get the default channel and add it to channels channel = get_object_or_404(Channel, project=token.project, default=True) channels.append(channel) """ # convert all channels to layers for channel in channels: tmp_layer = VizLayer() tmp_layer.layer_name = channel['channel_name'] tmp_layer.layer_description = project_description if channel['channel_type'] == 'timeseries': timeseries = True tmp_layer.layertype = channel['channel_type'] tmp_layer.token = project_name tmp_layer.channel = channel['channel_name'] if settings.OCP_SERVER is None: tmp_layer.server = request.META['HTTP_HOST']; else: tmp_layer.server = settings.OCP_SERVER tmp_layer.tilecache = False if channel['channel_name'] in channel_colors.keys(): tmp_layer.color = channel_colors[ channel['channel_name'] ].upper() layers.append(tmp_layer)
def editVizProjectSubmit(request): if request.method == 'POST': # parse the response projNameOrig = request.POST['oldProjectName'] response = request.POST # process layer updates layersNew = {} for item in response.keys(): if item.startswith('layer'): [itemtype, sep, layername] = item.partition('_') itemtype = itemtype[5:].lower() if layername not in layersNew.keys(): layersNew[layername] = {} layersNew[layername][itemtype] = response[item] # get the original project / layers try: proj = VizProject.objects.get(project_name = projNameOrig) except VizProject.DoesNotExist: return HttpResponseBadRequest('Error: Project {} does not exist!'.format(projNameOrig)) layers = proj.layers.select_related() # apply changes to project proj.project_name = response['projectName'] proj.project_description = response['projectDesc'] #proj.user = request.user if 'public' in response.keys(): proj.public = 1 else: proj.public = 0 proj.xoffset = response['xoffset'] proj.yoffset = response['yoffset'] proj.zoffset = response['zoffset'] proj.ximagesize = response['ximagesize'] proj.yimagesize = response['yimagesize'] proj.zimagesize = response['zimagesize'] proj.starttime = response['starttime'] proj.endtime = response['endtime'] proj.minres = response['minres'] proj.scalinglevels = response['scalinglevels'] # apply changes to layers for layer in layers: if layer.layer_name in layersNew.keys(): layerInfo = layersNew[layer.layer_name] layer.layer_name = layerInfo['name'] layer.layer_description = layerInfo['desc'] layer.server = layerInfo['server'] layer.layertype = layerInfo['type'] layer.token = layerInfo['token'] layer.channel = layerInfo['channel'] layer.color = layerInfo['color'] if 'tilecache' in layerInfo.keys(): layer.tilecache = True else: layer.tilecache = False if 'propagated' in layerInfo.keys(): layer.propagate = 2 else: layer.propagate = 0 # Note: Any error checking must be done before this point. After this point, all changes are saved to the DB. # add new layers for layerKey in layersNew.keys(): if layerKey.startswith('newlayer'): layer = VizLayer() layerInfo = layersNew[layerKey] layer.layer_name = layerInfo['name'] layer.layer_description = layerInfo['desc'] layer.server = layerInfo['server'] layer.layertype = layerInfo['type'] layer.token = layerInfo['token'] layer.channel = layerInfo['channel'] layer.color = layerInfo['color'] if 'tilecache' in layerInfo.keys(): layer.tilecache = True else: layer.tilecache = False if 'propagated' in layerInfo.keys(): layer.propagate = 2 else: layer.propagate = 0 # since this is a new layer, we need to associate it w/ the editing user layer.user = request.user # associate this layer with the vizproject layer.save() proj.layers.add(layer) # save changes made to existing objects for layer in layers: layer.save() # changing the primary key will create a new object if proj.project_name == projNameOrig: proj.save() else: # project name changed proj.save() # reassociate with vizlayers for layer in layers: proj.layers.add(layer) try: proj_old = VizProject.objects.get(project_name = projNameOrig) for layer in layers: proj_old.layers.remove(layer) proj_old.delete() except Exception, e: return HttpResponseBadRequest('Error changing name of VizProject: {}'.format(e)) proj.save() return HttpResponse('Saved Changes Successfully')
def tokenview(request, webargs): # we expect /ocp/viz/token/channel(s)/res/x/y/z/ # res (x,y,z) will center the map at (x,y,z) for a given res channels_str = None channels = None channel_colors = {} [token_str, restargs] = webargs.split('/', 1) restsplit = restargs.split('/') # initialize these variables, which will be passed to the template x = None y = None z = None res = None marker = False if len(restsplit) == 5: # assume no channels, just res/x/y/z/ res = int(restsplit[0]) x = int(restsplit[1]) y = int(restsplit[2]) z = int(restsplit[3]) marker = True elif len(restsplit) > 5: # assume channels + res/x/y/z channels_str = restsplit[0].split(',') res = int(restsplit[1]) x = int(restsplit[2]) y = int(restsplit[3]) z = int(restsplit[4]) marker = True elif len(restsplit) == 2: # assume just channels channels_str = restsplit[0].split(',') elif len(restsplit) == 1: # all channels (get from project) channels_str = None else: # return error return HttpResponseBadRequest('Error: Invalid REST arguments.') # get data from ocpuser token = get_object_or_404(Token, pk=token_str) project = Project.objects.get(pk=token.project_id) dataset = Dataset.objects.get(pk=project.dataset) if (channels_str is not None) and (len(channels_str[0]) > 0): channels = [] for channel_str in channels_str: if len(channel_str) > 0: if len(channel_str.split(':')) > 1: channels.append(get_object_or_404(Channel, channel_name=channel_str.split(':')[0], project=token.project)) channel_colors[channel_str.split(':')[0]] = channel_str.split(':')[1] else: channels.append(get_object_or_404(Channel, channel_name=channel_str, project=token.project)) else: # get all channels for projects channels = Channel.objects.filter(project=project) layers = [] timeseries = False # should we display timeseries controls? # we convert the channels to layers here """ # AB Note: I decided it would be better to get all channels than just the default # channel. But it is up for discussion. if channels is None: # assume default channel, single layer called by the token # get the default channel and add it to channels channel = get_object_or_404(Channel, project=token.project, default=True) channels.append(channel) """ # convert all channels to layers for channel in channels: tmp_layer = VizLayer() tmp_layer.layer_name = channel.channel_name tmp_layer.layer_description = token.token_description if channel.channel_type == 'timeseries': timeseries = True tmp_layer.layertype = channel.channel_type tmp_layer.token = token.token_name #tmp_layer.channel = channel tmp_layer.channel = channel.channel_name tmp_layer.server = request.META['HTTP_HOST']; tmp_layer.tilecache = False if channel.channel_name in channel_colors.keys(): tmp_layer.color = channel_colors[channel.channel_name].upper() layers.append(tmp_layer) # package data for the template xdownmax = (dataset.ximagesize + dataset.xoffset - 1)/(2**dataset.scalinglevels) ydownmax = (dataset.yimagesize + dataset.yoffset - 1)/(2**dataset.scalinglevels) # center the map on the image, if no other coordinate is specified if x is None: x = xdownmax/2 if y is None: y = ydownmax/2 if z is None: z = dataset.zoffset if res is None: res = dataset.scalinglevels context = { 'layers': layers, 'project_name': token.token_name, 'xsize': dataset.ximagesize, 'ysize': dataset.yimagesize, 'zsize': dataset.zimagesize, 'xoffset': dataset.xoffset, 'yoffset': dataset.yoffset, 'zoffset': dataset.zoffset, 'xdownmax': xdownmax, 'ydownmax': ydownmax, 'starttime': dataset.starttime, 'endtime': dataset.endtime, 'maxres': dataset.scalinglevels, 'minres':0, 'res': res, 'xstart': x, 'ystart': y, 'zstart': z, 'marker': marker, 'timeseries': timeseries, } return render(request, 'ocpviz/viewer.html', context)
def addVizProject(request): # note that this method will save a new vizproject with or without layers # layers can be added later in the edit vizproject page if request.method == 'POST': response = request.POST existingProj = VizProject.objects.filter(project_name = response['projectName']) if len(existingProj) > 0: return HttpResponseBadRequest('Error: Project {} already exists!'.format(response['projectName'])) # separate out layers layersNew = {} for item in response.keys(): if item.startswith('layer'): [itemtype, sep, layername] = item.partition('_') itemtype = itemtype[5:].lower() if layername not in layersNew.keys(): layersNew[layername] = {} layersNew[layername][itemtype] = response[item] # create new project proj = VizProject() proj.project_name = response['projectName'] proj.project_description = response['projectDesc'] proj.user = request.user if 'public' in response.keys(): proj.public = 1 else: proj.public = 0 proj.xoffset = response['xoffset'] proj.yoffset = response['yoffset'] proj.zoffset = response['zoffset'] proj.ximagesize = response['ximagesize'] proj.yimagesize = response['yimagesize'] proj.zimagesize = response['zimagesize'] proj.starttime = response['starttime'] proj.endtime = response['endtime'] proj.minres = response['minres'] proj.scalinglevels = response['scalinglevels'] layers = [] # add new layers for layerKey in layersNew.keys(): if layerKey.startswith('newlayer'): layer = VizLayer() layerInfo = layersNew[layerKey] layer.layer_name = layerInfo['name'] layer.layer_description = layerInfo['desc'] layer.server = layerInfo['server'] layer.layertype = layerInfo['type'] layer.token = layerInfo['token'] layer.channel = layerInfo['channel'] layer.color = layerInfo['color'] if 'tilecache' in layerInfo.keys(): layer.tilecache = True if len(layerInfo['tilecacheserver']) > 0: layer.tilecache_server = layerInfo['tilecacheserver'] else: layer.tilecache = False if 'propagated' in layerInfo.keys(): layer.propagate = 2 else: layer.propagate = 0 # since this is a new layer, we need to associate it w/ the editing user layer.user = request.user layers.append(layer) # after creating everything, save changes (this allows for error handling) try: # must save the project first due to foreign key contraints proj.save() for layer in layers: layer.save() proj.layers.add(layer) except Exception as e: return HttpResponseBadRequest('Error: Exception occurred during save! ({})'.format(e)) return HttpResponse('Added Project Successfully') else: context = { 'serverOptions': VizLayer.SERVER_CHOICES, 'layerOptions': VizLayer.LAYER_CHOICES, 'colorOptions': VizLayer.COLOR_CHOICES, } return render(request, 'manage/addvizproject.html', context)
def tokenview(request, webargs): # we expect /ocp/viz/token/channel(s)/res/x/y/z/ # res (x,y,z) will center the map at (x,y,z) for a given res channels_str = None channels = None channel_colors = {} [token_str, restargs] = webargs.split('/', 1) restsplit = restargs.split('/') # initialize these variables, which will be passed to the template x = None y = None z = None res = None marker = False if len(restsplit) == 5: # assume no channels, just res/x/y/z/ res = int(restsplit[0]) x = int(restsplit[1]) y = int(restsplit[2]) z = int(restsplit[3]) marker = True elif len(restsplit) > 5: # assume channels + res/x/y/z channels_str = restsplit[0].split(',') res = int(restsplit[1]) x = int(restsplit[2]) y = int(restsplit[3]) z = int(restsplit[4]) marker = True elif len(restsplit) == 2: # assume just channels channels_str = restsplit[0].split(',') elif len(restsplit) == 1: # all channels (get from project) channels_str = None else: # return error return HttpResponseBadRequest('Error: Invalid REST arguments.') # get data from ocpuser token = get_object_or_404(Token, pk=token_str) project = Project.objects.get(pk=token.project_id) dataset = Dataset.objects.get(pk=project.dataset) if (channels_str is not None) and (len(channels_str[0]) > 0): channels = [] for channel_str in channels_str: if len(channel_str) > 0: if len(channel_str.split(':')) > 1: channels.append( get_object_or_404( Channel, channel_name=channel_str.split(':')[0], project=token.project)) channel_colors[channel_str.split(':') [0]] = channel_str.split(':')[1] else: channels.append( get_object_or_404(Channel, channel_name=channel_str, project=token.project)) else: # get all channels for projects channels = Channel.objects.filter(project=project) layers = [] timeseries = False # should we display timeseries controls? # we convert the channels to layers here """ # AB Note: I decided it would be better to get all channels than just the default # channel. But it is up for discussion. if channels is None: # assume default channel, single layer called by the token # get the default channel and add it to channels channel = get_object_or_404(Channel, project=token.project, default=True) channels.append(channel) """ # convert all channels to layers for channel in channels: tmp_layer = VizLayer() tmp_layer.layer_name = channel.channel_name tmp_layer.layer_description = token.token_description if channel.channel_type == 'timeseries': timeseries = True tmp_layer.layertype = channel.channel_type tmp_layer.token = token.token_name #tmp_layer.channel = channel tmp_layer.channel = channel.channel_name tmp_layer.server = request.META['HTTP_HOST'] tmp_layer.tilecache = False if channel.channel_name in channel_colors.keys(): tmp_layer.color = channel_colors[channel.channel_name].upper() layers.append(tmp_layer) # package data for the template xdownmax = (dataset.ximagesize + dataset.xoffset - 1) / (2**dataset.scalinglevels) ydownmax = (dataset.yimagesize + dataset.yoffset - 1) / (2**dataset.scalinglevels) # center the map on the image, if no other coordinate is specified if x is None: x = xdownmax / 2 if y is None: y = ydownmax / 2 if z is None: z = dataset.zoffset if res is None: res = dataset.scalinglevels context = { 'layers': layers, 'project_name': token.token_name, 'xsize': dataset.ximagesize, 'ysize': dataset.yimagesize, 'zsize': dataset.zimagesize, 'xoffset': dataset.xoffset, 'yoffset': dataset.yoffset, 'zoffset': dataset.zoffset, 'xdownmax': xdownmax, 'ydownmax': ydownmax, 'starttime': dataset.starttime, 'endtime': dataset.endtime, 'maxres': dataset.scalinglevels, 'minres': 0, 'res': res, 'xstart': x, 'ystart': y, 'zstart': z, 'marker': marker, 'timeseries': timeseries, } return render(request, 'ocpviz/viewer.html', context)