def addApplet(new_user, protocolUrl):
    """
    adds an applet for the user, where the user becomes a manager for it.

    inputs
    ------

    new_user: a user oject (from testCreateUser)
    protocolURL: String, a valid URL to an activity set.

    returns
    -------
    applet response object

    """

    currentUser = authenticate(new_user)

    # TODO: create an activity-set that JUST for testing.
    # make sure it has all the weird qualities that can break

    userAppletsToStart = AppletModel().getAppletsForUser('manager',
                                                         currentUser,
                                                         active=True)

    userApplets = userAppletsToStart.copy()

    # for now, lets do the mindlogger demo
    protocol = ProtocolModel().getFromUrl(protocolUrl, 'protocol',
                                          currentUser)[0]
    randomAS = np.random.randint(1000000)
    ar = AppletModel().createAppletFromUrl(
        name="testProtocol{}".format(randomAS),
        protocolUrl=protocolUrl,
        user=currentUser,
        sendEmail=False)

    while len(userApplets) == len(userAppletsToStart):
        nightyNight(sleepInterval)
        userApplets = AppletModel().getAppletsForUser('manager',
                                                      currentUser,
                                                      active=True)

    ar = jsonld_expander.loadCache(userApplets[-1]['cached'])

    assert jsonld_expander.reprolibCanonize(
        ar['protocol']['url']
    ) == jsonld_expander.reprolibCanonize(protocolUrl), \
        'the URLS do not match! {} {}'.format(
            ar['protocol']['url'],
            protocolUrl
        )

    assert ar['applet']['_id'], 'there is no ID!'

    assert getAppletById(new_user,
                         ar) is not None, 'something wrong with getAppletById'
    return ar
def smartImport(IRI, user=None, refreshCache=False, modelType=None):
    from girderformindlogger.constants import MODELS
    from girderformindlogger.utility.jsonld_expander import loadCache,         \
        reprolibCanonize

    MODELS = MODELS()
    mt1 = "screen" if modelType in [None, "external JSON-LD document"
                                    ] else modelType
    model, modelType = MODELS[mt1]().getFromUrl(IRI,
                                                user=user,
                                                refreshCache=refreshCache,
                                                thread=False)
    return ((modelType, loadCache(model.get('cached',
                                            model)), reprolibCanonize(IRI)))
def cycleModels(IRIset, modelType=None, meta={}):
    from girderformindlogger.constants import HIERARCHY, REPROLIB_TYPES
    from girderformindlogger.models.folder import Folder as FolderModel
    from girderformindlogger.models.item import Item as ItemModel
    from girderformindlogger.utility.jsonld_expander import reprolibCanonize

    cachedDoc = None
    primary = [modelType] if isinstance(
        modelType, str) else [] if modelType is None else modelType
    secondary = [m for m in HIERARCHY if m not in primary]

    del modelType

    if len(primary):
        query = {
            '$and': [
                {  # search by type
                    '$or': [{
                        'meta.{}.@type'.format(modelType): {
                            "$in": [
                                t for t in [
                                    reprolibCanonize(REPROLIB_TYPES[modelType]
                                                     ), 'reproschema:{}'.
                                    format(suffix), 'reprolib:{}'.format(
                                        suffix), 'reprolib:schemas/{}'.format(
                                            suffix), suffix
                                ] if t is not None
                            ]
                            for suffix in
                            [REPROLIB_TYPES[modelType].split('/')[-1]]
                        }
                    } for modelType in primary if modelType in REPROLIB_TYPES]
                },
                {  # search by url
                    '$or': [{
                        'meta.{}.url'.format(modelType): {
                            '$in': list(IRIset)
                        }
                    } for modelType in primary if modelType in REPROLIB_TYPES]
                },
                *[{
                    'meta.{}'.format(key): meta[key]
                } for key in meta]
            ]
        }
        cachedDoc = (FolderModel()
                     if not any(['screen' in primary, 'item' in primary]) else
                     ItemModel()).findOne(query)
    if cachedDoc is None:
        query = {
            '$and': [{
                '$or': [{
                    'meta.{}.@type'.format(modelType): {
                        "$in": [
                            t for t in [
                                reprolibCanonize(REPROLIB_TYPES[modelType]),
                                'reproschema:{}'.format(
                                    suffix), 'reprolib:{}'.format(suffix),
                                'reprolib:schemas/{}'.format(suffix), suffix
                            ] if t is not None
                        ]
                        for suffix in
                        [REPROLIB_TYPES[modelType].split('/')[-1]]
                    }
                } for modelType in secondary if modelType in REPROLIB_TYPES]
            }, {
                '$or': [{
                    'meta.{}.url'.format(modelType): {
                        '$in': list(IRIset)
                    }
                } for modelType in secondary if modelType in REPROLIB_TYPES]
            }, *[{
                'meta.{}'.format(key): meta[key]
            } for key in meta]]
        }
        cachedDoc = FolderModel().findOne(query)
    if cachedDoc is None:
        cachedDoc = ItemModel().findOne(query)

    if cachedDoc is None:
        return (None, None)

    modelType = [
        rt for rt in list(REPROLIB_TYPES.keys())
        if '@type' in cachedDoc.get('meta', {}).get(rt, {})
    ]
    modelType = modelType[0] if len(modelType) else None

    print("Found {}/{}".format(modelType, str(cachedDoc['_id'])))
    return (modelType, cachedDoc)