Пример #1
0
    def test_new(self):
        """Tests that GET /applicationsettings/new returns an appropriate JSON object for creating a new application settings object.

        The properties of the JSON object are 'languages', 'users' and
        'orthographies' and their values are arrays/lists.
        """

        # Add some orthographies.
        orthography1 = h.generate_default_orthography1()
        orthography2 = h.generate_default_orthography2()
        Session.add_all([orthography1, orthography2])
        Session.commit()

        # Get the data currently in the db (see websetup.py for the test data).
        data = {
            'languages': h.get_languages(),
            'users': h.get_mini_dicts_getter('User')(),
            'orthographies': h.get_mini_dicts_getter('Orthography')()
        }

        # JSON.stringify and then re-Python-ify the data.  This is what the data
        # should look like in the response to a simulated GET request.
        data = json.loads(json.dumps(data, cls=h.JSONOLDEncoder))

        # GET /applicationsettings/new without params.  Expect a JSON array for
        # every store.
        response = self.app.get(url('new_applicationsetting'),
                                extra_environ=self.extra_environ_admin)
        resp = json.loads(response.body)
        assert response.content_type == 'application/json'
        assert resp['languages'] == data['languages']
        assert resp['users'] == data['users']
        assert resp['orthographies'] == data['orthographies']
        assert response.content_type == 'application/json'

        # GET /applicationsettings/new with params.  Param values are treated as
        # strings, not JSON.  If any params are specified, the default is to
        # return a JSON array corresponding to store for the param.  There are
        # three cases that will result in an empty JSON array being returned:
        # 1. the param is not specified
        # 2. the value of the specified param is an empty string
        # 3. the value of the specified param is an ISO 8601 UTC datetime
        #    string that matches the most recent datetime_modified value of the
        #    store in question.
        params = {
            # Value is empty string: 'languages' will not be in response.
            'languages': '',
            # Value is any string: 'users' will be in response.
            'users': 'anything can go here!',
            # Value is ISO 8601 UTC datetime string that does not match the most
            # recent Orthography.datetime_modified value: 'orthographies' *will*
            # be in the response.
            'orthographies': datetime.datetime.utcnow().isoformat(),
        }
        response = self.app.get(url('new_applicationsetting'), params,
                                extra_environ=self.extra_environ_admin)
        resp = json.loads(response.body)
        assert resp['languages'] == []
        assert resp['users'] == data['users']
        assert resp['orthographies'] == data['orthographies']
Пример #2
0
def get_data_for_new_edit(GET_params):
    """Return the data needed to create a new morpheme language model or edit one."""
    model_name_map = {'corpora': 'Corpus', 'morphologies': 'Morphology'}
    getter_map = {
        'corpora': h.get_mini_dicts_getter('Corpus'),
        'morphologies': h.get_mini_dicts_getter('Morphology'),
        'toolkits': lambda: h.language_model_toolkits
    }
    mandatory_attributes = ['toolkits']
    return h.get_data_for_new_action(GET_params, getter_map, model_name_map, mandatory_attributes)
Пример #3
0
def get_data_for_new_edit(GET_params):
    """Return the data needed to create a new morpheme language model or edit one."""
    model_name_map = {'corpora': 'Corpus', 'morphologies': 'Morphology'}
    getter_map = {
        'corpora': h.get_mini_dicts_getter('Corpus'),
        'morphologies': h.get_mini_dicts_getter('Morphology'),
        'toolkits': lambda: h.language_model_toolkits
    }
    mandatory_attributes = ['toolkits']
    return h.get_data_for_new_action(GET_params, getter_map, model_name_map,
                                     mandatory_attributes)
Пример #4
0
def get_data_for_new_edit(GET_params):
    """Return the data needed to create a new morphological parser or edit one."""
    model_name_map = {
        'morpheme_language_models': 'MorphemeLanguageModel',
        'phonologies': 'Phonology',
        'morphologies': 'Morphology'
    }
    getter_map = {
        'morpheme_language_models': h.get_mini_dicts_getter('MorphemeLanguageModel'),
        'phonologies': h.get_mini_dicts_getter('Phonology'),
        'morphologies': h.get_mini_dicts_getter('Morphology')
    }
    return h.get_data_for_new_action(GET_params, getter_map, model_name_map)
