Exemplo n.º 1
0
def update_corpus(corpus, data):
    """Update a corpus.

    :param corpus: the corpus model to be updated.
    :param dict data: representation of the updated corpus.
    :returns: the updated corpus model or, if ``changed`` has not been set
        to ``True``, ``False``.

    """
    changed = False
    # Unicode Data
    changed = corpus.set_attr('name', h.normalize(data['name']), changed)
    changed = corpus.set_attr('description', h.normalize(data['description']), changed)
    changed = corpus.set_attr('content', data['content'], changed)
    changed = corpus.set_attr('form_search', data['form_search'], changed)

    tags_to_add = [t for t in data['tags'] if t]
    forms_to_add = [f for f in data['forms'] if f]
    if set(tags_to_add) != set(corpus.tags):
        corpus.tags = tags_to_add
        changed = True
    if set(forms_to_add) != set(corpus.forms):
        corpus.forms = forms_to_add
        changed = True

    if changed:
        session['user'] = Session.merge(session['user'])
        corpus.modifier = session['user']
        corpus.datetime_modified = h.now()
        return corpus
    return changed
Exemplo n.º 2
0
def update_morphology(morphology, data):
    """Update a morphology.

    :param morphology: the morphology model to be updated.
    :param dict data: representation of the updated morphology.
    :returns: the updated morphology model or, if ``changed`` has not been set
        to ``True``, ``False``.

    """
    changed = False
    changed = morphology.set_attr('name', h.normalize(data['name']), changed)
    changed = morphology.set_attr('description', h.normalize(data['description']), changed)
    changed = morphology.set_attr('lexicon_corpus', data['lexicon_corpus'], changed)
    changed = morphology.set_attr('rules_corpus', data['rules_corpus'], changed)
    changed = morphology.set_attr('script_type', data['script_type'], changed)
    changed = morphology.set_attr('extract_morphemes_from_rules_corpus', data['extract_morphemes_from_rules_corpus'], changed)
    changed = morphology.set_attr('rules', data['rules'], changed)
    changed = morphology.set_attr('rich_upper', data['rich_upper'], changed)
    changed = morphology.set_attr('rich_lower', data['rich_lower'], changed)
    changed = morphology.set_attr('include_unknowns', data['include_unknowns'], changed)
    changed = morphology.set_attr('rare_delimiter', h.rare_delimiter, changed)
    changed = morphology.set_attr('word_boundary_symbol', h.word_boundary_symbol, changed)
    if changed:
        session['user'] = Session.merge(session['user'])
        morphology.modifier = session['user']
        morphology.datetime_modified = h.now()
        return morphology
    return changed
Exemplo n.º 3
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.º 4
0
def update_corpus(corpus, data):
    """Update a corpus.

    :param corpus: the corpus model to be updated.
    :param dict data: representation of the updated corpus.
    :returns: the updated corpus model or, if ``changed`` has not been set
        to ``True``, ``False``.

    """
    changed = False
    # Unicode Data
    changed = corpus.set_attr('name', h.normalize(data['name']), changed)
    changed = corpus.set_attr('description', h.normalize(data['description']),
                              changed)
    changed = corpus.set_attr('content', data['content'], changed)
    changed = corpus.set_attr('form_search', data['form_search'], changed)

    tags_to_add = [t for t in data['tags'] if t]
    forms_to_add = [f for f in data['forms'] if f]
    if set(tags_to_add) != set(corpus.tags):
        corpus.tags = tags_to_add
        changed = True
    if set(forms_to_add) != set(corpus.forms):
        corpus.forms = forms_to_add
        changed = True

    if changed:
        session['user'] = Session.merge(session['user'])
        corpus.modifier = session['user']
        corpus.datetime_modified = h.now()
        return corpus
    return changed
