示例#1
0
def save_region(session, item_dict):
    """Save player to database"""
    region = Region()
    region.id = item_dict['region_id']
    region.name = item_dict['region_name']
    session.add(region)
    return region
示例#2
0
def create_politician(data):
    politician = Politician(**data)
    politician.save()

    gender = Gender().fetch(data.get('gender'))
    region = Region().fetch(data.get('region'))
    political_office = PoliticalOffice().fetch(data.get('political_office'))
    political_party = PoliticalParty().fetch(data.get('political_party'))

    if gender:
        gender.politicians.add(politician)
        gender.save()

    if region:
        region.politicians.add(politician)
        region.save()

    if political_office:
        political_office.politicians.add(politician)
        political_office.save()

    if political_party:
        political_party.politicians.add(politician)
        political_party.save()

    return politician
示例#3
0
def save_region(session, region_id):
    """Save player to database"""
    region = Region()
    region.id = region_id
    region.name = 'UNKNOWN'
    session.add(region)
    return region
示例#4
0
 def get_dtl(self):
     result = super(Exam, self).to_dict()
     result = Region.bind_auto(result, 'name', 'city_id', 'id', 'city')
     result = Region.bind_auto(result, 'name', 'province_id', 'id', 'province')
     result = Region.bind_auto(result, 'name', 'area_id', 'id', 'area')
     result = School.bind_auto(result, 'name', 'school_id', 'id', 'school')
     return result
示例#5
0
def save_region(session, region_id):
    """Save region to database"""
    region = Region()
    region.id = region_id
    session.add(region)
    session.commit()
    return region
示例#6
0
    def mutate(self, info, **kwargs):
        Politician.delete_all()
        Gender.delete_all()
        Region.delete_all()
        PoliticalOffice.delete_all()
        PoliticalParty.delete_all()

        return DeleteDB(success=True)
示例#7
0
def save_region(session, region_dict):
    """Save region to database"""
    region = Region()
    region.id = region_dict['id']
    region.name = region_dict['name']
    session.add(region)
    session.commit()
    return region
示例#8
0
def regions(app, request):
    with app.app_context():
        db.session.add(Region(name='Metro', state='NSW'))
        db.session.add(Region(name='Remote', state='NSW'))
        db.session.add(Region(name='Metro', state='QLD'))

        db.session.commit()
        yield Region.query.all()
示例#9
0
    def get_exam(id):
        result = Exam.query.get(int(id))
        if result is not None:
            result = result.to_dict()
        else:
            return None
        result = Region.bind_auto(result, 'name', 'city_id', 'id', 'city')
        result = Region.bind_auto(result, 'name', 'province_id', 'id', 'province')
        result = Region.bind_auto(result, 'name', 'area_id', 'id', 'area')
        result = School.bind_auto(result, 'name', 'school_id', 'id', 'school')

        return result
示例#10
0
文件: cli.py 项目: jonodrew/fusion
 def generate_regions():
     """Generate regions data if it doesn't already exist"""
     if Region.query.count() < 4:
         regions = [
             Region(name='Wales'),
             Region(name='England'),
             Region(name='Scotland'),
             Region(name='London')
         ]
         for r in regions:
             db.session.add(r)
         db.session.commit()
    def setup_mock_data(self):
        # get the database instance
        db = self.get_db()

        # create a new crop
        ilocos = Region('Ilocos Region', 'Region 1', 'Region I')
        cagayan = Region('Cagayan Valley', 'Region 2', 'Region II')

        # commit the transaction
        db.session.add(ilocos)
        db.session.add(cagayan)
        db.session.commit()
示例#12
0
    def parse(self, **kwargs):
        worksheet = GoogleSheetsParser.get_worksheet(self, '1Mp9r7CNxVnKip-tLAFpbGp4K_MY2iUrbrBOQBcsKLVE')

        i = 2
        while True:
            values_list = worksheet.row_values(i)
            i += 1

            if not values_list[0]:
                break

            try:
                country = Country.objects.get(
                    name=values_list[4]
                )
            except ObjectDoesNotExist:
                country = Country(
                    name=values_list[4]
                )
                self.country_count += 1
                country.save()

            try:
                region = Region.objects.get(
                    name=values_list[1]
                )
            except ObjectDoesNotExist:
                region = Region(
                    name=values_list[1],
                    country=country
                )
                self.region_count += 1
                region.save()

            try:
                city = City.objects.get(
                    name=values_list[0]
                )
            except ObjectDoesNotExist:
                city = City(
                    name=values_list[0],
                    lat=values_list[2],
                    lon=values_list[3],
                    region=region
                )
                self.city_count += 1
                city.save()

        return [
            'New Countries: ' + str(self.country_count),
            'New Regions: ' + str(self.region_count),
            'New Cities: ' + str(self.city_count),
        ]
