Ejemplo n.º 1
0
def api_all_commentary(api_version, doc_id):
    if not get_doc(doc_id).is_transcription_validated:
        return make_403()

    tr = get_reference_transcription(doc_id)
    if tr is None:
        return make_404()

    commentaries = Commentary.query.filter(
        Commentary.doc_id == doc_id,
        Commentary.user_id == tr.user_id,
    ).all()

    return make_200(data=[c.serialize() for c in commentaries])
Ejemplo n.º 2
0
def api_documents_validate_commentaries(api_version, doc_id):
    doc = Document.query.filter(Document.id == doc_id).first()
    if doc is None:
        return make_404()

    if not doc.is_transcription_validated:
        return make_403()

    #if Commentary.query.filter(Commentary.doc_id == doc_id,
    #                           Commentary.user_id == doc.user_id).first() is None:
    #    return make_404()

    doc.is_commentaries_validated = True
    return commit_document_validation(doc)
Ejemplo n.º 3
0
def api_documents_unvalidate_translation(api_version, doc_id):
    doc = Document.query.filter(Document.id == doc_id).first()

    if doc is None:
        return make_404()

    if not doc.is_transcription_validated:
        return make_403()

    #if Translation.query.filter(Translation.doc_id == doc_id,
    #                            Translation.user_id == doc.user_id).first() is None:
    #    return make_404()

    doc.is_translation_validated = False
    return commit_document_validation(doc)
Ejemplo n.º 4
0
def api_documents_unvalidate_speech_parts(api_version, doc_id):
    doc = Document.query.filter(Document.id == doc_id).first()
    if doc is None:
        return make_404()

    if not doc.is_transcription_validated:
        return make_403()

    #from app.api.transcriptions.routes import get_reference_transcription
    #tr = get_reference_transcription(doc_id)
    #if AlignmentDiscours.query.filter(AlignmentDiscours.transcription_id == tr.id,
    #                                 AlignmentDiscours.user_id == doc.user_id).first() is None:
    #    return make_404()

    doc.is_speechparts_validated = False
    return commit_document_validation(doc)
Ejemplo n.º 5
0
def api_documents_validate_facsimile(api_version, doc_id):
    doc = Document.query.filter(Document.id == doc_id).first()

    if doc is None:
        return make_404()

    if not doc.is_transcription_validated:
        return make_403()

    #from app.api.transcriptions.routes import get_reference_transcription
    #tr = get_reference_transcription(doc_id)
    #if AlignmentImage.query.filter(AlignmentImage.transcription_id == tr.id,
    #                               AlignmentImage.user_id == doc.user_id).first() is None:
    #    return make_404()

    doc.is_facsimile_validated = True
    return commit_document_validation(doc)
Ejemplo n.º 6
0
def api_delete_user(api_version, user_id):
    try:
        target_user = User.query.filter(User.id == user_id).one()
        user = current_app.get_current_user()

        if target_user.is_admin and not user.is_admin:
            return make_403()

        try:
            db.session.delete(target_user)
            db.session.commit()
            return make_200(data=[])
        except Exception as e:
            db.session.rollback()
            print(str(e))
            return make_400(str(e))

    except NoResultFound:
        return make_404()
Ejemplo n.º 7
0
def api_post_users_roles(api_version, user_id):
    """
    {
        "data": [
            {
                "name": "admin"
            },
            {
                "name": "teacher"
            }
        ]
    }
    :param api_version:
    :param user_id:
    :return:
    """
    data = request.get_json()
    if "data" in data:
        data = data["data"]

        user = current_app.get_current_user()
        target_user = User.query.filter(User.id == user_id).first()

        for role_name in [r.get("name", None) for r in data]:

            if not role_name in target_user.roles:
                if role_name == "admin" and not user.is_admin:
                    return make_403()
                else:
                    role = Role.query.filter(Role.name == role_name).first()
                    if role:
                        target_user.roles.append(role)

        db.session.add(target_user)
        try:
            db.session.commit()
            return make_200(data=[r.serialize() for r in target_user.roles])
        except Exception as e:
            db.session.rollback()
            return make_400(str(e))
    else:
        return make_409()
Ejemplo n.º 8
0
def api_delete_users_roles(api_version, user_id):
    try:
        target_user = User.query.filter(User.id == user_id).one()
    except NoResultFound:
        return make_404()

    user = current_app.get_current_user()
    if target_user.is_admin and not user.is_admin:
        return make_403()

    target_user.roles = [Role.query.filter(Role.name == "student").first()]
    db.session.add(target_user)

    try:
        db.session.commit()
        return make_200()
    except Exception as e:
        db.session.rollback()
        print(str(e))
        return make_400(str(e))
