def show_current_session(session_id=None): if session_id is None: return redirect('/session/viewall') db = Database() # get all current entries and load up session_name = db.get_session_name(session_id) entries = [] for db_entry in db.get_entries_for_session(session_id): entry = { 'entry_id': db_entry['entry_id'], 'value': db_entry['value'], 'time_created': db_entry['time_created'] } if not is_logged_in(): entry['can_update'] = False else: if db.is_user_id_administrator( flask_session['user_id'] ) or db_entry['creator_id'] == flask_session['user_id']: entry['can_update'] = True entries.append(entry) return render_template('view_session.html', session_name=session_name, entries=entries)
def view_sessions(): db = Database() is_admin = False if is_logged_in() and db.is_user_id_administrator( flask_session['user_id']): is_admin = True sessions = [] for db_session in db.get_all_sessions(): session = { 'name': db_session['name'], 'session_id': db_session['session_id'], 'time_created': db_session['time_created'] } if is_logged_in(): session['can_resume'] = True if is_admin or flask_session['user_id'] == db_session['creator_id']: session['has_control'] = True else: session['has_control'] = False last_modified = db.last_modified(db_session['session_id'])[0] if (last_modified != None): session['last_modified'] = last_modified else: session['last_modified'] = '' sessions.append(session) return render_template('view_sessions.html', sessions=sessions)
def refresh_cache(): if is_logged_in(): user_id = get_current_user_id() db = Database() user = db.get_user_by_id(user_id) for key in user.keys(): flask_session[key] = user[key] pass
def add_freetext(): if not is_logged_in(): return 'User is not logged in; creator_id is not set' if not is_current_session_set(): return 'Current session is not set' db = Database() value = request.data.decode() category_id = db.get_category_id_by_name("FREETEXT") db.add_entry_with_category(get_current_session_id(), get_current_user_id(), category_id, value) return "OK"
def ext_get_alerts(api_key, session_id): if session_id is None or session_id == '': return "No unique session identifier provided", HTTPStatus.BAD_REQUEST.value db = Database() # db_alerts = db.get_all_alerts_and_disable_for_session_id(get_current_session_id()) db_alerts = db.get_all_alerts_and_disable_for_session_id(session_id) alerts = [] for db_alert in db_alerts: alert = { 'value': db_alert['value'], 'time_created': db_alert['time_created'] } alerts.append(alert) return json.dumps(alerts)
def ext_create_new_session(api_key): logging.debug("EXTERNAL: Create new Session") db = Database() user = db.get_user_by_api_key(api_key) if user is None: return "Provided API key ('{}') is not associated with any registered user".format( api_key), HTTPStatus.UNAUTHORIZED.value session_name = datetime.datetime.fromtimestamp( time.time()).strftime('%Y-%m-%d %H:%M:%S') logging.debug("Creating new session: %s (userid: %s)", session_name, user['user_id']) session_id = db.create_session(session_name, user['user_id']) logging.debug("New session id: %s", session_id) set_current_session(session_id, session_name) return "New Session ID: '{}'".format(session_id)
def ext_post_event(api_key, session_id): logging.debug("EXTERNAL: Event posted") payload = request.data if len(payload) == 0: return "No payload / message body received", HTTPStatus.BAD_REQUEST.value db = Database() user = db.get_user_by_api_key(api_key) if user is None: return "Provided API key ('{}') is not associated with any registered user".format( api_key), HTTPStatus.UNAUTHORIZED.value try: parsed = json.loads(payload) except json.decoder.JSONDecodeError as ex: return ex.msg, HTTPStatus.BAD_REQUEST.value if session_id is None: return "No unique session identifier provided", HTTPStatus.BAD_REQUEST.value # if get_current_session_id() is None: # ext_create_new_session(api_key) # session_name = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') # logging.debug("Creating new session: %s (userid: %s)", session_name, user['user_id']) # session_id = db.create_session(session_name, user['user_id']) # logging.debug("New session id: %s", session_id) # set_current_session(session_id, session_name) for message in parsed: try: print(message) db.add_entry(session_id, user['user_id'], message['message_data']) except (TypeError, KeyError) as ex: print(ex) return "Payload / message body has invalid formatting", HTTPStatus.BAD_REQUEST.value return "OK"
def console(): if not is_logged_in(): # REDIRECT TO LOGIN flash('You must be logged in to view this.', 'danger') return redirect('/') if not is_current_session_set(): # REDIRECT TO VIEW SESSIONS PAGE flash('You must have a current session set to view this.', 'danger') return redirect('/') db = Database() actions_ul = [] actions_ur = [] actions_ll = [] for action in db.get_entry_options_by_category_id(1): actions_ul.append({ 'value': action['value'], 'color_class': action['color_class'], 'category': action['category_name'] }) for action in db.get_entry_options_by_category_id(2): actions_ur.append({ 'value': action['value'], 'color_class': action['color_class'], 'category': action['category_name'] }) for action in db.get_entry_options_by_category_id(3): actions_ll.append({ 'value': action['value'], 'color_class': action['color_class'], 'category': action['category_name'] }) actions_ul.append({ 'value': action['value'], 'color_class': action['color_class'], 'category': action['category_name'] }) session_id = session['current_session_id'] dates = [] for db_entry in db.get_entries_for_session(session_id): dates += [getDate(db_entry['time_created'])] dates = list(sorted(set(dates), key=dates.index)) data = getData(dates, db.get_internal_entries_for_session(session_id)) return render_template('console.html', actions_ul=actions_ul, actions_ur=actions_ur, actions_ll=actions_ll, data=data)
def delete_entry(entry_id): if not is_logged_in(): return 'User is not logged in; creator_id is not set' db = Database() db_entry = db.get_entry(entry_id) if db_entry['creator_id'] == get_current_user_id( ) or db.is_user_id_administrator(get_current_user_id()): db.disable_entry(entry_id) else: return "Invalid permissions" return redirect_to_referrer()
def add_event(): if not is_logged_in(): return 'User is not logged in; creator_id is not set' if not is_current_session_set(): return 'Current session is not set' data = request.data.decode() category = data[data.index('=') + 1:data.index('\n')] data = data[data.index('\n') + 1:] db = Database() if category.upper() == "ALERT": logging.info("ALERT posted") db.add_alert(get_current_session_id(), get_current_user_id(), data) else: logging.info("EVENT posted") db.add_entry_with_category(get_current_session_id(), get_current_user_id(), db.get_category_id_by_name(category), data) return 'OK'
def modify_entry(entry_id): if not is_logged_in(): return 'User is not logged in; creator_id is not set' if not is_current_session_set(): return 'Session_id is not set' db = Database() db_entry = db.get_entry(entry_id) if db_entry['creator_id'] == get_current_user_id( ) or db.is_user_id_administrator(get_current_user_id()): new_value = request.data.decode('utf-8') db.update_entry(entry_id, new_value) return "OK" # clear entry_option_id # clear category_id?? # update user id?? (if admin changed it) else: return "Invalid permissions"
def delete_session(session_id): db = Database() db.disable_session(session_id) if is_current_session_set() and (get_current_session_id() == session_id): clear_current_session() return redirect_to_referrer()
def set_session(session_id): db = Database() name = db.get_session_name(session_id) set_current_session(session_id, name) session['session_id'] = session_id return redirect('/session/view/' + str(session_id))
def select_session(session_id): db = Database() name = db.get_session_name(session_id) set_current_session(session_id, name) session['session_id'] = session_id return redirect('/session/console')