示例#13
0
def create_region():
    # get_regions = Region.query.all()
    if not request.form['name']:
        abort(400)
    name = request.form['name']
    color = request.form['color']
    if not color:
        color = 'white'
    region = Region(name=name, color=color)
    db.session.add(region)
    db.session.commit()
    regions = Region.query.all()
    return jsonify({"regions": [region.dto() for region in regions]}, 201)
示例#14
0
    def _parse_region(self, data, region=None):
        name, acronym, description = None, None, None
        if 'name' in data:
            name = str(data['name']).strip()
        if 'acronym' in data:
            acronym = str(data['acronym']).strip()
        if 'description' in data:
            description = str(data['description']).strip()

        if region is None:
            region = Region(name=name, acronym=acronym, description=description)
        else:
            region.name = name
            region.acronym = acronym
            region.description = description
        return region
示例#15
0
def city():
    pro_id = request.args.get('pro_id')
    title = request.args.get('title', '')
    if not pro_id:
        raise JsonOutputException('need pro_id')
    cities = Region.get_city(pro_id, title)
    return {'code': 0, 'data': cities}
示例#16
0
def area():
    city_id = request.args.get('city_id')
    title = request.args.get('title', '')
    if not city_id:
        raise JsonOutputException('need city_id')
    areas = Region.get_area(city_id, title)
    return {'code': 0, 'data': areas}
示例#17
0
 def create_region(self):
     cont_id = self.create_cont().id
     region = Region(id=randint(0, 100),
                     name=self.faker.state(),
                     cont_id=cont_id)
     db.session.add(region)
     db.session.commit()
     return region
示例#18
0
文件: utils.py 项目: RVShershnev/hk
def add_city():
    """Добавить области в БД. Запустить только один раз!"""
    raions = ('Алексинский район', 'Арсеньевский район', 'Белёвский район',
              'Богородицкий район', 'Венёвский район', 'Воловский район',
              'Дубенский район', 'Ефремовский район', 'Заокский район',
              'Каменский район', 'Кимовский район', 'Киреевский район',
              'Куркинский район', 'Ленинский район', 'Новомосковский район',
              'Одоевский район', 'Плавский район', 'Суворовский район',
              'Тёпло-Огарёвский район', 'Узловский район', 'Чернский район',
              'Щёкинский район', 'Ясногорский район')
    region = Region()
    region.name = 'Тульская область'
    region.save()
    for raion in raions:
        city = City()
        city.region = region
        city.name = raion
        city.save()
