コード例 #1
0
ファイル: blob.py プロジェクト: AlexanderYAPPO/lingvodoc
def upload_user_blob(request):  # TODO: remove blob Object
    variables = {'auth': authenticated_userid(request)}
    response = dict()
    filename = request.POST['blob'].filename
    input_file = request.POST['blob'].file

    class Object(object):
        pass

    blob = Object()
    blob.client_id = variables['auth']
    client = DBSession.query(Client).filter_by(id=variables['auth']).first()
    blob.object_id = DBSession.query(UserBlobs).filter_by(client_id=client.id).count() + 1
    blob.data_type = request.POST['data_type']

    blob.filename = filename

    current_user = DBSession.query(User).filter_by(id=client.user_id).first()

    blob_object = UserBlobs(object_id=blob.object_id,
                            client_id=blob.client_id,
                            name=filename,
                            data_type=blob.data_type,
                            user_id=current_user.id)

    current_user.userblobs.append(blob_object)
    blob_object.real_storage_path, blob_object.content = create_object(request, input_file, blob_object, blob.data_type,
                                                                       blob.filename, json_input=False)
    DBSession.add(blob_object)
    DBSession.add(current_user)
    DBSession.flush()
    request.response.status = HTTPOk.code
    response = {"client_id": blob_object.client_id, "object_id": blob_object.object_id, "content": blob_object.content}
    return response
コード例 #2
0
ファイル: blob.py プロジェクト: st2135/lingvodoc
def upload_user_blob(request):  # TODO: remove blob Object
    variables = {'auth': authenticated_userid(request)}
    response = dict()
    filename = request.POST['blob'].filename
    input_file = request.POST['blob'].file

    class Object(object):
        pass

    blob = Object()
    blob.client_id = variables['auth']
    client = DBSession.query(Client).filter_by(id=variables['auth']).first()
    blob.data_type = request.POST['data_type']

    blob.filename = filename

    current_user = DBSession.query(User).filter_by(id=client.user_id).first()
    object_id = request.POST.get('object_id', None)

    blob_object = UserBlobs(object_id=object_id,
                            client_id=blob.client_id,
                            name=filename,
                            data_type=blob.data_type,
                            user_id=current_user.id)

    current_user.userblobs.append(blob_object)
    blob_object.real_storage_path, blob_object.content = create_object(
        request,
        input_file,
        blob_object,
        blob.data_type,
        blob.filename,
        json_input=False)
    if blob.data_type == "sociolinguistics":
        try:
            check_socio(blob_object.real_storage_path)
        except Exception as e:
            request.response.status = HTTPBadRequest.code
            response = {"error": str(e)}
            return response

    DBSession.add(blob_object)
    DBSession.add(current_user)
    DBSession.flush()

    request.response.status = HTTPOk.code
    response = {
        "client_id": blob_object.client_id,
        "object_id": blob_object.object_id,
        "content": blob_object.content
    }
    return response
コード例 #3
0
ファイル: blob.py プロジェクト: ispras/lingvodoc
def upload_user_blob(request):  # TODO: remove blob Object
    variables = {'auth': authenticated_userid(request)}
    response = dict()
    filename = request.POST['blob'].filename
    input_file = request.POST['blob'].file

    class Object(object):
        pass

    blob = Object()
    blob.client_id = variables['auth']
    client = DBSession.query(Client).filter_by(id=variables['auth']).first()
    blob.data_type = request.POST['data_type']

    blob.filename = filename

    current_user = DBSession.query(User).filter_by(id=client.user_id).first()
    object_id = request.POST.get('object_id', None)

    blob_object = UserBlobs(object_id=object_id,
                            client_id=blob.client_id,
                            name=filename,
                            data_type=blob.data_type,
                            user_id=current_user.id)

    current_user.userblobs.append(blob_object)
    blob_object.real_storage_path, blob_object.content = create_object(request, input_file, blob_object, blob.data_type,
                                                                       blob.filename, json_input=False)
    if blob.data_type == "sociolinguistics":
        try:
            check_socio(blob_object.real_storage_path)
        except Exception as e:
            request.response.status = HTTPBadRequest.code
            response = {"error": str(e)}
            return response

    DBSession.add(blob_object)
    DBSession.add(current_user)
    DBSession.flush()

    request.response.status = HTTPOk.code
    response = {"client_id": blob_object.client_id, "object_id": blob_object.object_id, "content": blob_object.content}
    return response