Exemplo n.º 5
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.º 6
0
def update_morphological_parser(morphological_parser, data):
    """Update a morphological parser.

    :param morphological_parser: the morphological parser model to be updated.
    :param dict data: representation of the updated morphological parser.
    :returns: the updated morphological parser model or, if ``changed`` has not been set
        to ``True``, ``False``.

    """
    changed = False
    changed = morphological_parser.set_attr('name', h.normalize(data['name']),
                                            changed)
    changed = morphological_parser.set_attr('description',
                                            h.normalize(data['description']),
                                            changed)
    changed = morphological_parser.set_attr('phonology', data['phonology'],
                                            changed)
    changed = morphological_parser.set_attr('morphology', data['morphology'],
                                            changed)
    changed = morphological_parser.set_attr('language_model',
                                            data['language_model'], changed)
    if changed:
        session['user'] = Session.merge(session['user'])
        morphological_parser.modifier = session['user']
        morphological_parser.datetime_modified = h.now()
        return morphological_parser
    return changed
Exemplo n.º 7
0
def update_morpheme_language_model(morpheme_language_model, data):
    """Update a morpheme language model.

    :param morpheme_language_model: the morpheme language model model to be updated.
    :param dict data: representation of the updated morpheme language model.
    :returns: the updated morpheme language model model or, if ``changed`` has not been set
        to ``True``, ``False``.

    """
    changed = False
    changed = morpheme_language_model.set_attr('name', h.normalize(data['name']), changed)
    changed = morpheme_language_model.set_attr('description', h.normalize(data['description']), changed)
    changed = morpheme_language_model.set_attr('vocabulary_morphology', data['vocabulary_morphology'], changed)
    changed = morpheme_language_model.set_attr('corpus', data['corpus'], changed)
    changed = morpheme_language_model.set_attr('toolkit', data['toolkit'], changed)
    changed = morpheme_language_model.set_attr('order', data['order'], changed)
    changed = morpheme_language_model.set_attr('smoothing', data['smoothing'], changed)
    changed = morpheme_language_model.set_attr('categorial', data['categorial'], changed)
    changed = morpheme_language_model.set_attr('rare_delimiter', h.rare_delimiter, changed)
    changed = morpheme_language_model.set_attr('start_symbol', h.lm_start, changed)
    changed = morpheme_language_model.set_attr('end_symbol', h.lm_end, changed)
    if changed:
        session['user'] = Session.merge(session['user'])
        morpheme_language_model.modifier = session['user']
        morpheme_language_model.datetime_modified = h.now()
        return morpheme_language_model
    return changed
Exemplo n.º 8
0
def update_morphological_parser(morphological_parser, data):
    """Update a morphological parser.

    :param morphological_parser: the morphological parser model to be updated.
    :param dict data: representation of the updated morphological parser.
    :returns: the updated morphological parser model or, if ``changed`` has not been set
        to ``True``, ``False``.

    """
    changed = False
    changed = morphological_parser.set_attr('name', h.normalize(data['name']), changed)
    changed = morphological_parser.set_attr('description', h.normalize(data['description']), changed)
    changed = morphological_parser.set_attr('phonology', data['phonology'], changed)
    changed = morphological_parser.set_attr('morphology', data['morphology'], changed)
    changed = morphological_parser.set_attr('language_model', data['language_model'], changed)
    if changed:
        session['user'] = Session.merge(session['user'])
        morphological_parser.modifier = session['user']
        morphological_parser.datetime_modified = h.now()
        return morphological_parser
    return changed
Exemplo n.º 9
0
def update_keyboard(keyboard, data):
    """Update a keyboard.

    :param keyboard: the keyboard model to be updated.
    :param dict data: representation of the updated keyboard.
    :returns: the updated keyboard model or, if ``changed`` has not been set
        to ``True``, ``False``.

    """
    changed = False
    changed = keyboard.set_attr('name', h.normalize(data['name']), changed)
    changed = keyboard.set_attr('description',
                                h.normalize(data['description']), changed)
    changed = keyboard.set_attr('keyboard', h.normalize(data['keyboard']),
                                changed)
    if changed:
        keyboard.datetime_modified = h.now()
        session['user'] = Session.merge(session['user'])
        keyboard.modifier = session['user']
        return keyboard
    return changed
