Exemplo n.º 1
0
    def test_create_language_with_parent(self):
        from lingvodoc.models import (Language, UserEntitiesTranslationString)
        response = self.app.post_json('/signin',
                                      params={
                                          'login': '******',
                                          'password': '******'
                                      })
        response = self.app.post_json('/language',
                                      params={
                                          'translation_string': 'imastring2',
                                          'translation': 'imatranslation2',
                                          'parent_object_id': 1,
                                          'parent_client_id': 1
                                      })
        self.assertEqual(response.status_int, HTTPOk.code)

        language = DBSession.query(Language).filter_by(
            translation_string='imastring2').first()
        self.assertNotEqual(language, None)
        self.assertEqual(language.object_id, 1)
        self.assertEqual(language.client_id, 2)

        parent = DBSession.query(Language).filter_by(client_id=1,
                                                     object_id=1).first()
        self.assertNotEqual(parent, None)
        self.assertEqual(parent, language.parent)

        uets = DBSession.query(UserEntitiesTranslationString).\
            filter_by(translation_string='imastring2', locale_id=1).first()
        self.assertNotEqual(uets, None)
        self.assertEqual(uets.translation, 'imatranslation2')
Exemplo n.º 2
0
def edit_language(request):  # tested & in docs
    try:
        response = dict()
        client_id = request.matchdict.get('client_id')
        object_id = request.matchdict.get('object_id')
        client = DBSession.query(Client).filter_by(id=request.authenticated_userid).first()

        if not client:
            raise KeyError("Invalid client id (not registered on server). Try to logout and then login.")
        language = DBSession.query(Language).filter_by(client_id=client_id, object_id=object_id).first()
        if language:
            if not language.marked_for_deletion:
                # TODO: Status 500 will be returned if arguments are invalid; add try/catch
                req = request.json_body
                if 'parent_client_id' in req:
                    language.parent_client_id = req['parent_client_id']
                if 'parent_object_id' in req:
                    language.parent_object_id = req['parent_object_id']
                if 'translation_gist_client_id' in req:
                    language.translation_gist_client_id = req['translation_gist_client_id']
                if 'translation_gist_object_id' in req:
                    language.translation_gist_object_id = req['translation_gist_object_id']
                request.response.status = HTTPOk.code
                return response
        request.response.status = HTTPNotFound.code
        return {'error': str("No such language in the system")}
    except KeyError as e:
        request.response.status = HTTPBadRequest.code
        return {'error': str(e)}
Exemplo n.º 3
0
def async_convert_dictionary(client_id, object_id, parent_client_id, parent_object_id, dictionary_client_id,
                             dictionary_object_id, perspective_client_id, perspective_object_id, user_id,
                             task_id=None):
    DBSession.configure(bind=celery_engine)
    client = DBSession.query(Client).filter_by(id=user_id).first()
    user = client.user
    blob = DBSession.query(UserBlobs).filter_by(client_id=client_id, object_id=object_id).first()

    # convert_one(blob.real_storage_path,
    #             user.login,
    #             user.password.hash,
    #             parent_client_id,
    #             parent_object_id)

    # NOTE: doesn't work on Mac OS otherwise

    convert_one(blob.real_storage_path,
                user.login,
                user.password.hash,
                parent_client_id,
                parent_object_id,
                dictionary_client_id,
                dictionary_object_id,
                perspective_client_id,
                perspective_object_id,
                task_id=task_id)

    return
Exemplo n.º 4
0
def edit_language(request):  # tested & in docs
    try:
        response = dict()
        client_id = request.matchdict.get('client_id')
        object_id = request.matchdict.get('object_id')
        client = DBSession.query(Client).filter_by(
            id=request.authenticated_userid).first()

        if not client:
            raise KeyError(
                "Invalid client id (not registered on server). Try to logout and then login."
            )
        language = DBSession.query(Language).filter_by(
            client_id=client_id, object_id=object_id).first()
        if language:
            if not language.marked_for_deletion:
                # TODO: Status 500 will be returned if arguments are invalid; add try/catch
                req = request.json_body
                if 'parent_client_id' in req:
                    language.parent_client_id = req['parent_client_id']
                if 'parent_object_id' in req:
                    language.parent_object_id = req['parent_object_id']
                if 'translation_gist_client_id' in req:
                    language.translation_gist_client_id = req[
                        'translation_gist_client_id']
                if 'translation_gist_object_id' in req:
                    language.translation_gist_object_id = req[
                        'translation_gist_object_id']
                request.response.status = HTTPOk.code
                return response
        request.response.status = HTTPNotFound.code
        return {'error': str("No such language in the system")}
    except KeyError as e:
        request.response.status = HTTPBadRequest.code
        return {'error': str(e)}
Exemplo n.º 5
0
def delete_group_entity(request):  # TODO: test
    response = dict()
    client_id = request.matchdict.get('client_id')
    object_id = request.matchdict.get('object_id')
    req = request.json_body
    field_client_id = req['field_client_id']
    field_object_id = req['field_object_id']
    # entities = DBSession.query(GroupingEntity).filter_by(parent_client_id=client_id, parent_object_id=object_id).all()
    # entities = list()
    field = DBSession.query(Field).filter_by(
        client_id=field_client_id, object_id=field_object_id).first()
    if not field:
        request.response.status = HTTPNotFound.code
        return {'error': str("No such field in the system")}
    elif field.data_type != 'Grouping Tag':
        request.response.status = HTTPBadRequest.code
        return {'error': str("Wrong type of field")}

    entities = DBSession.query(Entity).filter_by(
        field_client_id=field_client_id,
        field_object_id=field_object_id,
        parent_client_id=client_id,
        parent_object_id=object_id,
        marked_for_deletion=False).all()
    if entities:
        for entity in entities:
            entity.marked_for_deletion = True
        request.response.status = HTTPOk.code
        return response
    request.response.status = HTTPNotFound.code
    return {'error': str("No such entity in the system")}
Exemplo n.º 6
0
def list_user_blobs(request):  # TODO: test
    variables = {'auth': authenticated_userid(request)}
    allowed_global_types = ["sociolinguistics"]
    client = DBSession.query(Client).filter_by(id=variables['auth']).first()
    data_type = request.params.get('data_type')
    is_global = request.params.get('is_global')
    if data_type:
        if not is_global:
            user_blobs = DBSession.query(UserBlobs).filter_by(
                user_id=client.user_id, data_type=data_type).all()
        else:
            if data_type in allowed_global_types:
                user_blobs = DBSession.query(UserBlobs).filter_by(
                    data_type=data_type).all()
            else:
                request.response.status = HTTPForbidden.code
                return {"error": "You can not list that data type globally."}
    else:
        user_blobs = DBSession.query(UserBlobs).filter_by(
            user_id=client.user_id).all()
    request.response.status = HTTPOk.code
    response = [{
        'name': blob.name,
        'content': blob.content,
        'data_type': blob.data_type,
        'client_id': blob.client_id,
        'object_id': blob.object_id
    } for blob in user_blobs]
    return response
Exemplo n.º 7
0
def basic_sync_desktop(request):
    client =DBSession.query(Client).filter_by(id=authenticated_userid(request)).first()
    if client:
        user =DBSession.query(User).filter_by(id=client.user_id).first()
        return basic_tables_content(user.id, client_id=client.id)
    request.response.status = HTTPNotFound.code
    return {'error': str("Try to login again")}
Exemplo n.º 8
0
def create_group(request):
    try:
        variables = {'auth': request.authenticated_userid}

        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.",
                           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.")
        if not DBSession.query(Group).filter_by(id=req['id']).first():
            group = Group(id=req['id'],
                             base_group_id=req['base_group_id'],
                             subject_client_id=req['subject_client_id'],
                             subject_object_id=req['subject_object_id'])
            DBSession.add(group)
            for user_id in req['users']:
                curr_user = DBSession.query(User).filter_by(id=user_id).first()
                if curr_user not in group.users:
                    group.users.append(curr_user)

        return {}
    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)}