コード例 #4
0
def create_l2_entity(request):
    try:

        variables = {'auth': authenticated_userid(request)}
        response = dict()
        parent_client_id = request.matchdict.get('level_one_client_id')
        parent_object_id = request.matchdict.get('level_one_object_id')
        req = request.json_body
        client = DBSession.query(Client).filter_by(id=variables['auth']).first()
        if not client:
            raise KeyError("Invalid client id (not registered on server). Try to logout and then login.")
        user = DBSession.query(User).filter_by(id=client.user_id).first()
        if not user:
            raise CommonException("This client id is orphaned. Try to logout and then login once more.")

        # parent = DBSession.query(LevelOneEntity).filter_by(client_id=parent_client_id, object_id=parent_object_id).first()
        parent = None
        if not parent:
            request.response.status = HTTPNotFound.code
            return {'error': str("No such level one entity in the system")}
        additional_metadata = req.get('additional_metadata')
        # entity = LevelTwoEntity(client_id=client.id, object_id=DBSession.query(LevelTwoEntity).filter_by(client_id=client.id).count() + 1, entity_type=req['entity_type'],
        #                         locale_id=req['locale_id'], additional_metadata=additional_metadata,
        #                         parent=parent)
        entity = None

        DBSession.add(entity)
        DBSession.flush()
        data_type = req.get('data_type')
        filename = req.get('filename')
        real_location = None
        url = None
        if data_type == 'image' or data_type == 'sound' or data_type == 'markup':
            real_location, url = create_object(request, req['content'], entity, data_type, filename)

        if url and real_location:
            entity.content = url
            old_meta = entity.additional_metadata

            need_hash = True
            if old_meta:
                new_meta=json.loads(old_meta)
                if new_meta.get('hash'):
                    need_hash = False
            if need_hash:
                hash = hashlib.sha224(base64.urlsafe_b64decode(req['content'])).hexdigest()
                hash_dict = {'hash': hash}
                if old_meta:
                    new_meta = json.loads(old_meta)
                    new_meta.update(hash_dict)
                else:
                    new_meta = hash_dict
                entity.additional_metadata = json.dumps(new_meta)
        else:
            entity.content = req['content']
        DBSession.add(entity)
        request.response.status = HTTPOk.code
        response['client_id'] = entity.client_id
        response['object_id'] = entity.object_id
        return response
    except KeyError as e:
        request.response.status = HTTPBadRequest.code
        return {'error': str(e)}

    except IntegrityError as e:
        request.response.status = HTTPInternalServerError.code
        return {'error': str(e)}

    except CommonException as e:
        request.response.status = HTTPConflict.code
        return {'error': str(e)}
