Exemplo n.º 1
0
def update_collection_by_deletion_of_referenced_form(collection, referenced_form):
    """Update a collection based on the deletion of a form it references.

    This function is called in the :class:`FormsController` when a form is
    deleted.  It is called on each collection that references the deleted form
    and the changes to each of those collections are propagated through all of
    the collections that reference them, and so on.
    
    :param collection: a collection model object.
    :param referenced_form: a form model object.
    :returns: ``None``.

    """
    collection_dict = collection.get_full_dict()
    collection.contents = remove_references_to_this_form(collection.contents, referenced_form.id)
    collections_referenced = get_collections_referenced(collection.contents)
    collection.contents_unpacked = generate_contents_unpacked(
                                collection.contents, collections_referenced)
    collection.html = h.get_HTML_from_contents(collection.contents_unpacked,
                                              collection.markup_language)
    collection.datetime_modified = datetime.datetime.utcnow()
    backup_collection(collection_dict)
    update_collections_that_reference_this_collection(
        collection, OldcollectionsController.query_builder, contents_changed=True)
    Session.add(collection)
    Session.commit()
Exemplo n.º 2
0
    def update(self, id):
        """Update an application settings and return it.
        
        :URL: ``PUT /applicationsettings/id``
        :Request body: JSON object representing the application settings with updated attribute values.
        :param str id: the ``id`` value of the application settings to be updated.
        :returns: the updated application settings model.

        """
        application_settings = h.eagerload_application_settings(
            Session.query(ApplicationSettings)).get(int(id))
        if application_settings:
            try:
                schema = ApplicationSettingsSchema()
                values = json.loads(unicode(request.body, request.charset))
                data = schema.to_python(values)
                # Try to create an updated ApplicationSetting object.
                application_settings = update_application_settings(application_settings, data)
                # application_settings will be False if there are no changes
                if application_settings:
                    Session.add(application_settings)
                    Session.commit()
                    app_globals.application_settings = h.ApplicationSettings()
                    return application_settings
                else:
                    response.status_int = 400
                    return {'error': 'The update request failed because the submitted data were not new.'}
            except h.JSONDecodeError:
                response.status_int = 400
                return h.JSONDecodeErrorResponse
            except Invalid, e:
                response.status_int = 400
                return {'errors': e.unpack_errors()}
Exemplo n.º 3
0
Arquivo: tags.py Projeto: FieldDB/old
    def update(self, id):
        """Update a tag and return it.
        
        :URL: ``PUT /tags/id``
        :Request body: JSON object representing the tag with updated attribute values.
        :param str id: the ``id`` value of the tag to be updated.
        :returns: the updated tag model.

        """
        tag = Session.query(Tag).get(int(id))
        if tag:
            try:
                schema = TagSchema()
                values = json.loads(unicode(request.body, request.charset))
                state = h.get_state_object(values)
                state.id = id
                data = schema.to_python(values, state)
                tag = update_tag(tag, data)
                # tag will be False if there are no changes (cf. update_tag).
                if tag:
                    Session.add(tag)
                    Session.commit()
                    return tag
                else:
                    response.status_int = 400
                    return {'error':
                        u'The update request failed because the submitted data were not new.'}
            except h.JSONDecodeError:
                response.status_int = 400
                return h.JSONDecodeErrorResponse
            except Invalid, e:
                response.status_int = 400
                return {'errors': e.unpack_errors()}
Exemplo n.º 4
0
    def tearDown(self, **kwargs):
        clear_all_tables = kwargs.get('clear_all_tables', False)
        dirs_to_clear = kwargs.get('dirs_to_clear', [])
        del_global_app_set = kwargs.get('del_global_app_set', False)
        dirs_to_destroy = kwargs.get('dirs_to_destroy', [])

        if clear_all_tables:
            h.clear_all_tables(['language'])
        else:
            h.clear_all_models()
        administrator = h.generate_default_administrator()
        contributor = h.generate_default_contributor()
        viewer = h.generate_default_viewer()
        Session.add_all([administrator, contributor, viewer])
        Session.commit()

        for dir_path in dirs_to_clear:
            h.clear_directory_of_files(getattr(self, dir_path))

        for dir_name in dirs_to_destroy:
            {
                'user': lambda: h.destroy_all_directories('users', 'test.ini'),
                'corpus': lambda: h.destroy_all_directories('corpora', 'test.ini'),
                'phonology': lambda: h.destroy_all_directories('phonologies', 'test.ini'),
                'morphology': lambda: h.destroy_all_directories('morphologies', 'test.ini'),
                'morphological_parser': lambda: h.destroy_all_directories('morphological_parsers', 'test.ini'),
                'morpheme_language_model': lambda: h.destroy_all_directories('morpheme_language_models', 'test.ini')
            }.get(dir_name, lambda: None)()

        if del_global_app_set:
            # Perform a vacuous GET just to delete app_globals.application_settings
            # to clean up for subsequent tests.
            self.app.get(url('new_form'), extra_environ=self.extra_environ_admin_appset)
Exemplo n.º 5
0
    def update(self, id):
        """Update a source and return it.
        
        :URL: ``PUT /sources/id``
        :Request body: JSON object representing the source with updated attribute values.
        :param str id: the ``id`` value of the source to be updated.
        :returns: the updated source model.

        """
        source = Session.query(Source).get(int(id))
        if source:
            try:
                schema = SourceSchema()
                values = json.loads(unicode(request.body, request.charset))
                state = h.get_state_object(values)
                state.id = id
                data = schema.to_python(values, state)
                source = update_source(source, data)
                # source will be False if there are no changes (cf. update_source).
                if source:
                    Session.add(source)
                    Session.commit()
                    return source
                else:
                    response.status_int = 400
                    return {'error':
                        u'The update request failed because the submitted data were not new.'}
            except h.JSONDecodeError:
                response.status_int = 400
                return h.JSONDecodeErrorResponse
            except Invalid, e:
                response.status_int = 400
                return {'errors': e.unpack_errors()}
