示例#1
0
def am_sitedirectory(annroot, userhome, options):
    """
    Print name of Annalist site directory to standard output

    annroot     is the root directory for the Annalist software installation.
    userhome    is the home directory for the host system user issuing the command.
    options     contains options parsed from the command line.

    returns     0 if all is well, or a non-zero status code.
                This value is intended to be used as an exit status code
                for the calling program.
    """
    settings = am_get_settings(annroot, userhome, options)
    if not settings:
        print("Settings not found (%s)" % (options.configuration),
              file=sys.stderr)
        return am_errors.AM_NOSETTINGS
    if len(options.args) > 0:
        print("Unexpected arguments for %s: (%s)" %
              (options.command, " ".join(options.args)),
              file=sys.stderr)
        return am_errors.AM_UNEXPECTEDARGS
    status = am_errors.AM_SUCCESS
    with SuppressLogging(logging.INFO):
        sitesettings = importlib.import_module(settings.modulename)
    sitedirectory = sitesettings.BASE_SITE_DIR
    print(sitedirectory)
    # with open(sitedirectory, "r") as logfile:
    #     shutil.copyfileobj(logfile, sys.stdout)
    return status
 def test_bad_user_permissions_form_rendering(self):
     # Test handling of permissions not stored in entity as a list of values
     create_user_permissions(self.testcoll,
                             "baduserperms",
                             user_permissions="VIEW CREATE UPDATE DELETE")
     u = entitydata_edit_url("edit",
                             "testcoll",
                             "_user",
                             "baduserperms",
                             view_id="User_view")
     with SuppressLogging(logging.WARNING):
         r = self.client.get(u)
     self.assertEqual(r.status_code, 200)
     self.assertEqual(r.reason_phrase, "OK")
     field_vals = default_fields(coll_id="testcoll",
                                 type_id="_user",
                                 entity_id="baduserperms")
     formrow5 = """
         <div class="small-12 columns">
             <div class="row">
                 <div class="%(label_classes)s">
                     <p>Permissions</p>
                 </div>
                 <div class="%(input_classes)s">
                     <input type="text" size="64" name="User_permissions" 
                            placeholder="(user permissions; e.g. &#39;VIEW CREATE UPDATE DELETE&#39;)"
                            value="VIEW CREATE UPDATE DELETE"/>
                 </div>
             </div>
         </div>
         """ % field_vals(width=12)
     # log.info(r.content)
     self.assertContains(r, formrow5, html=True)
     return
示例#3
0
def am_initialize(annroot, userhome, userconfig, options):
    """
    Initialize Annalist server data, database, etc.

    annroot     is the root directory for theannalist software installation.
    userhome    is the home directory for the host system user issuing the initialize command.
    userconfig  is the directory used for user-specific configuration files.
    options     contains options parsed from the command line.

    returns     0 if all is well, or a non-zero status code.
                This value is intended to be used as an exit status code
                for the calling program.
    """
    settings = am_get_settings(annroot, userhome, options)
    if not settings:
        print("Settings not found (%s)"%(options.configuration), file=sys.stderr)
        return am_errors.AM_NOSETTINGS
    if len(options.args) != 0:
        print("Unexpected arguments for initialize: (%s)"%(" ".join(options.args)), file=sys.stderr)
        return am_errors.AM_UNEXPECTEDARGS
    # Get config base directory from settings, and make sure it exists
    with SuppressLogging(logging.INFO):
        sitesettings = importlib.import_module(settings.modulename)
    providersdir = os.path.join(sitesettings.CONFIG_BASE, "providers")
    databasedir  = os.path.dirname(sitesettings.DATABASES['default']['NAME'])
    ensure_dir(providersdir)
    ensure_dir(databasedir)
    # Initialze the database
    status = am_errors.AM_SUCCESS
    subprocess_command = "django-admin migrate --pythonpath=%s --settings=%s"%(annroot, settings.modulename)
    log.debug("am_initialize subprocess: %s"%subprocess_command)
    # OLD: status = os.system(subprocess_command)
    status = subprocess.call(subprocess_command.split())
    log.debug("am_initialize subprocess status: %s"%status)
    return status
 def test_TokenSetValueMapperEncode(self):
     fieldrender = TokenSetValueMapper()
     self.assertEqual(fieldrender.encode(["aa"]), "aa")
     self.assertEqual(fieldrender.encode(["aa", "bb", "cc" ]), "aa bb cc")
     # Rendering non-list generates warning
     with SuppressLogging(logging.WARNING):
         self.assertEqual(fieldrender.encode("aa"), "aa")
     return
 def test_set_alt_entities_loop(self):
     altcoll1  = Collection(self.testsite, "altcoll1")
     parents   = self.testcoll.set_alt_entities(altcoll1)
     parentids = [ p.get_id() for p in parents ]
     self.assertEqual( parentids, ["testcoll", "altcoll1", layout.SITEDATA_ID])
     with SuppressLogging(logging.ERROR):
         with self.assertRaises(ValueError):
             parents   = altcoll1.set_alt_entities(self.testcoll)
     return
