def add_from_file(filename): ''' Добавить данные в таблицу таксонов из файла filename (разделители - табуляция). Файл filename в формате csv, колонки: id parent_id old_id taxon_type name russian_name author source ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') row = reader.next() # пропускаем заголовки records = [line for line in reader] for row in records: id, parent_id, old_id, taxon_type, name, russian_name, author, source =\ [None if x == '' else x for x in row] taxon = Taxon(parent_id=parent_id, old_id=old_id, taxon_type=taxon_type, name=name, russian_name=russian_name, author=author, source=source) dbsession.add(taxon)
def add_from_file(filename): """ Добавить данные в таблицу таксонов из файла filename (разделители - табуляция). Файл filename в формате csv, колонки: id parent_id old_id taxon_type name russian_name author source """ import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter="\t") row = reader.next() # пропускаем заголовки records = [line for line in reader] for row in records: id, parent_id, old_id, taxon_type, name, russian_name, author, source = [ None if x == "" else x for x in row ] taxon = Taxon( parent_id=parent_id, old_id=old_id, taxon_type=taxon_type, name=name, russian_name=russian_name, author=author, source=source, ) dbsession.add(taxon)
def add_from_file(filename): ''' Добавить данные в таблицу из файла с разделителями filename. Файл filename в формате csv, колонки: id name fullname speciality degree organization position email phone address ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') row = reader.next() # пропускаем заголовки records = [line for line in reader] for (id, name, fullname, speciality, degree, organization, position, email, phone, address) in records: person = Person( name=name, fullname=fullname, speciality=speciality, degree=degree, organization=organization, position=position, email=email, phone=phone, address=address ) dbsession.add(person)
def add_from_file(associations_filename, shp_filename): ''' Добавить данные из shp-файла shp_filename. Первое поле аттрибутивной таблицы--идентификатор. Одновременно добавляются в таблицу связи данные из файла с разделителями associations_filename. Файл filename в формате csv (разделитель табуляция), колонки: square_id key_area_id ''' import transaction with transaction.manager: dbsession = DBSession() ogrData = ogr.Open(shp_filename) layer = ogrData.GetLayer(0) sq = layer.GetNextFeature() while sq is not None: id = sq.GetFieldAsString(0) geom = sq.GetGeometryRef() geom = geom.ExportToWkt() square = Squares(id=id, geom=WKTSpatialElement(geom, srid=3857)) dbsession.add(square) sq = layer.GetNextFeature() dbsession.flush() reader = csv.reader(open(associations_filename), delimiter='\t') reader.next() records = [line for line in reader] for id, key_area_id in records: # Определим ключевоq уч-к по его id key_a = dbsession.query(Key_area).filter_by(id=key_area_id).one() # Определим полигон по его id square = dbsession.query(Squares).filter_by(id=id).one() square.key_areas.append(key_a)
def add_from_file(filename): ''' Добавить данные в таблицу из файла с разделителями filename. Файл filename в формате csv, колонки: id name fullname speciality degree organization position email phone address ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') row = reader.next() # пропускаем заголовки records = [line for line in reader] for (id, name, fullname, speciality, degree, organization, position, email, phone, address) in records: person = Person(name=name, fullname=fullname, speciality=speciality, degree=degree, organization=organization, position=position, email=email, phone=phone, address=address) dbsession.add(person)
def table_item_save(request): person_id = None if ('person_id' in request.POST) and request.POST['person_id'].isdigit(): person_id = int(request.POST['person_id']) user_login = request.POST[ 'user_login'] if 'user_login' in request.POST else None if not user_login: raise HTTPBadRequest('"user_login" is required parameter') if not person_id: users = DBSession.query(User).filter(User.login == user_login).all() if len(users) > 0: return { 'Result': 'Error', 'Message': u'Такой логин уже присутствует в системе' } with transaction.manager: if person_id: person = DBSession.query(Person) \ .options(joinedload('user')) \ .filter(Person.id == person_id) \ .all()[0] user = person.user else: person = Person() DBSession.add(person) user = User() DBSession.add(user) person.user = user for attr in request.POST: table_name, field = attr.split('_') if field == 'id': continue if table_name == 'person': setattr(person, field, request.POST[attr]) if table_name == 'user': setattr(user, field, request.POST[attr]) if 'user_active' in request.POST and request.POST['user_active']: user.active = True else: user.active = False if 'user_password' in request.POST and request.POST['user_password']: user.password = User.password_hash(request.POST['user_password']) DBSession.flush() DBSession.refresh(user) DBSession.refresh(person) person_json = person.as_json_dict('person_') user_json = user.as_json_dict('user_') item_json = person_json.copy() item_json.update(user_json) return {'Result': 'OK', 'Record': item_json}
def new_card(request): with transaction.manager: dbsession = DBSession() card = Cards() _update_card_attributes(card, dict(request.POST)) card.added_date = datetime.now() card.edited_date = card.added_date dbsession.add(card) return {}
def table_item_save(request): session = DBSession() session.expire_on_commit = False if ('person_id' in request.POST) and request.POST['person_id'].isdigit(): person_id = int(request.POST['person_id']) person = session.query(Person) \ .options(joinedload('user')) \ .filter(Person.id == person_id) \ .all()[0] user = person.user else: person = Person() user = User() session.add(user) person.user = user for attr in request.POST: table_name, field = attr.split('_') if field == 'id': continue if table_name == 'person': setattr(person, field, request.POST[attr]) if table_name == 'user': setattr(user, field, request.POST[attr]) if 'user_active' in request.POST and request.POST['user_active']: user.active = True else: user.active = False if 'user_password' in request.POST and request.POST['user_password']: user.password = User.password_hash(request.POST['user_password']) session.add(person) try: transaction.commit() except IntegrityError: transaction.abort() return { 'Result': 'Error', 'Message': u'Такой логин уже присутствует в системе' } person_json = person.as_json_dict('person_') user_json = user.as_json_dict('user_') item_json = person_json.copy() item_json.update(user_json) session.close() return { 'Result': 'OK', 'Record': item_json }
def put(self): new_synonym_dict = dict(self.request.POST) with transaction.manager: dbsession = DBSession() synonym = Synonym() for k, v in new_synonym_dict.items(): if v == '': v = None if hasattr(synonym, k): setattr(synonym, k, v) synonym.species_id = int(self.request.matchdict['taxon_id']) dbsession.add(synonym)
def put(self): new_synonym_dict = dict(self.request.POST) with transaction.manager: dbsession = DBSession() synonym = Synonym() for k, v in new_synonym_dict.items(): if v == '': v = None if hasattr(synonym, k): setattr(synonym, k, v) synonym.species_id = int(self.request.matchdict['taxon_id']) dbsession.add(synonym)
def add_from_file(filename): ''' Добавить данные в таблицу из файла с разделителями filename. Файл filename в формате csv, колонки: id doc_type_id filename fullname author magazine pages mammals birds reptiles amphibians fish invertebrates vascular bryophytes lichens fungi maps ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') reader.next() records = [line for line in reader] for row in records: ( id, doc_type_id, filename, fullname, author, magazine, pages, mammals, birds, reptiles, amphibians, fish, invertebrates, vascular, bryophytes, lichens, fungi, maps ) = [None if x == '' else x for x in row] infores = Inforesources( doc_type_id=doc_type_id, filename=filename, fullname=fullname, author=author, magazine=magazine, pages=pages, mammals=mammals, birds=birds, reptiles=reptiles, amphibians=amphibians, fish=fish, invertebrates=invertebrates, vascular=vascular, bryophytes=bryophytes, lichens=lichens, fungi=fungi, maps=maps ) dbsession.add(infores)
def import_from_csv(path_to_file): with transaction.manager: session = DBSession() reader = csv.reader(open(path_to_file), delimiter='\t') reader.next() # пропускаем заголовки records = [line for line in reader] for row in records: card_id, image_id = [None if x == '' else x for x in row] card_image = CardsImages(card_id=card_id, image_id=image_id) session.add(card_image)
def import_from_csv(path_to_file): with transaction.manager: session = DBSession() reader = csv.reader(open(path_to_file), delimiter='\t') reader.next() # пропускаем заголовки records = [line for line in reader] for row in records: id_image, name, description, url, local, size = [None if x == '' else x for x in row] image = Images(name=name, description=description, url=url, local=local, size=size) session.add(image)
def upload_image(request): filename = request.POST['file'].filename input_file = request.POST['file'].file obj_id = request.matchdict['id'] obj_type = request.matchdict['type'] path_to_images = os.path.join(os.path.dirname(nextgisbio.__file__), 'static/data/images') date_now = datetime.datetime.now().strftime('%Y-%m-%d') path_to_images_now = os.path.join(path_to_images, date_now) if not os.path.exists(path_to_images_now): os.mkdir(path_to_images_now) # from http://stackoverflow.com/questions/2782229/most-lightweight-way-to-create-a-random-string-and-a-random-hexadecimal-number random_file_name = str(uuid.uuid4()) base_file_path = os.path.join(path_to_images_now, '.'.join([random_file_name, 'jpg'])) with open(base_file_path, 'wb') as output_file: shutil.copyfileobj(input_file, output_file) for key_size in THUMBNAIL_SIZES: try: im = Image.open(base_file_path) im.thumbnail(THUMBNAIL_SIZES[key_size], Image.BICUBIC) im.save(os.path.join( path_to_images_now, '.'.join([random_file_name + '_' + key_size, 'jpg'])), 'JPEG', quality=70) except IOError: print "cannot create thumbnail for '%s'" % base_file_path with transaction.manager: dbSession = DBSession() image = Images() image.name = filename image.url = '/static/data/images/%s/%s.jpg' % (date_now, random_file_name) image.size = os.path.getsize(base_file_path) image.local = base_file_path dbSession.add(image) if obj_type == 'card': card_image = CardsImages() card_image.image = image card_image.card = dbSession.query(Cards).filter_by(id=obj_id).one() dbSession.add(card_image) photo_json = image.as_json_dict() return photo_json
def import_from_csv(path_to_file): with transaction.manager: session = DBSession() reader = csv.reader(open(path_to_file), delimiter='\t') reader.next() # пропускаем заголовки records = [line for line in reader] for row in records: card_id, image_id = [None if x == '' else x for x in row] card_image = CardsImages(card_id=card_id, image_id=image_id) session.add(card_image)
def create_taxon(request): new_data = dict(request.POST) dbsession = DBSession() taxon = Taxon() for k, v in new_data.items(): if v == '': v = None if hasattr(taxon, k): setattr(taxon, k, v) dbsession.add(taxon) dbsession.flush() dbsession.refresh(taxon) return {'item': taxon.as_json_dict()}
def create_taxon(request): new_data = dict(request.POST) dbsession = DBSession() taxon = Taxon() for k, v in new_data.items(): if v == '': v = None if hasattr(taxon, k): setattr(taxon, k, v) dbsession.add(taxon) dbsession.flush() dbsession.refresh(taxon) return {'item': taxon.as_json_dict()}
def gen_sql(records): with transaction.manager: for r in records: id = None for t in 'Kingdom', 'Phylum', 'Class', 'Order', 'Family', 'Genus', 'Species': try: print t, r[t] taxon = DBSession.query(Taxon).filter_by(taxon_type = t, name = r[t], parent_id = id).one() print taxon except NoResultFound: taxon = Taxon(taxon_type = t, name = r[t], parent_id = id, author=r[t+'_author'], source=r[t+'_source']) DBSession.add(taxon) DBSession.flush() id = taxon.id print taxon.id, taxon.taxon_type, taxon.name, taxon.parent_id
def add_from_file(filename): ''' Добавить данные в таблицу из файла с разделителями filename. Файл filename в формате csv, колонки: idDocType,docType ''' dbsession = DBSession() reader = csv.reader(open(filename), delimiter=',') reader.next() records = [line for line in reader] for id, dt in records: dtp = Doc_type(doc_type=dt) dbsession.add(dtp) dbsession.flush()
def new_anlist(request): new_data = dict(request.POST) success = True try: import transaction with transaction.manager: dbsession = DBSession() anlist = Annotation() for k,v in new_data.items(): if v == '': v = None if hasattr(anlist, k): setattr(anlist, k, v) dbsession.add(anlist) except: success = False return {'success': success}
def new_anlist(request): new_data = dict(request.POST) success = True try: import transaction with transaction.manager: dbsession = DBSession() anlist = Annotation() for k,v in new_data.items(): if v == '': v = None if hasattr(anlist, k): setattr(anlist, k, v) dbsession.add(anlist) except: success = False return {'success': success}
def add_from_file(filename): ''' Добавить данные в таблицу из файла с разделителями filename. Файл filename в формате csv, колонки: idDocType,docType ''' dbsession = DBSession() reader = csv.reader(open(filename), delimiter=',') reader.next() records = [line for line in reader] for id, dt in records: dtp = Doc_type(doc_type=dt) dbsession.add(dtp) dbsession.flush()
def upload_image(request): filename = request.POST['file'].filename input_file = request.POST['file'].file obj_id = request.matchdict['id'] obj_type = request.matchdict['type'] path_to_images = os.path.join(os.path.dirname(nextgisbio.__file__), 'static/data/images') date_now = datetime.datetime.now().strftime('%Y-%m-%d') path_to_images_now = os.path.join(path_to_images, date_now) if not os.path.exists(path_to_images_now): os.mkdir(path_to_images_now) # from http://stackoverflow.com/questions/2782229/most-lightweight-way-to-create-a-random-string-and-a-random-hexadecimal-number random_file_name = str(uuid.uuid4()) base_file_path = os.path.join(path_to_images_now, '.'.join([random_file_name, 'jpg'])) with open(base_file_path, 'wb') as output_file: shutil.copyfileobj(input_file, output_file) for key_size in THUMBNAIL_SIZES: try: im = Image.open(base_file_path) im.thumbnail(THUMBNAIL_SIZES[key_size], Image.BICUBIC) im.save(os.path.join(path_to_images_now, '.'.join([random_file_name + '_' + key_size, 'jpg'])), 'JPEG', quality=70) except IOError: print "cannot create thumbnail for '%s'" % base_file_path with transaction.manager: dbSession = DBSession() image = Images() image.name = filename image.url = '/static/data/images/%s/%s.jpg' % (date_now, random_file_name) image.size = os.path.getsize(base_file_path) image.local = base_file_path dbSession.add(image) if obj_type == 'card': card_image = CardsImages() card_image.image = image card_image.card = dbSession.query(Cards).filter_by(id=obj_id).one() dbSession.add(card_image) photo_json = image.as_json_dict() return photo_json
def add_from_file(filename): ''' Добавить данные в таблицу из файла с разделителями filename. Файл filename в формате csv, колонки: id pheno description org_type ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') reader.next() records = [line for line in reader] for id, p, desc, t in records: ph = Pheno(pheno=p, description=desc, org_type=t) dbsession.add(ph)
def add_from_file(filename): ''' Добавить данные в таблицу из файла с разделителями filename. Файл filename в формате csv, колонки: id footprint org_type description ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') reader.next() records = [line for line in reader] for id, fpr, t, desc in records: fp = Footprint(footprint=fpr, description=desc, org_type=t) dbsession.add(fp)
def add_from_file(filename): ''' Добавить данные в таблицу из файла с разделителями filename. Файл filename в формате csv, колонки: id coord_type ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') reader.next() records = [line for line in reader] for id, ct in records: coord_type = Coord_type(coord_type=ct) dbsession.add(coord_type)
def add_from_file(filename): ''' Добавить данные в таблицу из файла с разделителями filename. Файл filename в формате csv, колонки: id abundance ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') reader.next() records = [line for line in reader] for id, ab in records: abn = Abundance(abundance=ab) dbsession.add(abn)
def add_from_file(filename): ''' Добавить данные в таблицу из файла с разделителями filename. Файл filename в формате csv, колонки: id precision count description ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') reader.next() records = [line for line in reader] for id, p, c, d in records: leg = Legend(precision=p, count=c, description=d) dbsession.add(leg)
def add_from_file(filename): ''' Добавить данные в таблицу из файла с разделителями filename. Файл filename в формате csv, колонки: id anthr_press description ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') reader.next() records = [line for line in reader] for id, ap, desc in records: an_p = Anthr_press(anthr_press=ap, description=desc) dbsession.add(an_p)
def add_from_file(filename): ''' Добавить данные в таблицу из файла с разделителями filename. Файл filename в формате csv, колонки: id pheno description org_type ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') reader.next() records = [line for line in reader] for id, p, desc, t in records: ph = Pheno(pheno=p, description=desc, org_type=t) dbsession.add(ph)
def add_from_file(filename): ''' Добавить данные в таблицу из файла с разделителями filename. Файл filename в формате csv, колонки: id vitality org_type description ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') reader.next() records = [line for line in reader] for id, vit, t, descr in records: vit = Vitality(vitality=vit, description=descr, org_type=t) dbsession.add(vit)
def add_from_file(filename): ''' Добавить данные в таблицу из файла с разделителями filename. Файл filename в формате csv, колонки: id abundance ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') reader.next() records = [line for line in reader] for id, ab in records: abn = Abundance(abundance=ab) dbsession.add(abn)
def add_from_file(filename): ''' Добавить данные в таблицу из файла с разделителями filename. Файл filename в формате csv, колонки: id footprint org_type description ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') reader.next() records = [line for line in reader] for id, fpr, t, desc in records: fp = Footprint(footprint=fpr, description=desc, org_type=t) dbsession.add(fp)
def add_from_file(filename): ''' Добавить данные в таблицу из файла с разделителями filename. Файл filename в формате csv, колонки: id anthr_press description ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') reader.next() records = [line for line in reader] for id, ap, desc in records: an_p = Anthr_press(anthr_press=ap, description=desc) dbsession.add(an_p)
def add_from_file(filename): ''' Добавить данные в таблицу из файла с разделителями filename. Файл filename в формате csv, колонки: id vitality org_type description ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') reader.next() records = [line for line in reader] for id, vit, t, descr in records: vit = Vitality(vitality=vit, description=descr, org_type= t) dbsession.add(vit)
def add_from_file(filename): ''' Добавить данные в таблицу из файла с разделителями filename. Файл filename в формате csv, колонки: id coord_type ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') reader.next() records = [line for line in reader] for id, ct in records: coord_type = Coord_type(coord_type=ct) dbsession.add(coord_type)
def add_from_file(filename): ''' Добавить данные в таблицу из файла с разделителями filename. Файл filename в формате csv, колонки: id precision count description ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') reader.next() records = [line for line in reader] for id, p, c, d in records: leg = Legend(precision=p, count=c, description=d) dbsession.add(leg)
def add_from_file(users_csv_file_path, md5_pass): import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(users_csv_file_path), delimiter='\t') reader.next() records = [line for line in reader] for row in records: (id, login, password, person_id, role) = [None if x == '' else x for x in row] user = User( login=login, password=password if md5_pass else User.password_hash(password), role=role, person_id=person_id, active=True ) dbsession.add(user)
def add_from_file(users_csv_file_path, md5_pass): import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(users_csv_file_path), delimiter='\t') reader.next() records = [line for line in reader] for row in records: (id, login, password, person_id, role) = [None if x == '' else x for x in row] user = User(login=login, password=password if md5_pass else User.password_hash(password), role=role, person_id=person_id, active=True) dbsession.add(user)
def add_from_file(filename): ''' Добавить данные в таблицу из файла с разделителями filename. Файл filename в формате csv, колонки: id area_type legend name ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') reader.next() records = [line for line in reader] for id, atype_id, pr_id, k in records: key_a = Key_area(area_type=atype_id, legend=pr_id, name=k) dbsession.add(key_a)
def add_from_file(filename): ''' Добавить данные в таблицу из файла с разделителями filename. Файл filename в формате csv, колонки: id taxa_scheme ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') reader.next() records = [line for line in reader] for id, t_scheme in records: t = Taxa_scheme(taxa_scheme=t_scheme) dbsession.add(t)
def add_from_file(filename): ''' Добавить данные в таблицу из файла с разделителями filename. Файл filename в формате csv, колонки: id taxa_scheme ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') reader.next() records = [line for line in reader] for id, t_scheme in records: t = Taxa_scheme(taxa_scheme=t_scheme) dbsession.add(t)
def add_from_file(filename): ''' Добавить данные в таблицу из файла с разделителями filename. Файл filename в формате csv, колонки: id area_type legend name ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') reader.next() records = [line for line in reader] for id, atype_id, pr_id, k in records: key_a = Key_area(area_type=atype_id, legend=pr_id, name=k) dbsession.add(key_a)
def import_from_csv(path_to_file): with transaction.manager: session = DBSession() reader = csv.reader(open(path_to_file), delimiter='\t') reader.next() # пропускаем заголовки records = [line for line in reader] for row in records: id_image, name, description, url, local, size = [ None if x == '' else x for x in row ] image = Images(name=name, description=description, url=url, local=local, size=size) session.add(image)
def add_from_file(filename): """ Добавить данные в таблицу синонимов таксонов из файла filename (разделители - табуляция). Файл filename в формате csv, колонки: id species_id synonym author source """ import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter="\t") reader.next() records = [line for line in reader] for row in records: id, species_id, synonym, author, source = [None if x == "" else x for x in row] synonym = Synonym(species_id=species_id, synonym=synonym, author=author, source=source) dbsession.add(synonym)
def table_item_save(request): session = DBSession() table, table_name = helpers.get_table_by_name(request) if ('id' in request.POST) and request.POST['id'].isdigit(): item_id = int(request.POST['id']) item = session.query(table).get(item_id) else: item = table() for attr in request.POST: if attr == 'id': continue setattr(item, attr, request.POST[attr]) session.add(item) item_as_json = item.as_json_dict() transaction.commit() session.close() return {'Result': 'OK', 'Record': item_as_json}
def add_from_file(filename): ''' Добавить данные в таблицу из файла с разделителями filename. Файл filename в формате csv, колонки: id species key_area identifier collecter biblioref original_name location lon lat biotop difference substrat status frequency quantity annotation infosourse year month day exposure ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') row = reader.next() # пропускаем заголовки records = [line for line in reader] for row in records: x = [None if x == '' else x for x in row] print str(len(x)) + ' - ' + str(x[0]) ( id, species, inserter, key_area, identifier, collecter, biblioref, original_name, location, lon, lat, biotop, difference, substrat, status, frequency, quantity, annotation, infosourse, year, month, day, exposure ) = [None if x == '' else x for x in row] ann = Annotation( species=species, inserter=inserter, key_area=key_area, identifier=identifier, collecter=collecter, biblioref=biblioref, original_name=original_name, location=location, lon=lon, lat=lat, biotop=biotop, difference=difference, substrat=substrat, status=status, frequency=frequency, quantity=quantity, annotation=annotation, infosourse=infosourse, year=year, month=month, day=day, exposure=exposure ) dbsession.add(ann)
def add_from_file(filename): ''' Добавить данные в таблицу синонимов таксонов из файла filename (разделители - табуляция). Файл filename в формате csv, колонки: id species_id synonym author source ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') reader.next() records = [line for line in reader] for row in records: id, species_id, synonym, author, source = [ None if x == '' else x for x in row ] synonym = Synonym(species_id=species_id, synonym=synonym, author=author, source=source) dbsession.add(synonym)
def add_from_file(filename): ''' Добавить данные в таблицу из файла с разделителями filename. Файл filename в формате csv, колонки: id doc_type_id filename fullname author magazine pages mammals birds reptiles amphibians fish invertebrates vascular bryophytes lichens fungi maps ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') reader.next() records = [line for line in reader] for row in records: (id, doc_type_id, filename, fullname, author, magazine, pages, mammals, birds, reptiles, amphibians, fish, invertebrates, vascular, bryophytes, lichens, fungi, maps) = [None if x == '' else x for x in row] infores = Inforesources(doc_type_id=doc_type_id, filename=filename, fullname=fullname, author=author, magazine=magazine, pages=pages, mammals=mammals, birds=birds, reptiles=reptiles, amphibians=amphibians, fish=fish, invertebrates=invertebrates, vascular=vascular, bryophytes=bryophytes, lichens=lichens, fungi=fungi, maps=maps) dbsession.add(infores)
def add_from_file(associations_filename, shp_filename): ''' Добавить данные из shp-файла shp_filename. Первое поле аттрибутивной таблицы--идентификатор. Одновременно добавляются в таблицу связи данные из файла с разделителями associations_filename. Файл filename в формате csv (разделитель табуляция), колонки: square_id key_area_id ''' import transaction with transaction.manager: dbsession = DBSession() ogrData = ogr.Open(shp_filename) layer = ogrData.GetLayer(0) sq = layer.GetNextFeature() while sq is not None: id = sq.GetFieldAsString(0) geom = sq.GetGeometryRef() geom = geom.ExportToWkt() square = Squares(id=id, geom=WKTSpatialElement(geom, srid=3857)) dbsession.add(square) sq = layer.GetNextFeature() dbsession.flush() reader = csv.reader(open(associations_filename), delimiter='\t') reader.next() records = [line for line in reader] for id, key_area_id in records: # Определим ключевоq уч-к по его id key_a = dbsession.query(Key_area).filter_by( id=key_area_id).one() # Определим полигон по его id square = dbsession.query(Squares).filter_by(id=id).one() square.key_areas.append(key_a)
def table_item_save(request): session = DBSession() table, table_name = helpers.get_table_by_name(request) if ('id' in request.POST) and request.POST['id'].isdigit(): item_id = int(request.POST['id']) item = session.query(table).get(item_id) else: item = table() for attr in request.POST: if attr == 'id': continue setattr(item, attr, request.POST[attr]) session.add(item) item_as_json = item.as_json_dict() transaction.commit() session.close() return { 'Result': 'OK', 'Record': item_as_json }
def add_from_file(filename): ''' Добавить данные в таблицу из файла filename (с разделителями табуляция). Файл filename в формате csv, колонки: id species inserter observer identifier taxa_scheme museum anthr_press vitality abundance footprint pheno inforesources original_name photo year month day time habitat substrat limit_fact protection area quantity unknown_age unknown_sex males females ad sad juv pull egs publications notes location lon lat coord_type ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') row = reader.next() # пропускаем заголовки records = [line for line in reader] for row in records: ( id, species, inserter, observer, identifier, taxa_scheme, museum, anthr_press, vitality, abundance, footprint, pheno, inforesources, original_name, photo, year, month, day, time, habitat, substrat, limit_fact, protection, area, quantity, unknown_age, unknown_sex, males, females, ad, sad, juv, pull, egs, publications, notes, location, lon, lat, coord_type ) = [None if x == '' else x for x in row] card = Cards( species=species, inserter=inserter, observer=observer, identifier=identifier, taxa_scheme=taxa_scheme, museum=museum, anthr_press=anthr_press, vitality=vitality, abundance=abundance, footprint=footprint, pheno=pheno, inforesources=inforesources, original_name=original_name, year=year, month=month, day=day, time=time, habitat=habitat, substrat=substrat, limit_fact=limit_fact, protection=protection, area=area, quantity=quantity, unknown_age=unknown_age, unknown_sex=unknown_sex, males=males, females=females, ad=ad, sad=sad, juv=juv, pull=pull, egs=egs, publications=publications, notes=notes, location=location, lon=lon, lat=lat, coord_type=coord_type ) dbsession.add(card)
def table_item_save(request): person_id = None if ('person_id' in request.POST) and request.POST['person_id'].isdigit(): person_id = int(request.POST['person_id']) user_login = request.POST['user_login'] if 'user_login' in request.POST else None if not user_login: raise HTTPBadRequest('"user_login" is required parameter') if not person_id: users = DBSession.query(User).filter(User.login == user_login).all() if len(users) > 0: return { 'Result': 'Error', 'Message': u'Такой логин уже присутствует в системе' } with transaction.manager: if person_id: person = DBSession.query(Person) \ .options(joinedload('user')) \ .filter(Person.id == person_id) \ .all()[0] user = person.user else: person = Person() DBSession.add(person) user = User() DBSession.add(user) person.user = user for attr in request.POST: table_name, field = attr.split('_') if field == 'id': continue if table_name == 'person': setattr(person, field, request.POST[attr]) if table_name == 'user': setattr(user, field, request.POST[attr]) if 'user_active' in request.POST and request.POST['user_active']: user.active = True else: user.active = False if 'user_password' in request.POST and request.POST['user_password']: user.password = User.password_hash(request.POST['user_password']) DBSession.flush() DBSession.refresh(user) DBSession.refresh(person) person_json = person.as_json_dict('person_') user_json = user.as_json_dict('user_') item_json = person_json.copy() item_json.update(user_json) return { 'Result': 'OK', 'Record': item_json }
def import_from_csv(path_to_file): session = DBSession() log = { 'not_found': [], 'duplicates': [], 'multiple': [] } reader = csv.reader(open(path_to_file), delimiter='\t') reader.next() records = [line for line in reader] red_books = {} for region, orig_name, lat_name, author, population, status, univ_status, year, bibl in records: if bibl in red_books: continue else: red_books[bibl] = True with transaction.manager: for red_book_name in red_books.keys(): red_book = RedBook( name=red_book_name ) session.add(red_book) red_books_db = session.query(RedBook).all() red_books = {} for red_book_db in red_books_db: red_books[red_book_db.name.encode('utf8')] = red_book_db.id with transaction.manager: for region, orig_name, lat_name, author, population, status, univ_status, year, bibl in records: lat_name = lat_name.strip() taxons = session.query(Taxon).filter_by(name=lat_name).all() taxons_count = len(taxons) if taxons_count == 1: taxon_id = taxons[0].id elif taxons_count > 1: taxons = session.query(Taxon).filter_by(name=lat_name).filter_by(author=author).all() taxon_id = taxons[0].id if len(taxons) > 1: log['multiple'].append(lat_name) continue else: log['not_found'].append(lat_name) continue red_book_id = red_books[bibl] count = session.query(func.count(RedBookSpecies.specie_id)) \ .filter(RedBookSpecies.red_book_id == red_book_id) \ .filter(RedBookSpecies.specie_id == taxon_id).scalar() if count > 0: log['duplicates'].append(taxons[0].name) continue red_book_specie = RedBookSpecies( red_book_id=red_book_id, specie_id=taxon_id, population=population, status=status, univ_status=univ_status, year=int(year) if year else None, region=region, author=author, orig_name=orig_name.strip() ) session.add(red_book_specie) session.flush() print '\n\rMULTIPLE:\n\r{0}'.format('\n\r'.join(log['multiple'])) print '\n\rNOT FOUND:\n\r{0}'.format('\n\r'.join(log['not_found'])) print '\n\rDUPLICATES:\n\r{0}'.format('\n\r'.join(log['duplicates']))
def add_from_file(filename): ''' Добавить данные в таблицу из файла filename (с разделителями табуляция). Файл filename в формате csv, колонки: id species inserter observer identifier taxa_scheme museum anthr_press vitality abundance footprint pheno inforesources original_name photo year month day time habitat substrat limit_fact protection area quantity unknown_age unknown_sex males females ad sad juv pull egs publications notes location lon lat coord_type ''' import transaction with transaction.manager: dbsession = DBSession() reader = csv.reader(open(filename), delimiter='\t') row = reader.next() # пропускаем заголовки records = [line for line in reader] for row in records: (id, species, inserter, observer, identifier, taxa_scheme, museum, anthr_press, vitality, abundance, footprint, pheno, inforesources, original_name, photo, year, month, day, time, habitat, substrat, limit_fact, protection, area, quantity, unknown_age, unknown_sex, males, females, ad, sad, juv, pull, egs, publications, notes, location, lon, lat, coord_type) = [None if x == '' else x for x in row] card = Cards(species=species, inserter=inserter, observer=observer, identifier=identifier, taxa_scheme=taxa_scheme, museum=museum, anthr_press=anthr_press, vitality=vitality, abundance=abundance, footprint=footprint, pheno=pheno, inforesources=inforesources, original_name=original_name, year=year, month=month, day=day, time=time, habitat=habitat, substrat=substrat, limit_fact=limit_fact, protection=protection, area=area, quantity=quantity, unknown_age=unknown_age, unknown_sex=unknown_sex, males=males, females=females, ad=ad, sad=sad, juv=juv, pull=pull, egs=egs, publications=publications, notes=notes, location=location, lon=lon, lat=lat, coord_type=coord_type) dbsession.add(card)
def import_from_csv(path_to_file): session = DBSession() log = {'not_found': [], 'duplicates': [], 'multiple': []} reader = csv.reader(open(path_to_file), delimiter='\t') reader.next() records = [line for line in reader] red_books = {} for region, orig_name, lat_name, author, population, status, univ_status, year, bibl in records: if bibl in red_books: continue else: red_books[bibl] = True with transaction.manager: for red_book_name in red_books.keys(): red_book = RedBook(name=red_book_name) session.add(red_book) red_books_db = session.query(RedBook).all() red_books = {} for red_book_db in red_books_db: red_books[red_book_db.name.encode('utf8')] = red_book_db.id with transaction.manager: for region, orig_name, lat_name, author, population, status, univ_status, year, bibl in records: lat_name = lat_name.strip() taxons = session.query(Taxon).filter_by(name=lat_name).all() taxons_count = len(taxons) if taxons_count == 1: taxon_id = taxons[0].id elif taxons_count > 1: taxons = session.query(Taxon).filter_by( name=lat_name).filter_by(author=author).all() taxon_id = taxons[0].id if len(taxons) > 1: log['multiple'].append(lat_name) continue else: log['not_found'].append(lat_name) continue red_book_id = red_books[bibl] count = session.query(func.count(RedBookSpecies.specie_id)) \ .filter(RedBookSpecies.red_book_id == red_book_id) \ .filter(RedBookSpecies.specie_id == taxon_id).scalar() if count > 0: log['duplicates'].append(taxons[0].name) continue red_book_specie = RedBookSpecies( red_book_id=red_book_id, specie_id=taxon_id, population=population, status=status, univ_status=univ_status, year=int(year) if year else None, region=region, author=author, orig_name=orig_name.strip()) session.add(red_book_specie) session.flush() print '\n\rMULTIPLE:\n\r{0}'.format('\n\r'.join(log['multiple'])) print '\n\rNOT FOUND:\n\r{0}'.format('\n\r'.join(log['not_found'])) print '\n\rDUPLICATES:\n\r{0}'.format('\n\r'.join(log['duplicates']))