示例#19
0
    def setUp(self):
        self.app = self.create_app()
        self.client = self.app.test_client()
        self.app_context = self.app.app_context()
        self.app_context.push()
        db.create_all()

        # Create some data to test against, but don't commit
        # Create a region
        maine = Region(name='Maine', slug='maine')
        db.session.add(maine)

        # Create a river
        andro = River(name='Androscoggin River',
                      slug='androscoggin')
        wild = River(name='Wild River of the Androsoggin',
                     slug='wild-androscoggin',
                     parent=andro)
        db.session.add(andro)
        db.session.add(wild)

        # Create a section
        wild_section = Section(name='Wild River from Hastings',
                               slug='wild-river',
                               river=wild,
                               putin='SRID=4326;POINT(-71.05996191501617 44.31595096222731)',
                               takeout='SRID=4326;POINT(-70.97963511943817 44.390833083196924)',
                               path='SRID=4326;LINESTRING(-71.05997800827026 44.316024368364864,-71.05881929397583 44.31798950769032,-71.05731725692749 44.31884923545022,-71.05444192886353 44.31851148676115,-71.05298280715942 44.31943261497028,-71.05096578598022 44.322687152160796,-71.05045080184937 44.32449856163325,-71.04984998703003 44.32495908054771,-71.04761838912964 44.325849406864485,-71.04568719863892 44.32649411748597,-71.04306936264038 44.32753791965937,-71.04049444198608 44.327138821021585,-71.03847742080688 44.32664761897048,-71.03607416152954 44.32572660403795,-71.03517293930054 44.32554239931617,-71.03431463241577 44.32594150881567,-71.03341341018677 44.32805981378074,-71.03264093399048 44.329103588092785,-71.02929353713989 44.32984035877702,-71.02843523025513 44.33008594694842,-71.02757692337036 44.33137526797721,-71.02804899215698 44.33309431859246,-71.02783441543579 44.33459844654652,-71.02543115615845 44.33582627750024,-71.02311372756958 44.33714616710255,-71.0210108757019 44.33926406740166,-71.01598978042603 44.34328479806066,-71.01294279098511 44.3441441551062,-71.00916624069214 44.346752840399844,-71.0082221031189 44.34972966884455,-71.00689172744751 44.35107993293547,-71.00551843643188 44.351171995263435,-71.00393056869507 44.350374116950185,-71.00148439407349 44.35000586175751,-70.9984803199768 44.350374116950185,-70.99642038345337 44.35163230473401,-70.99328756332397 44.354117913402796,-70.9923005104065 44.3552225945275,-70.99212884902954 44.35678752380721,-70.99401712417603 44.357830786775374,-70.99517583847046 44.3607763701854,-70.99680662155151 44.36571602660432,-70.99599123001099 44.368722570068854,-70.99448919296265 44.36961223191264,-70.99242925643921 44.37123813071097,-70.99054098129272 44.371422191881805,-70.98955392837524 44.372986688478704,-70.99075555801392 44.37507261892906,-70.98963975906372 44.37691308409485,-70.98848104476929 44.37832406821415,-70.9874939918518 44.38086988832067,-70.98470449447632 44.382403461463625,-70.98273038864136 44.384059675338044,-70.9810996055603 44.38531713976433,-70.97848176956177 44.3864825704798,-70.97749471664429 44.38746396782254,-70.97903966903687 44.38970271892836)',)
        db.session.add(wild_section)

        # Create a gage
        wild_gage = Gage(name='Wild River at Gilead',
                         slug='wild-river-gilead',
                         point='SRID=4326;POINT(-70.97963511943817 44.390833083196924)',
                         river=wild,
                         visible=True,
                         zipcode='04217',
                         local_town='Gilead, ME',
                         location='Wild River at Gilead above Rt 2',
                         key='password')
        db.session.add(wild_gage)

        # Create a sensor
        wild_sensor = Sensor(name='Gage Height',
                             stype='usgs-height',
                             local=False, remote_type='usgs',
                             remote_id='01054200',
                             gage=wild_gage)
        db.session.add(wild_sensor)

        # Create a sample
        wild_sample = Sample(sensor=wild_sensor,
                             datetime=datetime.datetime.now(),
                             value=5.8)
        db.session.add(wild_sample)
        db.session.commit()
示例#20
0
def list_quest_review_log():

    query = Preprocess.query.\
        filter_by(operator_id=g.user.id).\
        order_by(Preprocess.created_at.desc()).\
        join(Exam, Exam.id==Preprocess.exam_id)
    if request.args.get('name'):
        query = query.filter(
            Exam.name.like('%{}%'.format(request.args.get('name'))))
    if request.args.get('subject'):
        query = query.filter(Exam.subject == request.args.get('subject'))
    if request.args.get('paper_types'):
        query = query.filter(
            Exam.paper_types == request.args.get('paper_types'))
    if request.args.get('province_id'):
        query = query.filter(
            Exam.province_id == request.args.get('province_id'))
    if request.args.get('city_id'):
        query = query.filter(Exam.city_id == request.args.get('city_id'))
    if request.args.get('area_id'):
        query = query.filter(Exam.area_id == request.args.get('area_id'))
    if request.args.get('school_id'):
        query = query.filter(Exam.school_id == request.args.get('school_id'))
    if request.args.get('year'):
        query = query.filter(Exam.year == request.args.get('year'))
    if request.args.get('grade'):
        query = query.filter(Exam.grade == request.args.get('grade'))
    res = pagination(query)
    items = res.get('items', [])
    items = Exam.bind_auto(items, [
        'name', 'section', 'school_id', 'subject', 'grade', 'paper_types',
        'province_id', 'city_id', 'area_id'
    ])
    items = School.bind_auto(items, 'name', 'exam_school_id', 'id', 'school')
    items = Region.bind_auto(items, 'name', 'exam_province_id', 'id',
                             'province')
    items = Region.bind_auto(items, 'name', 'exam_city_id', 'id', 'city')
    items = Region.bind_auto(items, 'name', 'exam_area_id', 'id', 'area')
    res['items'] = items
    return render_api(res)
