示例#1
0
文件: blob.py 项目: st2135/lingvodoc
def delete_user_blob(request):
    user = get_user_by_client_id(authenticated_userid(request))
    if user is None:
        request.response.status = HTTPUnauthorized.code
        return {'error': "Guests can not delete resources."}
    client_id = request.matchdict.get('client_id')
    object_id = request.matchdict.get('object_id')
    if user != get_user_by_client_id(client_id):
        request.response.status = HTTPForbidden.code
        return {'error': "That file doesn't belong to you."}
    blob = DBSession.query(UserBlobs).filter_by(client_id=client_id,
                                                object_id=object_id).first()
    if not blob:
        request.response.status = HTTPNotFound.code
        return {'error': 'No such blob in the system'}

    filelocation = blob.real_storage_path
    DBSession.delete(blob)
    request.response.status = HTTPOk.code
    try:
        os.unlink(filelocation)
    except:
        # NOTE: intentionally not an error
        return {
            "warning":
            "File can not be deleted physically; deleting from DMBS only."
        }
    return
示例#2
0
文件: views.py 项目: ispras/lingvodoc
def testing(request):
    with_group = 0
    without_group = 0
    for group in DBSession.query(Group).filter_by(base_group_id=26).all():
        DBSession.delete(group)
    for persp in DBSession.query(DictionaryPerspective):
        group = DBSession.query(Group).filter_by(base_group_id=22, subject_client_id=persp.client_id,
                                                 subject_object_id=persp.object_id).first()
        if not group:
            without_group +=1
        new_group = Group(base_group_id=26, subject_client_id=persp.client_id,
                          subject_object_id=persp.object_id)
        for user in group.users:
            new_group.users.append(user)
        DBSession.add(new_group)
    return {"good": with_group, "bad": without_group}
示例#3
0
def update_perspective_fields(req, perspective_client_id,
                              perspective_object_id, client):
    response = dict()
    perspective = SyncDBSession.query(DictionaryPerspective).filter_by(
        client_id=perspective_client_id,
        object_id=perspective_object_id).first()
    client = SyncDBSession.query(Client).filter_by(
        id=client.id).first()  # variables['auth']
    if not client:
        raise KeyError(
            "Invalid client id (not registered on server). Try to logout and then login."
        )

    if perspective and not perspective.marked_for_deletion:
        try:
            link_gist = SyncDBSession.query(TranslationGist) \
                .join(TranslationAtom) \
                .filter(TranslationGist.type == 'Service',
                        TranslationAtom.content == 'Link',
                        TranslationAtom.locale_id == 2).one()
            link_ids = {
                'client_id': link_gist.client_id,
                'object_id': link_gist.object_id
            }
        except NoResultFound:
            return {'error': str("Something wrong with the base")}
        fields = SyncDBSession.query(DictionaryPerspectiveToField) \
            .filter_by(parent=perspective) \
            .all()
        SyncDBSession.flush()
        for field in fields:  ## ?
            SyncDBSession.delete(field)
        position = 1
        for field in req:
            create_nested_field(field=field,
                                perspective=perspective,
                                client_id=client.id,
                                upper_level=None,
                                link_ids=link_ids,
                                position=position)
            position += 1

        return response
    else:
        return {'error': str("No such perspective in the system")}
示例#4
0
def update_perspective_fields(req,
                              perspective_client_id,
                              perspective_object_id,
                              client):
    response = dict()
    perspective = DBSession.query(DictionaryPerspective).filter_by(client_id=perspective_client_id,
                                                                   object_id=perspective_object_id).first()
    client = DBSession.query(Client).filter_by(id=client.id).first() #variables['auth']
    if not client:
        raise KeyError("Invalid client id (not registered on server). Try to logout and then login.")
    if perspective and not perspective.marked_for_deletion:
        try:
            link_gist = DBSession.query(TranslationGist)\
                .join(TranslationAtom)\
                .filter(TranslationGist.type == 'Service',
                        TranslationAtom.content == 'Link',
                        TranslationAtom.locale_id == 2).one()
            link_ids = {'client_id':link_gist.client_id, 'object_id': link_gist.object_id}
        except NoResultFound:
            return {'error': str("Something wrong with the base")}
        fields = DBSession.query(DictionaryPerspectiveToField)\
            .filter_by(parent=perspective)\
            .all()
        DBSession.flush()
        for field in fields: ## ?
            DBSession.delete(field)
        position = 1
        for field in req:
            create_nested_field(field=field,
                                perspective=perspective,
                                client_id=client.id,
                                upper_level=None,
                                link_ids=link_ids, position=position)
            position += 1

        return response
    else:
        return {'error': str("No such perspective in the system")}