Exemplo n.º 9
0
def basic_sync_desktop(request):
    client =DBSession.query(Client).filter_by(id=authenticated_userid(request)).first()
    if client:
        user =DBSession.query(User).filter_by(id=client.user_id).first()
        return basic_tables_content(user.id, client_id=client.id)
    request.response.status = HTTPNotFound.code
    return {'error': str("Try to login again")}
Exemplo n.º 10
0
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
Exemplo n.º 11
0
 def inner(context, request):  # TODO: pass object better way (not too sure, request.object may be the best way)
     client_id = request.matchdict.get('client_id')
     object_id = request.matchdict.get('object_id')
     # translationatom = DBSession.query(TranslationAtom).filter_by(client_id=client_id, object_id=object_id).first()
     tables = [
         Dictionary,
         DictionaryPerspective,
         DictionaryPerspectiveToField,
         Field,
         LexicalEntry,
         Entity,
         Language,
         UserBlobs,
         TranslationAtom,
         TranslationGist
     ]
     queries = list()
     for table in tables:
         queries.append(DBSession.query(table.client_id, table.object_id, table.__tablename__))
     db_object = DBSession.query().filter_by(client_id=client_id, object_id=object_id).first()
     if db_object:
         request.object = db_object
         request.response.status = HTTPOk.code
         tmp = view_callable(context, request)
         return tmp
     request.response.status = HTTPNotFound.code
     return HTTPNotFound(json={'error': "No such translationgist in the system"})
Exemplo n.º 12
0
 def inner(
     context, request
 ):  # TODO: pass object better way (not too sure, request.object may be the best way)
     client_id = request.matchdict.get('client_id')
     object_id = request.matchdict.get('object_id')
     # translationatom = DBSession.query(TranslationAtom).filter_by(client_id=client_id, object_id=object_id).first()
     tables = [
         Dictionary, DictionaryPerspective, DictionaryPerspectiveToField,
         Field, LexicalEntry, Entity, Language, UserBlobs, TranslationAtom,
         TranslationGist
     ]
     queries = list()
     for table in tables:
         queries.append(
             DBSession.query(table.client_id, table.object_id,
                             table.__tablename__))
     db_object = DBSession.query().filter_by(client_id=client_id,
                                             object_id=object_id).first()
     if db_object:
         request.object = db_object
         request.response.status = HTTPOk.code
         tmp = view_callable(context, request)
         return tmp
     request.response.status = HTTPNotFound.code
     return HTTPNotFound(
         json={'error': "No such translationgist in the system"})
Exemplo n.º 13
0
def convert_dictionary_check(request):  # TODO: test
    import sqlite3
    req = request.json_body

    client_id = req['blob_client_id']
    object_id = req['blob_object_id']
    # parent_client_id = req['parent_client_id']
    # parent_object_id = req['parent_object_id']
    client = DBSession.query(Client).filter_by(id=authenticated_userid(request)).first()
    user = client.user

    blob = DBSession.query(UserBlobs).filter_by(client_id=client_id, object_id=object_id).first()
    filename = blob.real_storage_path
    sqconn = sqlite3.connect(filename)
    res = get_dict_attributes(sqconn)
    dialeqt_id = res['dialeqt_id']
    persps = DBSession.query(DictionaryPerspective).filter(DictionaryPerspective.import_hash == dialeqt_id).all()
    perspectives = []
    for perspective in persps:
        path = request.route_url('perspective',
                                 dictionary_client_id=perspective.parent_client_id,
                                 dictionary_object_id=perspective.parent_object_id,
                                 perspective_client_id=perspective.client_id,
                                 perspective_object_id=perspective.object_id)
        subreq = Request.blank(path)
        subreq.method = 'GET'
        subreq.headers = request.headers
        resp = request.invoke_subrequest(subreq)
        if 'error' not in resp.json:
            perspectives += [resp.json]
    request.response.status = HTTPOk.code
    return perspectives
Exemplo n.º 14
0
def basic_tables_content(user_id=None, client_id=None):
    response = dict()
    for table in [
            Client, User, BaseGroup, Field, Locale, TranslationAtom,
            TranslationGist, Group, Language
    ]:
        tmp_resp = [row2dict(entry) for entry in DBSession.query(table)]
        if tmp_resp:
            tmp_resp = create_nested_content(tmp_resp)
        response[table.__tablename__] = tmp_resp
    if not user_id:
        response['user_to_group_association'] = DBSession.query(
            user_to_group_association).all()
    elif client_id:
        tmp_resp = [
            row2dict(entry) for entry in DBSession.query(Group).filter_by(
                subject_client_id=client_id)
        ]
        if tmp_resp:
            tmp_resp = create_nested_content(tmp_resp)
        response['group'] = tmp_resp
        response['user_to_group_association'] = DBSession.query(user_to_group_association)\
            .join(Group).filter(user_to_group_association.c.user_id==user_id, Group.subject_client_id==client_id).all()
    else:
        response['user_to_group_association'] = DBSession.query(
            user_to_group_association).filter_by(user_id=user_id).all()
    return response
def create_group_entity(request, client, user):  # tested
    response = dict()
    req = request
    tags = list()
    if 'tag' in req:
        tags.append(req['tag'])
    field_client_id = req['field_client_id']
    field_object_id = req['field_object_id']
    field = DBSession.query(Field).\
        filter_by(client_id=field_client_id, object_id=field_object_id).first()

    if not field:
        return {'error': str("No such field in the system")}

    for par in req['connections']:
        parent = DBSession.query(LexicalEntry).\
            filter_by(client_id=par['client_id'], object_id=par['object_id']).first()
        if not parent:
            return {'error': str("No such lexical entry in the system")}
        par_tags = find_all_tags(parent, field_client_id, field_object_id)
        for tag in par_tags:
            if tag not in tags:
                tags.append(tag)
    if not tags:
        n = 10  # better read from settings
        tag = time.ctime() + ''.join(
            random.SystemRandom().choice(string.ascii_uppercase +
                                         string.digits) for c in range(n))
        tags.append(tag)
    lexical_entries = find_lexical_entries_by_tags(tags, field_client_id,
                                                   field_object_id)
    for par in req['connections']:
        parent = DBSession.query(LexicalEntry).\
            filter_by(client_id=par['client_id'], object_id=par['object_id']).first()
        if parent not in lexical_entries:
            lexical_entries.append(parent)

    for lex in lexical_entries:
        for tag in tags:
            tag_entity = DBSession.query(Entity) \
                .join(Entity.field) \
                .filter(Entity.parent == lex,
                        Field.client_id == field_client_id,
                        Field.object_id == field_object_id,
                        Entity.content == tag).first()
            if not tag_entity:
                tag_entity = Entity(client_id=client.id,
                                    field=field,
                                    content=tag,
                                    parent=lex)

                group = DBSession.query(Group).join(BaseGroup).filter(
                    BaseGroup.subject == 'lexical_entries_and_entities',
                    Group.subject_client_id ==
                    tag_entity.parent.parent.client_id, Group.subject_object_id
                    == tag_entity.parent.parent.object_id,
                    BaseGroup.action == 'create').one()
                if user in group.users:
                    tag_entity.publishingentity.accepted = True