Exemplo n.º 6
0
    def delete(self, id):
        """Delete an existing collection and return it.

        :URL: ``DELETE /collections/id``
        :param str id: the ``id`` value of the collection to be deleted.
        :returns: the deleted collection model.

        .. note::

           Only administrators and a collection's enterer can delete it.

        """
        collection = h.eagerload_collection(Session.query(Collection),
                                            eagerload_forms=True).get(id)
        if collection:
            if session['user'].role == u'administrator' or \
            collection.enterer is session['user']:
                session['user'] = Session.merge(session['user'])
                collection.modifier = session['user']
                collection_dict = collection.get_full_dict()
                backup_collection(collection_dict)
                update_collections_that_reference_this_collection(
                    collection, self.query_builder, deleted=True)
                Session.delete(collection)
                Session.commit()
                return collection_dict
            else:
                response.status_int = 403
                return h.unauthorized_msg
        else:
            response.status_int = 404
            return {'error': 'There is no collection with id %s' % id}
Exemplo n.º 7
0
def update_collection_by_deletion_of_referenced_form(collection,
                                                     referenced_form):
    """Update a collection based on the deletion of a form it references.

    This function is called in the :class:`FormsController` when a form is
    deleted.  It is called on each collection that references the deleted form
    and the changes to each of those collections are propagated through all of
    the collections that reference them, and so on.
    
    :param collection: a collection model object.
    :param referenced_form: a form model object.
    :returns: ``None``.

    """
    collection_dict = collection.get_full_dict()
    collection.contents = remove_references_to_this_form(
        collection.contents, referenced_form.id)
    collections_referenced = get_collections_referenced(collection.contents)
    collection.contents_unpacked = generate_contents_unpacked(
        collection.contents, collections_referenced)
    collection.html = h.get_HTML_from_contents(collection.contents_unpacked,
                                               collection.markup_language)
    collection.datetime_modified = datetime.datetime.utcnow()
    backup_collection(collection_dict)
    update_collections_that_reference_this_collection(
        collection,
        OldcollectionsController.query_builder,
        contents_changed=True)
    Session.add(collection)
    Session.commit()
Exemplo n.º 8
0
    def create(self):
        """Create a new user resource and return it.

        :URL: ``POST /users``
        :request body: JSON object representing the user to create.
        :returns: the newly created user.

        .. note::
        
            Only administrators are authorized to create users.

        """
        try:
            schema = UserSchema()
            values = json.loads(unicode(request.body, request.charset))
            data = schema.to_python(values)
            user = create_new_user(data)
            Session.add(user)
            Session.commit()
            return user.get_full_dict()
        except h.JSONDecodeError:
            response.status_int = 400
            return h.JSONDecodeErrorResponse
        except Invalid, e:
            response.status_int = 400
            return {'errors': e.unpack_errors()}
Exemplo n.º 9
0
    def update(self, id):
        """Update a keyboard and return it.

        :URL: ``PUT /keyboards/id``
        :Request body: JSON object representing the keyboard with updated
            attribute values.
        :param str id: the ``id`` value of the keyboard to be updated.
        :returns: the updated keyboard model.

        """
        keyboard = Session.query(Keyboard).get(int(id))
        if keyboard:
            try:
                schema = KeyboardSchema()
                values = json.loads(unicode(request.body, request.charset))
                state = h.get_state_object(values)
                state.id = id
                data = schema.to_python(values, state)
                keyboard = update_keyboard(keyboard, data)
                # keyboard will be False if there are no changes (cf.
                # update_keyboard).
                if keyboard:
                    Session.add(keyboard)
                    Session.commit()
                    return keyboard
                else:
                    response.status_int = 400
                    return {'error': (u'The update request failed because the'
                        u' submitted data were not new.')}
            except h.JSONDecodeError:
                response.status_int = 400
                return h.JSONDecodeErrorResponse
            except Invalid, e:
                response.status_int = 400
                return {'errors': e.unpack_errors()}
Exemplo n.º 10
0
    def delete(self, id):
        """Delete an existing orthography and return it.

        :URL: ``DELETE /orthographies/id``
        :param str id: the ``id`` value of the orthography to be deleted.
        :returns: the deleted orthography model.

        .. note::
        
            Contributors can only delete orthographies that are not used in the
            active application settings.

        """
        orthography = Session.query(Orthography).get(id)
        if orthography:
            app_set = h.get_application_settings()
            if session['user'].role == u'administrator' or orthography not in (
            app_set.storage_orthography, app_set.input_orthography, app_set.output_orthography):
                Session.delete(orthography)
                Session.commit()
                return orthography
            else:
                response.status = 403
                return {'error': u'Only administrators are permitted to delete orthographies that are used in the active application settings.'}
        else:
            response.status_int = 404
            return {'error': 'There is no orthography with id %s' % id}
Exemplo n.º 11
0
    def update(self, id):
        """Update a morpheme language model and return it.

        :URL: ``PUT /morphemelanguagemodels/id``
        :Request body: JSON object representing the morpheme language model with updated attribute values.
        :param str id: the ``id`` value of the morpheme language model to be updated.
        :returns: the updated morpheme language model model.

        """
        morpheme_language_model = h.eagerload_morpheme_language_model(Session.query(MorphemeLanguageModel)).get(int(id))
        if morpheme_language_model:
            try:
                schema = MorphemeLanguageModelSchema()
                values = json.loads(unicode(request.body, request.charset))
                state = h.get_state_object(values)
                state.id = id
                data = schema.to_python(values, state)
                morpheme_language_model_dict = morpheme_language_model.get_dict()
                morpheme_language_model = update_morpheme_language_model(morpheme_language_model, data)
                # morpheme_language_model will be False if there are no changes (cf. update_morpheme_language_model).
                if morpheme_language_model:
                    backup_morpheme_language_model(morpheme_language_model_dict)
                    Session.add(morpheme_language_model)
                    Session.commit()
                    return morpheme_language_model
                else:
                    response.status_int = 400
                    return {'error': u'The update request failed because the submitted data were not new.'}
            except h.JSONDecodeError:
                response.status_int = 400
                return h.JSONDecodeErrorResponse
            except Invalid, e:
                response.status_int = 400
                return {'errors': e.unpack_errors()}
Exemplo n.º 12
0
def _create_test_form_searches(n=100):
    """Create n form searches with various properties.  A testing ground for searches!
    """
    users = h.get_users()
    contributor = [u for u in users if u.role == u'contributor'][0]

    for i in range(1, n + 1):
        fs = model.FormSearch()

        fs.enterer_id = contributor.id
        fs.search = unicode(json.dumps(
                {'query': {'filter': ['Form', 'transcription', 'regex', '%d' % i]}}))
        if i % 2 == 0:
            fs.name = u'Form Search %d' % i
        else:
            fs.name = u'form search %d' % i

        if i > 50:
            fs.description = u'I really like this search and my favourite number is %d' % i

        if i > 20:
            fs.datetime_modified = today_timestamp
        else:
            fs.datetime_modified = yesterday_timestamp

        Session.add(fs)
    Session.commit()