示例#5
0
文件: blob.py 项目: ispras/lingvodoc
def delete_user_blob(request):
    user = get_user_by_client_id(authenticated_userid(request))
    if user is None:
        request.response.status = HTTPUnauthorized.code
        return {'error': "Guests can not delete resources."}
    client_id = request.matchdict.get('client_id')
    object_id = request.matchdict.get('object_id')
    if user != get_user_by_client_id(client_id):
        request.response.status = HTTPForbidden.code
        return {'error': "That file doesn't belong to you."}
    blob = DBSession.query(UserBlobs).filter_by(client_id=client_id, object_id=object_id).first()
    if not blob:
        request.response.status = HTTPNotFound.code
        return {'error': 'No such blob in the system'}

    filelocation = blob.real_storage_path
    DBSession.delete(blob)
    request.response.status = HTTPOk.code
    try:
        os.unlink(filelocation)
    except:
        # NOTE: intentionally not an error
        return {"warning": "File can not be deleted physically; deleting from DMBS only."}
    return
示例#6
0
def move_lexical_entry_bulk(request):
    req = request.json_body
    real_delete = req.get('real_delete')  # With great power comes great responsibility
    # Maybe there needs to be check for permission of some sort (can really delete only when updating dictionary)
    variables = {'auth': request.authenticated_userid}
    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.",
                       variables['auth'])
    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.")
    groups = DBSession.query(Group)\
        .join(BaseGroup, BaseGroup.id == Group.base_group_id)\
        .filter(BaseGroup.subject == 'lexical_entries_and_entities')\
        .filter(BaseGroup.action == 'create')\
        .join(User, Group.users)\
        .filter(User.id == user.id)\
        .group_by(Group)\
        .order_by('subject_override')\
        .all()

    wat = [o for o in groups]
    override = False
    ids = [{'client_id': o.subject_client_id,'object_id': o.subject_object_id} for o in groups]
    for group in groups:
        if group.subject_override:
            override = True
            break
    for par in req['move_list']:
        cli_id = par['client_id']
        obj_id = par['object_id']
        parent = DBSession.query(LexicalEntry).filter_by(client_id=cli_id, object_id=obj_id).first()
        can = True
        if parent:
            if not override:
                if {'client_id': parent.parent_client_id, 'object_id': parent.parent_object_id} not in ids:
                    can = False
            if can:
                for ent in par['lexical_entries']:
                    can = True
                    object_id = ent['object_id']
                    client_id = ent['client_id']
                    entry = DBSession.query(LexicalEntry).filter_by(client_id=client_id, object_id=object_id).first()
                    if entry:
                        if not override:
                            if {'client_id': entry.parent_client_id, 'object_id': entry.parent_object_id} not in ids:
                                can = False

                        if can:
                                if entry:
                                    if parent.moved_to is None:
                                        if entry.moved_to is None:

                                            if not entry.marked_for_deletion and not parent.marked_for_deletion:
                                                # l1e = DBSession.query(LevelOneEntity).filter_by(parent = entry).all()
                                                l1e = list()
                                                for entity in l1e:
                                                    # ent = DBSession.query(LevelOneEntity)\
                                                    #     .filter_by(parent=parent,
                                                    #                entity_type=entity.entity_type,
                                                    #                content = entity.content)\
                                                    #     .first()
                                                    ent = None
                                                    if ent:
                                                        entity.marked_for_deletion = True
                                                        if real_delete:
                                                            for publent in entity.publishleveloneentity:
                                                                DBSession.delete(publent)
                                                            DBSession.delete(entity)
                                                            continue
                                                    entity.parent = parent

                                                    for publent in entity.publishleveloneentity:
                                                        publent.marked_for_deletion = True
                                                        publent.parent = parent
                                                    DBSession.flush()
                                                # ge = DBSession.query(GroupingEntity).filter_by(parent = entry).all()
                                                ge = list()
                                                for entity in ge:
                                                    entity.parent = parent
                                                    for publent in entity.publishgroupingentity:
                                                        publent.marked_for_deletion = True
                                                        publent.parent = parent
                                                    DBSession.flush()
                                                entry.moved_to = str(cli_id) + '/' + str(obj_id)
                                                entry.marked_for_deletion = True
    request.response.status = HTTPOk.code
    return {}