示例#21
0
def showRegions():
    form = NewRegion()
    if form.validate_on_submit():
        newRegion = Region(name=form.region.data)
        db.session.add(newRegion)
        db.session.commit()
        flash('Новый регион "{}" добавлен!'.format(form.region.data))
        return redirect(url_for('clients.showRegions'))
    regions = Region.query.order_by(Region.name.asc())
    return render_template('/clients/regions.html',
                           title='Regions',
                           form=form,
                           regions=regions)
示例#22
0
def get_or_create_region(region_name, country):
    session = db_session()
    session.add(country)
    region = session.query(Region).filter_by(name=region_name, country_id=country.id).first()
    if not region:
        new_region = Region(name=region_name, country_id=country.id)
        session.add(new_region)
        session.commit()
        session.expunge_all()
        session.close()
        return new_region
    session.expunge_all()
    session.close()
    return region
示例#23
0
def seed_regions():

    region = Region(name='Kanto')
    region2 = Region(name='Johto')
    region3 = Region(name='Hoenn')
    region4 = Region(name='Sinnoh')
    region5 = Region(name='Unova')
    region6 = Region(name='Kalos')
    region7 = Region(name='Alola')
    region8 = Region(name='Galar')

    db.session.add(region)
    db.session.add(region2)
    db.session.add(region3)
    db.session.add(region4)
    db.session.add(region5)
    db.session.add(region6)
    db.session.add(region7)
    db.session.add(region8)

    db.session.commit()
示例#24
0
def province():
    title = request.args.get('title', '')
    provinces = Region.get_province(title)
    return {'code': 0, 'data': provinces}
示例#25
0
def create_filter():
    form = CreateFilterForm()
    if form.validate_on_submit():
        local_filter = Filter.query.filter_by(
            user_id=current_user.id,
            status_id=int(form.status_name.data)).first()

        if not local_filter:
            local_filter = Filter(name=form.name.data,
                                  user_id=current_user.id,
                                  status_id=int(form.status_name.data))
            db.session.add(local_filter)
            db.session.flush()

        # Create tags-----------------------
        raw_tags = form.tags.data.split('$')
        clean_tags = [
            tag.lower() for tag in [tag.strip() for tag in raw_tags]
            if ' ' not in tag and tag
        ]
        tags = []

        for each_clean_tag in clean_tags:
            temp_tag = Tag.query.filter_by(name=each_clean_tag).first()

            if not temp_tag:
                temp_tag = Tag(name=each_clean_tag)
                db.session.add(temp_tag)

            tags.append(temp_tag)

        # Create region-------------------------
        if form.region_name.data:
            local_region = Region.query.filter_by(
                name=form.region_name.data).first()
        else:
            local_region = Region(latitude=form.latitude.data,
                                  longitude=form.longitude.data,
                                  radius=form.radius.data)
            db.session.add(local_region)

        # Create Recurrence Pattern----------------
        recurrence = RecurrencePattern(
            recurrence_type=form.recurrence_type.data
            if form.recurrence_type.data else None,
            start_time=form.start_time.data,
            end_time=form.end_time.data,
            start_date=form.start_date.data,
            end_date=form.end_date.data,
            separation_count=form.separation_count.data
            if form.separation_count.data > 0 else None,
            day_of_week=form.day_of_week.data
            if form.day_of_week.data else None,
            week_of_month=form.week_of_month.data
            if form.week_of_month.data else None,
            day_of_month=form.day_of_month.data
            if form.day_of_month.data else None,
            month_of_year=form.month_of_year.data
            if form.month_of_year.data else None)
        db.session.add(recurrence)
        db.session.flush()

        local_filter.regions.append(local_region)
        local_filter.tags.extend(tags)
        local_filter.recurrences.append(recurrence)

        db.session.commit()
        flash('success$Filter has been created !')
        return redirect(url_for('index'))

    return render_template('new_filter.html', form=form)