Ejemplo n.º 9
0
def api_put_commentary(api_version, doc_id, user_id):
    """
        {
            "data":
                {
                    "content" :  "My first transcription"
                    "notes": [{
                       "id": 1,
                       "type_id": 0 (by default),
                       "content": "aaa",
                       "ptr_start": 3,
                       "ptr_end": 12
                    }]
                }
        }
        :param api_version:
        :param doc_id:
        :return:
        """
    doc = get_doc(doc_id)
    # teachers can still post notes in if the commentaries are validated
    current_user = current_app.get_current_user()
    if not current_user.is_teacher and doc.is_commentaries_validated:
        return make_403()

    forbid = is_closed(doc_id)
    if forbid:
        return forbid

    data = request.get_json()
    if "data" in data:
        data = data["data"]
        c = Commentary.query.filter(
            data["type_id"] == Commentary.type_id,
            doc_id == Commentary.doc_id,
            user_id == Commentary.user_id,
        ).first()

        if c is None:
            return make_404()

        try:
            if "content" in data:
                error = check_no_XMLParserError(data["content"])
                if error:
                    raise Exception('Commentary content is malformed: %s',
                                    str(error))
                c.content = data["content"]
                db.session.add(c)
                db.session.commit()
            if "notes" in data:

                current_commentary_notes = CommentaryHasNote.query.filter(
                    CommentaryHasNote.commentary_id == c.id).all()
                # remove all notes not present in the transcription anymore
                for current_chn in current_commentary_notes:
                    if (current_chn.note.id, current_chn.ptr_start, current_chn.ptr_end) not in \
                            [(note.get('id', None), note["ptr_start"], note["ptr_end"])
                             for note in data["notes"]]:
                        note = current_chn.note
                        db.session.delete(current_chn)
                        print("delete chn", note)
                        db.session.flush()
                        note.delete_if_unused()

                for note in data["notes"]:

                    note_id = note.get('id', None)
                    chn = CommentaryHasNote.query.filter(
                        CommentaryHasNote.note_id == note_id,
                        CommentaryHasNote.commentary_id == c.id,
                        CommentaryHasNote.ptr_start == note["ptr_start"],
                        CommentaryHasNote.ptr_end == note["ptr_end"]).first()
                    if chn is None:
                        # try to find a note in other contents
                        reused_note = findNoteInDoc(doc_id, user_id, note_id)
                        if reused_note is None:
                            raise Exception(
                                'Cannot reuse note: note %s unknown' % note_id)
                        chn = CommentaryHasNote(commentary_id=c.id,
                                                note_id=reused_note.id,
                                                ptr_start=note["ptr_start"],
                                                ptr_end=note["ptr_end"])
                        db.session.add(chn)
                        db.session.flush()

                    error = check_no_XMLParserError(note["content"])
                    if error:
                        raise Exception('Note content is malformed: %s',
                                        str(error))

                    chn.ptr_start = note['ptr_start']
                    chn.ptr_end = note['ptr_end']
                    chn.note.content = note['content']
                    chn.note.type_id = note['type_id']

                    db.session.add(chn)
                    db.session.add(chn.note)

                db.session.commit()
        except Exception as e:
            db.session.rollback()
            print('Error', str(e))
            return make_400(str(e))
        return make_200(data=c.serialize())
    else:
        return make_400("no data")