示例#7
0
def move_lexical_entry(request):
    req = request.json_body
    variables = {'auth': request.authenticated_userid}
    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.",
                       variables['auth'])
    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.")
    object_id = request.matchdict.get('object_id')
    client_id = request.matchdict.get('client_id')
    cli_id = req['client_id']
    obj_id = req['object_id']
    real_delete = req.get('real_delete')   # With great power comes great responsibility
    # Maybe there needs to be check for permission of some sort (can really delete only when updating dictionary)
    entry = DBSession.query(LexicalEntry).filter_by(client_id=client_id, object_id=object_id).first()
    parent = DBSession.query(LexicalEntry).filter_by(client_id=cli_id, object_id=obj_id).first()
    if entry and parent:
        groupoverride = DBSession.query(Group)\
            .filter_by(subject_override=True)\
            .join(BaseGroup)\
            .filter_by(subject='lexical_entries_and_entities')\
            .first()
        group = DBSession.query(Group)\
            .filter_by(subject_client_id=parent.parent_client_id, subject_object_id=parent.parent_object_id)\
            .join(BaseGroup)\
            .filter_by(subject='lexical_entries_and_entities')\
            .first()
        if user not in groupoverride.users and  user not in group.users:
                raise CommonException("You should only move to lexical entires you own")
        if parent.moved_to is None:
            if entry.moved_to is None:

                if not entry.marked_for_deletion and not parent.marked_for_deletion:
                    # l1e = DBSession.query(LevelOneEntity).filter_by(parent = entry).all()
                    l1e = list()
                    for entity in l1e:
                        # ent = DBSession.query(LevelOneEntity)\
                        #     .filter_by(parent=parent, entity_type=entity.entity_type, content = entity.content)\
                        #     .first()
                        ent = None
                        if ent:
                            entity.marked_for_deletion = True
                            if real_delete:
                                for publent in entity.publishleveloneentity:
                                    DBSession.delete(publent)
                                DBSession.delete(entity)
                                continue
                        entity.parent = parent

                        for publent in entity.publishleveloneentity:
                            publent.marked_for_deletion = True
                            publent.parent = parent
                        DBSession.flush()
                    # ge = DBSession.query(GroupingEntity).filter_by(parent = entry).all()
                    ge = list()
                    for entity in ge:
                        entity.parent = parent
                        for publent in entity.publishgroupingentity:
                            publent.marked_for_deletion = True
                            publent.parent = parent
                        DBSession.flush()
                    entry.moved_to = str(cli_id) + '/' + str(obj_id)
                    entry.marked_for_deletion = True
                    request.response.status = HTTPOk.code
                    return {}
    request.response.status = HTTPNotFound.code
    return {'error': str("No such lexical entry in the system")}
