def initialize(): init_db() with transaction.manager: settings = Session.query(ImporterSettings).first() if not settings: settings = ImporterSettings() settings.id = 1 Session.add(settings) Session.flush()
def settings(): settings_query = Session.query(ImporterSettings).first() if request.method == 'POST': settings_query.csv_store_pat = request.form['csv_store_path'] settings_query.device_file_name = request.form['device_file_name'] settings_query.content_file_name = request.form['content_file_name'] settings_query.default_csv_delimiter = request.form[ 'default_csv_delimiter'] Session.flush() context = {'settings': settings_query} return render_template('settings.jinja2', **context)
def import_device_content_data(self): """ Method used for importing DeviceContent data from CSV file. """ csv_file_rows = self.read_csv_file(self.content_file_name) if not csv_file_rows: return None for index, row in enumerate(csv_file_rows, start=1): parsed_row = self.get_parsed_device_content_row(row, index) if not parsed_row: continue device_content = Session.query(DeviceContent).filter( DeviceContent.id == parsed_row['id']).first() if device_content: if device_content.expire_date < parsed_row['expire_date']: device_content.name = parsed_row['name'] device_content.description = parsed_row['description'] device_content.expire_date = parsed_row['expire_date'] device_content.status = parsed_row['status'] device_content.device_id = parsed_row['device_id'] device_content.date_updated = now() Session.flush() else: device_content = DeviceContent() device_content.id = parsed_row['id'] device_content.device_id = parsed_row['device_id'] device_content.name = parsed_row['name'] device_content.description = parsed_row['description'] device_content.expire_date = parsed_row['expire_date'] device_content.status = parsed_row['status'] Session.add(device_content) Session.flush()
def get_parsed_device_row(self, row, row_number): """ Method used for getting dict of Device data from CSV row. :param row: CSV Device data row, list :param row_number: integer :return: dict { 'id': integer, required, 'name': string or None for invalid data, 'description': string or None for invalid data, 'code': string or None for invalid data, 'expire_date': datetime or None for invalid data, 'status': integer or None for invalid data } or None for error """ if len(row) < 6: csv_error_logger.error( u'Device row {0} has invalid number of columns'.format( row_number)) return None try: device_id = str(row[0]) if not device_id.isdigit(): csv_error_logger.error( u'Device in row {0} has invalid ID: {1}'.format( row_number, row[0])) return None except Exception as e: csv_error_logger.error( u'Device in row {0} has invalid ID: {1}'.format(row_number, e)) return None device_name = row[1] name_len = len(device_name) if name_len > 32 or name_len < 1: csv_error_logger.error( u'Device in row {0} has invalid name: {1}'.format( row_number, device_name)) device_name = None device_description = row[2] if len(device_description) < 1: csv_error_logger.error( u'Device in row {0} has invalid description: {1}'.format( row_number, device_description)) device_description = None device_code = row[3] if len(device_code) > 30: csv_error_logger.error( u'Device in row {0} has invalid code: {1}'.format( row_number, device_code)) device_code = None if device_code: code_exists = Session.query(Device).filter( Device.code == device_code).first() if code_exists: csv_error_logger.error( u'Code for device in row {0} has duplicate code: {1}'. format(row_number, device_code)) device_code = None try: device_expire_date = parser.parse(row[4]) except Exception as e: csv_error_logger.error( u'Device in row {0} has invalid expire date: {1}'.format( row_number, row[4])) device_expire_date = None status_name = row[5] device_status = StatusConstants.get_mapped_status().get( status_name, None) if not device_status: csv_error_logger.error( u'Device in row {0} has invalid status: {1}'.format( row_number, status_name)) device_status = None return { 'id': device_id, 'name': device_name, 'description': device_description, 'code': device_code, 'expire_date': device_expire_date, 'status': device_status }
def set_settings(self): settings = Session.query(ImporterSettings).first() self.csv_store_path = settings.csv_store_path self.device_file_name = settings.device_file_name self.content_file_name = settings.content_file_name self.default_csv_delimiter = settings.default_csv_delimiter
def index(): devices = Session.query(Device).all() context = {'devices': devices} return render_template('index.jinja2', **context)
def device_content(): device_content_query = Session.query(DeviceContent).all() context = {'device_content': device_content_query} return render_template('device_content.jinja2', **context)