source: src/mapservice/server_impl/demo/DbSetup.py@ da2be70

debian/bullseye
Last change on this file since da2be70 was ae2ff15, checked in by Joachim Langenbach <joachim.langenbach@…>, 7 years ago
  • Fixed testing environment
    • Fixed some issues in the API and it's serving
  • Property mode set to 100644
File size: 5.6 KB
RevLine 
[186affa]1'''
2 Copyright (C) 2019 - 2020 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16'''
17
18import os
19import pytz
[be66b78]20import flask
[186affa]21
22from ..database.Model import db, User, ApiKey, Water, OsmType, Gauge, Device, Log, Queue, ProcessingStatus, db_setup
23from ..tasks.tasks import log_process
24
25def setup():
26 db_setup()
27
[ae2ff15]28 print("CREATING DATABASE DEMO CONTENT...")
[186affa]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()
[ae2ff15]52
[186affa]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()
[ae2ff15]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'))
[186affa]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")
122
123def _sample_log_add(water, gauge, gauge_height, device, submitter, log_file):
124 if len(Log.query.filter_by(submitter=submitter).all()) > 0:
[ae2ff15]125 return None
[be66b78]126
[186affa]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
[ae2ff15]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
[186affa]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)
[ae2ff15]151 return log
Note: See TracBrowser for help on using the repository browser.