示例#6
0
def get_site_settings(annroot, userhome, options):
    """
    Access site settings, set up correspondingh django configuration and return the settings module
    """
    settings = am_get_settings(annroot, userhome, options)
    if not settings:
        print("Settings not found (%s)"%(options.configuration), file=sys.stderr)
        return None
    with SuppressLogging(logging.INFO):
        os.environ['DJANGO_SETTINGS_MODULE'] = settings.modulename
        django.setup()
        site_settings = importlib.import_module(settings.modulename)
    return site_settings
示例#7
0
 def test_get_view_no_view(self):
     u = entitydata_edit_url("edit",
                             "testcoll",
                             "testtype",
                             entity_id="entity1",
                             view_id="no_view")
     with SuppressLogging(logging.WARNING):
         r = self.client.get(u)
     self.assertEqual(r.status_code, 200)
     self.assertEqual(r.reason_phrase, "OK")
     msg_text = make_message(message.RECORD_VIEW_NOT_EXISTS, id="no_view")
     self.assertContains(r, msg_text)
     return
示例#8
0
 def test_get_view_no_type(self):
     u = entitydata_edit_url("edit",
                             "testcoll",
                             "no_type",
                             entity_id="entity1",
                             view_id="Type_view")
     with SuppressLogging(logging.WARNING):
         r = self.client.get(u)
     self.assertEqual(r.status_code, 404)
     self.assertEqual(r.reason_phrase, "Not found")
     msg_text = make_message(message.RECORD_TYPE_NOT_EXISTS, id="no_type")
     self.assertContains(r, msg_text, status_code=404)
     return
 def test_get_view_no_view(self):
     u = entitydata_edit_url("edit",
                             "testcoll",
                             "_field",
                             entity_id="entity1",
                             view_id="no_view")
     with SuppressLogging(logging.WARNING):
         r = self.client.get(u)
     self.assertEqual(r.status_code, 404)
     self.assertEqual(r.reason_phrase, "Not found")
     self.assertContains(
         r,
         "Record view no_view in collection testcoll does not exist",
         status_code=404)
     return
