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)}
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")}
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'))
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'))
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
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 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)}
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 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
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"})
def create_nested_field(field, perspective, client_id, upper_level, link_ids, position): field_object = DictionaryPerspectiveToField( client_id=client_id, parent=perspective, field_client_id=field['client_id'], field_object_id=field['object_id'], upper_level=upper_level, position=position) if field.get('link'): field_object.link_client_id = field['link']['client_id'] field_object.link_object_id = field['link']['object_id'] DBSession.flush() contains = field.get('contains', None) if contains: inner_position = 1 for subfield in contains: create_nested_field(subfield, perspective, client_id, upper_level=field_object, link_ids=link_ids, position=inner_position) inner_position += 1 return
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)}
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')
def setUp(self): self.config = testing.setUp() from sqlalchemy import create_engine engine = create_engine('sqlite://') from lingvodoc.models import (Base) DBSession.configure(bind=engine) Base.metadata.create_all(engine)
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 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'))
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"})
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")}
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
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'))
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
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
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
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'}
def setUp(self): self.config = testing.setUp() from sqlalchemy import create_engine engine = create_engine('sqlite://') from lingvodoc.models import ( Base ) DBSession.configure(bind=engine) Base.metadata.create_all(engine)
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
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
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
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 setUp(self): self.config = testing.setUp() from sqlalchemy import create_engine engine = create_engine('sqlite://') from lingvodoc.models import (Base, Language) DBSession.configure(bind=engine) Base.metadata.create_all(engine) with transaction.manager: new_lang = Language(client_id=1, object_id=1, translation_string='test') DBSession.add(new_lang)
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)
def setUp(self): self.config = testing.setUp() self.config.testing_securitypolicy(userid='1', permissive=True) import webtest from pyramid import paster from sqlalchemy import create_engine engine = create_engine('sqlite://') myapp = paster.get_app('testing.ini') self.app = webtest.TestApp(myapp) from lingvodoc.models import (Base) DBSession.configure(bind=engine) Base.metadata.create_all(engine)
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)}
def setUp(self): self.config = testing.setUp() from sqlalchemy import create_engine engine = create_engine('sqlite://') from lingvodoc.models import ( Base, Language ) DBSession.configure(bind=engine) Base.metadata.create_all(engine) with transaction.manager: new_lang = Language(client_id=1, object_id=1, translation_string='test') DBSession.add(new_lang)
def convert_all(blob_client_id, blob_object_id, language_client_id, language_object_id, user_id, gist_client_id, gist_object_id, sqlalchemy_url, storage): log = logging.getLogger(__name__) log.setLevel(logging.DEBUG) try: engine = create_engine(sqlalchemy_url) DBSession.configure(bind=engine) status = convert_db_new( blob_client_id, blob_object_id, language_client_id, language_object_id, user_id, gist_client_id, gist_object_id, storage) except Exception as e: log.error("Converting failed") log.error(e.__traceback__) raise log.debug(status) log.debug('we are the champions') return status
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')
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
def upload_audio(sound_ids, ids_map, fields_dict, sound_and_markup_cursor, audio_hashes, markup_hashes, folder_name, user_id, is_a_regular_form, client, storage): log = logging.getLogger(__name__) sound_field = "Sound" markup_field = "Markup" if "Sounds of Paradigmatic forms" in fields_dict: sound_field = "Sounds of Paradigmatic forms" if "Paradigm Markup" in fields_dict: markup_field = "Paradigm Markup" markup__without_audio_sequence = [] audio_sequence = [] for cursor in sound_and_markup_cursor: blob_id = cursor[0] description_type = int(cursor[5]) if description_type == 1: audio = cursor[2] markup = cursor[1] common_name = str(cursor[3]) word_id = cursor[4] if word_id in sound_ids: continue sound_ids.add(word_id) audio_hash = hashlib.sha224(audio).hexdigest() if audio_hash not in audio_hashes: ###filename = common_name + ".wav" if common_name: fname = os.path.splitext(common_name)[0] fname = fname.replace(".", "_") filename = "%s.wav" % fname else: filename = 'noname.noext' audio_hashes.add(audio_hash) audio_sequence.append((ids_map[int(word_id)][0], ids_map[int(word_id)][1], fields_dict[sound_field][0], fields_dict[sound_field][1], None, client, filename, audio)) lvl = create_entity(ids_map[int(word_id)][0], ids_map[int(word_id)][1], fields_dict[sound_field][0], fields_dict[sound_field][1], None, client, filename=filename, content=base64.urlsafe_b64encode(audio).decode(), folder_name=folder_name, storage=storage) if len(markup__without_audio_sequence) > 50: DBSession.flush() if len(audio_sequence) > 50: DBSession.flush() audio_sequence = [] if len(markup__without_audio_sequence) > 50: DBSession.flush() if len(audio_sequence) != 0: DBSession.flush() audio_sequence = [] if len(markup__without_audio_sequence) != 0: DBSession.flush()
def convert_all(dictionary_client_id, dictionary_object_id, user_id, client_id, object_id, sqlalchemy_url, storage, eaf_url, sound_url=None): engine = create_engine(sqlalchemy_url) DBSession.configure(bind=engine) convert_five_tiers(dictionary_client_id, dictionary_object_id, user_id, client_id, object_id, sqlalchemy_url, storage, eaf_url, sound_url) DBSession.flush()
def setUp(self): self.config = testing.setUp() self.config.testing_securitypolicy(userid='1', permissive=True) import webtest from pyramid import paster from sqlalchemy import create_engine engine = create_engine('sqlite://') myapp = paster.get_app('testing.ini') self.app = webtest.TestApp(myapp) from lingvodoc.models import ( Base ) DBSession.configure(bind=engine) Base.metadata.create_all(engine)
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)}
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
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'}
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
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
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")}
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")}
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
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
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 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 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)}
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
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