示例#8
0
def move_lexical_entry_bulk(request):
    req = request.json_body
    real_delete = req.get(
        'real_delete')  # With great power comes great responsibility
    # Maybe there needs to be check for permission of some sort (can really delete only when updating dictionary)
    variables = {'auth': request.authenticated_userid}
    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.",
            variables['auth'])
    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."
        )
    groups = DBSession.query(Group)\
        .join(BaseGroup, BaseGroup.id == Group.base_group_id)\
        .filter(BaseGroup.subject == 'lexical_entries_and_entities')\
        .filter(BaseGroup.action == 'create')\
        .join(User, Group.users)\
        .filter(User.id == user.id)\
        .group_by(Group)\
        .order_by('subject_override')\
        .all()

    wat = [o for o in groups]
    override = False
    ids = [{
        'client_id': o.subject_client_id,
        'object_id': o.subject_object_id
    } for o in groups]
    for group in groups:
        if group.subject_override:
            override = True
            break
    for par in req['move_list']:
        cli_id = par['client_id']
        obj_id = par['object_id']
        parent = DBSession.query(LexicalEntry).filter_by(
            client_id=cli_id, object_id=obj_id).first()
        can = True
        if parent:
            if not override:
                if {
                        'client_id': parent.parent_client_id,
                        'object_id': parent.parent_object_id
                } not in ids:
                    can = False
            if can:
                for ent in par['lexical_entries']:
                    can = True
                    object_id = ent['object_id']
                    client_id = ent['client_id']
                    entry = DBSession.query(LexicalEntry).filter_by(
                        client_id=client_id, object_id=object_id).first()
                    if entry:
                        if not override:
                            if {
                                    'client_id': entry.parent_client_id,
                                    'object_id': entry.parent_object_id
                            } not in ids:
                                can = False

                        if can:
                            if entry:
                                if parent.moved_to is None:
                                    if entry.moved_to is None:

                                        if not entry.marked_for_deletion and not parent.marked_for_deletion:
                                            # l1e = DBSession.query(LevelOneEntity).filter_by(parent = entry).all()
                                            l1e = list()
                                            for entity in l1e:
                                                # ent = DBSession.query(LevelOneEntity)\
                                                #     .filter_by(parent=parent,
                                                #                entity_type=entity.entity_type,
                                                #                content = entity.content)\
                                                #     .first()
                                                ent = None
                                                if ent:
                                                    entity.marked_for_deletion = True
                                                    if real_delete:
                                                        for publent in entity.publishleveloneentity:
                                                            DBSession.delete(
                                                                publent)
                                                        DBSession.delete(
                                                            entity)
                                                        continue
                                                entity.parent = parent

                                                for publent in entity.publishleveloneentity:
                                                    publent.marked_for_deletion = True
                                                    publent.parent = parent
                                                DBSession.flush()
                                            # ge = DBSession.query(GroupingEntity).filter_by(parent = entry).all()
                                            ge = list()
                                            for entity in ge:
                                                entity.parent = parent
                                                for publent in entity.publishgroupingentity:
                                                    publent.marked_for_deletion = True
                                                    publent.parent = parent
                                                DBSession.flush()
                                            entry.moved_to = str(
                                                cli_id) + '/' + str(obj_id)
                                            entry.marked_for_deletion = True
    request.response.status = HTTPOk.code
    return {}
示例#9
0
def move_lexical_entry(request):
    req = request.json_body
    variables = {'auth': request.authenticated_userid}
    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.",
            variables['auth'])
    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."
        )
    object_id = request.matchdict.get('object_id')
    client_id = request.matchdict.get('client_id')
    cli_id = req['client_id']
    obj_id = req['object_id']
    real_delete = req.get(
        'real_delete')  # With great power comes great responsibility
    # Maybe there needs to be check for permission of some sort (can really delete only when updating dictionary)
    entry = DBSession.query(LexicalEntry).filter_by(
        client_id=client_id, object_id=object_id).first()
    parent = DBSession.query(LexicalEntry).filter_by(client_id=cli_id,
                                                     object_id=obj_id).first()
    if entry and parent:
        groupoverride = DBSession.query(Group)\
            .filter_by(subject_override=True)\
            .join(BaseGroup)\
            .filter_by(subject='lexical_entries_and_entities')\
            .first()
        group = DBSession.query(Group)\
            .filter_by(subject_client_id=parent.parent_client_id, subject_object_id=parent.parent_object_id)\
            .join(BaseGroup)\
            .filter_by(subject='lexical_entries_and_entities')\
            .first()
        if user not in groupoverride.users and user not in group.users:
            raise CommonException(
                "You should only move to lexical entires you own")
        if parent.moved_to is None:
            if entry.moved_to is None:

                if not entry.marked_for_deletion and not parent.marked_for_deletion:
                    # l1e = DBSession.query(LevelOneEntity).filter_by(parent = entry).all()
                    l1e = list()
                    for entity in l1e:
                        # ent = DBSession.query(LevelOneEntity)\
                        #     .filter_by(parent=parent, entity_type=entity.entity_type, content = entity.content)\
                        #     .first()
                        ent = None
                        if ent:
                            entity.marked_for_deletion = True
                            if real_delete:
                                for publent in entity.publishleveloneentity:
                                    DBSession.delete(publent)
                                DBSession.delete(entity)
                                continue
                        entity.parent = parent

                        for publent in entity.publishleveloneentity:
                            publent.marked_for_deletion = True
                            publent.parent = parent
                        DBSession.flush()
                    # ge = DBSession.query(GroupingEntity).filter_by(parent = entry).all()
                    ge = list()
                    for entity in ge:
                        entity.parent = parent
                        for publent in entity.publishgroupingentity:
                            publent.marked_for_deletion = True
                            publent.parent = parent
                        DBSession.flush()
                    entry.moved_to = str(cli_id) + '/' + str(obj_id)
                    entry.marked_for_deletion = True
                    request.response.status = HTTPOk.code
                    return {}
    request.response.status = HTTPNotFound.code
    return {'error': str("No such lexical entry in the system")}