Exemplo n.º 16
0
def view_perspective_from_object(request, perspective):
    response = dict()
    if perspective:
        if not perspective.marked_for_deletion:
            response['parent_client_id'] = perspective.parent_client_id
            response['parent_object_id'] = perspective.parent_object_id
            response['client_id'] = perspective.client_id
            response['object_id'] = perspective.object_id
            response[
                'translation_gist_client_id'] = perspective.translation_gist_client_id
            response[
                'translation_gist_object_id'] = perspective.translation_gist_object_id
            response[
                'state_translation_gist_client_id'] = perspective.state_translation_gist_client_id
            response[
                'state_translation_gist_object_id'] = perspective.state_translation_gist_object_id
            atom = DBSession.query(TranslationAtom).filter_by(
                parent_client_id=perspective.state_translation_gist_client_id,
                parent_object_id=perspective.state_translation_gist_object_id,
                locale_id=int(request.cookies['locale_id'])).first()
            if atom:
                response['status'] = atom.content
            response['marked_for_deletion'] = perspective.marked_for_deletion
            response['is_template'] = perspective.is_template
            # response['additional_metadata'] = perspective.additional_metadata
            if perspective.additional_metadata:
                response['additional_metadata'] = [
                    key for key in perspective.additional_metadata
                ]
            response['translation'] = perspective.get_translation(
                request.cookies['locale_id'])
            if perspective.additional_metadata:
                meta = perspective.additional_metadata
                if 'location' in meta:
                    response['location'] = meta['location']
                if 'info' in meta:
                    response['info'] = meta['info']
                    remove_list = []
                    info_list = response['info']['content']
                    for info in info_list:
                        content = info['info']['content']
                        blob = DBSession.query(UserBlobs).filter_by(
                            client_id=content['client_id'],
                            object_id=content['object_id']).first()
                        if blob:
                            prevnamewaswrong = {
                                'name': blob.name,
                                'content': blob.content,
                                'data_type': blob.data_type,
                                'client_id': blob.client_id,
                                'object_id': blob.object_id
                            }
                            info['info']['content'] = prevnamewaswrong
                        else:
                            if info not in remove_list:
                                remove_list.append(info)
            return response
    return {'error': 'no persp'}
Exemplo n.º 17
0
def create_group_entity(request):  # tested
    try:
        variables = {'auth': authenticated_userid(request)}
        response = dict()
        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.")

        tags = []
        if 'tag' in req:
            tags += [req['tag']]
        for par in req['connections']:
            parent = DBSession.query(LexicalEntry).\
                filter_by(client_id=par['client_id'], object_id=par['object_id']).first()
            if not parent:
                request.response.status = HTTPNotFound.code
                return {'error': str("No such lexical entry in the system")}
            # par_tags = DBSession.query(GroupingEntity).\
            #     filter_by(entity_type=req['entity_type'], parent=parent).all()
            par_tags = None
            tags += [o.content for o in par_tags]
        if not tags:
            n = 10  # better read from settings
            tag = time.ctime() + ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits)
                                         for c in range(n))
            tags += [tag]
        parents = req['connections']
        for par in parents:
            parent = DBSession.query(LexicalEntry).\
                filter_by(client_id=par['client_id'], object_id=par['object_id']).first()
            for tag in tags:
                # ent = DBSession.query(GroupingEntity).\
                #     filter_by(entity_type=req['entity_type'], content=tag, parent=parent).first()
                ent = None
                if not ent:
                    # entity = GroupingEntity(client_id=client.id, object_id=DBSession.query(GroupingEntity).filter_by(client_id=client.id).count() + 1,
                    #                         entity_type=req['entity_type'], content=tag, parent=parent)
                    entity = None
                    DBSession.add(entity)
                    DBSession.flush()
        log.debug('TAGS: %s', tags)
        request.response.status = HTTPOk.code
        return {}
    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)}
Exemplo n.º 18
0
def create_language(request):  # tested & in docs
    try:
        variables = {'auth': request.authenticated_userid}

        req = request.json_body
        try:
            parent_client_id = req['parent_client_id']
            parent_object_id = req['parent_object_id']
        except:
            parent_client_id = None
            parent_object_id = None
        translation_gist_client_id = req['translation_gist_client_id']
        translation_gist_object_id = req['translation_gist_object_id']
        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.",
                           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.")

        parent = None
        if parent_client_id and parent_object_id:
            parent = DBSession.query(Language).filter_by(client_id=parent_client_id, object_id=parent_object_id).first()
        language = Language(client_id=variables['auth'],
                            object_id=object_id,
                                translation_gist_client_id=translation_gist_client_id,
                                translation_gist_object_id=translation_gist_object_id)
        DBSession.add(language)
        if parent:
            language.parent = parent
        DBSession.flush()
        basegroups = []
        basegroups += [DBSession.query(BaseGroup).filter_by(name="Can edit languages").first()]
        basegroups += [DBSession.query(BaseGroup).filter_by(name="Can delete languages").first()]
        if not object_id:
            groups = []
            for base in basegroups:
                group = Group(subject_client_id=language.client_id, subject_object_id=language.object_id, parent=base)
                groups += [group]
            for group in groups:
                add_user_to_group(user, group)
        request.response.status = HTTPOk.code
        return {'object_id': language.object_id,
                'client_id': language.client_id}
    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)}
Exemplo n.º 19
0
def create_lexical_entry_bulk(request):  # TODO: test
    try:
        dictionary_client_id = request.matchdict.get('dictionary_client_id')
        dictionary_object_id = request.matchdict.get('dictionary_object_id')
        perspective_client_id = request.matchdict.get('perspective_client_id')
        perspective_object_id = request.matchdict.get('perspective_object_id')

        count = request.json_body.get('count') or 0

        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."
            )
        perspective = DBSession.query(DictionaryPerspective). \
            filter_by(client_id=perspective_client_id, object_id = perspective_object_id).first()
        if not perspective:
            request.response.status = HTTPNotFound.code
            return {'error': str("No such perspective in the system")}

        lexes_list = []
        for i in range(0, count):
            lexentr = LexicalEntry(client_id=variables['auth'],
                                   parent_object_id=perspective_object_id,
                                   parent=perspective)
            DBSession.add(lexentr)
            lexes_list.append(lexentr)
        DBSession.flush()
        lexes_ids_list = []
        for lexentr in lexes_list:
            lexes_ids_list.append({
                'client_id': lexentr.client_id,
                'object_id': lexentr.object_id
            })

        request.response.status = HTTPOk.code
        return lexes_ids_list
    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)}
Exemplo n.º 20
0
def list_user_blobs(request):  # TODO: test
    variables = {'auth': authenticated_userid(request)}
    #    user_client_ids = [cl_id.id for cl_id in DBSession.query(Client).filter_by(id=variables['auth']).all()]
    #    user_blobs = DBSession.query(UserBlobs).filter_by(client_id.in_(user_client_ids)).all()
    client = DBSession.query(Client).filter_by(id=variables['auth']).first()
    user_blobs = DBSession.query(UserBlobs).filter_by(user_id=client.user_id).all()
    request.response.status = HTTPOk.code
    response = [{'name': blob.name, 'content': blob.content, 'data_type': blob.data_type,
                 'client_id': blob.client_id, 'object_id': blob.object_id} for blob in user_blobs]
    return response