Exemplo n.º 10
0
def update_keyboard(keyboard, data):
    """Update a keyboard.

    :param keyboard: the keyboard model to be updated.
    :param dict data: representation of the updated keyboard.
    :returns: the updated keyboard model or, if ``changed`` has not been set
        to ``True``, ``False``.

    """
    changed = False
    changed = keyboard.set_attr('name', h.normalize(data['name']), changed)
    changed = keyboard.set_attr('description',
        h.normalize(data['description']), changed)
    changed = keyboard.set_attr('keyboard',
        h.normalize(data['keyboard']), changed)
    if changed:
        keyboard.datetime_modified = h.now()
        session['user'] = Session.merge(session['user'])
        keyboard.modifier = session['user']
        return keyboard
    return changed
Exemplo n.º 11
0
def update_phonology(phonology, data):
    """Update a phonology.

    :param page: the phonology model to be updated.
    :param dict data: representation of the updated phonology.
    :returns: the updated phonology model or, if ``changed`` has not been set
        to ``True``, ``False``.

    """
    changed = False
    # Unicode Data
    changed = phonology.set_attr('name', h.normalize(data['name']), changed)
    changed = phonology.set_attr('description', h.normalize(data['description']), changed)
    changed = phonology.set_attr('script', h.normalize(data['script']), changed)
    changed = phonology.set_attr('word_boundary_symbol', h.word_boundary_symbol, changed)

    if changed:
        session['user'] = Session.merge(session['user'])
        phonology.modifier = session['user']
        phonology.datetime_modified = h.now()
        return phonology
    return changed
Exemplo n.º 12
0
def update_morpheme_language_model(morpheme_language_model, data):
    """Update a morpheme language model.

    :param morpheme_language_model: the morpheme language model model to be updated.
    :param dict data: representation of the updated morpheme language model.
    :returns: the updated morpheme language model model or, if ``changed`` has not been set
        to ``True``, ``False``.

    """
    changed = False
    changed = morpheme_language_model.set_attr('name',
                                               h.normalize(data['name']),
                                               changed)
    changed = morpheme_language_model.set_attr(
        'description', h.normalize(data['description']), changed)
    changed = morpheme_language_model.set_attr('vocabulary_morphology',
                                               data['vocabulary_morphology'],
                                               changed)
    changed = morpheme_language_model.set_attr('corpus', data['corpus'],
                                               changed)
    changed = morpheme_language_model.set_attr('toolkit', data['toolkit'],
                                               changed)
    changed = morpheme_language_model.set_attr('order', data['order'], changed)
    changed = morpheme_language_model.set_attr('smoothing', data['smoothing'],
                                               changed)
    changed = morpheme_language_model.set_attr('categorial',
                                               data['categorial'], changed)
    changed = morpheme_language_model.set_attr('rare_delimiter',
                                               h.rare_delimiter, changed)
    changed = morpheme_language_model.set_attr('start_symbol', h.lm_start,
                                               changed)
    changed = morpheme_language_model.set_attr('end_symbol', h.lm_end, changed)
    if changed:
        session['user'] = Session.merge(session['user'])
        morpheme_language_model.modifier = session['user']
        morpheme_language_model.datetime_modified = h.now()
        return morpheme_language_model
    return changed