Exemplo n.º 13
0
    def create(self):
        """Create a new collection resource and return it.

        :URL: ``POST /collections``
        :request body: JSON object representing the collection to create.
        :returns: the newly created collection.

        """
        try:
            unrestricted_users = h.get_unrestricted_users()
            user = session['user']
            schema = CollectionSchema()
            values = json.loads(unicode(request.body, request.charset))
            collections_referenced = get_collections_referenced(values['contents'],
                                                        user, unrestricted_users)
            values = add_contents_unpacked_to_values(values, collections_referenced)
            values = add_form_ids_list_to_values(values)
            state = h.get_state_object(values)
            data = schema.to_python(values, state)
            collection = create_new_collection(data, collections_referenced)
            Session.add(collection)
            Session.commit()
            return collection.get_full_dict()
        except h.JSONDecodeError:
            response.status_int = 400
            return h.JSONDecodeErrorResponse
        except InvalidCollectionReferenceError, e:
            response.status_int = 400
            return {'error': u'Invalid collection reference error: there is no collection with id %d' % e.args[0]}
Exemplo n.º 14
0
def createForm(i, commit=False):
    f = model.Form()
    f.transcription = u'transcription'

    g1 = model.Gloss()
    g1.gloss = u'gloss'
    f.glosses.append(g1)
    if i % 2 == 0:
        g2 = model.Gloss()
        g2.gloss = u'gloss'
        f.glosses.append(g2)

    u = model.User()
    u.name = u'name'
    f.elicitor = u

    if i % 3 == 0:
        f1 = model.File()
        f1.filename = u'file%s-1.wav' % str(i)
        f2 = model.File()
        f2.filename = u'file%s-2.wav' % str(i)
        f3 = model.File()
        f3.filename = u'file%s-3.wav' % str(i)
        f.files = [f1, f2, f3]

    Session.add(f)
    if commit:
        Session.commit()
    return f
Exemplo n.º 15
0
    def delete(self, id):
        """Delete an existing collection and return it.

        :URL: ``DELETE /collections/id``
        :param str id: the ``id`` value of the collection to be deleted.
        :returns: the deleted collection model.

        .. note::

           Only administrators and a collection's enterer can delete it.

        """
        collection = h.eagerload_collection(Session.query(Collection),
                                           eagerload_forms=True).get(id)
        if collection:
            if session['user'].role == u'administrator' or \
            collection.enterer is session['user']:
                session['user'] = Session.merge(session['user'])
                collection.modifier = session['user']
                collection_dict = collection.get_full_dict()
                backup_collection(collection_dict)
                update_collections_that_reference_this_collection(collection,
                                                self.query_builder, deleted=True)
                Session.delete(collection)
                Session.commit()
                return collection_dict
            else:
                response.status_int = 403
                return h.unauthorized_msg
        else:
            response.status_int = 404
            return {'error': 'There is no collection with id %s' % id}
Exemplo n.º 16
0
    def delete(self, id):
        """Delete an existing orthography and return it.

        :URL: ``DELETE /orthographies/id``
        :param str id: the ``id`` value of the orthography to be deleted.
        :returns: the deleted orthography model.

        .. note::
        
            Contributors can only delete orthographies that are not used in the
            active application settings.

        """
        orthography = Session.query(Orthography).get(id)
        if orthography:
            app_set = h.get_application_settings()
            if session['user'].role == u'administrator' or orthography not in (
                    app_set.storage_orthography, app_set.input_orthography,
                    app_set.output_orthography):
                Session.delete(orthography)
                Session.commit()
                return orthography
            else:
                response.status = 403
                return {
                    'error':
                    u'Only administrators are permitted to delete orthographies that are used in the active application settings.'
                }
        else:
            response.status_int = 404
            return {'error': 'There is no orthography with id %s' % id}
Exemplo n.º 17
0
    def update(self, id):
        """Update a form search and return it.
        
        :URL: ``PUT /formsearches/id``
        :Request body: JSON object representing the form search with updated
            attribute values.
        :param str id: the ``id`` value of the form search to be updated.
        :returns: the updated form search model.

        """
        form_search = h.eagerload_form_search(Session.query(FormSearch)).get(int(id))
        if form_search:
            try:
                schema = FormSearchSchema()
                values = json.loads(unicode(request.body, request.charset))
                state = h.get_state_object(values)
                state.id = id
                state.config = config
                data = schema.to_python(values, state)
                form_search = update_form_search(form_search, data)
                # form_search will be False if there are no changes (cf. update_form_search).
                if form_search:
                    Session.add(form_search)
                    Session.commit()
                    return form_search
                else:
                    response.status_int = 400
                    return {'error':
                        u'The update request failed because the submitted data were not new.'}
            except h.JSONDecodeError:
                response.status_int = 400
                return h.JSONDecodeErrorResponse
            except Invalid, e:
                response.status_int = 400
                return {'errors': e.unpack_errors()}
Exemplo n.º 18
0
    def update(self, id):
        """Update an elicitation method and return it.
        
        :URL: ``PUT /elicitationmethods/id``
        :Request body: JSON object representing the elicitation method with updated attribute values.
        :param str id: the ``id`` value of the elicitation method to be updated.
        :returns: the updated elicitation method model.

        """
        elicitation_method = Session.query(ElicitationMethod).get(int(id))
        if elicitation_method:
            try:
                schema = ElicitationMethodSchema()
                values = json.loads(unicode(request.body, request.charset))
                state = h.get_state_object(values)
                state.id = id
                data = schema.to_python(values, state)
                elicitation_method = update_elicitation_method(elicitation_method, data)
                # elicitation_method will be False if there are no changes (cf. update_elicitation_method).
                if elicitation_method:
                    Session.add(elicitation_method)
                    Session.commit()
                    return elicitation_method
                else:
                    response.status_int = 400
                    return {'error':
                        u'The update request failed because the submitted data were not new.'}
            except h.JSONDecodeError:
                response.status_int = 400
                return h.JSONDecodeErrorResponse
            except Invalid, e:
                response.status_int = 400
                return {'errors': e.unpack_errors()}
