def switch_control_lists_access( corpus: Corpus, users: List[User], old_control_lists_id: int, ): """Switch user access from one control list to another. :param list users: list of users to switch :param int old_control_lists_id: ID of old control list """ for user in users: # do not delete access to old control list if another corpus uses it for user_corpus in Corpus.for_user(user): if user_corpus.control_lists_id == old_control_lists_id and user_corpus.id != corpus.id: break else: control_lists_user = ControlListsUser.query.filter( ControlListsUser.control_lists_id == old_control_lists_id, ControlListsUser.user_id == user.id, ).one_or_none() # do not delete access to old control list if user is owner if control_lists_user and not control_lists_user.is_owner: db.session.delete(control_lists_user) # add access to new control list ControlLists.link(corpus, user) db.session.commit()
def corpus_new(): """ Register a new corpus """ lemmatizers = current_app.config.get("LEMMATIZERS", []) def normal_view(): return render_template_with_nav_info( 'main/corpus_new.html', lemmatizers=lemmatizers, public_control_lists=_get_available(), tsv=request.form.get("tsv", "") ) def error(): return normal_view(), 400 if request.method == "POST": if not current_user.is_authenticated: abort(403) elif not len(strip_or_none(request.form.get("name", ""))): flash("You forgot to give a name to your corpus", category="error") return error() else: form_kwargs = { "name": request.form.get("name"), "context_left": request.form.get("context_left", None), "context_right": request.form.get("context_right", None), "delimiter_token": strip_or_none(request.form.get("sep_token", "")) or None, "columns": [ Column(heading="Lemma"), Column(heading="POS"), Column(heading="Morph"), Column(heading="Similar"), ] } for column in form_kwargs["columns"]: column.hidden = bool( request.form.get(f"{column.heading.lower()}Column", "") ) if ( "lemmaColumn" in request.form and "posColumn" in request.form and "morphColumn" in request.form ): flash( "You can't disable Lemma and POS and Morph. Keep at least one of them.", category="error" ) return error() if request.form.get("control_list") == "reuse": tokens = read_input_tokens(request.form.get("tsv")) try: control_list = ControlLists.query.get_or_404(request.form.get("control_list_select")) except Exception: flash("This control list does not exist", category="error") return error() form_kwargs.update({"word_tokens_dict": tokens, "control_list": control_list}) cl_owner = False else: tokens, allowed_lemma, allowed_morph, allowed_POS = create_input_format_convertion( request.form.get("tsv"), request.form.get("allowed_lemma", None), request.form.get("allowed_morph", None), request.form.get("allowed_POS", None) ) cl_owner = True form_kwargs.update({"word_tokens_dict": tokens, "allowed_lemma": allowed_lemma, "allowed_POS": allowed_POS, "allowed_morph": allowed_morph}) try: corpus = Corpus.create(**form_kwargs) db.session.add(CorpusUser(corpus=corpus, user=current_user, is_owner=True)) # Add a link to the control list ControlLists.link(corpus=corpus, user=current_user, is_owner=cl_owner) db.session.commit() flash("New corpus registered", category="success") return redirect(url_for(".corpus_get", corpus_id=corpus.id)) except (sqlalchemy.exc.StatementError, sqlalchemy.exc.IntegrityError) as e: db.session.rollback() flash("The corpus cannot be registered. Check your data", category="error") if str(e.orig) == "UNIQUE constraint failed: corpus.name": flash("You have already a corpus going by the name {}".format(request.form.get("name")), category="error") return error() except MissingTokenColumnValue as exc: db.session.rollback() flash("At least one line of your corpus is missing a token/form. Check line %s " % exc.line, category="error") return error() except NoTokensInput: db.session.rollback() flash("You did not input any text.", category="error") return error() except ValidationError as exception: db.session.rollback() flash(exception, category="error") return error() except Exception as e: db.session.rollback() flash("The corpus cannot be registered. Check your data", category="error") return error() return normal_view()