Exemplo n.º 21
0
def get_user_info(request):  # tested
    response = dict()
    client_id = request.params.get('client_id')
    user_id = request.params.get('user_id')
    if client_id:
        client = DBSession.query(Client).filter_by(id=client_id).first()
        if not client:

            request.response.status = HTTPNotFound.code
            return {'error': str("No such client in the system")}
        user = DBSession.query(User).filter_by(id=client.user_id).first()
        if not user:
            request.response.status = HTTPNotFound.code
            return {'error': str("No such user in the system")}
    elif user_id:
        user = DBSession.query(User).filter_by(id=user_id).first()
        if not user:
            request.response.status = HTTPNotFound.code
            return {'error': str("No such user in the system")}
    else:
        client = DBSession.query(Client).filter_by(id=authenticated_userid(request)).first()
        if not client:
            request.response.status = HTTPNotFound.code
            return {'error': str("No such client in the system")}
        user = DBSession.query(User).filter_by(id=client.user_id).first()
        if not user:
            request.response.status = HTTPNotFound.code
            return {'error': str("No such user in the system")}
    response['id']= user.id
    response['login'] = user.login
    response['name'] = user.name
    response['intl_name'] = user.intl_name
    response['default_locale_id'] = user.default_locale_id
    response['birthday'] = str(user.birthday)
    response['signup_date'] = str(user.signup_date)
    response['is_active'] = str(user.is_active)
    email = None
    if user.email:
        for em in user.email:
            email = em.email
            break
    response['email'] = email
    about = None
    if user.about:
        for ab in user.about:
            about = ab.content
            break
    response['about'] = about
    organizations = []
    for organization in user.organizations:
        organizations += [{'organization_id':organization.id}]
    response['organizations'] = organizations
    request.response.status = HTTPOk.code
    return response
def create_group_entity(request, client, user):  # tested
        response = dict()
        req = request
        tags = list()
        if 'tag' in req:
            tags.append(req['tag'])
        field_client_id=req['field_client_id']
        field_object_id=req['field_object_id']
        field = DBSession.query(Field).\
            filter_by(client_id=field_client_id, object_id=field_object_id).first()

        if not field:
            return {'error': str("No such field in the system")}

        for par in req['connections']:
            parent = DBSession.query(LexicalEntry).\
                filter_by(client_id=par['client_id'], object_id=par['object_id']).first()
            if not parent:
                return {'error': str("No such lexical entry in the system")}
            par_tags = find_all_tags(parent, field_client_id, field_object_id)
            for tag in par_tags:
                if tag not in tags:
                    tags.append(tag)
        if not tags:
            n = 10  # better read from settings
            tag = time.ctime() + ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits)
                                         for c in range(n))
            tags.append(tag)
        lexical_entries = find_lexical_entries_by_tags(tags, field_client_id, field_object_id)
        for par in req['connections']:
            parent = DBSession.query(LexicalEntry).\
                filter_by(client_id=par['client_id'], object_id=par['object_id']).first()
            if parent not in lexical_entries:
                lexical_entries.append(parent)

        for lex in lexical_entries:
            for tag in tags:
                tag_entity = DBSession.query(Entity) \
                    .join(Entity.field) \
                    .filter(Entity.parent == lex,
                            Field.client_id == field_client_id,
                            Field.object_id == field_object_id,
                            Entity.content == tag).first()
                if not tag_entity:
                    tag_entity = Entity(client_id=client.id,
                                        field=field, content=tag, parent=lex)

                    group = DBSession.query(Group).join(BaseGroup).filter(
                        BaseGroup.subject == 'lexical_entries_and_entities',
                        Group.subject_client_id == tag_entity.parent.parent.client_id,
                        Group.subject_object_id == tag_entity.parent.parent.object_id,
                        BaseGroup.action == 'create').one()
                    if user in group.users:
                        tag_entity.publishingentity.accepted = True
Exemplo n.º 23
0
 def test_edit_language_parent(self):
     from lingvodoc.models import (
         Language
         )
     response = self.app.post_json('/signin', params={'login': '******', 'password': '******'})
     response = self.app.put_json('/language/1/1', params={'parent_object_id': 2, 'parent_client_id': 1})
     self.assertEqual(response.status_int, HTTPOk.code)
     language = DBSession.query(Language).filter_by(client_id=1, object_id=1).first()
     self.assertNotEqual(language, None)
     parent = DBSession.query(Language).filter_by(client_id=1, object_id=2).first()
     self.assertNotEqual(parent, None)
     self.assertEqual(language.parent, parent)
Exemplo n.º 24
0
def create_translationgist(request):
    try:
        variables = {'auth': request.authenticated_userid}

        req = request.json_body
        object_id = req.get('object_id', None)
        type = req['type']
        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."
            )
        translationgist = TranslationGist(client_id=variables['auth'],
                                          object_id=object_id,
                                          type=type)
        DBSession.add(translationgist)
        DBSession.flush()
        basegroups = []
        basegroups += [
            DBSession.query(BaseGroup).filter_by(
                name="Can delete translationgist").first()
        ]
        if not object_id:
            groups = []
            for base in basegroups:
                group = Group(subject_client_id=translationgist.client_id,
                              subject_object_id=translationgist.object_id,
                              parent=base)
                groups += [group]
            for group in groups:
                add_user_to_group(user, group)
        request.response.status = HTTPOk.code
        return {
            'object_id': translationgist.object_id,
            'client_id': translationgist.client_id
        }
    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)}
Exemplo n.º 25
0
def create_translationatom(request):
    try:
        variables = {'auth': request.authenticated_userid}

        req = request.json_body
        parent_client_id = req['parent_client_id']
        parent_object_id = req['parent_object_id']
        locale_id = req['locale_id']
        content = req['content']
        object_id = req.get('object_id', None)
        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.")
        parent = DBSession.query(TranslationGist).filter_by(client_id=parent_client_id, object_id=parent_object_id).first()
        if not parent.marked_for_deletion:
            translationatom = TranslationAtom(client_id=variables['auth'],
                                              object_id=object_id,
                                              parent=parent,
                                              locale_id=locale_id,
                                              content=content)
            DBSession.add(translationatom)
            DBSession.flush()
            if not object_id:
                basegroups = []
                basegroups += [DBSession.query(BaseGroup).filter_by(name="Can edit translationatom").first()]
                if not object_id:
                    groups = []
                    for base in basegroups:
                        group = Group(subject_client_id=translationatom.client_id, subject_object_id=translationatom.object_id, parent=base)
                        groups += [group]
                    for group in groups:
                        add_user_to_group(user, group)
            request.response.status = HTTPOk.code
            return {'object_id': translationatom.object_id,
                    'client_id': translationatom.client_id}
        else:
            raise KeyError("TranslationGist deleted")
    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)}
Exemplo n.º 26
0
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
Exemplo n.º 27
0
 def test_edit_language_name(self):
     from lingvodoc.models import (
         Language,
         UserEntitiesTranslationString
         )
     response = self.app.post_json('/signin', params={'login': '******', 'password': '******'})
     response = self.app.put_json('/language/1/1', params={'translation_string': 'test', 'translation': 'working'})
     self.assertEqual(response.status_int, HTTPOk.code)
     language = DBSession.query(Language).filter_by(client_id=1, object_id=1).first()
     self.assertNotEqual(language, None)
     uets = DBSession.query(UserEntitiesTranslationString).\
         filter_by(translation_string=language.translation_string, locale_id=1).first()
     self.assertNotEqual(uets, None)
     self.assertEqual(uets.translation, 'working')