Exemplo n.º 19
0
    def create(self):
        """Create a new corpus resource and return it.

        :URL: ``POST /corpora``
        :request body: JSON object representing the corpus to create.
        :returns: the newly created corpus.

        """
        try:
            schema = CorpusSchema()
            values = json.loads(unicode(request.body, request.charset))
            state = h.get_state_object(values)
            state.config = config
            data = schema.to_python(values, state)
            corpus = create_new_corpus(data)
            Session.add(corpus)
            Session.commit()
            create_corpus_dir(corpus)
            return corpus
        except h.JSONDecodeError:
            response.status_int = 400
            return h.JSONDecodeErrorResponse
        except Invalid, e:
            response.status_int = 400
            return {'errors': e.unpack_errors()}
Exemplo n.º 20
0
    def update(self, id):
        """Update a speaker and return it.
        
        :URL: ``PUT /speakers/id``
        :Request body: JSON object representing the speaker with updated attribute values.
        :param str id: the ``id`` value of the speaker to be updated.
        :returns: the updated speaker model.

        """
        speaker = Session.query(Speaker).get(int(id))
        if speaker:
            try:
                schema = SpeakerSchema()
                values = json.loads(unicode(request.body, request.charset))
                data = schema.to_python(values)
                speaker = update_speaker(speaker, data)
                # speaker will be False if there are no changes (cf. update_speaker).
                if speaker:
                    Session.add(speaker)
                    Session.commit()
                    return speaker
                else:
                    response.status_int = 400
                    return {'error':
                        u'The update request failed because the submitted data were not new.'}
            except h.JSONDecodeError:
                response.status_int = 400
                return h.JSONDecodeErrorResponse
            except Invalid, e:
                response.status_int = 400
                return {'errors': e.unpack_errors()}
Exemplo n.º 21
0
Arquivo: pages.py Projeto: FieldDB/old
    def update(self, id):
        """Update a page and return it.
        
        :URL: ``PUT /pages/id``
        :Request body: JSON object representing the page with updated attribute values.
        :param str id: the ``id`` value of the page to be updated.
        :returns: the updated page model.

        """
        page = Session.query(Page).get(int(id))
        if page:
            try:
                schema = PageSchema()
                values = json.loads(unicode(request.body, request.charset))
                data = schema.to_python(values)
                page = update_page(page, data)
                # page will be False if there are no changes (cf. update_page).
                if page:
                    Session.add(page)
                    Session.commit()
                    return page
                else:
                    response.status_int = 400
                    return {'error':
                        u'The update request failed because the submitted data were not new.'}
            except h.JSONDecodeError:
                response.status_int = 400
                return  h.JSONDecodeErrorResponse
            except Invalid, e:
                response.status_int = 400
                return {'errors': e.unpack_errors()}
Exemplo n.º 22
0
def createForm(i, commit=False):
    f = model.Form()
    f.transcription = u'transcription'

    g1 = model.Gloss()
    g1.gloss = u'gloss'
    f.glosses.append(g1)
    if i % 2 == 0:
        g2 = model.Gloss()
        g2.gloss = u'gloss'
        f.glosses.append(g2)

    u = model.User()
    u.name = u'name'
    f.elicitor = u

    if i % 3 == 0:
        f1 = model.File()
        f1.filename = u'file%s-1.wav' % str(i)
        f2 = model.File()
        f2.filename = u'file%s-2.wav' % str(i)
        f3 = model.File()
        f3.filename = u'file%s-3.wav' % str(i)
        f.files = [f1, f2, f3]

    Session.add(f)
    if commit:
        Session.commit()
    return f
Exemplo n.º 23
0
def _create_test_form_searches(n=100):
    """Create n form searches with various properties.  A testing ground for searches!
    """
    users = h.get_users()
    contributor = [u for u in users if u.role == u'contributor'][0]

    for i in range(1, n + 1):
        fs = model.FormSearch()

        fs.enterer_id = contributor.id
        fs.search = unicode(
            json.dumps({
                'query': {
                    'filter': ['Form', 'transcription', 'regex',
                               '%d' % i]
                }
            }))
        if i % 2 == 0:
            fs.name = u'Form Search %d' % i
        else:
            fs.name = u'form search %d' % i

        if i > 50:
            fs.description = u'I really like this search and my favourite number is %d' % i

        if i > 20:
            fs.datetime_modified = today_timestamp
        else:
            fs.datetime_modified = yesterday_timestamp

        Session.add(fs)
    Session.commit()
Exemplo n.º 24
0
    def create(self):
        """Create a new collection resource and return it.

        :URL: ``POST /collections``
        :request body: JSON object representing the collection to create.
        :returns: the newly created collection.

        """
        try:
            unrestricted_users = h.get_unrestricted_users()
            user = session['user']
            schema = CollectionSchema()
            values = json.loads(unicode(request.body, request.charset))
            collections_referenced = get_collections_referenced(
                values['contents'], user, unrestricted_users)
            values = add_contents_unpacked_to_values(values,
                                                     collections_referenced)
            values = add_form_ids_list_to_values(values)
            state = h.get_state_object(values)
            data = schema.to_python(values, state)
            collection = create_new_collection(data, collections_referenced)
            Session.add(collection)
            Session.commit()
            return collection.get_full_dict()
        except h.JSONDecodeError:
            response.status_int = 400
            return h.JSONDecodeErrorResponse
        except InvalidCollectionReferenceError, e:
            response.status_int = 400
            return {
                'error':
                u'Invalid collection reference error: there is no collection with id %d'
                % e.args[0]
            }
Exemplo n.º 25
0
    def create(self):
        """Create a new user resource and return it.

        :URL: ``POST /users``
        :request body: JSON object representing the user to create.
        :returns: the newly created user.

        .. note::
        
            Only administrators are authorized to create users.

        """
        try:
            schema = UserSchema()
            values = json.loads(unicode(request.body, request.charset))
            data = schema.to_python(values)
            user = create_new_user(data)
            Session.add(user)
            Session.commit()
            return user.get_full_dict()
        except h.JSONDecodeError:
            response.status_int = 400
            return h.JSONDecodeErrorResponse
        except Invalid, e:
            response.status_int = 400
            return {'errors': e.unpack_errors()}