Ejemplo n.º 10
0
def api_post_commentary(api_version, doc_id, user_id):
    """
    {
        "data":
            {
                "type_id": 2,
                "content" : "This is a commentary",
                "notes": [
                        {
                           "content": "note1 content",
                           "ptr_start": 5,
                           "ptr_end": 7
                       }
                ]
            }
    }
    :param api_version:
    :param doc_id:
    :return:
    """
    doc = get_doc(doc_id)
    if doc is None:
        return make_404()

    # teachers can still post notes in if the commentaries are validated
    user = current_app.get_current_user()
    if not user.is_teacher and doc.is_commentaries_validated:
        return make_403()

    data = request.get_json()
    if "data" in data:
        data = data["data"]

        data["user_id"] = user_id  # user.id

        try:
            c = None
            # case 1) "content" in data
            if "content" in data:
                error = check_no_XMLParserError(data["content"])
                if error:
                    raise Exception('Commentary content is malformed: %s',
                                    str(error))
                c = Commentary(doc_id=doc_id,
                               user_id=user_id,
                               type_id=data["type_id"],
                               content=data["content"])
                db.session.add(c)
                db.session.flush()
            # case 2) there's "notes" in data
            elif "notes" in data:
                c = Commentary.query.filter(
                    Commentary.doc_id == doc_id, Commentary.user_id == user_id,
                    Commentary.type_id == data["type_id"]).first()

            print(doc, c, user, data)
            if c is None:
                return make_404()

            if "notes" in data:
                print("======= notes =======")
                for note in data["notes"]:
                    # 1) simply reuse notes which come with an id
                    note_id = note.get('id', None)
                    if note_id is not None:
                        reused_note = Note.query.filter(
                            Note.id == note_id,
                            Note.user_id == user_id).first()
                        if reused_note is None:
                            return make_400(details="Wrong note id %s" %
                                            note_id)
                        db.session.add(reused_note)
                        db.session.flush()
                        chn = CommentaryHasNote.query.filter(
                            CommentaryHasNote.note_id == reused_note.id,
                            CommentaryHasNote.commentary_id == c.id).first()
                        # 1.a) the note is already present in the commentary, so update its ptrs
                        if chn is not None:
                            raise Exception(
                                "Commentary note already exists. Consider using PUT method"
                            )
                        else:
                            # 1.b) the note is not present on the transcription side, so create it
                            chn = CommentaryHasNote(
                                commentary_id=c.id,
                                note_id=reused_note.id,
                                ptr_start=note["ptr_start"],
                                ptr_end=note["ptr_end"])
                        db.session.add(chn)
                        print("reuse:", chn.transcription_id, chn.note_id)
                    else:
                        # 2) make new note
                        error = check_no_XMLParserError(note["content"])
                        if error:
                            raise Exception('Note content is malformed: %s',
                                            str(error))
                        new_note = Note(type_id=note.get("type_id", 0),
                                        user_id=user_id,
                                        content=note["content"])
                        db.session.add(new_note)
                        db.session.flush()
                        chn = CommentaryHasNote(commentary_id=c.id,
                                                note_id=new_note.id,
                                                ptr_start=note["ptr_start"],
                                                ptr_end=note["ptr_end"])
                        db.session.add(chn)
                        print("make:", chn.commentary_id, chn.note_id)
                    db.session.flush()
                    print("====================")
                    db.session.add(chn)
            db.session.commit()

        except (Exception, KeyError) as e:
            db.session.rollback()
            print(str(e))
            return make_400(str(e))

        return make_200(c.serialize())
    else:
        return make_400("no data")
Ejemplo n.º 11
0
def api_post_documents_translations(api_version, doc_id, user_id):
    """
    at least one of "content" or "notes" is required
    {
        "data":
            {
                "content" :  "My first translation"
                "notes": [
                        {
                           "content": "note1 content",
                           "ptr_start": 5,
                           "ptr_end": 7
                       }
                ]
            }
    }
    :param user_id:
    :param api_version:
    :param doc_id:
    :return:
    """

    #forbid = forbid_if_other_user(current_app, user_id)
    #if forbid:
    #    return forbid

    # teachers can still post notes in validated translation
    current_user = current_app.get_current_user()
    if not current_user.is_teacher and get_doc(doc_id).is_translation_validated:
        return make_403()

    forbid = is_closed(doc_id)
    if forbid:
        return forbid

    data = request.get_json()
    if "data" in data:
        data = data["data"]
        try:
            tr = None
            # case 1) "content" in data
            if "content" in data:
                error = check_no_XMLParserError(data["content"])
                if error:
                    raise Exception('Translation content is malformed: %s', str(error))
                tr = Translation(doc_id=doc_id, content=data["content"], user_id=user_id)
                db.session.add(tr)
                db.session.flush()
            # case 2) there's only "notes" in data
            if "notes" in data:
                tr = Translation.query.filter(Translation.doc_id == doc_id,
                                              Translation.user_id == user_id).first()
            if tr is None:
                return make_404()

            if "notes" in data:
                print("======= notes =======")
                for note in data["notes"]:
                    # 1) simply reuse notes which come with an id
                    note_id = note.get('id', None)
                    if note_id is not None:
                        reused_note = Note.query.filter(Note.id == note_id).first()
                        if reused_note is None:
                            return make_400(details="Wrong note id %s" % note_id)
                        db.session.add(reused_note)
                        db.session.flush()
                        thn = TranslationHasNote.query.filter(TranslationHasNote.note_id == reused_note.id,
                                                              TranslationHasNote.translation_id == tr.id).first()
                        # 1.a) the note is already present in the translation, so update its ptrs
                        if thn is not None:
                            raise Exception("Translation note already exists. Consider using PUT method")
                        else:
                            # 1.b) the note is not present on the translation side, so create it
                            thn = TranslationHasNote(translation_id=tr.id,
                                                     note_id=reused_note.id,
                                                     ptr_start=note["ptr_start"],
                                                     ptr_end=note["ptr_end"])
                        db.session.add(thn)
                        print("reuse:", thn.translation_id, thn.note_id)
                    else:
                        # 2) make new note
                        error = check_no_XMLParserError(note["content"])
                        if error:
                            raise Exception('Note content is malformed: %s', str(error))
                        new_note = Note(type_id=note.get("type_id", 0), user_id=user_id, content=note["content"])
                        db.session.add(new_note)
                        db.session.flush()
                        thn = TranslationHasNote(translation_id=tr.id,
                                                 note_id=new_note.id,
                                                 ptr_start=note["ptr_start"],
                                                 ptr_end=note["ptr_end"])
                        db.session.add(thn)
                        print("make:", thn.translation_id, thn.note_id)
                db.session.flush()
                print("thn:", [thn.note.id for thn in tr.translation_has_note])
                print("====================")
            db.session.add(tr)
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            print("Error", str(e))
            return make_400(str(e))

        return make_200(data=tr.serialize_for_user(user_id))
    else:
        return make_400("no data")