示例#26
0
def new_note():
    form = CreateNoteForm()
    if form.validate_on_submit():
        local_note = Note(
            content=form.content.data,
            created_by=current_user.id,
            is_public=not form.is_private.data,
            is_recurring=form.is_recurring.data,
            is_shared_with_friends=form.is_shared_with_friends.data,
            is_commentable=form.is_commentable.data)
        db.session.add(local_note)

        # Create tags-----------------------
        raw_tags = form.tags.data.split('$')
        clean_tags = [
            tag.lower() for tag in [tag.strip() for tag in raw_tags]
            if ' ' not in tag and tag
        ]
        tags = []

        for each_clean_tag in clean_tags:
            temp_tag = Tag.query.filter_by(name=each_clean_tag).first()

            if not temp_tag:
                temp_tag = Tag(name=each_clean_tag)
                db.session.add(temp_tag)

            tags.append(temp_tag)

        # Create region-------------------------
        if form.region_name.data:
            local_region = Region.query.filter_by(
                name=form.region_name.data).first()
        else:
            local_region = Region(latitude=form.latitude.data,
                                  longitude=form.longitude.data,
                                  radius=form.radius.data)
            db.session.add(local_region)

        # Create Recurrence Pattern----------------
        recurrence = RecurrencePattern(
            recurrence_type=form.recurrence_type.data
            if form.recurrence_type.data else None,
            start_time=form.start_time.data,
            end_time=form.end_time.data,
            start_date=form.start_date.data,
            end_date=form.end_date.data,
            separation_count=form.separation_count.data
            if form.separation_count.data > 0 else None,
            day_of_week=form.day_of_week.data
            if form.day_of_week.data else None,
            week_of_month=form.week_of_month.data
            if form.week_of_month.data else None,
            day_of_month=form.day_of_month.data
            if form.day_of_month.data else None,
            month_of_year=form.month_of_year.data
            if form.month_of_year.data else None)
        db.session.add(recurrence)
        db.session.flush()

        # Adjust note visibility
        local_note.region_id = local_region.id
        if not local_note.is_public:
            local_note.visibility.append(current_user)
            if local_note.is_shared_with_friends:
                local_note.visibility.extend(current_user.get_friends())

        local_note.tags.extend(tags)
        local_note.pattern.append(recurrence)

        db.session.commit()
        flash('success$Note has been created !')
        return redirect(url_for('note', note_id=local_note.id))

    return render_template('new_note.html', form=form)
示例#27
0
    def parse(self, **kwargs):
        worksheet = GoogleSheetsParser.get_worksheet(self, '15Q8sDyG_eBUHMcriIAHTmwDcdSdJSSLNAo34iBZKyJk')

        i = 2
        while True:
            values_list = worksheet.row_values(i)
            i += 1

            if not values_list[0]:
                break

            if values_list[5] == kwargs['options']['wave']:
                try:
                    university = University.objects.get(
                        name=values_list[0]
                    )
                except ObjectDoesNotExist:
                    try:
                        city = City.objects.get(
                            name=values_list[3]
                        )
                    except ObjectDoesNotExist:
                        try:
                            region = Region.objects.get(
                                name='unknown'
                            )
                        except ObjectDoesNotExist:
                            try:
                                country = Country.objects.get(
                                    name=values_list[2]
                                )
                            except ObjectDoesNotExist:
                                country = Country(
                                    name=values_list[2]
                                )
                                country.save()
                            region = Region(
                                name='unknown',
                                country=country,
                            )
                            self.unknown_region = True
                            region.save()
                        city = City(
                            name=values_list[3],
                            region=region,
                            lat=0,
                            lon=0
                        )
                        self.city_count += 1
                        city.save()

                    university = University(
                        name=values_list[0],
                        abbr=values_list[1],
                        city=city,
                        site=values_list[4]
                    )
                    self.university_count += 1
                    university.save()

        result = [
            'Wave ' + str(kwargs['options']['wave']),
            'New University: ' + str(self.university_count),
        ]
        if self.city_count > 0:
            result.append('------')
            result.append('Check this situation! It is not normal!')
            result.append('------')
            result.append('New City: ' + str(self.city_count))
            if self.unknown_region:
                result.append('Added Region "unknown"')

        return result
示例#28
0
    def mutate(self, info, **kwargs):
        region = Region(**kwargs)
        region.save()

        return CreateRegion(region=region, success=True)