Exemplo n.º 26
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']
Exemplo n.º 27
0
    def create(self):
        """Create a new corpus resource and return it.

        :URL: ``POST /corpora``
        :request body: JSON object representing the corpus to create.
        :returns: the newly created corpus.

        """
        try:
            schema = CorpusSchema()
            values = json.loads(unicode(request.body, request.charset))
            state = h.get_state_object(values)
            state.config = config
            data = schema.to_python(values, state)
            corpus = create_new_corpus(data)
            Session.add(corpus)
            Session.commit()
            create_corpus_dir(corpus)
            return corpus
        except h.JSONDecodeError:
            response.status_int = 400
            return h.JSONDecodeErrorResponse
        except Invalid, e:
            response.status_int = 400
            return {'errors': e.unpack_errors()}
Exemplo n.º 28
0
    def update(self, id):
        """Update a syntactic category and return it.
        
        :URL: ``PUT /syntacticcategorys/id``
        :Request body: JSON object representing the syntactic category with updated attribute values.
        :param str id: the ``id`` value of the syntactic category to be updated.
        :returns: the updated syntactic category model.

        """
        syntactic_category = Session.query(SyntacticCategory).get(int(id))
        if syntactic_category:
            try:
                old_name = syntactic_category.name
                schema = SyntacticCategorySchema()
                values = json.loads(unicode(request.body, request.charset))
                state = h.get_state_object(values)
                state.id = id
                data = schema.to_python(values, state)
                syntactic_category = update_syntactic_category(syntactic_category, data)
                # syntactic_category will be False if there are no changes (cf. update_syntactic_category).
                if syntactic_category:
                    Session.add(syntactic_category)
                    Session.commit()
                    if syntactic_category.name != old_name:
                        update_forms_referencing_this_category(syntactic_category)
                    return syntactic_category
                else:
                    response.status_int = 400
                    return {"error": u"The update request failed because the submitted data were not new."}
            except h.JSONDecodeError:
                response.status_int = 400
                return h.JSONDecodeErrorResponse
            except Invalid, e:
                response.status_int = 400
                return {"errors": e.unpack_errors()}
Exemplo n.º 29
0
    def update(self, id):
        """Update a user and return it.
        
        :URL: ``PUT /users/id``
        :Request body: JSON object representing the user with updated attribute values.
        :param str id: the ``id`` value of the user to be updated.
        :returns: the updated user model.

        """
        user = Session.query(User).get(int(id))
        if user:
            try:
                schema = UserSchema()
                values = json.loads(unicode(request.body, request.charset))
                state = h.get_state_object(values)
                state.user_to_update = user.get_full_dict()
                current_user = Session.query(User).get(session['user'].id)
                state.user = current_user.get_full_dict()
                data = schema.to_python(values, state)
                user = update_user(user, data)
                # user will be False if there are no changes (cf. update_user).
                if user:
                    Session.add(user)
                    Session.commit()
                    return user.get_full_dict()
                else:
                    response.status_int = 400
                    return {'error':
                        u'The update request failed because the submitted data were not new.'}
            except h.JSONDecodeError:
                response.status_int = 400
                return h.JSONDecodeErrorResponse
            except Invalid, e:
                response.status_int = 400
                return {'errors': e.unpack_errors()}
Exemplo n.º 30
0
def compile_phonology(**kwargs):
    """Compile the foma script of a phonology and save it to the db with values that indicating compilation success.
    """
    phonology = Session.query(model.Phonology).get(kwargs['phonology_id'])
    phonology.compile(kwargs['timeout'])
    phonology.datetime_modified = h.now()
    phonology.modifier_id = kwargs['user_id']
    Session.commit()
Exemplo n.º 31
0
def add_default_application_settings():
    """Add the default application settings to the database."""
    orthography1 = h.generate_default_orthography1()
    orthography2 = h.generate_default_orthography2()
    contributor = Session.query(User).filter(User.role==u'contributor').first()
    application_settings = h.generate_default_application_settings([orthography1, orthography2], [contributor])
    Session.add(application_settings)
    Session.commit()
    return application_settings
Exemplo n.º 32
0
    def test_delete(self):
        """Tests that DELETE /tags/id deletes the tag with id=id."""

        # Create a tag to delete.
        params = json.dumps({'name': u'name', 'description': u'description'})
        response = self.app.post(url('tags'), params, self.json_headers,
                                 self.extra_environ_admin)
        resp = json.loads(response.body)
        tag_count = Session.query(Tag).count()
        tag_id = resp['id']

        # Now delete the tag
        response = self.app.delete(url('tag', id=tag_id), headers=self.json_headers,
            extra_environ=self.extra_environ_admin)
        resp = json.loads(response.body)
        new_tag_count = Session.query(Tag).count()
        assert new_tag_count == tag_count - 1
        assert resp['id'] == tag_id
        assert response.content_type == 'application/json'

        # Trying to get the deleted tag from the db should return None
        deleted_tag = Session.query(Tag).get(tag_id)
        assert deleted_tag == None

        # Delete with an invalid id
        id = 9999999999999
        response = self.app.delete(url('tag', id=id),
            headers=self.json_headers, extra_environ=self.extra_environ_admin,
            status=404)
        assert u'There is no tag with id %s' % id in json.loads(response.body)['error']
        assert response.content_type == 'application/json'

        # Delete without an id
        response = self.app.delete(url('tag', id=''), status=404,
            headers=self.json_headers, extra_environ=self.extra_environ_admin)
        assert json.loads(response.body)['error'] == 'The resource could not be found.'
        assert response.content_type == 'application/json'

        # Create a form, tag it, delete the tag and show that the form no longer
        # has the tag.
        tag = model.Tag()
        tag.name = u'tag'
        form = model.Form()
        form.transcription = u'test'
        form.tags.append(tag)
        Session.add_all([form, tag])
        Session.commit()
        form_id = form.id
        tag_id = tag.id
        response = self.app.delete(url('tag', id=tag_id),
            headers=self.json_headers, extra_environ=self.extra_environ_admin)
        deleted_tag = Session.query(Tag).get(tag_id)
        form = Session.query(model.Form).get(form_id)
        assert response.content_type == 'application/json'
        assert deleted_tag == None
        assert form.tags == []