コード例 #5
0
ファイル: entity.py プロジェクト: AlexanderYAPPO/lingvodoc
def create_entity(request):  # tested
    try:
        variables = {'auth': authenticated_userid(request)}
        response = dict()
        parent_client_id = request.matchdict.get('lexical_entry_client_id')
        parent_object_id = request.matchdict.get('lexical_entry_object_id')
        req = request.json_body
        client = DBSession.query(Client).filter_by(id=variables['auth']).first()
        if not client:
            raise KeyError("Invalid client id (not registered on server). Try to logout and then login.")
        user = DBSession.query(User).filter_by(id=client.user_id).first()
        if not user:
            raise CommonException("This client id is orphaned. Try to logout and then login once more.")

        parent = DBSession.query(LexicalEntry).filter_by(client_id=parent_client_id, object_id=parent_object_id).first()
        if not parent:
            request.response.status = HTTPNotFound.code
            return {'error': str("No such lexical entry in the system")}
        if 'content' not in req is None:
            request.response.status = HTTPBadRequest.code
            return {'error': 'Missing value: content'}
        additional_metadata = req.get('additional_metadata')
        parent_entity = None
        # import pdb
        # pdb.set_trace()
        # data_type = DBSession.query(TranslationAtom).filter(TranslationAtom.locale_id == 2).join(TranslationGist, and_(
        #     TranslationAtom.parent_client_id == TranslationGist.client_id,
        #     TranslationAtom.parent_object_id == TranslationGist.object_id)).join(Field, and_(
        #     TranslationGist.client_id == Field.data_type_translation_gist_client_id,
        #     TranslationGist.object_id == Field.data_type_translation_gist_object_id)).filter(
        #     Field.client_id == req['field_client_id'], Field.object_id == req['field_object_id']).first()
        tr_atom = DBSession.query(TranslationAtom).join(TranslationGist, and_(
            TranslationAtom.parent_client_id == TranslationGist.client_id,
            TranslationAtom.parent_object_id == TranslationGist.object_id)).join(Field, and_(
            TranslationGist.client_id == Field.data_type_translation_gist_client_id,
            TranslationGist.object_id == Field.data_type_translation_gist_object_id)).filter(
            Field.client_id == req['field_client_id'], Field.object_id == req['field_object_id']).first()
        data_type = tr_atom.content
        if req.get('entity_client_id') and req.get('entity_object_id'):
            parent_entity = DBSession.query(Entity).filter_by(client_id=req['entity_client_id'],
                                                              object_id=req['entity_object_id']).first()
            if not parent_entity:
                return {'error': str("No such parent entity in the system")}
        entity = Entity(client_id=client.id,
                        field_client_id=req['field_client_id'],
                        field_object_id=req['field_object_id'],
                        locale_id=req['locale_id'],
                        additional_metadata=additional_metadata,
                        parent=parent)
        if parent_entity:
            entity.parent_entity = parent_entity
        filename = req.get('filename')
        real_location = None
        url = None
        if data_type == 'image' or data_type == 'sound' or data_type == 'markup':
            real_location, url = create_object(request, req['content'], entity, data_type, filename)
            entity.content = url
            old_meta = entity.additional_metadata
            need_hash = True
            if old_meta:
                new_meta = json.loads(old_meta)
                if new_meta.get('hash'):
                    need_hash = False
            if need_hash:
                hash = hashlib.sha224(base64.urlsafe_b64decode(req['content'])).hexdigest()
                hash_dict = {'hash': hash}
                if old_meta:
                    new_meta = json.loads(old_meta)
                    new_meta.update(hash_dict)
                else:
                    new_meta = hash_dict
                entity.additional_metadata = json.dumps(new_meta)
        elif data_type == 'link':
            try:
                entity.link_client_id = req['content']['client_id']
                entity.link_object_id = req['content']['object_id']
            except (KeyError, TypeError):
                request.response.status = HTTPBadRequest.code
                return {'Error': "The field is of link type. You should provide client_id and object id in the content"}
        else:
            entity.content = req['content']
        return None
        DBSession.add(entity)
        request.response.status = HTTPOk.code
        response['client_id'] = entity.client_id
        response['object_id'] = entity.object_id
        return response

    except IntegrityError as e:
        request.response.status = HTTPInternalServerError.code
        return {'error': str(e)}

    except CommonException as e:
        request.response.status = HTTPConflict.code
        return {'error': str(e)}