示例#29
0
    def setUp(self):
        self.app = create_app('testing')
        self.client = self.app.test_client()
        self.app_context = self.app.app_context()
        self.app_context.push()
        db.create_all()

        # Create some data to test against, but don't commit
        # Create a region
        maine = Region(name='Maine', slug='maine')
        db.session.add(maine)

        # Create a river
        andro = River(name='Androscoggin River', slug='androscoggin')
        wild = River(name='Wild River of the Androsoggin',
                     slug='wild-river',
                     parent=andro)
        db.session.add(andro)
        db.session.add(wild)

        # Create a section
        wild_section = Section(
            name='Wild River from Hastings',
            slug='hasting-to-gilead',
            river=wild,
            putin='SRID=4326;POINT(-71.05996191501617 44.31595096222731)',
            takeout='SRID=4326;POINT(-70.97963511943817 44.390833083196924)',
            path=
            'SRID=4326;LINESTRING(-71.05997800827026 44.316024368364864,-71.05881929397583 44.31798950769032,-71.05731725692749 44.31884923545022,-71.05444192886353 44.31851148676115,-71.05298280715942 44.31943261497028,-71.05096578598022 44.322687152160796,-71.05045080184937 44.32449856163325,-71.04984998703003 44.32495908054771,-71.04761838912964 44.325849406864485,-71.04568719863892 44.32649411748597,-71.04306936264038 44.32753791965937,-71.04049444198608 44.327138821021585,-71.03847742080688 44.32664761897048,-71.03607416152954 44.32572660403795,-71.03517293930054 44.32554239931617,-71.03431463241577 44.32594150881567,-71.03341341018677 44.32805981378074,-71.03264093399048 44.329103588092785,-71.02929353713989 44.32984035877702,-71.02843523025513 44.33008594694842,-71.02757692337036 44.33137526797721,-71.02804899215698 44.33309431859246,-71.02783441543579 44.33459844654652,-71.02543115615845 44.33582627750024,-71.02311372756958 44.33714616710255,-71.0210108757019 44.33926406740166,-71.01598978042603 44.34328479806066,-71.01294279098511 44.3441441551062,-71.00916624069214 44.346752840399844,-71.0082221031189 44.34972966884455,-71.00689172744751 44.35107993293547,-71.00551843643188 44.351171995263435,-71.00393056869507 44.350374116950185,-71.00148439407349 44.35000586175751,-70.9984803199768 44.350374116950185,-70.99642038345337 44.35163230473401,-70.99328756332397 44.354117913402796,-70.9923005104065 44.3552225945275,-70.99212884902954 44.35678752380721,-70.99401712417603 44.357830786775374,-70.99517583847046 44.3607763701854,-70.99680662155151 44.36571602660432,-70.99599123001099 44.368722570068854,-70.99448919296265 44.36961223191264,-70.99242925643921 44.37123813071097,-70.99054098129272 44.371422191881805,-70.98955392837524 44.372986688478704,-70.99075555801392 44.37507261892906,-70.98963975906372 44.37691308409485,-70.98848104476929 44.37832406821415,-70.9874939918518 44.38086988832067,-70.98470449447632 44.382403461463625,-70.98273038864136 44.384059675338044,-70.9810996055603 44.38531713976433,-70.97848176956177 44.3864825704798,-70.97749471664429 44.38746396782254,-70.97903966903687 44.38970271892836)',
        )
        db.session.add(wild_section)

        # Create a gage
        wild_gage = Gage(
            name='Wild River at Gilead',
            slug='wild-river-gilead',
            point='SRID=4326;POINT(-70.97963511943817 44.390833083196924)',
            river=wild,
            visible=True,
            zipcode='04217',
            local_town='Gilead, ME',
            location='Wild River at Gilead above Rt 2',
            key='password')
        db.session.add(wild_gage)

        # Create a sensor
        wild_sensor = Sensor(name='Gage Height',
                             stype='usgs-height',
                             local=False,
                             remote_type='usgs',
                             remote_id='01054200',
                             gage=wild_gage)
        diamond_height = Sensor(name='Gage Height',
                                stype='usgs-height',
                                local=False,
                                remote_type='usgs',
                                remote_id='01052500')
        diamond_discharge = Sensor(name='Discharge',
                                   stype='usgs-discharge',
                                   local=False,
                                   remote_type='usgs',
                                   remote_id='01052500',
                                   remote_parameter='00060')
        rapid_cfs = Sensor(name='Rapid CFS',
                           stype='h2oline-cfs',
                           local=False,
                           remote_type='h2oline',
                           remote_id='235127')
        azicohos_level = Sensor(name='Aziscohol height',
                                stype='h2oline-height',
                                local=False,
                                remote_type='h2oline',
                                remote_id='235130',
                                remote_parameter='FT')
        neilson_flow = Sensor(name='Neilson flow',
                              stype='cehq-flow',
                              local=False,
                              remote_type='cehq',
                              remote_id='050915')
        skeena_level = Sensor(name='Skeena Level',
                              stype='cawater-level',
                              local=False,
                              remote_type='cawater',
                              remote_id='BC_08EB003')
        humber_level = Sensor(name='Humber Level',
                              stype='cawater-level',
                              local=False,
                              remote_type='cawater',
                              remote_id='NL_02YL012')
        cheakamus_discharge = Sensor(name='Cheakamus Discharge',
                                     stype='cawater-discharge',
                                     local=False,
                                     remote_type='cawater',
                                     remote_id='BC_08GA043',
                                     remote_parameter='discharge')
        canaseraga_stage = Sensor(name='Canaseraga Creek',
                                  stype='canaseraga-stage',
                                  local=False,
                                  remote_type='corps',
                                  remote_id='DSVN6')
        db.session.add(wild_sensor)
        db.session.add(diamond_height)
        db.session.add(diamond_discharge)
        db.session.add(rapid_cfs)
        db.session.add(azicohos_level)
        db.session.add(neilson_flow)
        db.session.add(skeena_level)
        db.session.add(humber_level)
        db.session.add(canaseraga_stage)

        # Create a sample
        dt = datetime.datetime.now() - datetime.timedelta(hours=12)
        wild_sample = Sample(sensor=wild_sensor, datetime=dt, value=5.8)
        db.session.add(wild_sample)
        wild_correlation = Correlation(sensor=wild_sensor,
                                       section=wild_section,
                                       minimum=3.5,
                                       low=4.0,
                                       medium=5.0,
                                       high=6.0,
                                       huge=9.0)
        db.session.add(wild_correlation)
        db.session.commit()
