Exemplo n.º 1
0
def download_all(request):
    import requests
    import transaction
    from pyramid.request import Request
    print('locking client')
    log.error('locking client')
    DBSession.execute("LOCK TABLE client IN EXCLUSIVE MODE;")
    client = DBSession.query(Client).filter_by(id=authenticated_userid(request)).first()
    if not client:
        request.response.status = HTTPNotFound.code
        return {'error': str("Try to login again")}
    path = request.route_url('check_version')
    subreq = Request.blank(path)
    subreq.method = 'GET'
    subreq.headers = request.headers
    resp = request.invoke_subrequest(subreq)
    if resp.status_code != 200:
        request.response.status = HTTPInternalServerError.code
        return {'error': 'network error'}

    path = request.route_url('basic_sync')
    subreq = Request.blank(path)
    subreq.method = 'POST'
    subreq.headers = request.headers
    resp = request.invoke_subrequest(subreq)
    if resp.status_code != 200:
        request.response.status = HTTPInternalServerError.code
        return {'error': 'network error'}

    path = request.route_url('diff_desk')
    subreq = Request.blank(path)
    subreq.method = 'POST'
    subreq.headers = request.headers
    resp = request.invoke_subrequest(subreq)
    if resp.status_code != 200:
        request.response.status = HTTPInternalServerError.code
        return {'error': 'network error'}

    for dict_obj in DBSession.query(Dictionary).all():
        path = request.route_url('download_dictionary')
        subreq = Request.blank(path)
        subreq.method = 'POST'
        subreq.headers = request.headers
        subreq.json = {"client_id":dict_obj.client_id,
                                                       "object_id":dict_obj.object_id}
        resp = request.invoke_subrequest(subreq)
        if resp.status_code != 200:
            request.response.status = HTTPInternalServerError.code
            return {'error': 'network error'}

    path = request.route_url('new_client')
    subreq = Request.blank(path)
    subreq.method = 'POST'
    subreq.headers = request.headers
    resp = request.invoke_subrequest(subreq)
    if resp.status_code != 200:
        request.response.status = HTTPInternalServerError.code
        return {'error': 'network error'}

    request.response.status = HTTPOk.code
    return HTTPOk(json_body={})
Exemplo n.º 2
0
def download_all(request):
    import requests
    import transaction
    from pyramid.request import Request
    print('locking client')
    log.error('locking client')
    DBSession.execute("LOCK TABLE client IN EXCLUSIVE MODE;")
    client = DBSession.query(Client).filter_by(id=authenticated_userid(request)).first()
    if not client:
        request.response.status = HTTPNotFound.code
        return {'error': str("Try to login again")}
    path = request.route_url('check_version')
    subreq = Request.blank(path)
    subreq.method = 'GET'
    subreq.headers = request.headers
    resp = request.invoke_subrequest(subreq)
    if resp.status_code != 200:
        request.response.status = HTTPInternalServerError.code
        return {'error': 'network error'}

    path = request.route_url('basic_sync')
    subreq = Request.blank(path)
    subreq.method = 'POST'
    subreq.headers = request.headers
    resp = request.invoke_subrequest(subreq)
    if resp.status_code != 200:
        request.response.status = HTTPInternalServerError.code
        return {'error': 'network error'}

    path = request.route_url('diff_desk')
    subreq = Request.blank(path)
    subreq.method = 'POST'
    subreq.headers = request.headers
    resp = request.invoke_subrequest(subreq)
    if resp.status_code != 200:
        request.response.status = HTTPInternalServerError.code
        return {'error': 'network error'}

    for dict_obj in DBSession.query(Dictionary).all():
        path = request.route_url('download_dictionary')
        subreq = Request.blank(path)
        subreq.method = 'POST'
        subreq.headers = request.headers
        subreq.json = {"client_id":dict_obj.client_id,
                                                       "object_id":dict_obj.object_id}
        resp = request.invoke_subrequest(subreq)
        if resp.status_code != 200:
            request.response.status = HTTPInternalServerError.code
            return {'error': 'network error'}

    path = request.route_url('new_client')
    subreq = Request.blank(path)
    subreq.method = 'POST'
    subreq.headers = request.headers
    resp = request.invoke_subrequest(subreq)
    if resp.status_code != 200:
        request.response.status = HTTPInternalServerError.code
        return {'error': 'network error'}

    request.response.status = HTTPOk.code
    return HTTPOk(json_body={})