Пример #5
0
def get_data_for_new_edit(GET_params):
    """Return the data needed to create a new corpus or edit one."""
    mandatory_attributes = ['corpus_formats']
    model_name_map = {
        'form_searches': 'FormSearch',
        'users': 'User',
        'tags': 'Tag'
    }
    getter_map = {
        'form_searches': h.get_mini_dicts_getter('FormSearch'),
        'users': h.get_mini_dicts_getter('User'),
        'tags': h.get_mini_dicts_getter('Tag'),
        'corpus_formats': lambda: h.corpus_formats.keys()
    }
    return h.get_data_for_new_action(GET_params, getter_map, model_name_map, mandatory_attributes)
Пример #6
0
def get_data_for_new_edit(GET_params):
    """Return the data needed to create a new corpus or edit one."""
    mandatory_attributes = ['corpus_formats']
    model_name_map = {
        'form_searches': 'FormSearch',
        'users': 'User',
        'tags': 'Tag'
    }
    getter_map = {
        'form_searches': h.get_mini_dicts_getter('FormSearch'),
        'users': h.get_mini_dicts_getter('User'),
        'tags': h.get_mini_dicts_getter('Tag'),
        'corpus_formats': lambda: h.corpus_formats.keys()
    }
    return h.get_data_for_new_action(GET_params, getter_map, model_name_map,
                                     mandatory_attributes)
Пример #7
0
def get_data_for_new_edit(GET_params):
    """Return the data needed to create a new morphological parser or edit one."""
    model_name_map = {
        'morpheme_language_models': 'MorphemeLanguageModel',
        'phonologies': 'Phonology',
        'morphologies': 'Morphology'
    }
    getter_map = {
        'morpheme_language_models':
        h.get_mini_dicts_getter('MorphemeLanguageModel'),
        'phonologies':
        h.get_mini_dicts_getter('Phonology'),
        'morphologies':
        h.get_mini_dicts_getter('Morphology')
    }
    return h.get_data_for_new_action(GET_params, getter_map, model_name_map)
Пример #8
0
def get_new_user_data(GET_params):
    """Return the data necessary to create a new OLD user or update an existing one.
    
    :param GET_params: the ``request.GET`` dictionary-like object generated by
        Pylons which contains the query string parameters of the request.
    :returns: A dictionary whose values are lists of objects needed to create or
        update user.

    If ``GET_params`` has no keys, then return all data.  If ``GET_params`` does
    have keys, then for each key whose value is a non-empty string (and not a
    valid ISO 8601 datetime) add the appropriate list of objects to the return
    dictionary.  If the value of a key is a valid ISO 8601 datetime string, add
    the corresponding list of objects *only* if the datetime does *not* match
    the most recent ``datetime_modified`` value of the resource.  That is, a
    non-matching datetime indicates that the requester has out-of-date data.

    """
    # model_name_map maps param names to the OLD model objects from which they are
    # derived.
    model_name_map = {'orthographies': 'Orthography'}

    # getter_map maps param names to getter functions that retrieve the
    # appropriate data from the db.
    getter_map = {'orthographies': h.get_mini_dicts_getter('Orthography')}

    # result is initialized as a dict with empty list values.
    result = dict([(key, []) for key in getter_map])
    result['roles'] = h.user_roles
    result['markup_languages'] = h.markup_languages

    # There are GET params, so we are selective in what we return.
    if GET_params:
        for key in getter_map:
            val = GET_params.get(key)
            # Proceed so long as val is not an empty string.
            if val:
                val_as_datetime_obj = h.datetime_string2datetime(val)
                if val_as_datetime_obj:
                    # Value of param is an ISO 8601 datetime string that
                    # does not match the most recent datetime_modified of the
                    # relevant model in the db: therefore we return a list
                    # of objects/dicts.  If the datetimes do match, this
                    # indicates that the requester's own stores are
                    # up-to-date so we return nothing.
                    if val_as_datetime_obj != h.get_most_recent_modification_datetime(
                    model_name_map[key]):
                        result[key] = getter_map[key]()
                else:
                    result[key] = getter_map[key]()

    # There are no GET params, so we get everything from the db and return it.
    else:
        for key in getter_map:
            result[key] = getter_map[key]()

    return result