Exemplo n.º 28
0
def get_user_info(request):  # tested
    response = dict()
    client_id = request.params.get('client_id')
    user_id = request.params.get('user_id')
    if client_id:
        client = DBSession.query(Client).filter_by(id=client_id).first()
        if not client:

            request.response.status = HTTPNotFound.code
            return {'error': str("No such client in the system")}
        user = DBSession.query(User).filter_by(id=client.user_id).first()
        if not user:
            request.response.status = HTTPNotFound.code
            return {'error': str("No such user in the system")}
    elif user_id:
        user = DBSession.query(User).filter_by(id=user_id).first()
        if not user:
            request.response.status = HTTPNotFound.code
            return {'error': str("No such user in the system")}
    else:
        client = DBSession.query(Client).filter_by(
            id=authenticated_userid(request)).first()
        if not client:
            request.response.status = HTTPNotFound.code
            return {'error': str("No such client in the system")}
        user = DBSession.query(User).filter_by(id=client.user_id).first()
        if not user:
            request.response.status = HTTPNotFound.code
            return {'error': str("No such user in the system")}
    response['id'] = user.id
    response['login'] = user.login
    response['name'] = user.name
    response['intl_name'] = user.intl_name
    response['default_locale_id'] = user.default_locale_id
    response['birthday'] = str(user.birthday)
    response['created_at'] = user.created_at
    response['is_active'] = user.is_active
    if user.email:
        response['email'] = user.email.email
    meta = None
    if user.additional_metadata:
        meta = user.additional_metadata
    if meta and meta.get('about'):
        response['about'] = meta['about']
    organizations = []
    for organization in user.organizations:
        organizations += [{'organization_id': organization.id}]
    response['organizations'] = organizations
    request.response.status = HTTPOk.code
    return response
Exemplo n.º 29
0
def create_lexical_entry_bulk(request):  # TODO: test
    try:
        dictionary_client_id = request.matchdict.get('dictionary_client_id')
        dictionary_object_id = request.matchdict.get('dictionary_object_id')
        perspective_client_id = request.matchdict.get('perspective_client_id')
        perspective_object_id = request.matchdict.get('perspective_object_id')

        count = request.json_body.get('count') or 0

        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.")
        perspective = DBSession.query(DictionaryPerspective). \
            filter_by(client_id=perspective_client_id, object_id = perspective_object_id).first()
        if not perspective:
            request.response.status = HTTPNotFound.code
            return {'error': str("No such perspective in the system")}

        lexes_list = []
        for i in range(0, count):
            lexentr = LexicalEntry(client_id=variables['auth'],
                                   parent_object_id=perspective_object_id, parent=perspective)
            DBSession.add(lexentr)
            lexes_list.append(lexentr)
        DBSession.flush()
        lexes_ids_list = []
        for lexentr in lexes_list:
            lexes_ids_list.append({'client_id': lexentr.client_id, 'object_id': lexentr.object_id})

        request.response.status = HTTPOk.code
        return lexes_ids_list
    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)}
Exemplo n.º 30
0
def view_language(request):  # tested & in docs
    response = dict()
    client_id = request.matchdict.get('client_id')
    object_id = request.matchdict.get('object_id')
    language = DBSession.query(Language).filter_by(
        client_id=client_id, object_id=object_id).first()
    if language:
        if not language.marked_for_deletion:
            response['parent_client_id'] = language.parent_client_id
            response['parent_object_id'] = language.parent_object_id
            response['client_id'] = language.client_id
            response['object_id'] = language.object_id
            response[
                'translation_gist_client_id'] = language.translation_gist_client_id
            response[
                'translation_gist_object_id'] = language.translation_gist_object_id
            response['translation'] = language.get_translation(
                request.cookies['locale_id'])
            if language.locale:
                response['locale_exist'] = True
            else:
                response['locale_exist'] = False

            request.response.status = HTTPOk.code
            return response
    request.response.status = HTTPNotFound.code
    return {'error': str("No such language in the system")}
Exemplo n.º 31
0
def all_languages_with_dicts(dicts, request):
    def row2dict(r):
        return {c.name: getattr(r, c.name) for c in r.__table__.columns}

    result = list()
    lang_to_dict_mapping = dict()
    ds = dicts.join(
        Language,
        and_(Dictionary.parent_client_id == Language.client_id,
             Dictionary.parent_object_id == Language.object_id)).all()
    for i in ds:
        if i.parent_client_id not in lang_to_dict_mapping:
            lang_to_dict_mapping[i.parent_client_id] = dict()
        if i.parent_object_id not in lang_to_dict_mapping[i.parent_client_id]:
            lang_to_dict_mapping[i.parent_client_id][
                i.parent_object_id] = list()
        dictionary = row2dict(i)
        dictionary['translation'] = i.get_translation(
            request.cookies.get('locale_id', 1))
        dictionary['category'] = categories[i.category]
        del dictionary['created_at']
        del dictionary['domain']
        del dictionary['marked_for_deletion']
        lang_to_dict_mapping[i.parent_client_id][i.parent_object_id].append(
            dictionary)

    top_level_langs = DBSession.query(Language).filter_by(
        marked_for_deletion=False, parent=None).all()

    for lang in top_level_langs:
        l = tree_in_breadth(lang, lang_to_dict_mapping, request)
        if l:
            result.append(l)

    return result
Exemplo n.º 32
0
def all_languages_with_dicts(dicts, request):
    def row2dict(r): return {c.name: getattr(r, c.name) for c in r.__table__.columns}
    result = list()
    lang_to_dict_mapping = dict()
    ds = dicts.join(Language, and_(Dictionary.parent_client_id == Language.client_id, Dictionary.parent_object_id == Language.object_id)).all()
    for i in ds:
        if i.parent_client_id not in lang_to_dict_mapping:
            lang_to_dict_mapping[i.parent_client_id] = dict()
        if i.parent_object_id not in lang_to_dict_mapping[i.parent_client_id]:
            lang_to_dict_mapping[i.parent_client_id][i.parent_object_id] = list()
        dictionary = row2dict(i)
        dictionary['translation'] = i.get_translation(request.cookies.get('locale_id', 1))
        dictionary['category'] = categories[i.category]
        del dictionary['created_at']
        del dictionary['domain']
        del dictionary['marked_for_deletion']
        lang_to_dict_mapping[i.parent_client_id][i.parent_object_id].append(dictionary)

    top_level_langs = DBSession.query(Language).filter_by(marked_for_deletion=False, parent=None).all()

    for lang in top_level_langs:
        l = tree_in_breadth(lang, lang_to_dict_mapping, request)
        if l:
            result.append(l)

    return result
Exemplo n.º 33
0
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
Exemplo n.º 34
0
def login_cheat(request):  # TODO: test
    next = request.params.get('next') or request.route_url('dashboard')
    login = request.json_body.get('login', '')
    passwordhash = request.json_body.get('passwordhash', '')
    log.debug("Logging in with cheat method:" + login)
    user = DBSession.query(User).filter_by(login=login).first()
    if user and user.password.hash == passwordhash:
        log.debug("Login successful")
        client = Client(user_id=user.id)
        user.clients.append(client)
        DBSession.add(client)
        DBSession.flush()
        headers = remember(request, principal=client.id)
        response = Response()
        response.headers = headers
        locale_id = user.default_locale_id
        if not locale_id:
            locale_id = 1
        response.set_cookie(key='locale_id', value=str(locale_id))
        response.set_cookie(key='client_id', value=str(client.id))
        headers = remember(request, principal=client.id)
        return response

    log.debug("Login unsuccessful for " + login)
    return HTTPUnauthorized(location=request.route_url('login'))
Exemplo n.º 35
0
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}
Exemplo n.º 36
0
def new_client_server(request):
    old_client = DBSession.query(Client).filter_by(
        id=authenticated_userid(request)).first()
    if old_client:
        user = old_client.user
        if user:
            client = Client(user_id=user.id, is_browser_client=False)
            user.clients.append(client)
            DBSession.add(client)
            DBSession.flush()
            headers = remember(request, principal=client.id)
            response = Response()
            response.headers = headers
            locale_id = user.default_locale_id
            if not locale_id:
                locale_id = 1
            response.set_cookie(key='locale_id', value=str(locale_id))
            response.set_cookie(key='client_id', value=str(client.id))
            result = dict()
            result['client_id'] = client.id
            request.response.status = HTTPOk.code
            # request.response.headers = headers
            # return response
            return HTTPOk(headers=response.headers, json_body=result)
            # return result
    return HTTPUnauthorized(location=request.route_url('login'))