コード例 #6
0
def create_entity(request):  # tested
    try:
        variables = {'auth': authenticated_userid(request)}
        response = dict()
        parent_client_id = request.matchdict.get('lexical_entry_client_id')
        parent_object_id = request.matchdict.get('lexical_entry_object_id')
        req = request.json_body
        client = DBSession.query(Client).filter_by(
            id=variables['auth']).first()
        object_id = req.get('object_id', None)
        if not client:
            raise KeyError(
                "Invalid client id (not registered on server). Try to logout and then login."
            )
        user = DBSession.query(User).filter_by(id=client.user_id).first()
        if not user:
            raise CommonException(
                "This client id is orphaned. Try to logout and then login once more."
            )

        parent = DBSession.query(LexicalEntry).filter_by(
            client_id=parent_client_id, object_id=parent_object_id).first()
        if not parent:
            request.response.status = HTTPNotFound.code
            return {'error': str("No such lexical entry in the system")}
        if 'content' not in req is None:
            request.response.status = HTTPBadRequest.code
            return {'error': 'Missing value: content'}
        additional_metadata = req.get('additional_metadata')
        upper_level = None
        # import pdb
        # pdb.set_trace(v
        # data_type = DBSession.query(TranslationAtom).filter(TranslationAtom.locale_id == 2).join(TranslationGist, and_(
        #     TranslationAtom.parent_client_id == TranslationGist.client_id,
        #     TranslationAtom.parent_object_id == TranslationGist.object_id)).join(Field, and_(
        #     TranslationGist.client_id == Field.data_type_translation_gist_client_id,
        #     TranslationGist.object_id == Field.data_type_translation_gist_object_id)).filter(
        #     Field.client_id == req['field_client_id'], Field.object_id == req['field_object_id']).first()
        tr_atom = DBSession.query(TranslationAtom).join(
            TranslationGist,
            and_(
                TranslationAtom.locale_id == 2,
                TranslationAtom.parent_client_id == TranslationGist.client_id,
                TranslationAtom.parent_object_id ==
                TranslationGist.object_id)).join(
                    Field,
                    and_(
                        TranslationGist.client_id ==
                        Field.data_type_translation_gist_client_id,
                        TranslationGist.object_id ==
                        Field.data_type_translation_gist_object_id)).filter(
                            Field.client_id == req['field_client_id'],
                            Field.object_id == req['field_object_id']).first()
        data_type = tr_atom.content.lower()
        if req.get('self_client_id') and req.get('self_object_id'):
            upper_level = DBSession.query(Entity).filter_by(
                client_id=req['self_client_id'],
                object_id=req['self_object_id']).first()
            if not upper_level:
                return {'error': str("No such upper level in the system")}
        entity = Entity(client_id=client.id,
                        object_id=object_id,
                        field_client_id=req['field_client_id'],
                        field_object_id=req['field_object_id'],
                        locale_id=req.get('locale_id'),
                        additional_metadata=additional_metadata,
                        parent=parent)
        group = DBSession.query(Group).join(BaseGroup).filter(
            BaseGroup.subject == 'lexical_entries_and_entities',
            Group.subject_client_id == entity.parent.parent.client_id,
            Group.subject_object_id == entity.parent.parent.object_id,
            BaseGroup.action == 'create').one()
        if user in group.users:
            entity.publishingentity.accepted = True
        if upper_level:
            entity.upper_level = upper_level
        filename = req.get('filename')
        real_location = None
        url = None
        if data_type == 'image' or data_type == 'sound' or 'markup' in data_type:
            real_location, url = create_object(request, req['content'], entity,
                                               data_type, filename)
            entity.content = url
            old_meta = entity.additional_metadata
            need_hash = True
            if old_meta:
                if old_meta.get('hash'):
                    need_hash = False
            if need_hash:
                hash = hashlib.sha224(base64.urlsafe_b64decode(
                    req['content'])).hexdigest()
                hash_dict = {'hash': hash}
                if old_meta:
                    old_meta.update(hash_dict)
                else:
                    old_meta = hash_dict
                entity.additional_metadata = old_meta
            if 'markup' in data_type:
                name = filename.split('.')
                ext = name[len(name) - 1]
                if ext.lower() == 'textgrid':
                    data_type = 'praat markup'
                elif ext.lower() == 'eaf':
                    data_type = 'elan markup'
            entity.additional_metadata['data_type'] = data_type
        elif data_type == 'link':
            try:
                entity.link_client_id = req['link_client_id']
                entity.link_object_id = req['link_object_id']
            except (KeyError, TypeError):
                request.response.status = HTTPBadRequest.code
                return {
                    'Error':
                    "The field is of link type. You should provide client_id and object id in the content"
                }
        else:
            entity.content = req['content']
        # return None
        DBSession.add(entity)
        request.response.status = HTTPOk.code
        response['client_id'] = entity.client_id
        response['object_id'] = entity.object_id
        return response

    except IntegrityError as e:
        request.response.status = HTTPInternalServerError.code
        return {'error': str(e)}

    except CommonException as e:
        request.response.status = HTTPConflict.code
        return {'error': str(e)}