def export_model(self, out, session, selected_model=''): model = Session.resolve_model(selected_model) cols = [getattr(model, col.name) for col in model.__table__.columns] out.writerow([col.name for col in cols]) for attendee in session.query(model).all(): row = [] for col in cols: if isinstance(col.type, Choice): # Choice columns are integers with a single value with an automatic # _label property, e.g. the "shirt" column has a "shirt_label" # property, so we'll use that. row.append(getattr(attendee, col.name + '_label')) elif isinstance(col.type, MultiChoice): # MultiChoice columns are comma-separated integer lists with an # automatic _labels property which is a list of string labels. # So we'll get that and then separate the labels with slashes. row.append(' / '.join(getattr(attendee, col.name + '_labels'))) elif isinstance(col.type, UTCDateTime): # Use the empty string if this is null, otherwise use strftime. # Also you should fill in whatever actual format you want. val = getattr(attendee, col.name) row.append(val.strftime('%Y-%m-%d %H:%M:%S') if val else '') else: # For everything else we'll just dump the value, although we might # consider adding more special cases for things like foreign keys. row.append(getattr(attendee, col.name)) out.writerow(row)
def export_model(self, out, session, selected_model=''): model = Session.resolve_model(selected_model) cols = [getattr(model, col.name) for col in model.__table__.columns] out.writerow([col.name for col in cols]) for attendee in session.query(model).all(): row = [] for col in cols: if isinstance(col.type, Choice): # Choice columns are integers with a single value with an automatic # _label property, e.g. the "shirt" column has a "shirt_label" # property, so we'll use that. row.append(getattr(attendee, col.name + '_label')) elif isinstance(col.type, MultiChoice): # MultiChoice columns are comma-separated integer lists with an # automatic _labels property which is a list of string labels. # So we'll get that and then separate the labels with slashes. row.append(' / '.join( getattr(attendee, col.name + '_labels'))) elif isinstance(col.type, UTCDateTime): # Use the empty string if this is null, otherwise use strftime. # Also you should fill in whatever actual format you want. val = getattr(attendee, col.name) row.append( val.strftime('%Y-%m-%d %H:%M:%S') if val else '') else: # For everything else we'll just dump the value, although we might # consider adding more special cases for things like foreign keys. row.append(getattr(attendee, col.name)) out.writerow(row)
def undo_delete(self, session, id, message='', page='1', who='', what='', action=''): if cherrypy.request.method == "POST": model_class = None tracked_delete = session.query(Tracking).get(id) if tracked_delete.action != c.DELETED: message = 'Only a delete can be undone' else: model_class = Session.resolve_model(tracked_delete.model) if model_class: params = json.loads(tracked_delete.snapshot) model_id = params.get('id').strip() if model_id: existing_model = session.query(model_class).filter( model_class.id == model_id).first() if existing_model: message = '{} has already been undeleted'.format(tracked_delete.which) else: model = model_class(id=model_id).apply(params, restricted=False) else: model = model_class().apply(params, restricted=False) if not message: session.add(model) message = 'Successfully undeleted {}'.format(tracked_delete.which) else: message = 'Could not resolve {}'.format(tracked_delete.model) raise HTTPRedirect('feed?page={}&who={}&what={}&action={}&message={}', page, who, what, action, message)
def fk(self): try: from uber.models import Session model_class = Session.resolve_model(self.model) query = self.session.query(model_class) return query.filter_by(id=self.fk_id).first() except: return None
def _get_guidebook_models(session, selected_model=''): model = selected_model.split('_')[0] if '_' in selected_model else selected_model model_query = session.query(Session.resolve_model(model)) if '_band' in selected_model: return model_query.filter_by(group_type=c.BAND) elif '_guest' in selected_model: return model_query.filter_by(group_type=c.GUEST) elif '_dealer' in selected_model: return model_query.filter_by(is_dealer=True) elif '_panels' in selected_model: return model_query.filter(Event.location.in_(c.PANEL_ROOMS)) elif 'Game' in selected_model: return model_query.filter_by(has_been_accepted=True) else: return model_query
def import_model(self, session, model_import, selected_model='', date_format="%Y-%m-%d"): model = Session.resolve_model(selected_model) message = '' cols = { col.name: getattr(model, col.name) for col in model.__table__.columns } result = csv.DictReader( model_import.file.read().decode('utf-8').split('\n')) id_list = [] for row in result: if 'id' in row: id = row.pop('id') # id needs special treatment try: # get the instance if it already exists model_instance = getattr(session, selected_model)( id, allow_invalid=True) except Exception: session.rollback() # otherwise, make a new one and add it to the session for when we commit model_instance = model() session.add(model_instance) for colname, val in row.items(): col = cols[colname] if not val: # in a lot of cases we'll just have the empty string, so we'll just # do nothing for those cases continue if isinstance(col.type, Boolean): if isinstance(val, six.string_types): val = val.strip().lower() not in ('f', 'false', 'n', 'no', '0') else: val = bool(val) elif isinstance(col.type, Choice): # the export has labels, and we want to convert those back into their # integer values, so let's look that up (note: we could theoretically # modify the Choice class to do this automatically in the future) label_lookup = { val: key for key, val in col.type.choices.items() } val = label_lookup[val] elif isinstance(col.type, MultiChoice): # the export has labels separated by ' / ' and we want to convert that # back into a comma-separate list of integers label_lookup = {val: key for key, val in col.type.choices} vals = [label_lookup[label] for label in val.split(' / ')] val = ','.join(map(str, vals)) elif isinstance(col.type, UTCDateTime): # we'll need to make sure we use whatever format string we used to # export this date in the first place try: val = UTC.localize( datetime.strptime(val, date_format + ' %H:%M:%S')) except Exception: val = UTC.localize(datetime.strptime(val, date_format)) elif isinstance(col.type, Date): val = datetime.strptime(val, date_format).date() elif isinstance(col.type, Integer): val = int(val) # now that we've converted val to whatever it actually needs to be, we # can just set it on the model setattr(model_instance, colname, val) try: session.commit() except Exception: log.error('ImportError', exc_info=True) session.rollback() message = 'Import unsuccessful' id_list.append(model_instance.id) all_instances = session.query(model).filter( model.id.in_(id_list)).all() if id_list else None return self.index(message, all_instances)
def export_model(self, out, session, selected_model=''): model = Session.resolve_model(selected_model) rows = prepare_model_export(model, filtered_models=session.query(model).all()) for row in rows: out.writerow(row)
def import_model(self, session, model_import, selected_model='', date_format="%Y-%m-%d"): model = Session.resolve_model(selected_model) message = '' cols = { col.name: getattr(model, col.name) for col in model.__table__.columns } result = csv.DictReader( model_import.file.read().decode('utf-8').split('\n')) id_list = [] for row in result: if 'id' in row: id = row.pop('id') # id needs special treatment try: # get the instance if it already exists model_instance = getattr(session, selected_model)( id, allow_invalid=True) except Exception: session.rollback() # otherwise, make a new one and add it to the session for when we commit model_instance = model() session.add(model_instance) else: model_instance = model() session.add(model_instance) for colname, val in row.items(): col = cols[colname] if not val: # in a lot of cases we'll just have the empty string, so we'll just # do nothing for those cases continue if isinstance(col.type, Boolean): if isinstance(val, six.string_types): val = val.strip().lower() not in ('f', 'false', 'n', 'no', '0') else: val = bool(val) elif isinstance(col.type, UTCDateTime): # we'll need to make sure we use whatever format string we used to # export this date in the first place try: val = UTC.localize( datetime.strptime(val, date_format + ' %H:%M:%S')) except Exception: val = UTC.localize(datetime.strptime(val, date_format)) elif isinstance(col.type, Date): val = datetime.strptime(val, date_format).date() elif isinstance(col.type, Integer): val = int(val) elif isinstance(col.type, JSONB): val = json.loads(val) # now that we've converted val to whatever it actually needs to be, we # can just set it on the model setattr(model_instance, colname, val) try: session.commit() except Exception: log.error('ImportError', exc_info=True) session.rollback() message = 'Import unsuccessful' id_list.append(model_instance.id) all_instances = session.query(model).filter( model.id.in_(id_list)).all() if id_list else None return self.csv_import(message, all_instances)
def model_class(self): if self.model and self.model != 'n/a': from uber.models import Session return Session.resolve_model(self.model) else: return None
def import_model(self, session, model_import, selected_model='', date_format="%Y-%m-%d"): model = Session.resolve_model(selected_model) message = '' cols = {col.name: getattr(model, col.name) for col in model.__table__.columns} result = csv.DictReader(model_import.file.read().decode('utf-8').split('\n')) id_list = [] for row in result: if 'id' in row: id = row.pop('id') # id needs special treatment try: # get the instance if it already exists model_instance = getattr(session, selected_model)(id, allow_invalid=True) except Exception: session.rollback() # otherwise, make a new one and add it to the session for when we commit model_instance = model() session.add(model_instance) for colname, val in row.items(): col = cols[colname] if not val: # in a lot of cases we'll just have the empty string, so we'll just # do nothing for those cases continue if isinstance(col.type, Boolean): if isinstance(val, six.string_types): val = val.strip().lower() not in ('f', 'false', 'n', 'no', '0') else: val = bool(val) elif isinstance(col.type, Choice): # the export has labels, and we want to convert those back into their # integer values, so let's look that up (note: we could theoretically # modify the Choice class to do this automatically in the future) label_lookup = {val: key for key, val in col.type.choices.items()} val = label_lookup[val] elif isinstance(col.type, MultiChoice): # the export has labels separated by ' / ' and we want to convert that # back into a comma-separate list of integers label_lookup = {val: key for key, val in col.type.choices} vals = [label_lookup[label] for label in val.split(' / ')] val = ','.join(map(str, vals)) elif isinstance(col.type, UTCDateTime): # we'll need to make sure we use whatever format string we used to # export this date in the first place try: val = UTC.localize(datetime.strptime(val, date_format + ' %H:%M:%S')) except Exception: val = UTC.localize(datetime.strptime(val, date_format)) elif isinstance(col.type, Date): val = datetime.strptime(val, date_format).date() elif isinstance(col.type, Integer): val = int(val) # now that we've converted val to whatever it actually needs to be, we # can just set it on the model setattr(model_instance, colname, val) try: session.commit() except Exception: log.error('ImportError', exc_info=True) session.rollback() message = 'Import unsuccessful' id_list.append(model_instance.id) all_instances = session.query(model).filter(model.id.in_(id_list)).all() if id_list else None return self.index(message, all_instances)