Ignore:
Timestamp:
Apr 11, 2020, 4:46:15 PM (7 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
debian/bullseye, master
Children:
3db3d13
Parents:
da2be70
Message:
  • Added yapf style config (src/python-style.yapf)
    • Updated all files to the yapf formatted version
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/mapservice/server_impl/demo/DbSetup.py

    rda2be70 r7be94aa  
    2323from ..tasks.tasks import log_process
    2424
     25
    2526def setup():
    26         db_setup()
    27        
    28         print("CREATING DATABASE DEMO CONTENT...")
    29         with db.session.no_autoflush:
    30                 test_user_john = None
    31                 query = User.query.filter_by(username='john').all()
    32                 if len(query) == 1:
    33                         test_user_john = query[0]
    34                 else:
    35                         test_user_john = User.factory('john', 'testpass2', 'john@example.org')
    36                         test_user_john.activation_code = 'dac9630aec642a428cd73f4be0a03569'
    37                         test_user_john.license_accepted = True
    38                         db.session.add(test_user_john)
    39                         db.session.commit()
    40                
    41                 test_user_jane = None
    42                 query = User.query.filter_by(username='jane').all()
    43                 if len(query) == 1:
    44                         test_user_jane = query[0]
    45                 else:
    46                         test_user_jane = User.factory('jane', 'testpass', 'jane@example.org')
    47                         test_user_jane.activated = True
    48                         test_user_jane.first_name = 'Jane'
    49                         test_user_jane.license_accepted = True
    50                         db.session.add(test_user_jane)
    51                         db.session.commit()
    52                        
    53                 if len(ApiKey.query.filter_by(key='DelApiKey4711').all()) < 1:
    54                         db.session.add(ApiKey(user=test_user_jane, key='DelApiKey4711'))
    55                         db.session.commit()
    56                 if len(ApiKey.query.filter_by(key='test_key_jane').all()) < 1:
    57                         db.session.add(ApiKey(user=test_user_jane, key='test_key_jane'))
    58                         db.session.commit()
    59                
    60                 mississippi = None
    61                 query = Water.query.filter_by(name='Mississippi').all()
    62                 if len(query) == 1:
    63                         mississippi = query[0]
    64                 else:
    65                         mississippi = Water(name='Mississippi', manager=test_user_jane, osm_id=1756854, osm_type=OsmType.Relation)
    66                         db.session.add(mississippi)
    67                         db.session.commit()
    68                
    69                 if len(Water.query.filter_by(name='Forggensee').all()) < 1:
    70                         forggensee = Water(name='Forggensee', manager=test_user_john, osm_id=897899, osm_type=OsmType.Relation)
    71                         db.session.add(forggensee)
    72                         db.session.commit()
    73                
    74                 pfuhlersee = None
    75                 query = Water.query.filter_by(name='Pfuhler See').all()
    76                 if len(query) == 1:
    77                         pfuhlersee = query[0]
    78                 else:
    79                         pfuhlersee = Water(name='Pfuhler See', manager=test_user_jane, osm_id=225888565, osm_type=OsmType.Way)
    80                         db.session.add(pfuhlersee)
    81                         db.session.commit()
    82                
    83                 new_orleans = None
    84                 query = Gauge.query.filter_by(name='New Orleans').all()
    85                 if len(query) == 1:
    86                         new_orleans = query[0]
    87                 else:
    88                         new_orleans = Gauge(name='New Orleans', manager=test_user_jane, water=mississippi, location='SRID=4326;POINT(-90.136111 29.934722)', reference_water_level_gauge_height=3)
    89                         db.session.add(new_orleans)
    90                         db.session.commit()
    91                        
    92                 pfuhlersee_gauge = None
    93                 query = Gauge.query.filter_by(name='Stairs at Kiosk').all()
    94                 if len(query) == 1:
    95                         pfuhlersee_gauge = query[0]
    96                 else:
    97                         pfuhlersee_gauge = Gauge(name='Stairs at Kiosk', manager=test_user_jane, water=pfuhlersee, location='SRID=4326;POINT(10.0427 48.4275)', reference_water_level_gauge_height=0.4, comment='Level zero is the level of the step where the handrail is fixed.')
    98                         db.session.add(pfuhlersee_gauge)
    99                         db.session.commit()
    100                
    101                 gnuboat_one = None
    102                 query = Device.query.filter_by(name='GnuBoat 1').all()
    103                 if len(query) == 1:
    104                         gnuboat_one = query[0]
    105                 else:
    106                         gnuboat_one = Device(name='GnuBoat 1', owner=test_user_jane, sensor_depth=0.2, comment='SUP attachment')
    107                         db.session.add(gnuboat_one)
    108                         db.session.commit()
    109                
    110                 log_file = 'sample-logs/nmea0183-2017-12-09.log'
    111                 sample_log = os.path.join('/usr/share/doc/gnuboat', log_file)
    112                 if os.path.isfile(sample_log):
    113                         _sample_log_add(pfuhlersee, pfuhlersee_gauge, 0, gnuboat_one, test_user_jane, sample_log)
    114                 else:
    115                         sample_log = os.path.join(os.path.dirname(__file__), '../../../../../../', log_file)
    116                         if os.path.isfile(sample_log):
    117                                 _sample_log_add(pfuhlersee, pfuhlersee_gauge, 0, gnuboat_one, test_user_jane, sample_log)
    118                         else:
    119                                 print('Demo log not found --> Skipping')
    120                
    121                 print("...Finished")
     27    db_setup()
     28
     29    print("CREATING DATABASE DEMO CONTENT...")
     30    with db.session.no_autoflush:
     31        test_user_john = None
     32        query = User.query.filter_by(username='john').all()
     33        if len(query) == 1:
     34            test_user_john = query[0]
     35        else:
     36            test_user_john = User.factory(
     37                'john', 'testpass2', 'john@example.org'
     38            )
     39            test_user_john.activation_code = 'dac9630aec642a428cd73f4be0a03569'
     40            test_user_john.license_accepted = True
     41            db.session.add(test_user_john)
     42            db.session.commit()
     43
     44        test_user_jane = None
     45        query = User.query.filter_by(username='jane').all()
     46        if len(query) == 1:
     47            test_user_jane = query[0]
     48        else:
     49            test_user_jane = User.factory(
     50                'jane', 'testpass', 'jane@example.org'
     51            )
     52            test_user_jane.activated = True
     53            test_user_jane.first_name = 'Jane'
     54            test_user_jane.license_accepted = True
     55            db.session.add(test_user_jane)
     56            db.session.commit()
     57
     58        if len(ApiKey.query.filter_by(key='DelApiKey4711').all()) < 1:
     59            db.session.add(ApiKey(user=test_user_jane, key='DelApiKey4711'))
     60            db.session.commit()
     61        if len(ApiKey.query.filter_by(key='test_key_jane').all()) < 1:
     62            db.session.add(ApiKey(user=test_user_jane, key='test_key_jane'))
     63            db.session.commit()
     64
     65        mississippi = None
     66        query = Water.query.filter_by(name='Mississippi').all()
     67        if len(query) == 1:
     68            mississippi = query[0]
     69        else:
     70            mississippi = Water(
     71                name='Mississippi',
     72                manager=test_user_jane,
     73                osm_id=1756854,
     74                osm_type=OsmType.Relation
     75            )
     76            db.session.add(mississippi)
     77            db.session.commit()
     78
     79        if len(Water.query.filter_by(name='Forggensee').all()) < 1:
     80            forggensee = Water(
     81                name='Forggensee',
     82                manager=test_user_john,
     83                osm_id=897899,
     84                osm_type=OsmType.Relation
     85            )
     86            db.session.add(forggensee)
     87            db.session.commit()
     88
     89        pfuhlersee = None
     90        query = Water.query.filter_by(name='Pfuhler See').all()
     91        if len(query) == 1:
     92            pfuhlersee = query[0]
     93        else:
     94            pfuhlersee = Water(
     95                name='Pfuhler See',
     96                manager=test_user_jane,
     97                osm_id=225888565,
     98                osm_type=OsmType.Way
     99            )
     100            db.session.add(pfuhlersee)
     101            db.session.commit()
     102
     103        new_orleans = None
     104        query = Gauge.query.filter_by(name='New Orleans').all()
     105        if len(query) == 1:
     106            new_orleans = query[0]
     107        else:
     108            new_orleans = Gauge(
     109                name='New Orleans',
     110                manager=test_user_jane,
     111                water=mississippi,
     112                location='SRID=4326;POINT(-90.136111 29.934722)',
     113                reference_water_level_gauge_height=3
     114            )
     115            db.session.add(new_orleans)
     116            db.session.commit()
     117
     118        pfuhlersee_gauge = None
     119        query = Gauge.query.filter_by(name='Stairs at Kiosk').all()
     120        if len(query) == 1:
     121            pfuhlersee_gauge = query[0]
     122        else:
     123            pfuhlersee_gauge = Gauge(
     124                name='Stairs at Kiosk',
     125                manager=test_user_jane,
     126                water=pfuhlersee,
     127                location='SRID=4326;POINT(10.0427 48.4275)',
     128                reference_water_level_gauge_height=0.4,
     129                comment=
     130                'Level zero is the level of the step where the handrail is fixed.'
     131            )
     132            db.session.add(pfuhlersee_gauge)
     133            db.session.commit()
     134
     135        gnuboat_one = None
     136        query = Device.query.filter_by(name='GnuBoat 1').all()
     137        if len(query) == 1:
     138            gnuboat_one = query[0]
     139        else:
     140            gnuboat_one = Device(
     141                name='GnuBoat 1',
     142                owner=test_user_jane,
     143                sensor_depth=0.2,
     144                comment='SUP attachment'
     145            )
     146            db.session.add(gnuboat_one)
     147            db.session.commit()
     148
     149        log_file = 'sample-logs/nmea0183-2017-12-09.log'
     150        sample_log = os.path.join('/usr/share/doc/gnuboat', log_file)
     151        if os.path.isfile(sample_log):
     152            _sample_log_add(
     153                pfuhlersee, pfuhlersee_gauge, 0, gnuboat_one, test_user_jane,
     154                sample_log
     155            )
     156        else:
     157            sample_log = os.path.join(
     158                os.path.dirname(__file__), '../../../../../../', log_file
     159            )
     160            if os.path.isfile(sample_log):
     161                _sample_log_add(
     162                    pfuhlersee, pfuhlersee_gauge, 0, gnuboat_one,
     163                    test_user_jane, sample_log
     164                )
     165            else:
     166                print('Demo log not found --> Skipping')
     167
     168        print("...Finished")
     169
    122170
    123171def _sample_log_add(water, gauge, gauge_height, device, submitter, log_file):
    124         if len(Log.query.filter_by(submitter=submitter).all()) > 0:
    125                 return None
    126        
    127         log = Log(submitter=submitter, water=water, device=device, gauge=gauge, gauge_height=0, comment='Demo Log from {}'.format(log_file))
    128         db.session.add(log)
    129         db.session.commit()
    130        
    131         if not 'CELERY_BROKER_URL' in flask.current_app.config or not flask.current_app.config['CELERY_BROKER_URL']:
    132                 print('Do not add log, since their is no CELERY_BROKER_URL configured')
    133                 return log
    134        
    135         with open(log_file, 'r') as logfile:
    136                 data = logfile.read()
    137  
    138         processing_state = ProcessingStatus.query.filter_by(label='Queued for Parsing').all()
    139         if len(processing_state) != 1:
    140                 raise MapserviceBadRequest(detail='Processing status Queued for Parsing not found database')
    141         processing_state = processing_state[0]
    142        
    143         # Add Log to Queued table and change the processing state of Log
    144         queue = Queue(log=log)
    145         db.session.add(queue)
    146         log.processingstatus = processing_state
    147         db.session.commit()
    148        
    149         # request log parsing
    150         log_process.delay(log.id, data)
    151         return log
     172    if len(Log.query.filter_by(submitter=submitter).all()) > 0:
     173        return None
     174
     175    log = Log(
     176        submitter=submitter,
     177        water=water,
     178        device=device,
     179        gauge=gauge,
     180        gauge_height=0,
     181        comment='Demo Log from {}'.format(log_file)
     182    )
     183    db.session.add(log)
     184    db.session.commit()
     185
     186    if not 'CELERY_BROKER_URL' in flask.current_app.config or not flask.current_app.config[
     187        'CELERY_BROKER_URL']:
     188        print('Do not add log, since their is no CELERY_BROKER_URL configured')
     189        return log
     190
     191    with open(log_file, 'r') as logfile:
     192        data = logfile.read()
     193
     194    processing_state = ProcessingStatus.query.filter_by(
     195        label='Queued for Parsing'
     196    ).all()
     197    if len(processing_state) != 1:
     198        raise MapserviceBadRequest(
     199            detail='Processing status Queued for Parsing not found database'
     200        )
     201    processing_state = processing_state[0]
     202
     203    # Add Log to Queued table and change the processing state of Log
     204    queue = Queue(log=log)
     205    db.session.add(queue)
     206    log.processingstatus = processing_state
     207    db.session.commit()
     208
     209    # request log parsing
     210    log_process.delay(log.id, data)
     211    return log
Note: See TracChangeset for help on using the changeset viewer.