示例#10
0
def am_updatesite(annroot, userhome, options):
    """
    Update site data, leaving user data alone

    annroot     is the root directory for the Annalist software installation.
    userhome    is the home directory for the host system user issuing the command.
    options     contains options parsed from the command line.

    returns     0 if all is well, or a non-zero status code.
                This value is intended to be used as an exit status code
                for the calling program.
    """
    settings = am_get_settings(annroot, userhome, options)
    if not settings:
        print("Settings not found (%s)" % (options.configuration),
              file=sys.stderr)
        return am_errors.AM_NOSETTINGS
    if len(options.args) > 0:
        print("Unexpected arguments for %s: (%s)" %
              (options.command, " ".join(options.args)),
              file=sys.stderr)
        return am_errors.AM_UNEXPECTEDARGS
    status = am_errors.AM_SUCCESS
    with SuppressLogging(logging.INFO):
        sitesettings = importlib.import_module(settings.modulename)
    sitebasedir = os.path.join(sitesettings.BASE_DATA_DIR, "annalist_site")
    # Test if site exists
    if not os.path.exists(sitebasedir):
        print("Site %s not found" % (sitebasedir), file=sys.stderr)
        return am_errors.AM_NOTEXISTS
    # --- Copy built-in types and views data to target area
    site_layout = Layout(sitesettings.BASE_DATA_DIR)
    sitedatasrc = os.path.join(annroot, "annalist/sitedata")
    sitedatatgt = os.path.join(sitebasedir, site_layout.SITEDATA_DIR)
    print("Copy Annalist site data from %s to %s" % (sitedatasrc, sitedatatgt))
    for sdir in ("types", "lists", "views", "groups", "fields", "enums"):
        s = os.path.join(sitedatasrc, sdir)
        d = os.path.join(sitedatatgt, sdir)
        print("- %s => %s" % (sdir, d))
        replacetree(s, d)
    for sdir in ("users", ):
        s = os.path.join(sitedatasrc, sdir)
        d = os.path.join(sitedatatgt, sdir)
        print("- %s +> %s" % (sdir, d))
        updatetree(s, d)
    return status
示例#11
0
def am_createsite(annroot, userhome, options):
    """
    Create Annalist empty site data.

    annroot     is the root directory for the Annalist software installation.
    userhome    is the home directory for the host system user issuing the command.
    options     contains options parsed from the command line.

    returns     0 if all is well, or a non-zero status code.
                This value is intended to be used as an exit status code
                for the calling program.
    """
    settings = am_get_settings(annroot, userhome, options)
    if not settings:
        print("Settings not found (%s)" % (options.configuration),
              file=sys.stderr)
        return am_errors.AM_NOSETTINGS
    options.configuration = "runtests"
    testsettings = am_get_settings(annroot, "/nouser/", options)
    if not testsettings:
        print("Settings not found (%s)" % ("runtests"), file=sys.stderr)
        return am_errors.AM_NOSETTINGS
    if len(options.args) > 0:
        print("Unexpected arguments for %s: (%s)" %
              (options.command, " ".join(options.args)),
              file=sys.stderr)
        return am_errors.AM_UNEXPECTEDARGS
    status = am_errors.AM_SUCCESS
    emptysitedir = os.path.join(annroot, "sampledata/empty/annalist_site")
    with SuppressLogging(logging.INFO):
        sitesettings = importlib.import_module(settings.modulename)
    sitebasedir = os.path.join(sitesettings.BASE_DATA_DIR, "annalist_site")
    # Test if old site exists
    if os.path.exists(sitebasedir):
        if options.force:
            # --- Remove old site data from target area
            print("Removing old Annalist site at %s" % (sitebasedir))
            log.info("rmtree: %s" % (sitebasedir))
            removetree(sitebasedir)
        else:
            print(
                "Old data already exists at %s (use --force lor -f to overwrite)"
                % (sitebasedir),
                file=sys.stderr)
            return am_errors.AM_EXISTS
    # --- Copy empty site data to target area
    print("Initializing Annalist site in %s" % (sitebasedir))
    log.info("copytree: %s to %s" % (emptysitedir, sitebasedir))
    shutil.copytree(emptysitedir, sitebasedir)
    # --- Copy built-in types and views data to target area
    site_layout = Layout(sitesettings.BASE_DATA_DIR)
    sitedatasrc = os.path.join(annroot, "annalist/sitedata")
    sitedatatgt = os.path.join(sitebasedir, site_layout.SITEDATA_DIR)
    print("Copy Annalist site data from %s to %s" % (sitedatasrc, sitedatatgt))
    for sdir in ("types", "lists", "views", "groups", "fields", "enums",
                 "users"):
        s = os.path.join(sitedatasrc, sdir)
        d = os.path.join(sitedatatgt, sdir)
        print("- %s -> %s" % (sdir, d))
        shutil.copytree(s, d)
    return status
示例#12
0
 def test_bad_user_id(self):
     self.login_user("user-bad-name")
     with SuppressLogging(logging.WARNING):
         self.assertEqual(self.view_site_home_page().status_code, 400)
     return