Пример #9
0
def get_new_user_data(GET_params):
    """Return the data necessary to create a new OLD user or update an existing one.
    
    :param GET_params: the ``request.GET`` dictionary-like object generated by
        Pylons which contains the query string parameters of the request.
    :returns: A dictionary whose values are lists of objects needed to create or
        update user.

    If ``GET_params`` has no keys, then return all data.  If ``GET_params`` does
    have keys, then for each key whose value is a non-empty string (and not a
    valid ISO 8601 datetime) add the appropriate list of objects to the return
    dictionary.  If the value of a key is a valid ISO 8601 datetime string, add
    the corresponding list of objects *only* if the datetime does *not* match
    the most recent ``datetime_modified`` value of the resource.  That is, a
    non-matching datetime indicates that the requester has out-of-date data.

    """
    # model_name_map maps param names to the OLD model objects from which they are
    # derived.
    model_name_map = {'orthographies': 'Orthography'}

    # getter_map maps param names to getter functions that retrieve the
    # appropriate data from the db.
    getter_map = {'orthographies': h.get_mini_dicts_getter('Orthography')}

    # result is initialized as a dict with empty list values.
    result = dict([(key, []) for key in getter_map])
    result['roles'] = h.user_roles
    result['markup_languages'] = h.markup_languages

    # There are GET params, so we are selective in what we return.
    if GET_params:
        for key in getter_map:
            val = GET_params.get(key)
            # Proceed so long as val is not an empty string.
            if val:
                val_as_datetime_obj = h.datetime_string2datetime(val)
                if val_as_datetime_obj:
                    # Value of param is an ISO 8601 datetime string that
                    # does not match the most recent datetime_modified of the
                    # relevant model in the db: therefore we return a list
                    # of objects/dicts.  If the datetimes do match, this
                    # indicates that the requester's own stores are
                    # up-to-date so we return nothing.
                    if val_as_datetime_obj != h.get_most_recent_modification_datetime(
                            model_name_map[key]):
                        result[key] = getter_map[key]()
                else:
                    result[key] = getter_map[key]()

    # There are no GET params, so we get everything from the db and return it.
    else:
        for key in getter_map:
            result[key] = getter_map[key]()

    return result
Пример #10
0
def get_new_application_settings_data(GET_params):
    """Return the data necessary to create a new application settings or update an existing one.
    
    :param GET_params: the ``request.GET`` dictionary-like object generated by
        Pylons which contains the query string parameters of the request.
    :returns: A dictionary whose values are lists of objects needed to create or
        update application settings.

    If ``GET_params`` has no keys, then return all required data.  If
    ``GET_params`` does have keys, then for each key whose value is a non-empty
    string (and not a valid ISO 8601 datetime) add the appropriate list of
    objects to the return dictionary.  If the value of a key is a valid ISO 8601
    datetime string, add the corresponding list of objects *only* if the
    datetime does *not* match the most recent ``datetime_modified`` value of the
    resource.  That is, a non-matching datetime indicates that the requester has
    out-of-date data.

    """

    # model_name_map maps param names to the OLD model objects from which they are
    # derived.
    model_name_map = {
        'users': 'User',
        'orthographies': 'Orthography',
        'languages': 'Language'
    }

    # getter_map maps param names to getter functions that retrieve the
    # appropriate data from the db.
    getter_map = {
        'users': h.get_mini_dicts_getter('User'),
        'orthographies': h.get_mini_dicts_getter('Orthography'),
        'languages': h.get_languages
    }

    result = h.get_data_for_new_action(GET_params, getter_map, model_name_map)

    return result
