Exemple #1
0
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
Exemple #2
0
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}
Exemple #3
0
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
    }
Exemple #4
0
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
    }
Exemple #5
0
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)