def new_intervention_ems(event_id): event = Event.query.get_or_404(event_id) intervention_ems = InterventionEMS(event_id=event_id, unit_progressive=event.unit_dispatched + 1) db.session.add(intervention_ems) db.session.commit() log_cad(db, priority=1, event_id=event.id, intervention_id=intervention_ems.id, log_action='intervention_ems_created', log_message='InterventionEMS ' + str(intervention_ems.id) + ' Created for Event ' + str(event_id)) event.unit_dispatched += 1 event.interventions_ems.append(intervention_ems) db.session.add(event) db.session.commit() if request.json: intervention_ems.import_data(request.json) db.session.add(intervention_ems) db.session.commit() return {}, 201, {'Location': event.get_url()}
def update_phase(self, data): if data.get('phase') is not None and data.get('phase') in ['IN', 'PA', 'AR', 'CA', 'FIN']: set_field(self, field='date_' + data.get('phase'), data=datetime.datetime.now()) self.updated = datetime.datetime.now() log_cad(db=db, priority=2, event_id=self.event_id, intervention_ems_id=self.id, log_action='InterventionEMS Phase Modified', log_message="{" + "'date_" + data.get('phase') + ": '" + str(datetime.datetime.now()) + "'}")
def import_data(self, data, log=False): fields = get_fields(self) updated = False update_address = update_lat_lng = False for field in fields: if data.get(field) is not None: updated = True if field in ['lat', 'lng']: update_address = True continue elif field in ['current_address']: if data.get(field) is '': set_field(self, 'lat', None) set_field(self, 'lng', None) set_field(self, 'current_address', None) else: set_field(self, 'current_address', data[field]) update_lat_lng = True continue elif field in ['active']: if data[field] is 'True': set_field(self, field, True) elif data[field] is 'False': set_field(self, field, False) continue else: set_field(self, field, data[field]) if update_address: from cad.utils import get_formatted_address set_field(self, 'current_address', get_formatted_address('{} {}'.format(self.lat, self.lng))) if update_lat_lng: from cad.utils import get_lat_lng, get_formatted_address lat, lng = get_lat_lng(self.current_address) set_field(self, 'current_address', get_formatted_address(self.current_address)) set_field(self, 'lat', lat) set_field(self, 'lng', lng) if updated and not log: log_cad(db=db, priority=1, intervention_ems_id=self.intervention_dispatched, event_id=self.event_dispatched, log_action='Unit Data Modified', log_message=str(data)) return self
def new_unit(): unit = Unit() db.session.add(unit) db.session.commit() log_cad(db=db, priority=1, log_action='Unit Created') if request.json: unit = Unit.query.get_or_404(unit.id) unit.import_data(request.json) db.session.add(unit) db.session.commit() return {}, 201, {'Location': unit.get_url()}
def new_user(): user = User() db.session.add(user) db.session.commit() log_cad(db=db, priority=1, log_action='User Created') if request.json: user = User.query.get_or_404(user.id) user.import_data(request.json) db.session.add(user) db.session.commit() return {}, 201, {'Location': user.get_url()}
def new_event(): event = Event() db.session.add(event) db.session.commit() log_cad(db, event_id=event.id, log_action='event_created') if request.json: event = Event.query.get_or_404(event.id) event.import_data(request.json) db.session.add(event) db.session.commit() return {}, 201, {'Location': event.get_url()}
def import_data(self, data): fields = get_fields(self) updated = False for field in fields: if data.get(field) is not None: updated = True if field is 'password_hash' or 'password': self.set_password(data[field]) else: # Caso generico set_field(self, field, data[field]) if updated: log_cad(db=db, log_action='User Data Modified', log_message=str(data)) return self
def import_data(self, data): fields = get_fields(self) updated = False log_priority = 1 for field in fields: if data.get(field) is not None: if field in ['alarmed', 'blu_event', 'is_editing', 'is_managed']: if data[field] == 'True': set_field(self, field, True) elif data[field] == 'False': set_field(self, field, False) if field in ['blu_event']: log_priority = 2 updated = True continue elif field in ['active']: if data[field] == 'True': set_field(self, field, True) elif data[field] == 'False': set_field(self, 'closed', datetime.datetime.now()) set_field(self, 'active', False) updated = True continue elif field in ['date_IN', 'date_PA', 'date_AR', 'date_CA', 'date_FIN']: from dateutil import parser set_field(self, field, parser.parse(data[field])) updated = True continue elif field in ['id', 'event_id', 'unit_id', 'unit_progressive']: log_cad(db=db, priority=5, level='WARN', event_id=self.event_id, intervention_ems_id=self.id, unit_id=self.unit_id, log_action='Try to edit non editable data', log_message="{'" + field + "': '" + data[field] + "'}") continue else: set_field(self, field, data[field]) updated = True if updated: self.updated = datetime.datetime.now() log_cad(db=db, priority=log_priority, event_id=self.event_id, intervention_ems_id=self.id, unit_id=self.unit_id, log_action='InterventionEMS Data Modified', log_message=str(data)) return self
def import_data(self, data): fields = get_fields(self) updated = False for field in fields: if data.get(field) is not None: if field is 'active': if data[field] == 'True': set_field(self, 'active', True) elif data[field] == 'False': set_field(self, 'active', False) updated = True elif field is 'place' or 'code' or 'criticity': old_place = getattr(self, 'place') or '*' old_code = getattr(self, 'code') or '*' old_criticity = getattr(self, 'criticity') or '*' place = data.get('place') or '*' code = data.get('code') or '*' criticity = data.get('criticity') or '*' formatted_code = (place or old_place) + (code or old_code) + (criticity or old_criticity) set_field(self, field, data[field]) set_field(self, 'formatted_code', formatted_code) updated = True elif field is 'lat' or 'lng': set_field(self, field, float(data[field])) updated = True elif field is 'managing_user': set_field(self, field, int(data[field])) updated = True elif field is 'geolocation_data': set_field(self, field, int(data[field])) updated = True elif field is 'id' or 'unit_dispatched' or 'created': # Questi campi non possono essere modificati manualmente # ma sono settati automaticamente dal sistema print("Non modificabile") pass else: # Caso generico set_field(self, field, data[field]) updated = True if updated: self.updated = datetime.datetime.now() log_cad(db=db, event_id=self.id, log_action='Event Data Modified', log_message=str(data)) return self
unit3 = Unit(created_by=1, call_sign='MODENA 1', profile='ILS', type='AMBULANZA', current_address="via Emilia Est 590, Modena", active=True) db.session.add(unit1) db.session.add(unit2) db.session.add(unit3) db.session.commit() if __name__ == '__main__': app = create_app(os.environ.get('FLASK_CONFIG', 'development')) with app.app_context(): db.create_all() # create a development user if User.query.get(1) is None: user = User(username='******') user.set_password('cat') db.session.add(user) db.session.commit() init_db() log_cad(db, log_action='First User Created') app.run()