Exemplo n.º 3
0
def basic_sync(request):
    import requests
    import transaction

    return_date_time = lambda r: {key: datetime.datetime.fromtimestamp(r[key]) if key == 'created_at' else r[key] for
                                  key in r}
    settings = request.registry.settings
    existing = basic_tables_content()
    path = settings['desktop']['central_server'] + 'sync/basic/server'
    with open('authentication_data.json', 'r') as f:
        cookies = json.loads(f.read())
    session = requests.Session()
    session.headers.update({'Connection': 'Keep-Alive'})
    adapter = requests.adapters.HTTPAdapter(pool_connections=1, pool_maxsize=1, max_retries=10)
    session.mount('http://', adapter)
    status = session.get(path, cookies=cookies)
    server = status.json()
    new_entries = list()
    old_langs = dict()
    langs = list()
    for table in [Locale, User, Client, BaseGroup, TranslationGist, TranslationAtom, Field, Group, Language]:
        curr_server = server[table.__tablename__]
        curr_existing = existing[table.__tablename__]
        curr_old = list()
        if hasattr(table, 'id'):
            for key in curr_server:
                if key in curr_existing:
                    if curr_server[key] != curr_existing[key]:
                        kwargs = return_date_time(curr_server[key])
                        curr_old.append(kwargs)
                else:
                    kwargs = return_date_time(curr_server[key])
                    if table != Language:
                        new_entries.append(table(**kwargs))
                    else:
                        langs.append(table(**kwargs))
        else:
            for client_id in curr_server:
                if client_id in curr_existing:
                    for object_id in curr_server[client_id]:
                        if object_id in curr_existing[client_id]:
                            if curr_server[client_id][object_id] != curr_existing[client_id][object_id]:
                                kwargs = return_date_time(curr_server[client_id][object_id])
                                curr_old.append(kwargs)
                        else:
                            kwargs = return_date_time(curr_server[client_id][object_id])
                            if table != Language:
                                new_entries.append(table(**kwargs))
                            else:
                                langs.append(table(**kwargs))

                else:
                    for object_id in curr_server[client_id]:
                        kwargs = return_date_time(curr_server[client_id][object_id])
                        if table != Language:
                            new_entries.append(table(**kwargs))
                        else:
                            langs.append(table(**kwargs))

        if table != Language:
            all_entries = DBSession.query(table).all()
            if hasattr(table, 'client_id'):
                    for entry in all_entries:
                        client_id = str(entry.client_id)
                        object_id = str(entry.object_id)
                        if client_id in curr_server and object_id in curr_server[client_id]:
                            for key, value in list(return_date_time(curr_server[client_id][object_id]).items()):
                                setattr(entry, key, value)
            else:
                for entry in all_entries:
                    id = str(entry.id)
                    if id in curr_server:
                        for key, value in list(return_date_time(curr_server[id]).items()):
                            if key != 'counter' and table != User:
                                setattr(entry, key, value)
            new_entries.extend(all_entries)
        else:
            old_langs = curr_server
    DBSession.flush()
    parent_langs_ids = DBSession.query(Language.client_id, Language.object_id).all()
    parent_langs = [lang for lang in langs if not lang.parent_client_id]
    parent_langs_ids.extend([(lang.client_id, lang.object_id) for lang in langs if not lang.parent_client_id])
    new_langs = [lang for lang in langs if (lang.client_id, lang.object_id) not in parent_langs_ids]
    while new_langs:
        parent_langs.extend([lang for lang in langs if (
        lang.client_id, lang.object_id) not in parent_langs_ids and (
        lang.parent_client_id, lang.parent_object_id) in parent_langs_ids])
        parent_langs_ids.extend([(lang.client_id, lang.object_id) for lang in langs if (
        lang.client_id, lang.object_id) not in parent_langs_ids and (
        lang.parent_client_id, lang.parent_object_id) in parent_langs_ids])
        new_langs = [lang for lang in langs if (lang.client_id, lang.object_id) not in parent_langs_ids]
    new_entries.extend(parent_langs)
    for entry in DBSession.query(Language).all():
        client_id = str(entry.client_id)
        object_id = str(entry.object_id)
        if client_id in curr_server and object_id in old_langs[client_id]:
                for key, value in list(return_date_time(curr_server[client_id][object_id]).items()):
                    setattr(entry, key, value)
    DBSession.bulk_save_objects(new_entries)
    # client = DBSession.query(Client).filter_by(id=authenticated_userid(request)).first()
    # if not client:
    #     request.response.status = HTTPNotFound.code
    #     return {'error': str("Try to login again")}
    # user = DBSession.query(User).filter_by(id=client.user_id).first()
    # if not user:
    #     request.response.status = HTTPNotFound.code
    #     return {'error': str("Try to login again")}

    for entry in server['user_to_group_association']:
        if not DBSession.query(user_to_group_association).filter_by(user_id=entry[0], group_id=entry[1]).first():
            insertion = user_to_group_association.insert().values(user_id=entry[0], group_id=entry[1])
            DBSession.execute(insertion)
    request.response.status = HTTPOk.code
    return HTTPOk(json_body={})