Exemplo n.º 13
0
def update_collection(collection, data, collections_referenced):
    """Update a collection model.

    :param collection: the collection model to be updated.
    :param dict data: representation of the updated collection.
    :param dict collections_referenced: the collection models recursively referenced in ``data['contents']``.
    :returns: a 3-tuple where the second and third elements are invariable
        booleans indicating whether the collection has become restricted or has
        had its ``contents`` value changed as a result of the update,
        respectively.  The first element is the updated collection or ``False``
        of the no update has occurred.

    """
    changed = False
    restricted = False
    contents_changed = False

    # Unicode Data
    changed = collection.set_attr('title', h.normalize(data['title']), changed)
    changed = collection.set_attr('type', h.normalize(data['type']), changed)
    changed = collection.set_attr('url', h.normalize(data['url']), changed)
    changed = collection.set_attr('description',
                                  h.normalize(data['description']), changed)
    changed = collection.set_attr('markup_language',
                                  h.normalize(data['markup_language']),
                                  changed)
    submitted_contents = h.normalize(data['contents'])
    if collection.contents != submitted_contents:
        collection.contents = submitted_contents
        contents_changed = changed = True
    changed = collection.set_attr('contents_unpacked',
                                  h.normalize(data['contents_unpacked']),
                                  changed)
    changed = collection.set_attr(
        'html',
        h.get_HTML_from_contents(collection.contents_unpacked,
                                 collection.markup_language), changed)

    # User-entered date: date_elicited
    changed = collection.set_attr('date_elicited', data['date_elicited'],
                                  changed)

    # Many-to-One Data
    changed = collection.set_attr('elicitor', data['elicitor'], changed)
    changed = collection.set_attr('speaker', data['speaker'], changed)
    changed = collection.set_attr('source', data['source'], changed)

    # Many-to-Many Data: files, forms & tags
    # Update only if the user has made changes.
    files_to_add = [f for f in data['files'] if f]
    forms_to_add = [f for f in data['forms'] if f]
    tags_to_add = [t for t in data['tags'] if t]

    if set(files_to_add) != set(collection.files):
        collection.files = files_to_add
        changed = True

    if set(forms_to_add) != set(collection.forms):
        collection.forms = forms_to_add
        changed = True

    # Restrict the entire collection if it is associated to restricted forms or
    # files or if it references a restricted collection.
    tags = [
        f.tags for f in collection.files + collection.forms +
        collections_referenced.values()
    ]
    tags = [tag for tag_list in tags for tag in tag_list]
    restricted_tags = [tag for tag in tags if tag.name == u'restricted']
    if restricted_tags:
        restricted_tag = restricted_tags[0]
        if restricted_tag not in tags_to_add:
            tags_to_add.append(restricted_tag)

    if set(tags_to_add) != set(collection.tags):
        if u'restricted' in [t.name for t in tags_to_add] and \
        u'restricted' not in [t.name for t in collection.tags]:
            restricted = True
        collection.tags = tags_to_add
        changed = True

    if changed:
        collection.datetime_modified = datetime.datetime.utcnow()
        session['user'] = Session.merge(session['user'])
        collection.modifier = session['user']
        return collection, restricted, contents_changed
    return changed, restricted, contents_changed
Exemplo n.º 14
0
 def update_modification_values(collection, now):
     collection.datetime_modified = now
     session['user'] = Session.merge(session['user'])
     collection.modifier = session['user']
Exemplo n.º 15
0
                    if not restricted and "restricted" in [
                            t.name for t in form.tags
                    ]:
                        restricted = True
                    f.write(writer(form))
        gzipped_corpus_file_path = h.compress_file(corpus_file_path)
        create_tgrep2_corpus_file(gzipped_corpus_file_path, format_)
    except Exception, e:
        destroy_file(corpus_file_path)
        response.status_int = 400
        return error_msg(e)

    # Update/create the corpus_file object
    try:
        now = h.now()
        session['user'] = Session.merge(session['user'])
        user = session['user']
        corpus_filename = os.path.split(corpus_file_path)[1]
        if update:
            try:
                update_corpus_file(corpus, corpus_filename, user, now,
                                   restricted)
            except Exception:
                generate_new_corpus_file(corpus, corpus_filename, format_,
                                         user, now, restricted)
        else:
            generate_new_corpus_file(corpus, corpus_filename, format_, user,
                                     now, restricted)
    except Exception, e:
        destroy_file(corpus_file_path)
        response.status_int = 400