Пример #11
0
def get_new_application_settings_data(GET_params):
    """Return the data necessary to create a new application settings or update an existing one.
    
    :param GET_params: the ``request.GET`` dictionary-like object generated by
        Pylons which contains the query string parameters of the request.
    :returns: A dictionary whose values are lists of objects needed to create or
        update application settings.

    If ``GET_params`` has no keys, then return all required data.  If
    ``GET_params`` does have keys, then for each key whose value is a non-empty
    string (and not a valid ISO 8601 datetime) add the appropriate list of
    objects to the return dictionary.  If the value of a key is a valid ISO 8601
    datetime string, add the corresponding list of objects *only* if the
    datetime does *not* match the most recent ``datetime_modified`` value of the
    resource.  That is, a non-matching datetime indicates that the requester has
    out-of-date data.

    """

    # model_name_map maps param names to the OLD model objects from which they are
    # derived.
    model_name_map = {
        'users': 'User',
        'orthographies': 'Orthography',
        'languages': 'Language'
    }

    # getter_map maps param names to getter functions that retrieve the
    # appropriate data from the db.
    getter_map = {
        'users': h.get_mini_dicts_getter('User'),
        'orthographies': h.get_mini_dicts_getter('Orthography'),
        'languages': h.get_languages
    }

    result = h.get_data_for_new_action(GET_params, getter_map, model_name_map)

    return result
Пример #12
0
def get_new_edit_file_data(GET_params):
    """Return the data necessary to create a new OLD file or update an existing one.
    
    :param GET_params: the ``request.GET`` dictionary-like object generated by
        Pylons which contains the query string parameters of the request.
    :returns: A dictionary whose values are lists of objects needed to create or
        update files.

    If ``GET_params`` has no keys, then return all relevant data lists.  If
    ``GET_params`` does have keys, then for each key whose value is a non-empty
    string (and not a valid ISO 8601 datetime) add the appropriate list of
    objects to the return dictionary.  If the value of a key is a valid ISO 8601
    datetime string, add the corresponding list of objects *only* if the
    datetime does *not* match the most recent ``datetime_modified`` value of the
    resource.  That is, a non-matching datetime indicates that the requester has
    out-of-date data.

    """
    # Map param names to the OLD model objects from which they are derived.
    param_name2model_name = {
        'tags': 'Tag',
        'speakers': 'Speaker',
        'users': 'User'
    }

    # map_ maps param names to functions that retrieve the appropriate data
    # from the db.
    map_ = {
        'tags': h.get_mini_dicts_getter('Tag'),
        'speakers': h.get_mini_dicts_getter('Speaker'),
        'users': h.get_mini_dicts_getter('User')
    }

    # result is initialized as a dict with empty list values.
    result = dict([(key, []) for key in map_])
    result['utterance_types'] = h.utterance_types
    result['allowed_file_types'] = h.allowed_file_types

    # There are GET params, so we are selective in what we return.
    if GET_params:
        for key in map_:
            val = GET_params.get(key)
            # Proceed so long as val is not an empty string.
            if val:
                val_as_datetime_obj = h.datetime_string2datetime(val)
                if val_as_datetime_obj:
                    # Value of param is an ISO 8601 datetime string that
                    # does not match the most recent datetime_modified of the
                    # relevant model in the db: therefore we return a list
                    # of objects/dicts.  If the datetimes do match, this
                    # indicates that the requester's own stores are
                    # up-to-date so we return nothing.
                    if val_as_datetime_obj != h.get_most_recent_modification_datetime(
                    param_name2model_name[key]):
                        result[key] = map_[key]()
                else:
                    result[key] = map_[key]()

    # There are no GET params, so we get everything from the db and return it.
    else:
        for key in map_:
            result[key] = map_[key]()

    return result