Exemplo n.º 37
0
def view_connected_words(request):
    response = list()
    client_id = request.matchdict.get('client_id')
    object_id = request.matchdict.get('object_id')
    field_client_id = request.params.get('field_client_id')
    field_object_id = request.params.get('field_object_id')
    lexical_entry = DBSession.query(LexicalEntry).filter_by(
        client_id=client_id, object_id=object_id).first()
    if lexical_entry and not lexical_entry.marked_for_deletion:
        tags = find_all_tags(lexical_entry, field_client_id, field_object_id)
        lexes = find_lexical_entries_by_tags(tags, field_client_id,
                                             field_object_id)
        for lex in lexes:
            path = request.route_url(
                'lexical_entry',  # todo: method in utils (or just use track)
                client_id=lex.client_id,
                object_id=lex.object_id)
            subreq = Request.blank(path)
            subreq.method = 'GET'
            subreq.headers = request.headers
            try:
                resp = request.invoke_subrequest(subreq)
                if resp.json not in response:
                    response.append(resp.json)
            except HTTPForbidden:
                pass
        request.response.status = HTTPOk.code
        return response

    request.response.status = HTTPNotFound.code
    return {'error': str("No such lexical entry in the system")}
def create_field(translation_gist_client_id, translation_gist_object_id,
                 data_type_translation_gist_client_id,
                 data_type_translation_gist_object_id, client):
    try:

        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()
        field = Field(
            client_id=client.id,  ###
            data_type_translation_gist_client_id=
            data_type_translation_gist_client_id,
            data_type_translation_gist_object_id=
            data_type_translation_gist_object_id,
            translation_gist_client_id=translation_gist_client_id,
            translation_gist_object_id=translation_gist_object_id)

        DBSession.add(field)
        DBSession.flush()

        return {'object_id': field.object_id, 'client_id': field.client_id}
    except KeyError as e:

        return {'error': str(e)}
Exemplo n.º 39
0
def login_cheat(request):  # TODO: test
    next = request.params.get('next') or request.route_url('dashboard')
    login = request.json_body.get('login', '')
    passwordhash = request.json_body.get('passwordhash', '')
    log.debug("Logging in with cheat method:" + login)
    user = DBSession.query(User).filter_by(login=login).first()
    if user and user.password.hash == passwordhash:
        log.debug("Login successful")
        client = Client(user_id=user.id)
        user.clients.append(client)
        DBSession.add(client)
        DBSession.flush()
        headers = remember(request, principal=client.id)
        response = Response()
        response.headers = headers
        locale_id = user.default_locale_id
        if not locale_id:
            locale_id = 1
        response.set_cookie(key='locale_id', value=str(locale_id))
        response.set_cookie(key='client_id', value=str(client.id))
        headers = remember(request, principal=client.id)
        return response

    log.debug("Login unsuccessful for " + login)
    return HTTPUnauthorized(location=request.route_url('login'))
Exemplo n.º 40
0
def users_list(request):  # tested
    response = dict()
    search = None
    try:
        search = request.params.get('search')
    except:
        pass
    users_temp = DBSession.query(User).join(User.email)
    users = []
    if search:
        name = search + '%'
        users_temp = users_temp.filter(
            or_(User.name.startswith(name), User.login.startswith(name),
                User.intl_name.startswith(name), Email.email.startswith(name)))
    for user in users_temp:
        users += [{
            'id': user.id,
            'name': user.name,
            'login': user.login,
            'intl_name': user.intl_name
        }]

    response['users'] = users
    request.response.status = HTTPOk.code

    return response
Exemplo n.º 41
0
def signin(request):
    req = request.json_body
    login = req['login']
    password = req['password']
    # login = request.POST.get('login', '')
    # password = request.POST.get('password', '')
    desktop = req.get('desktop', False)

    user = DBSession.query(User).filter_by(login=login).first()
    if user and user.check_password(password):
        client = Client(user_id=user.id, is_browser_client=not desktop)
        user.clients.append(client)
        DBSession.add(client)
        DBSession.flush()
        headers = remember(request, principal=client.id, max_age=315360000)
        response = Response()
        response.headers = headers
        locale_id = user.default_locale_id
        if not locale_id:
            locale_id = 1
        response.set_cookie(key='locale_id',
                            value=str(locale_id),
                            max_age=datetime.timedelta(days=3650))
        response.set_cookie(key='client_id',
                            value=str(client.id),
                            max_age=datetime.timedelta(days=3650))
        result = dict()
        result['client_id'] = client.id
        request.response.status = HTTPOk.code
        # request.response.headers = headers
        # return response
        return HTTPOk(headers=response.headers, json_body=result)
        # return result
    return HTTPUnauthorized(location=request.route_url('login'))
Exemplo n.º 42
0
def view_lexical_entry(request):  # TODO: test
    response = dict()
    client_id = request.matchdict.get('client_id')
    object_id = request.matchdict.get('object_id')

    # entry = DBSession.query(LexicalEntry) \
    #     .options(joinedload('leveloneentity').joinedload('leveltwoentity').joinedload('publishleveltwoentity')) \
    #     .options(joinedload('leveloneentity').joinedload('publishleveloneentity')) \
    #     .options(joinedload('groupingentity').joinedload('publishgroupingentity')) \
    #     .options(joinedload('publishleveloneentity')) \
    #     .options(joinedload('publishleveltwoentity')) \
    #     .options(joinedload('publishgroupingentity')) \
    #     .filter_by(client_id=client_id, object_id=object_id).first()

    entry = DBSession.query(LexicalEntry) \
        .filter_by(client_id=client_id, object_id=object_id).first()
    if entry:
        if entry.moved_to:
            url = request.route_url('lexical_entry',
                                    client_id=entry.moved_to.split("/")[0],
                                    object_id=entry.moved_to.split("/")[1])
            subreq = Request.blank(url)
            subreq.method = 'GET'
            subreq.headers = request.headers
            return request.invoke_subrequest(subreq)
        else:
            if not entry.marked_for_deletion:
                response = entry.track(
                    False, int(request.cookies.get('locale_id') or 2))
                # response['client_id'] = entry.client_id
                # response['object_id'] = entry.object_id
                request.response.status = HTTPOk.code
                return response
    request.response.status = HTTPNotFound.code
    return {'error': str("No such lexical entry in the system")}
def find_all_tags(lexical_entry, field_client_id, field_object_id):
    tag = None
    for entity in lexical_entry.entity:
        if entity.field.data_type == 'Grouping Tag':
            tag = entity.content
            break
    if not tag:
        return []
    else:
        tags = [tag]
        new_tags = [tag]
        while new_tags:
            lexical_entries = find_lexical_entries_by_tags(new_tags, field_client_id, field_object_id)
            new_tags = list()
            for lex in lexical_entries:
                entities = DBSession.query(Entity) \
                    .join(Entity.field) \
                    .join(Entity.publishingentity) \
                    .filter(Entity.parent == lex,
                            PublishingEntity.accepted == True,
                            Field.client_id == field_client_id,
                            Field.object_id == field_object_id).all()
                for entity in entities:
                    if entity.content not in tags:
                        tags.append(entity.content)
                        new_tags.append(entity.content)
        return tags