Exemplo n.º 16
0
def update_collection(collection, data, collections_referenced):
    """Update a collection model.

    :param collection: the collection model to be updated.
    :param dict data: representation of the updated collection.
    :param dict collections_referenced: the collection models recursively referenced in ``data['contents']``.
    :returns: a 3-tuple where the second and third elements are invariable
        booleans indicating whether the collection has become restricted or has
        had its ``contents`` value changed as a result of the update,
        respectively.  The first element is the updated collection or ``False``
        of the no update has occurred.

    """
    changed = False
    restricted = False
    contents_changed = False

    # Unicode Data
    changed = collection.set_attr('title', h.normalize(data['title']), changed)
    changed = collection.set_attr('type', h.normalize(data['type']), changed)
    changed = collection.set_attr('url', h.normalize(data['url']), changed)
    changed = collection.set_attr('description', h.normalize(data['description']), changed)
    changed = collection.set_attr('markup_language', h.normalize(data['markup_language']), changed)
    submitted_contents = h.normalize(data['contents'])
    if collection.contents != submitted_contents:
        collection.contents = submitted_contents
        contents_changed = changed = True
    changed = collection.set_attr('contents_unpacked', h.normalize(data['contents_unpacked']), changed)
    changed = collection.set_attr('html', h.get_HTML_from_contents(collection.contents_unpacked,
                                                      collection.markup_language), changed)

    # User-entered date: date_elicited
    changed = collection.set_attr('date_elicited', data['date_elicited'], changed)

    # Many-to-One Data
    changed = collection.set_attr('elicitor', data['elicitor'], changed)
    changed = collection.set_attr('speaker', data['speaker'], changed)
    changed = collection.set_attr('source', data['source'], changed)

    # Many-to-Many Data: files, forms & tags
    # Update only if the user has made changes.
    files_to_add = [f for f in data['files'] if f]
    forms_to_add = [f for f in data['forms'] if f]
    tags_to_add = [t for t in data['tags'] if t]

    if set(files_to_add) != set(collection.files):
        collection.files = files_to_add
        changed = True

    if set(forms_to_add) != set(collection.forms):
        collection.forms = forms_to_add
        changed = True

    # Restrict the entire collection if it is associated to restricted forms or
    # files or if it references a restricted collection.
    tags = [f.tags for f in collection.files + collection.forms + collections_referenced.values()]
    tags = [tag for tag_list in tags for tag in tag_list]
    restricted_tags = [tag for tag in tags if tag.name == u'restricted']
    if restricted_tags:
        restricted_tag = restricted_tags[0]
        if restricted_tag not in tags_to_add:
            tags_to_add.append(restricted_tag)

    if set(tags_to_add) != set(collection.tags):
        if u'restricted' in [t.name for t in tags_to_add] and \
        u'restricted' not in [t.name for t in collection.tags]:
            restricted = True
        collection.tags = tags_to_add
        changed = True

    if changed:
        collection.datetime_modified = datetime.datetime.utcnow()
        session['user'] = Session.merge(session['user'])
        collection.modifier = session['user']
        return collection, restricted, contents_changed
    return changed, restricted, contents_changed
Exemplo n.º 17
0
 def update_modification_values(collection, now):
     collection.datetime_modified = now
     session['user'] = Session.merge(session['user'])
     collection.modifier = session['user']
Exemplo n.º 18
0
                for id in form_references:
                    form = forms[id]
                    if not restricted and "restricted" in [t.name for t in form.tags]:
                        restricted = True
                    f.write(writer(form))
        gzipped_corpus_file_path = h.compress_file(corpus_file_path)
        create_tgrep2_corpus_file(gzipped_corpus_file_path, format_)
    except Exception, e:
        destroy_file(corpus_file_path)
        response.status_int = 400
        return error_msg(e)

    # Update/create the corpus_file object
    try:
        now = h.now()
        session['user'] = Session.merge(session['user'])
        user = session['user']
        corpus_filename = os.path.split(corpus_file_path)[1]
        if update:
            try:
                update_corpus_file(corpus, corpus_filename, user, now, restricted)
            except Exception:
                generate_new_corpus_file(corpus, corpus_filename, format_, user,
                                      now, restricted)
        else:
            generate_new_corpus_file(corpus, corpus_filename, format_, user, now,
                                  restricted)
    except Exception, e:
        destroy_file(corpus_file_path)
        response.status_int = 400
        return error_msg(e)