Пример #13
0
def get_data_for_new_edit(GET_params):
    """Return the data needed to create a new morphology or edit one."""
    model_name_map = {'corpora': 'Corpus'}
    getter_map = {'corpora': h.get_mini_dicts_getter('Corpus')}
    return h.get_data_for_new_action(GET_params, getter_map, model_name_map)
Пример #14
0
    def test_edit(self):
        """Tests that GET /applicationsettings/id/edit returns a JSON object for editing an existing application settings.

        The JSON object is of the form {application_settings: {...}, data: {...}}
        or {'error': '...'} (and a 404 status code) depending on whether the id
        is valid or invalid/unspecified, respectively.
        """

        # Not logged in: expect 401 Unauthorized
        response = self.app.get(
            url('edit_applicationsetting', id=100000000000), status=401)
        resp = json.loads(response.body)
        assert resp['error'] == u'Authentication is required to access this resource.'
        assert response.content_type == 'application/json'

        # Invalid id: expect 404 Not Found
        id = 100000000000
        response = self.app.get(url('edit_applicationsetting', id=id),
                            extra_environ=self.extra_environ_admin, status=404)
        assert json.loads(response.body)['error'] == \
            u'There is no application settings with id %s' % id
        assert response.content_type == 'application/json'

        # No id: expect 404 Not Found
        response = self.app.get(url('edit_applicationsetting', id=''),
            status=404, extra_environ=self.extra_environ_admin)
        assert json.loads(response.body)['error'] == \
            'The resource could not be found.'

        # Add the default application settings.
        application_settings = add_default_application_settings()
        application_settings = h.get_application_settings()
        application_settings_id = application_settings.id

        # Valid id
        response = self.app.get(url('edit_applicationsetting', id=application_settings_id),
                                extra_environ=self.extra_environ_admin)
        resp = json.loads(response.body)
        assert response.content_type == 'application/json'
        assert type(resp) == type({})
        assert resp['application_settings']['object_language_name'] == \
            application_settings.object_language_name

        # Valid id with GET params.  Param values are treated as strings, not
        # JSON.  If any params are specified, the default is to return a JSON
        # array corresponding to store for the param.  There are three cases
        # that will result in an empty JSON array being returned:
        # 1. the param is not specified
        # 2. the value of the specified param is an empty string
        # 3. the value of the specified param is an ISO 8601 UTC datetime
        #    string that matches the most recent datetime_modified value of the
        #    store in question.

        # Get the data currently in the db (see websetup.py for the test data).
        data = {
            'languages': h.get_languages(),
            'users': h.get_mini_dicts_getter('User')(),
            'orthographies': h.get_mini_dicts_getter('Orthography')()
        }
        # JSON.stringify and then re-Python-ify the data.  This is what the data
        # should look like in the response to a simulated GET request.
        data = json.loads(json.dumps(data, cls=h.JSONOLDEncoder))

        params = {
            # Value is a non-empty string: 'users' will be in response.
            'users': 'give me some users!',
            # Value is empty string: 'languages' will not be in response.
            'languages': '',
            # Value is ISO 8601 UTC datetime string that does not match the most
            # recent Orthography.datetime_modified value: 'orthographies' *will*
            # be in the response.
            'orthographies': datetime.datetime.utcnow().isoformat(),
        }
        response = self.app.get(url('edit_applicationsetting', id=application_settings_id), params,
                                extra_environ=self.extra_environ_admin)
        resp = json.loads(response.body)
        assert resp['data']['users'] == data['users']
        assert resp['data']['languages'] == []
        assert resp['data']['orthographies'] == data['orthographies']

        # Invalid id with GET params.  It should still return a 404 Not Found.
        params = {
            # If id were valid, this would cause a users array to be returned also.
            'users': 'True',
        }
        response = self.app.get(
            url('edit_applicationsetting', id=id), params,
            extra_environ=self.extra_environ_admin, status=404)
        assert json.loads(response.body)['error'] == \
            u'There is no application settings with id %s' % id