Exemplo n.º 44
0
def login_post(request):  # tested
    next = request.params.get('next') or request.route_url('home')
    login = request.POST.get('login', '')
    password = request.POST.get('password', '')
    # print(login)
    log.debug(login)
    user = DBSession.query(User).filter_by(login=login).first()
    if user and user.check_password(password):
        client = Client(user_id=user.id)
        user.clients.append(client)
        DBSession.add(client)
        DBSession.flush()
        headers = remember(request, principal=client.id)
        response = Response()
        response.headers = headers
        locale_id = user.default_locale_id
        if not locale_id:
            locale_id = 1
        response.set_cookie(key='locale_id', value=str(locale_id))
        response.set_cookie(key='client_id', value=str(client.id))
        headers = remember(request, principal=client.id)
        # return HTTPFound(location=next, headers=response.headers)
        return HTTPOk(headers=response.headers, json_body={})
        # return {}
    return HTTPUnauthorized(location=request.route_url('login'))
Exemplo n.º 45
0
def signin(request):
    req = request.json_body
    login = req['login']
    password = req['password']
    # login = request.POST.get('login', '')
    # password = request.POST.get('password', '')

    user = DBSession.query(User).filter_by(login=login).first()
    if user and user.check_password(password):
        client = Client(user_id=user.id)
        user.clients.append(client)
        DBSession.add(client)
        DBSession.flush()
        headers = remember(request, principal=client.id)
        response = Response()
        response.headers = headers
        locale_id = user.default_locale_id
        if not locale_id:
            locale_id = 1
        response.set_cookie(key='locale_id', value=str(locale_id))
        response.set_cookie(key='client_id', value=str(client.id))
        result = dict()
        result['client_id'] = client.id
        request.response.status = HTTPOk.code
        # request.response.headers = headers
        # return response
        return HTTPOk(headers=response.headers, json_body=result)
        # return result
    return HTTPUnauthorized(location=request.route_url('login'))
Exemplo n.º 46
0
def view_group_entity(request):
    response = dict()
    client_id = request.matchdict.get('client_id')
    object_id = request.matchdict.get('object_id')

    # entity = DBSession.query(GroupingEntity).filter_by(client_id=client_id, object_id=object_id).first()
    entity = None
    if entity:
        if not entity.marked_for_deletion:
            ent = dict()
            ent['entity_type'] = entity.entity_type
            ent['tag'] = entity.content
            entities2 = DBSession.query(Entity).join(Entity.field).filter(
                Entity.content == entity.content,
                Field.field.data_type == 'Grouping Tag').all()
            # entities2 = list()
            objs = []
            for entry in entities2:
                obj = {
                    'client_id': entry.parent_client_id,
                    'object_id': entry.parent_object_id
                }
                if obj not in objs:
                    objs += [obj]
            ent['connections'] = objs
            response = ent
            request.response.status = HTTPOk.code
            return response
    request.response.status = HTTPNotFound.code
    return {'error': str("No entities in the system")}
Exemplo n.º 47
0
def view_field_from_object(request, field):
    response = dict()
    if field and not field.marked_for_deletion:
        response['client_id'] = field.client_id
        response['object_id'] = field.object_id
        response['created_at'] = field.created_at
        response[
            'data_type_translation_gist_client_id'] = field.data_type_translation_gist_client_id
        response[
            'data_type_translation_gist_object_id'] = field.data_type_translation_gist_object_id
        response['translation'] = field.get_translation(
            request.cookies['locale_id'])
        response['is_translatable'] = field.is_translatable
        response[
            'translation_gist_client_id'] = field.translation_gist_client_id
        response[
            'translation_gist_object_id'] = field.translation_gist_object_id
        atom = DBSession.query(TranslationAtom).filter_by(
            parent_client_id=field.data_type_translation_gist_client_id,
            parent_object_id=field.data_type_translation_gist_object_id,
            locale_id=int(request.cookies['locale_id'])).first()
        if atom:
            response['data_type'] = atom.content
        else:
            print('no atom content for ids',
                  field.data_type_translation_gist_client_id,
                  field.data_type_translation_gist_object_id)
        return response
    return {'error': 'no field'}
Exemplo n.º 48
0
def login_post(request):  # tested
    # next = request.params.get('next') or request.route_url('home')
    login = request.POST.get('login', '')
    password = request.POST.get('password', '')
    # print(login)
    log.debug(login)
    user = DBSession.query(User).filter_by(login=login).first()
    if user and user.check_password(password):
        client = Client(user_id=user.id)
        user.clients.append(client)
        DBSession.add(client)
        DBSession.flush()
        headers = remember(request, principal=client.id)
        response = Response()
        response.headers = headers
        locale_id = user.default_locale_id
        if not locale_id:
            locale_id = 1
        response.set_cookie(key='locale_id', value=str(locale_id))
        response.set_cookie(key='client_id', value=str(client.id))
        headers = remember(request, principal=client.id)
        # return HTTPFound(location=next, headers=response.headers)
        return HTTPOk(headers=response.headers, json_body={})
        # return {}
    return HTTPUnauthorized(location=request.route_url('login'))
def find_all_tags(lexical_entry, field_client_id, field_object_id):
    tag = None
    for entity in lexical_entry.entity:
        if entity.field.data_type == 'Grouping Tag':
            tag = entity.content
            break
    if not tag:
        return []
    else:
        tags = [tag]
        new_tags = [tag]
        while new_tags:
            lexical_entries = find_lexical_entries_by_tags(new_tags, field_client_id, field_object_id)
            new_tags = list()
            for lex in lexical_entries:
                entities = DBSession.query(Entity) \
                    .join(Entity.field) \
                    .join(Entity.publishingentity) \
                    .filter(Entity.parent == lex,
                            PublishingEntity.accepted == True,
                            Field.client_id == field_client_id,
                            Field.object_id == field_object_id).all()
                for entity in entities:
                    if entity.content not in tags:
                        tags.append(entity.content)
                        new_tags.append(entity.content)
        return tags
Exemplo n.º 50
0
def view_group_entity(request):
    response = dict()
    client_id = request.matchdict.get('client_id')
    object_id = request.matchdict.get('object_id')

    # entity = DBSession.query(GroupingEntity).filter_by(client_id=client_id, object_id=object_id).first()
    entity = None
    if entity:
        if not entity.marked_for_deletion:
            ent = dict()
            ent['entity_type'] = entity.entity_type
            ent['tag'] = entity.content
            entities2 = DBSession.query(Entity).join(Entity.field).filter(Entity.content == entity.content,
                                                                         Field.field.data_type == 'Grouping Tag').all()
            # entities2 = list()
            objs = []
            for entry in entities2:
                obj = {'client_id': entry.parent_client_id, 'object_id': entry.parent_object_id}
                if obj not in objs:
                    objs += [obj]
            ent['connections'] = objs
            response = ent
            request.response.status = HTTPOk.code
            return response
    request.response.status = HTTPNotFound.code
    return {'error': str("No entities in the system")}
Exemplo n.º 51
0
    def make_query(searchstring, perspectives):

        results_cursor = DBSession.query(LexicalEntry).join(Entity.parent) \
            .join(Entity.field).join(TranslationAtom,
                                     and_(Field.translation_gist_client_id == TranslationAtom.parent_client_id,
                                          Field.translation_gist_object_id == TranslationAtom.parent_object_id)) \
            .distinct(Entity.parent_client_id, Entity.parent_object_id)
        if perspectives:
            results_cursor = results_cursor.filter(
                tuple_(LexicalEntry.parent_client_id,
                       LexicalEntry.parent_object_id).in_(perspectives))
        if not searchstring['searchstring']:
            raise HTTPBadRequest
        search_parts = searchstring['searchstring'].split()
        search_expression = Entity.content.like('%' + search_parts[0] + '%')
        to_do_or = searchstring.get('search_by_or', True)

        for part in search_parts[1:]:
            search_expression = or_(search_expression,
                                    Entity.content.like('%' + part + '%'))
        if 'entity_type' in searchstring and searchstring['entity_type']:
            search_expression = and_(
                search_expression,
                TranslationAtom.content == searchstring['entity_type'],
                TranslationAtom.locale_id == 2)
        results_cursor = results_cursor.filter(search_expression)
        return results_cursor, to_do_or