Ejemplo n.º 12
0
def api_put_documents_translations(api_version, doc_id, user_id):
    """
     {
         "data":
             {
                 "content" :  "My first translation"
                 "notes": [{
                    "id": 1,
                    "type_id": 0 (by default),
                    "content": "aaa",
                    "ptr_start": 3,
                    "ptr_end": 12
                 }]
             }
     }
     :param user_id:
     :param api_version:
     :param doc_id:
     :return:
     """
    #forbid = forbid_if_other_user(current_app, user_id)
    #if forbid:
    #    return forbid

    # teachers can still update validated translation
    current_user = current_app.get_current_user()
    if not current_user.is_teacher and get_doc(doc_id).is_translation_validated:
        return make_403()

    forbid = is_closed(doc_id)
    if forbid:
        return forbid

    data = request.get_json()
    if "data" in data:
        data = data["data"]
        tr = get_translation(doc_id=doc_id, user_id=user_id)
        if tr is None:
            return make_404()
        try:
            if "content" in data:
                error = check_no_XMLParserError(data["content"])
                if error:
                    raise Exception('Translation content is malformed: %s', str(error))
                tr.content = data["content"]
                db.session.add(tr)
                db.session.commit()
            if "notes" in data:
                current_translation_notes = TranslationHasNote.query.filter(
                    TranslationHasNote.translation_id == tr.id).all()
                # remove all notes not present anymore in the translation
                print("current thn", current_translation_notes)
                for current_thn in current_translation_notes:
                    if (current_thn.note.id, current_thn.ptr_start, current_thn.ptr_end) not in \
                            [(note.get('id', None), note["ptr_start"], note["ptr_end"])
                             for note in data["notes"]]:
                        note = current_thn.note
                        db.session.delete(current_thn)
                        print("delete thn", note)
                        db.session.flush()
                        note.delete_if_unused()

                for note in data["notes"]:
                    note_id = note.get('id', None)
                    thn = TranslationHasNote.query.filter(TranslationHasNote.note_id == note_id,
                                                          TranslationHasNote.translation_id == tr.id,
                                                          TranslationHasNote.ptr_start == note["ptr_start"],
                                                          TranslationHasNote.ptr_end == note["ptr_end"]
                                                          ).first()
                    if thn is None or note_id is None:
                        # try to find the note in other contents
                        reused_note = findNoteInDoc(doc_id, user_id, note_id)
                        if reused_note is None:
                            raise Exception('Cannot reuse note: note %s unknown' % note_id)

                        # bind the note on the translation side
                        thn = TranslationHasNote(translation_id=tr.id,
                                                 note_id=reused_note.id,
                                                 ptr_start=note["ptr_start"],
                                                 ptr_end=note["ptr_end"])
                        db.session.add(thn)
                        db.session.flush()

                    error = check_no_XMLParserError(note["content"])
                    if error:
                        raise Exception('Note content is malformed: %s', str(error))

                    thn.ptr_start = note['ptr_start']
                    thn.ptr_end = note['ptr_end']
                    thn.note.content = note['content']
                    thn.note.type_id = note['type_id']

                    db.session.add(thn)
                    db.session.add(thn.note)

                db.session.commit()
        except Exception as e:
            db.session.rollback()
            print('Error', str(e))
            return make_400(str(e))
        return make_200(data=tr.serialize_for_user(user_id))
    else:
        return make_400("no data")