Exemplo n.º 4
0
def basic_sync(request):
    import requests
    import transaction

    return_date_time = lambda r: {key: datetime.datetime.fromtimestamp(r[key]) if key == 'created_at' else r[key] for
                                  key in r}
    settings = request.registry.settings
    existing = basic_tables_content()
    path = settings['desktop']['central_server'] + 'sync/basic/server'
    with open('authentication_data.json', 'r') as f:
        cookies = json.loads(f.read())
    session = requests.Session()
    session.headers.update({'Connection': 'Keep-Alive'})
    adapter = requests.adapters.HTTPAdapter(pool_connections=1, pool_maxsize=1, max_retries=10)
    session.mount('http://', adapter)
    status = session.get(path, cookies=cookies)
    server = status.json()
    new_entries = list()
    langs = list()
    for table in [Locale, User, Client, BaseGroup, TranslationGist, TranslationAtom, Field, Group, Language]:
        curr_server = server[table.__tablename__]
        curr_existing = existing[table.__tablename__]
        curr_old = list()
        if hasattr(table, 'id'):
            for key in curr_server:
                if key in curr_existing:
                    if curr_server[key] != curr_existing[key]:
                        kwargs = return_date_time(curr_server[key])
                        curr_old.append(kwargs)
                else:
                    kwargs = return_date_time(curr_server[key])
                    if table != Language:
                        new_entries.append(table(**kwargs))
                    else:
                        langs.append(table(**kwargs))
        else:
            for client_id in curr_server:
                if client_id in curr_existing:
                    for object_id in curr_server[client_id]:
                        if object_id in curr_existing[client_id]:
                            if curr_server[client_id][object_id] != curr_existing[client_id][object_id]:
                                kwargs = return_date_time(curr_server[client_id][object_id])
                                curr_old.append(kwargs)
                        else:
                            kwargs = return_date_time(curr_server[client_id][object_id])
                            if table != Language:
                                new_entries.append(table(**kwargs))
                            else:
                                langs.append(table(**kwargs))

                else:
                    for object_id in curr_server[client_id]:
                        kwargs = return_date_time(curr_server[client_id][object_id])
                        if table != Language:
                            new_entries.append(table(**kwargs))
                        else:
                            langs.append(table(**kwargs))

        all_entries = DBSession.query(table).all()
        if hasattr(table, 'client_id'):
            for entry in all_entries:
                client_id = str(entry.client_id)
                object_id = str(entry.object_id)
                if client_id in curr_server:
                    if object_id in curr_server[client_id]:
                        for key, value in list(return_date_time(curr_server[client_id][object_id]).items()):
                            setattr(entry, key, value)
        else:
            for entry in all_entries:
                id = str(entry.id)
                if id in curr_server:
                    for key, value in list(return_date_time(curr_server[id]).items()):
                        if key != 'counter' and table != User:
                            setattr(entry, key, value)
        new_entries.extend(all_entries)

    parent_langs_ids = DBSession.query(Language.client_id, Language.object_id).all()
    parent_langs = [lang for lang in langs if not lang.parent_client_id]
    parent_langs_ids.extend([(lang.client_id, lang.object_id) for lang in langs if not lang.parent_client_id])
    new_langs = [lang for lang in langs if (lang.client_id, lang.object_id) not in parent_langs_ids]
    while new_langs:
        parent_langs.extend([lang for lang in langs if (
        lang.client_id, lang.object_id) not in parent_langs_ids and (
        lang.parent_client_id, lang.parent_object_id) in parent_langs_ids])
        parent_langs_ids.extend([(lang.client_id, lang.object_id) for lang in langs if (
        lang.client_id, lang.object_id) not in parent_langs_ids and (
        lang.parent_client_id, lang.parent_object_id) in parent_langs_ids])
        new_langs = [lang for lang in langs if (lang.client_id, lang.object_id) not in parent_langs_ids]
    new_entries.extend(parent_langs)
    DBSession.bulk_save_objects(new_entries)
    # client = DBSession.query(Client).filter_by(id=authenticated_userid(request)).first()
    # if not client:
    #     request.response.status = HTTPNotFound.code
    #     return {'error': str("Try to login again")}
    # user = DBSession.query(User).filter_by(id=client.user_id).first()
    # if not user:
    #     request.response.status = HTTPNotFound.code
    #     return {'error': str("Try to login again")}

    for entry in server['user_to_group_association']:
        if not DBSession.query(user_to_group_association).filter_by(user_id=entry[0], group_id=entry[1]).first():
            insertion = user_to_group_association.insert().values(user_id=entry[0], group_id=entry[1])
            DBSession.execute(insertion)
    request.response.status = HTTPOk.code
    return HTTPOk(json_body={})