Exemplo n.º 33
0
def _create_test_models(n=100):
    _add_test_models_to_session('Tag', n, ['name'])
    _add_test_models_to_session('Speaker', n, ['first_name', 'last_name', 'dialect'])
    _add_test_models_to_session('Source', n, ['author_first_name', 'author_last_name',
                                            'title'])
    _add_test_models_to_session('ElicitationMethod', n, ['name'])
    _add_test_models_to_session('SyntacticCategory', n, ['name'])
    _add_test_models_to_session('File', n, ['name'])
    restricted_tag = h.generate_restricted_tag()
    Session.add(restricted_tag)
    Session.commit()
Exemplo n.º 34
0
def _create_test_models(n=100):
    _add_test_models_to_session('Tag', n, ['name'])
    _add_test_models_to_session('Speaker', n,
                                ['first_name', 'last_name', 'dialect'])
    _add_test_models_to_session(
        'Source', n, ['author_first_name', 'author_last_name', 'title'])
    _add_test_models_to_session('ElicitationMethod', n, ['name'])
    _add_test_models_to_session('SyntacticCategory', n, ['name'])
    _add_test_models_to_session('File', n, ['name'])
    restricted_tag = h.generate_restricted_tag()
    Session.add(restricted_tag)
    Session.commit()
Exemplo n.º 35
0
    def update(self, id):
        """Update a collection and return it.
        
        :URL: ``PUT /collections/id``
        :Request body: JSON object representing the collection with updated attribute values.
        :param str id: the ``id`` value of the collection to be updated.
        :returns: the updated collection model.

        """
        collection = h.eagerload_collection(Session.query(Collection),
                                           eagerload_forms=True).get(int(id))
        if collection:
            unrestricted_users = h.get_unrestricted_users()
            user = session['user']
            if h.user_is_authorized_to_access_model(user, collection, unrestricted_users):
                try:
                    schema = CollectionSchema()
                    values = json.loads(unicode(request.body, request.charset))
                    collections_referenced = get_collections_referenced(
                                values['contents'], user, unrestricted_users, id)
                    values = add_contents_unpacked_to_values(values, collections_referenced)
                    values = add_form_ids_list_to_values(values)
                    state = h.get_state_object(values)
                    data = schema.to_python(values, state)
                    collection_dict = collection.get_full_dict()
                    collection, restricted, contents_changed = update_collection(
                        collection, data, collections_referenced)
                    # collection will be False if there are no changes (cf. update_collection).
                    if collection:
                        backup_collection(collection_dict)
                        update_collections_that_reference_this_collection(collection, self.query_builder,
                                            restricted=restricted, contents_changed=contents_changed)
                        Session.add(collection)
                        Session.commit()
                        return collection.get_full_dict()
                    else:
                        response.status_int = 400
                        return {'error':
                            u'The update request failed because the submitted data were not new.'}
                except h.JSONDecodeError:
                    response.status_int = 400
                    return h.JSONDecodeErrorResponse
                except CircularCollectionReferenceError, e:
                    response.status_int = 400
                    return {'error':
                        u'Circular collection reference error: collection %d references collection %d.' % (id, e.args[0])}
                except InvalidCollectionReferenceError, e:
                    response.status_int = 400
                    return {'error': u'Invalid collection reference error: there is no collection with id %d' % e.args[0]}
                except UnauthorizedCollectionReferenceError:
                    response.status_int = 403
                    return {'error': u'Unauthorized collection reference error: you are not authorized to access collection %d' % e.args[0]}
Exemplo n.º 36
0
def createCollection(i, forms=None, commit=False):
    c = model.Collection()
    c.title = u'title'
    if i % 3 == 0:
        c.forms = forms
    elif i % 2 == 0:
        c.forms = forms[::3]
    else:
        c.forms = forms[:10]
    Session.add(c)
    if commit:
        Session.commit()
    return c
Exemplo n.º 37
0
 def persist(self):
     """Update the persistence layer with the value of ``self._store``.
     """
     if self.updated:
         persisted = [parse.transcription for parse in Session.query(Parse).\
             filter(Parse.parser_id==self.parser.id).\
             filter(Parse.transcription.in_(self._store.keys())).all()]
         unpersisted = [Parse(transcription=k, parse=v, parser=self.parser)
             for k, v in self._store.iteritems() if k not in persisted]
         Session.add_all(unpersisted)
         Session.commit()
         # log.warn('DB_CACHE: PERSISTED %s' % u', '.join([p.transcription for p in unpersisted]))
         self.updated = False
Exemplo n.º 38
0
 def test_zzz_cleanup(self):
     """Clean up after the tests."""
     # Destruction
     h.clear_all_tables()
     h.destroy_all_directories(directory_name='users', config_filename='test.ini')
     h.destroy_all_directories(directory_name='corpora', config_filename='test.ini')
     # Creation
     languages = h.get_language_objects('test.ini', self.config)
     administrator = h.generate_default_administrator()
     contributor = h.generate_default_contributor()
     viewer = h.generate_default_viewer()
     Session.add_all([administrator, contributor, viewer] + languages)
     Session.commit()
Exemplo n.º 39
0
 def add_loremipsum_to_db(loremipsum_path, via_request=False):
     """Add the contents of the file at ``loremipsum_path`` to the database."""
     categories = {}
     with open(loremipsum_path, 'r') as f:
         i = 0
         for l in f:
             if i % 100 == 0:
                 if not via_request: Session.commit()
                 log.debug('%d lines processed' % i)
             i = i + 1
             categories = create_model(l.replace('\n', ''), categories,
                                       via_request)
         Session.commit()
Exemplo n.º 40
0
def createCollection(i, forms=None, commit=False):
    c = model.Collection()
    c.title = u'title'
    if i % 3 == 0:
        c.forms = forms
    elif i % 2 == 0:
        c.forms = forms[::3]
    else:
        c.forms = forms[:10]
    Session.add(c)
    if commit:
        Session.commit()
    return c
Exemplo n.º 41
0
 def add_loremipsum_to_db(loremipsum_path, via_request=False):
     """Add the contents of the file at ``loremipsum_path`` to the database."""
     categories = {}
     with open(loremipsum_path, 'r') as f:
         i = 0
         for l in f:
             if i % 100 == 0:
                 if not via_request: Session.commit()
                 log.debug('%d lines processed' % i)
             i = i + 1
             categories = create_model(l.replace('\n', ''), categories,
                                      via_request)
         Session.commit()