示例#30
0
def populate(csv_path):
    """
    This function is used for populating the application database with the data in the provided csv path.
    As of writing this, the function does Not handle re-seeding with an updated version of the same dataset.
    """
    logger.info("Populating")

    # We will store our temporary models here to avoid duplications
    cities = {}
    counties = {}
    regions = {}

    # Each row seems to reflect a city, so we create a city for each row.
    with open(csv_path, 'r') as f:
        reader = csv.reader(f, delimiter=";")

        for index, row in enumerate(reader):
            if index == 0:
                headers = row
                continue

            # Convert the row into a more usable format
            row_dict = row_to_dict(headers, row)

            # Take some keys to be used as foreign keys
            county_code = str(row_dict["Code Département"])
            region_code = str(row_dict["Code Région"])

            city = City(code_insee=row_dict["Code INSEE"],
                        population=row_dict["Population"],
                        code_postal=row_dict["Code Postal"],
                        name=row_dict["Commune"],
                        area=row_dict["Superficie"],
                        county_code=county_code)
            cities[city.code_insee] = city

            # If this county hasn't been encountered yet, we will create a model for it
            if not county_code in counties:
                county = County(code=county_code,
                                name=row_dict["Département"],
                                region_code=region_code)
                counties[county_code] = county

            # If this region hasn't been encountered yet, we will create a model for it
            if not region_code in regions:
                region = Region(code=region_code, name=row_dict["Région"])
                regions[region_code] = region

    logger.info("There are %s cities", len(cities))
    logger.info("There are %s counties", len(counties))
    logger.info("There are %s regions", len(regions))

    # We will use add_all for now, but it means we can't repopulate due to IntegrityErrors
    items = ([city for _, city in cities.items()] +
             [county for _, county in counties.items()] +
             [region for _, region in regions.items()])

    db.session.add_all(items)
    try:
        db.session.commit()
    except exc.IntegrityError:
        logger.error(
            "Failed to add the resources to the database as some already exist"
        )
        db.session.rollback()