def dump_data(): dir_name = 'nextgisbio/initial_data/csv/' + time.strftime("%Y_%m_%d_%H_%M_%S") if not os.path.exists(dir_name): os.makedirs(dir_name) else: raise Exception('Directory for exported csv files already exists') get_path_name = lambda name: os.path.join(dir_name, name) with transaction.manager: Annotation.export_to_file(get_path_name('annotation.csv')) Cards.export_to_file(get_path_name('cards.csv')) Person.export_to_file(get_path_name('person.csv')) Taxa_scheme.export_to_file(get_path_name('taxa_scheme.csv')) Museum.export_to_file(get_path_name('museum.csv')) Coord_type.export_to_file(get_path_name('coord_type.csv')) Anthr_press.export_to_file(get_path_name('anthr_press.csv')) Vitality.export_to_file(get_path_name('vitality.csv')) Abundance.export_to_file(get_path_name('abundance.csv')) Footprint.export_to_file(get_path_name('footprint.csv')) Pheno.export_to_file(get_path_name('pheno.csv')) Inforesources.export_to_file(get_path_name('inforesources.csv')) Legend.export_to_file(get_path_name('legend.csv')) Area_type.export_to_file(get_path_name('area_type.csv')) Key_area.export_to_file(get_path_name('key_area.csv')) RedBook.export_to_file(get_path_name('redbooks.csv')) User.export_to_file(get_path_name('user.csv')) Squares.export_to_file(get_path_name('square_karea_association.csv')) Taxon.export_to_file(get_path_name('taxon.csv')) Synonym.export_to_file(get_path_name('synonym.csv')) Images.export_to_file(get_path_name('images.csv')) CardsImages.export_to_file(get_path_name('cards_images.csv')) return dir_name
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 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 main(argv=sys.argv): if len(argv) != 2 and len(argv) != 3: usage(argv) config_uri = argv[1] setup_logging(config_uri) settings = get_appsettings(config_uri) engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) md5_pass = False if len(argv) == 3 and argv[2] == '--md5-pass': md5_pass = True Base.metadata.drop_all(engine) Base.metadata.create_all(engine) # Заполним таблицы данными: # Таксоны taxons_file = 'nextgisbio/initial_data/csv/taxon.csv' Taxon.add_from_file(taxons_file) synonym_file = 'nextgisbio/initial_data/csv/synonym.csv' Synonym.add_from_file(synonym_file) # Справочники person_file = 'nextgisbio/initial_data/csv/person.csv' Person.add_from_file(person_file) taxa_file = 'nextgisbio/initial_data/csv/taxa_scheme.csv' Taxa_scheme.add_from_file(taxa_file) museum_file = 'nextgisbio/initial_data/csv/museum.csv' Museum.add_from_file(museum_file) coord_type_file = 'nextgisbio/initial_data/csv/coord_type.csv' Coord_type.add_from_file(coord_type_file) ant_file = 'nextgisbio/initial_data/csv/anthr_press.csv' Anthr_press.add_from_file(ant_file) vital_file = 'nextgisbio/initial_data/csv/vitality.csv' Vitality.add_from_file(vital_file) abundance_file = 'nextgisbio/initial_data/csv/abundance.csv' Abundance.add_from_file(abundance_file) footprint_file = 'nextgisbio/initial_data/csv/footprint.csv' Footprint.add_from_file(footprint_file) pheno_file = 'nextgisbio/initial_data/csv/pheno.csv' Pheno.add_from_file(pheno_file) infores_file = 'nextgisbio/initial_data/csv/inforesources.csv' Inforesources.add_from_file(infores_file) area_type_file = 'nextgisbio/initial_data/csv/area_type.csv' Area_type.add_from_file(area_type_file) legend_file = 'nextgisbio/initial_data/csv/legend.csv' Legend.add_from_file(legend_file) key_area_file = 'nextgisbio/initial_data/csv/key_area.csv' Key_area.add_from_file(key_area_file) # Нужно добавить шейпы и заполнить данными таблицу # связей (square_keyarea_association) многие-ко-многим между Squares и Key_area shp_file = 'nextgisbio/initial_data/shp/key_areas_25km.shp' association_file = 'nextgisbio/initial_data/csv/square_karea_association.csv' Squares.add_from_file(association_file, shp_file) # Карточки и аннотации cards_file = 'nextgisbio/initial_data/csv/cards.csv' Cards.add_from_file(cards_file) ann_file = 'nextgisbio/initial_data/csv/annotation.csv' Annotation.add_from_file(ann_file) # Пользователи users_file = 'nextgisbio/initial_data/csv/user.csv' User.add_from_file(users_file, md5_pass) red_books_csv = 'nextgisbio/initial_data/csv/redbooks.csv' RedBook.import_from_csv(red_books_csv) images_csv = 'nextgisbio/initial_data/csv/images.csv' Images.import_from_csv(images_csv) cards_images_csv = 'nextgisbio/initial_data/csv/cards_images.csv' CardsImages.import_from_csv(cards_images_csv)