Example #1
0
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)
Example #2
0
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')
Example #3
0
    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.token = token_str
    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()
    tmp_layer.propagate = channel['propagate']
    layers.append(tmp_layer)

  # package data for the template
  xdownmax = (ximagesize + xoffset - 1)/(2**scalinglevels)
  ydownmax = (yimagesize + yoffset - 1)/(2**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 = zoffset
  if res is None:
    res = scalinglevels