def remove_config(): # get id from URL: /config?id=id_ id_ = request.args.get('id', -1) # get confirmation for removal confirm = request.args.get('confirm', 0) # cast confirm as int try: confirm = int(confirm) except: confirm = 0 if confirm > 0: # remove configuration HardwareConnections.query.filter_by(id=id_).delete() # commit change to database try: db_session.commit() except IntegrityError as e: db_session.rollback() print str(e) except SQLAlchemyError as e: db_session.rollback() print str(e) # redirect to configurations page return redirect(url_for('configurations'))
lhc.caen_board_24_channel_48 = 'WC1' lhc.caen_board_24_channel_49 = 'WC2' lhc.caen_board_24_channel_50 = 'WC3' lhc.caen_board_24_channel_51 = 'WC4' lhc.caen_board_24_channel_52 = 'BEAMON' lhc.caen_board_24_channel_53 = 'TOFUS' lhc.caen_board_24_channel_54 = 'TOFDS' lhc.caen_board_24_channel_55 = 'PUNCH' lhc.caen_board_24_channel_56 = 'HALO' lhc.caen_board_24_channel_57 = 'PULSER' lhc.caen_board_24_channel_58 = 'COSMICON' lhc.caen_board_24_channel_59 = 'COSMIC' lhc.caen_board_24_channel_60 = 'PILEUP' lhc.caen_board_24_channel_61 = 'MICHEL' lhc.caen_board_24_channel_62 = 'LARSCINT' lhc.caen_board_24_channel_63 = 'MURS' db_session.add(lhc) try: db_session.commit() except IntegrityError as e: db_session.rollback() print str(e) except SQLAlchemyError as e: db_session.rollback() print str(e) db_session.remove()
def submit_config(): if request.method == 'POST': keys = [] submission = request.form id_ = submission.get('id_', '') date_time = submission.get('date-time', '') if date_time == '': response = make_response(redirect(url_for('add_config'))) return response date_time = datetime.strptime(date_time, '%m/%d/%Y %I:%M %p') for key, value in submission.iteritems(): if value: keys.append(key) #print key, value result = HardwareConnections.query.get(id_) if not result: response = make_response(redirect(url_for('add_config'))) return response lhc_dict = dict(result.__dict__) lhc_dict.pop('_sa_instance_state', None) lhc_dict.pop('id', None) lhc_dict.pop('date_time', None) lhc_dict.pop('date_time_added', None) lhc_dict.pop('date_time_updated', None) db_session.remove() for key in keys: lhc_dict[key] = submission.get(key) #print key, submission.get(key) keys.extend(lhc_dict.keys()) keys = list(set(keys)) lhc = HardwareConnections( date_time=date_time, date_time_added=datetime.now() ) for key, value in lhc_dict.iteritems(): if value == 'None': setattr(lhc, key, None) else: setattr(lhc, key, value) db_session.add(lhc) try: db_session.commit() except IntegrityError as e: db_session.rollback() print str(e) except SQLAlchemyError as e: db_session.rollback() print str(e) db_session.remove() response = make_response(redirect(url_for('configurations'))) return response return 'Error!'