Exemplo n.º 42
0
    def update(self, id):
        """Update an orthography and return it.
        
        :URL: ``PUT /orthographies/id``
        :Request body: JSON object representing the orthography with updated attribute values.
        :param str id: the ``id`` value of the orthography to be updated.
        :returns: the updated orthography model.

        .. note::
        
            Contributors can only update orthographies that are not used in the
            active application settings.

        """
        orthography = Session.query(Orthography).get(int(id))
        user = session['user']
        if orthography:
            app_set = h.get_application_settings()
            if user.role == u'administrator' or orthography not in (
                    app_set.storage_orthography, app_set.input_orthography,
                    app_set.output_orthography):
                try:
                    schema = OrthographySchema()
                    values = json.loads(unicode(request.body, request.charset))
                    state = h.get_state_object(values)
                    state.id = id
                    result = schema.to_python(values, state)
                    orthography = update_orthography(orthography, result)
                    # orthography will be False if there are no changes (cf. update_orthography).
                    if orthography:
                        Session.add(orthography)
                        Session.commit()
                        return orthography
                    else:
                        response.status_int = 400
                        return {
                            'error':
                            u'The update request failed because the submitted data were not new.'
                        }
                except h.JSONDecodeError:
                    response.status_int = 400
                    return h.JSONDecodeErrorResponse
                except Invalid, e:
                    response.status_int = 400
                    return {'errors': e.unpack_errors()}
            else:
                response.status = 403
                return {
                    'error':
                    u'Only administrators are permitted to update orthographies that are used in the active application settings.'
                }
Exemplo n.º 43
0
    def test_e_cleanup(self):
        """Clean up the database after /rememberedforms tests."""

        h.clear_all_models()
        administrator = h.generate_default_administrator()
        contributor = h.generate_default_contributor()
        viewer = h.generate_default_viewer()
        Session.add_all([administrator, contributor, viewer])
        Session.commit()

        # Perform a vacuous GET just to delete app_globals.application_settings
        # to clean up for subsequent tests.
        extra_environ = {'test.authentication.role': u'administrator',
                         'test.application_settings': True}
        self.app.get(url('forms'), extra_environ=extra_environ)
Exemplo n.º 44
0
 def test_zzz_cleanup(self):
     """Clean up after the tests."""
     # Destruction
     h.clear_all_tables()
     h.destroy_all_directories(directory_name='users',
                               config_filename='test.ini')
     h.destroy_all_directories(directory_name='corpora',
                               config_filename='test.ini')
     # Creation
     languages = h.get_language_objects('test.ini', self.config)
     administrator = h.generate_default_administrator()
     contributor = h.generate_default_contributor()
     viewer = h.generate_default_viewer()
     Session.add_all([administrator, contributor, viewer] + languages)
     Session.commit()
Exemplo n.º 45
0
    def delete(self, id):
        """Delete an existing speaker and return it.

        :URL: ``DELETE /speakers/id``
        :param str id: the ``id`` value of the speaker to be deleted.
        :returns: the deleted speaker model.

        """
        speaker = Session.query(Speaker).get(id)
        if speaker:
            Session.delete(speaker)
            Session.commit()
            return speaker
        else:
            response.status_int = 404
            return {'error': 'There is no speaker with id %s' % id}
Exemplo n.º 46
0
    def delete(self, id):
        """Delete an existing source and return it.

        :URL: ``DELETE /sources/id``
        :param str id: the ``id`` value of the source to be deleted.
        :returns: the deleted source model.

        """
        source = Session.query(Source).get(id)
        if source:
            Session.delete(source)
            Session.commit()
            return source
        else:
            response.status_int = 404
            return {'error': 'There is no source with id %s' % id}
Exemplo n.º 47
0
    def delete(self, id):
        """Delete an existing elicitation method and return it.

        :URL: ``DELETE /elicitationmethods/id``
        :param str id: the ``id`` value of the elicitation method to be deleted.
        :returns: the deleted elicitation method model.

        """
        elicitation_method = Session.query(ElicitationMethod).get(id)
        if elicitation_method:
            Session.delete(elicitation_method)
            Session.commit()
            return elicitation_method
        else:
            response.status_int = 404
            return {'error': 'There is no elicitation method with id %s' % id}
Exemplo n.º 48
0
    def delete(self, id):
        """Delete an existing page and return it.

        :URL: ``DELETE /pages/id``
        :param str id: the ``id`` value of the page to be deleted.
        :returns: the deleted page model.

        """
        page = Session.query(Page).get(id)
        if page:
            Session.delete(page)
            Session.commit()
            return page
        else:
            response.status_int = 404
            return {'error': 'There is no page with id %s' % id}
Exemplo n.º 49
0
    def delete(self, id):
        """Delete an existing keyboard and return it.

        :URL: ``DELETE /keyboards/id``
        :param str id: the ``id`` value of the keyboard to be deleted.
        :returns: the deleted keyboard model.

        """
        keyboard = Session.query(Keyboard).get(id)
        if keyboard:
            Session.delete(keyboard)
            Session.commit()
            return keyboard
        else:
            response.status_int = 404
            return {'error': 'There is no keyboard with id %s' % id}
Exemplo n.º 50
0
    def test_create(self):
        """Tests that POST /applicationsettings correctly creates a new application settings."""

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

        params = self.application_settings_create_params.copy()
        params.update({
            'object_language_name': u'test_create object language name',
            'object_language_id': u'tco',
            'metalanguage_name': u'test_create metalanguage name',
            'metalanguage_id': u'tcm',
            'orthographic_validation': u'Warning',
            'narrow_phonetic_validation': u'Error',
            'morpheme_break_is_orthographic': False,
            'morpheme_delimiters': u'-,+',
            'punctuation': u'!?.,;:-_',
            'grammaticalities': u'*,**,***,?,??,???,#,##,###',
            'unrestricted_users': [Session.query(User).filter(
                User.role==u'viewer').first().id],
            'storage_orthography': orthography2_id,
            'input_orthography': orthography2_id,
            'output_orthography': orthography2_id
        })
        params = json.dumps(params)

        response = self.app.post(url('applicationsettings'), params,
                                 self.json_headers, self.extra_environ_admin)
        resp = json.loads(response.body)
        assert resp['object_language_name'] == u'test_create object language name'
        assert resp['morpheme_break_is_orthographic'] is False
        assert resp['storage_orthography']['orthography'] == orthography2_orthography
        assert resp['unrestricted_users'][0]['first_name'] == u'Viewer'
        assert 'password' not in resp['unrestricted_users'][0]
        assert response.content_type == 'application/json'

        # Attempt the same above creation as a contributor and expect to fail.
        response = self.app.post(url('applicationsettings'), params,
            self.json_headers, self.extra_environ_contrib, status=403)
        resp = json.loads(response.body)
        assert response.content_type == 'application/json'
        assert resp['error'] == u'You are not authorized to access this resource.'