Exemplo n.º 52
0
def view_connected_words(request):
    response = list()
    client_id = request.matchdict.get('client_id')
    object_id = request.matchdict.get('object_id')
    field_client_id=request.params.get('field_client_id')
    field_object_id=request.params.get('field_object_id')
    lexical_entry = DBSession.query(LexicalEntry).filter_by(client_id=client_id, object_id=object_id).first()
    if lexical_entry and not lexical_entry.marked_for_deletion:
        tags = find_all_tags(lexical_entry, field_client_id, field_object_id)
        lexes = find_lexical_entries_by_tags(tags, field_client_id, field_object_id)
        for lex in lexes:
            path = request.route_url('lexical_entry',  # todo: method in utils (or just use track)
                                     client_id=lex.client_id,
                                     object_id=lex.object_id)
            subreq = Request.blank(path)
            subreq.method = 'GET'
            subreq.headers = request.headers
            try:
                resp = request.invoke_subrequest(subreq)
                if resp.json not in response:
                    response.append(resp.json)
            except HTTPForbidden:
                pass
        request.response.status = HTTPOk.code
        return response

    request.response.status = HTTPNotFound.code
    return {'error': str("No such lexical entry in the system")}
Exemplo n.º 53
0
def view_lexical_entry(request):  # TODO: test
    response = dict()
    client_id = request.matchdict.get('client_id')
    object_id = request.matchdict.get('object_id')

    # entry = DBSession.query(LexicalEntry) \
    #     .options(joinedload('leveloneentity').joinedload('leveltwoentity').joinedload('publishleveltwoentity')) \
    #     .options(joinedload('leveloneentity').joinedload('publishleveloneentity')) \
    #     .options(joinedload('groupingentity').joinedload('publishgroupingentity')) \
    #     .options(joinedload('publishleveloneentity')) \
    #     .options(joinedload('publishleveltwoentity')) \
    #     .options(joinedload('publishgroupingentity')) \
    #     .filter_by(client_id=client_id, object_id=object_id).first()

    entry = DBSession.query(LexicalEntry) \
        .filter_by(client_id=client_id, object_id=object_id).first()
    if entry:
        if entry.moved_to:
            url = request.route_url('lexical_entry',
                                    client_id=entry.moved_to.split("/")[0],
                                    object_id=entry.moved_to.split("/")[1])
            subreq = Request.blank(url)
            subreq.method = 'GET'
            subreq.headers = request.headers
            return request.invoke_subrequest(subreq)
        else:
            if not entry.marked_for_deletion:
                response = entry.track(False, int(request.cookies.get('locale_id') or 2))
                # response['client_id'] = entry.client_id
                # response['object_id'] = entry.object_id
                request.response.status = HTTPOk.code
                return response
    request.response.status = HTTPNotFound.code
    return {'error': str("No such lexical entry in the system")}
Exemplo n.º 54
0
def group_by_organizations(dicts, request):
    dicts_with_users = []
    for dct in dicts:
        users = []
        for client in participated_clients_list(dct, request):
            user = DBSession.query(User).join(Client).filter_by(
                id=client).first()
            if user not in users:
                users += [user]
        dicts_with_users += [(dct.object_id, dct.client_id, users)]
    organizations = []
    for organization in DBSession.query(Organization).filter_by(
            marked_for_deletion=False).all():
        dictionaries = []
        for dct in dicts_with_users:
            for user in dct[2]:
                if user in organization.users:
                    dictionaries += [dct]
        path = request.route_url('organization',
                                 organization_id=organization.id)
        subreq = Request.blank(path)
        subreq.method = 'GET'
        subreq.headers = request.headers
        resp = request.invoke_subrequest(subreq)
        if 'error' not in resp.json:
            org = resp.json

            dictstemp = [{
                'client_id': o[1],
                'object_id': o[0]
            } for o in dictionaries]
            dictionaries = dicts
            if dictstemp:
                prevdicts = dictionaries\
                    .filter_by(client_id=dictstemp[0]['client_id'],
                               object_id=dictstemp[0]['object_id'])
                dictstemp.remove(dictstemp[0])
                for dicti in dictstemp:
                    prevdicts = prevdicts.subquery().select()
                    prevdicts = dictionaries.filter_by(client_id=dicti['client_id'], object_id=dicti['object_id'])\
                        .union_all(prevdicts)

                dictionaries = prevdicts

            org['dicts'] = dictionaries
            organizations += [org]
    return organizations
Exemplo n.º 55
0
def edit_organization(request):  # TODO: test
    try:
        response = dict()
        organization_id = request.matchdict.get('organization_id')
        organization = DBSession.query(Organization).filter_by(id=organization_id).first()

        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.")
        creator = DBSession.query(User).filter_by(id=client.user_id).first()
        if not creator:
            raise CommonException("This client id is orphaned. Try to logout and then login once more.")
        if organization:
            if not organization.marked_for_deletion:
                req = request.json_body
                if 'add_users' in req:
                    for user_id in req['add_users']:
                        user = DBSession.query(User).filter_by(id=user_id).first()
                        if user not in organization.users:
                            if not user in organization.users:
                                organization.users.append(user)
                            bases = DBSession.query(BaseGroup).filter_by(subject='organization')
                            for base in bases:
                                group = DBSession.query(Group).filter_by(base_group_id=base.id,
                                                                         subject_object_id=organization.id).first()
                                if not user in group.users:
                                    group.users.append(user)
                if 'delete_users' in req:
                    for user_id in req['delete_users']:
                        if user_id == creator.id:
                            raise CommonException("You shouldn't delete yourself")
                        user = DBSession.query(User).filter_by(id=user_id).first()
                        if user in organization.users:
                            organization.users.remove(user)
                            bases = DBSession.query(BaseGroup).filter_by(subject='organization')
                            for base in bases:
                                group = DBSession.query(Group).filter_by(base_group_id=base.id,
                                                                         subject_object_id=organization.id).first()
                                group.users.remove(user)
                if 'name' in req:
                    organization.name = req['name']
                if 'about' in req:
                    organization.about = req['about']
                request.response.status = HTTPOk.code
                return response

        request.response.status = HTTPNotFound.code
        return {'error': str("No such organization in the system")}
    except KeyError as e:
        request.response.status = HTTPBadRequest.code
        return {'error': str(e)}
Exemplo n.º 56
0
def translation_service_search_all(searchstring):
    translationatom = DBSession.query(TranslationAtom)\
        .join(TranslationGist).\
        filter(TranslationAtom.content == searchstring,
               TranslationAtom.locale_id == 2)\
        .one()
    response = translationgist_contents(translationatom.parent)
    return response
Exemplo n.º 57
0
def diff_server(request):
    existing = [row2dict(entry) for entry in DBSession.query(ObjectTOC)]
    req = request.json_body
    upload = list()
    for entry in req:
        if entry not in existing:
            upload.append(entry)
    return upload
Exemplo n.º 58
0
def group_by_languages(dicts, request):
    langs = DBSession.query(Language).filter_by(marked_for_deletion=False, parent=None).all()
    languages = []
    for lang in langs:
        la = language_with_dicts(lang, dicts, request)
        if la:
            languages += [la]
    return languages
def get_translation(translation_gist_client_id, translation_gist_object_id, locale_id):
    log = logging.getLogger(__name__)
    log.setLevel(logging.DEBUG)
    translation = DBSession.query(TranslationAtom).filter_by(parent_client_id=translation_gist_client_id,
                                                             parent_object_id=translation_gist_object_id,
                                                             locale_id=locale_id).first()
    DBSession.flush()
    return translation.content