Ejemplo n.º 1
0
def LoadAppFromJSON(manifest_string, app_params):
  r = simplejson.loads(manifest_string)

  if "override_index" in app_params:
      r["index"] = app_params["override_index"]

  if "override_icon" in app_params:
      r["icon"] = app_params["override_icon"]

  enabled_by_default = False
  if "enabled_by_default" in app_params:
      enabled_by_default = app_params["enabled_by_default"]

  manifest_string = json.dumps(r, sort_keys=True, indent=4)

  if r["mode"] in ("background", "helper"):
      a = HelperApp.objects.create(
                       description = r["description"],
                       consumer_key = r["id"],
                       secret = 'smartapp-secret',
                       name =r["name"],
                       email=r["id"],
                       manifest=manifest_string)
      
  elif r["mode"] in ("ui", "frame_ui"):

      if "optimalBrowserEnvironments" not in r:
          r["optimalBrowserEnvironments"] = ["desktop"]
      if "supportedBrowserEnvironments" not in r:
          r["supportedBrowserEnvironments"] = ["desktop", "mobile", "tablet"]
              
      exists = PHA.objects.filter(email=r["id"])
      assert len(exists) <2, "Found >1 PHA by the name %s"%r["id"]
      if len(exists)==1:
          print exists[0]
          print "deleting, exists."
          exists[0].delete()

      a = PHA.objects.create(
                       description = r["description"],
                       consumer_key = r["id"],
                       secret = 'smartapp-secret',
                       name =r["name"],
                       email=r["id"],
                       mode=r["mode"],
                       icon_url=r["icon"],
                       enabled_by_default=enabled_by_default,
                       optimal_environments=",".join(r["optimalBrowserEnvironments"]),
                       supported_environments=",".join(r["supportedBrowserEnvironments"]),
                       manifest=manifest_string)
  else: a = None

  if "index" in r:
      act_name = "main"
      act_url  = r["index"]
      AppActivity.objects.create(app=a, name=act_name, url=act_url)
  
  if "requires" in r:  
    capabilities = get_capabilities()
    for k in r["requires"]:
        for m in r["requires"][k]["methods"]:
            if m not in capabilities[k]["methods"]:
                print "WARNING! This app requires an unsupported method:", k, m
                
  if "smart_version" in r:  
    if r["smart_version"] != settings.VERSION:
        print "WARNING! This app requires SMART version", r["smart_version"]

  if "web_hooks" in r:
    for (hook_name, hook_data) in r["web_hooks"].iteritems():
      hook_url = hook_data["url"]

      try: rpc = hook_data['requires_patient_context']
      except: rpc = False
      
      AppWebHook.objects.create(app=a,
                              name=hook_name, 
                              description=hook_data["description"],
                              url=hook_url,
                              requires_patient_context=rpc)
Ejemplo n.º 2
0
def LoadAppFromJSON(manifest_string, app_params=None):
    """ Reads an app manifest
    """
    if app_params == None:
        app_params = {}

    if "secret" not in app_params:
        print "No consumer secret among the app params. Generating consumer secret."
        app_params["secret"] = random_string(16)

    r = simplejson.loads(manifest_string)
    secret = app_params["secret"]

    messages = app_manifest_structure_validator(r)
    if len(messages) > 0:
        msg = "WARNING! This app manifest is invalid: %s (app %s)" % ('. '.join(messages), r['id'])
        raise Exception(msg)

    if "override_index" in app_params:
        r["index"] = app_params["override_index"]

    if "override_icon" in app_params:
        r["icon"] = app_params["override_icon"]

    enabled_by_default = False
    if "enabled_by_default" in app_params:
        enabled_by_default = app_params["enabled_by_default"]

    manifest_string = json.dumps(r, sort_keys=True, indent=4)

    # background app
    if r["mode"] in ("background", "helper"):
        a = HelperApp.objects.create(
            description=r["description"],
            consumer_key=r["id"],
            secret=secret,
            name=r["name"],
            email=r["id"],
            manifest=manifest_string
        )

    # ui app
    elif r["mode"] in ("ui", "frame_ui"):

        # extract optimal environments
        if "optimalBrowserEnvironments" not in r:
            r["optimalBrowserEnvironments"] = ["desktop"]
        if "supportedBrowserEnvironments" not in r:
            r["supportedBrowserEnvironments"] = ["desktop", "mobile", "tablet"]
        opt_browsers = ",".join(r["optimalBrowserEnvironments"])
        sup_browsers = ",".join(r["supportedBrowserEnvironments"])
        
        # extract standalone
        is_standalone = False
        if "standalone" in r:
            is_standalone = r["standalone"]

        exists = PHA.objects.filter(email=r["id"])
        assert len(exists) < 2, "Found >1 PHA by the name %s" % r["id"]
        if len(exists) == 1:
            print exists[0]
            print "deleting, exists."
            exists[0].delete()
        
        a = PHA.objects.create(
            description=r["description"],
            consumer_key=r["id"],
            secret=secret,
            name=r["name"],
            email=r["id"],
            mode=r["mode"],
            standalone=is_standalone,
            icon_url=r["icon"],
            enabled_by_default=enabled_by_default,
            optimal_environments=opt_browsers,
            supported_environments=sup_browsers,
            manifest=manifest_string
        )
    else:
        a = None

    # should probably return here if no App was created
    if a is None:
        return None


    if "requires" in r:
        capabilities = get_capabilities()
        for k in r["requires"]:
            if k not in capabilities:
                print "WARNING! This app requires an unsupported datatype:", k
                break
            for m in r["requires"][k]["methods"]:
                if m not in capabilities[k]["methods"]:
                    print "WARNING! This app requires an unsupported method:", k, m

    if "smart_version" in r:
        if r["smart_version"] != settings.VERSION:
            print "WARNING! This app requires SMART version", r["smart_version"]

    return a