Exemplo n.º 51
0
    def update(self, id):
        """Update a user's remembered forms and return them.

        :URL: ``PUT /rememberedforms/id``
        :Request body: JSON object of the form ``{"forms": [...]}`` where the
            array contains the form ``id`` values that will constitute the
            user's ``remembered_forms`` collection after update.
        :param str id: the ``id`` value of the user model whose
            ``remembered_forms`` attribute is to be updated.
        :returns: the list of remembered forms of the user.

        .. note::

            Administrators can update any user's remembered forms;
            non-administrators can only update their own.

        """
        user = Session.query(User).options(subqueryload(
            User.remembered_forms)).get(id)
        if user:
            try:
                schema = FormIdsSchemaNullable
                values = json.loads(unicode(request.body, request.charset))
                data = schema.to_python(values)
                forms = [f for f in data['forms'] if f]
                accessible = h.user_is_authorized_to_access_model
                unrestricted_users = h.get_unrestricted_users()
                unrestricted_forms = [
                    f for f in forms if accessible(user, f, unrestricted_users)
                ]
                if set(user.remembered_forms) != set(unrestricted_forms):
                    user.remembered_forms = unrestricted_forms
                    user.datetime_modified = h.now()
                    Session.commit()
                    return user.remembered_forms
                else:
                    response.status_int = 400
                    return {
                        'error':
                        u'The update request failed because the submitted data were not new.'
                    }
            except h.JSONDecodeError:
                response.status_int = 400
                return h.JSONDecodeErrorResponse
            except Invalid, e:
                response.status_int = 400
                return {'errors': e.unpack_errors()}
Exemplo n.º 52
0
    def delete(self, id):
        """Delete an existing syntactic category and return it.

        :URL: ``DELETE /syntacticcategorys/id``
        :param str id: the ``id`` value of the syntactic category to be deleted.
        :returns: the deleted syntactic category model.

        """
        syntactic_category = Session.query(SyntacticCategory).get(id)
        if syntactic_category:
            Session.delete(syntactic_category)
            Session.commit()
            update_forms_referencing_this_category(syntactic_category)
            return syntactic_category
        else:
            response.status_int = 404
            return {'error': 'There is no syntactic category with id %s' % id}
Exemplo n.º 53
0
    def delete(self, id):
        """Delete an existing user and return it.

        :URL: ``DELETE /users/id``
        :param str id: the ``id`` value of the user to be deleted.
        :returns: the deleted user model.

        """
        user = Session.query(User).get(id)
        if user:
            h.destroy_user_directory(user)
            Session.delete(user)
            Session.commit()
            return user.get_full_dict()
        else:
            response.status_int = 404
            return {'error': 'There is no user with id %s' % id}
Exemplo n.º 54
0
    def test_e_cleanup(self):
        """Clean up the database after /rememberedforms tests."""

        h.clear_all_models()
        administrator = h.generate_default_administrator()
        contributor = h.generate_default_contributor()
        viewer = h.generate_default_viewer()
        Session.add_all([administrator, contributor, viewer])
        Session.commit()

        # Perform a vacuous GET just to delete app_globals.application_settings
        # to clean up for subsequent tests.
        extra_environ = {
            'test.authentication.role': u'administrator',
            'test.application_settings': True
        }
        self.app.get(url('forms'), extra_environ=extra_environ)
Exemplo n.º 55
0
    def email_reset_password(self):
        """Reset the user's password and email them a new one.

        :URL: ``POST /login/email_reset_password``
        :request body: a JSON object with a ``"username"`` attribute.
        :returns: a dictionary with ``'valid_username'`` and ``'password_reset'``
            keys whose values are booleans.

        """
        try:
            schema = PasswordResetSchema()
            values = json.loads(unicode(request.body, request.charset))
            result = schema.to_python(values)
            user = Session.query(User).filter(
                User.username == result['username']).first()
            if user:
                try:
                    new_password = h.generate_password()
                    h.send_password_reset_email_to(user,
                                                   new_password,
                                                   config=config)
                    user.password = unicode(
                        h.encrypt_password(new_password, str(user.salt)))
                    Session.add(user)
                    Session.commit()
                    if os.path.split(config['__file__'])[-1] == 'test.ini':
                        return {
                            'valid_username': True,
                            'password_reset': True,
                            'new_password': new_password
                        }
                    else:
                        return {'valid_username': True, 'password_reset': True}
                except:  # socket.error was too specific ...
                    response.status_int = 500
                    return {'error': 'The server is unable to send email.'}
            else:
                response.status_int = 400
                return {'error': 'The username provided is not valid.'}
        except h.JSONDecodeError:
            response.status_int = 400
            return h.JSONDecodeErrorResponse
        except Invalid, e:
            response.status_int = 400
            return {'errors': e.unpack_errors()}
Exemplo n.º 56
0
    def delete(self, id):
        """Delete an existing form search and return it.

        :URL: ``DELETE /formsearches/id``
        :param str id: the ``id`` value of the form search to be deleted.
        :returns: the deleted form search model.

        """

        form_search = h.eagerload_form_search(
            Session.query(FormSearch)).get(id)
        if form_search:
            Session.delete(form_search)
            Session.commit()
            return form_search
        else:
            response.status_int = 404
            return {'error': 'There is no form search with id %s' % id}
Exemplo n.º 57
0
    def test_a_initialize(self):
        """Initialize the database for /rememberedforms tests."""
        h.clear_all_models()
        administrator = h.generate_default_administrator()
        contributor = h.generate_default_contributor()
        viewer = h.generate_default_viewer()
        Session.add_all([administrator, contributor, viewer])
        Session.commit()

        _create_test_data(self.n)
        self._add_SEARCH_to_web_test_valid_methods()

        # Create an application settings where the contributor is unrestricted
        viewer, contributor, administrator = get_users()
        application_settings = h.generate_default_application_settings()
        application_settings.unrestricted_users = [contributor]
        Session.add(application_settings)
        Session.commit()