Пример #1
0
def add_city():
    """Добавить области в БД. Запустить только один раз!"""
    raions = ('Алексинский район', 'Арсеньевский район', 'Белёвский район',
              'Богородицкий район', 'Венёвский район', 'Воловский район',
              'Дубенский район', 'Ефремовский район', 'Заокский район',
              'Каменский район', 'Кимовский район', 'Киреевский район',
              'Куркинский район', 'Ленинский район', 'Новомосковский район',
              'Одоевский район', 'Плавский район', 'Суворовский район',
              'Тёпло-Огарёвский район', 'Узловский район', 'Чернский район',
              'Щёкинский район', 'Ясногорский район')
    region = Region()
    region.name = 'Тульская область'
    region.save()
    for raion in raions:
        city = City()
        city.region = region
        city.name = raion
        city.save()
Пример #2
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()
Пример #3
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)
Пример #4
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)
Пример #5
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
Пример #6
0
    def mutate(self, info, **kwargs):
        region = Region(**kwargs)
        region.save()

        return CreateRegion(region=region, success=True)
Пример #7
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()
Пример #8
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()