def login(): form = LoginForm() if form.validate_on_submit(): user_name = form.username.data password = form.password.data user_model = UserModel(db.get_connection()) exists = user_model.exists(user_name, password) print(exists) if (exists[0]): session['username'] = user_name session['user_id'] = exists[1] current_user_mode = exists[2] return redirect("/index") return render_template('login.html', title='Авторизация', form=form)
def reset_state(self): self.asked_for_preference = False self.selected_restaurand = None self.user_model = UserModel() self.current_state = ConverstationSates.Information self.previous_response = '' self.restaurant_info.reset()
def put(self, id): data = request.get_json() user = UserModel.find_by_id(id) user.username = data["username"] user.password = data["password"] db.session.add(user) db.session.commit() return user.json()
def register(): form = RegisterForm() if form.validate_on_submit(): name = form.username.data password = form.password.data passwordagain = form.password_again.data email = form.email.data user_type = form.mode.data if password == passwordagain: um = UserModel(db.get_connection()) um.insert(name, password, email, user_type) exi = um.exists(name, password) session['username'] = name session['user_id'] = exi[1] return redirect("/succes_register") else: return redirect("/passw_dont_match") return render_template('register.html', form=form)
def is_exit(self, name, password): user = UserModel(name, password) sql = "select password from users where name = %s" self.con.cur.execute(sql, [name]) data = self.con.cur.fetchone() if not data: return (False, None) else: return (True, data[0])
def load(cls, json): instance = StorageModel() entity_json = json['entity'] if 'entity' in json else None permissions_json = json[ 'permissions'] if 'permissions' in json else None if not entity_json: return None instance.identifier = entity_json['id'] instance.name = entity_json['name'] instance.mask = entity_json['mask'] instance.type = entity_json['type'] instance.path = entity_json[ 'pathMask'] if 'pathMask' in entity_json else None if 'owner' in entity_json and entity_json['owner'].lower( ) != 'unauthorized': user = UserModel() user.username = entity_json['owner'] instance.users.append(user) if permissions_json is not None: for permission_json in permissions_json: if 'sid' in permission_json and 'mask' in permission_json: mask = int(permission_json['mask']) if mask & 1 == 1 and mask & 4 == 4: sid_json = permission_json['sid'] if 'principal' in sid_json and sid_json[ 'principal'] and 'name' in sid_json: user = UserModel() user.username = sid_json['name'] instance.users.append(user) return instance
def sample_users_from_population(self, query_duration): if self.sampled_users: self.sampled_users = [] # reset the seed so that the same users are # generated every time this function is called self.population_model.reset_random_seed() # sampling user params for one user at a time for ui in xrange(self.num_users): A, D, V, L = self.population_model.generate_user_params() self.sampled_users.append(UserModel(A, D, V, L)) # with open('debugging/py.all.users', 'w') as utf: # for A,D,V,L in self.sampled_users: # print >> utf, '\t'.join(map(str,[D,A,V])) # sampling all user params all together for all users # --> - results in 0.1 increase in score on average. # - note that this is due to differences in random sampling order # given the initial seed # for user_params in self.population_model.generate_user_params(self.num_users): # self.sampled_users.append(UserModel(*user_params)) # # with open('debugging/py.all.together.users', 'w') as utf: # # for user in self.sampled_users: # # print >> utf, '\t'.join(map(str,[user.D, user.A, user.V])) # for each user generate session and away times useri = 0 for user in self.sampled_users: #user = UserModel(*user_params) #user.generate_session_away_lengths(240*3600, True) user.generate_session_away_lengths(query_duration, True) # #logger.debug(str(len(user.session_away_durations))) # with open('debugging/py.ssn.away.times.user-{}'.format(useri+1), 'w') as utf: # for ssn, away in user.session_away_durations: # print >> utf, '\t'.join(map(str,[ssn, away])) useri += 1
proc_events[key][e.event_type].append(e) proc_events[None][e.event_type].append(e) if not isinstance(e, WindowChangeEvent) or window_event.filename_string() != key: if not None in windowed_events: windowed_events[None] = [] windowed_events[None].append(e) if not key in windowed_events: windowed_events[key] = [] windowed_events[key].append(e) user_model = UserModel() for window_event in proc_events: window_model = gen_window_model(window_event, proc_events) if window_model is None: continue user_model.add_window_model(window_event, window_model) default_model = gen_window_model(None, proc_events) user_model.set_default_model(default_model)
from db import DB from user_model import UserModel from news_model import NewsModel from items_model import ItemsModel db = DB() users_model = UserModel(db.get_connection()) users_model.init_table() users_model.insert("test1", "11111111", "*****@*****.**", "admin") users_model.insert("admin", "password11", "*****@*****.**", "admin") news_model = NewsModel(db.get_connection()) news_model.init_table() items_model = ItemsModel(db.get_connection()) items_model.init_table() items_model.insert("Defaul candy", "www.yandex.ru", "30", "Classic candy with honey and cream.", "25")
120, # These values _do not_ matter since the sessions are going to be loaded from the trail files regardless 60, 10800, 5400, args.lateness_decay) outfnamepre = args.user_params[:-len("user.params")] # load sessions from file logger.warning('loading user reading speeds') with open(args.user_params) as uf: line = uf.readline() #skip header for line in uf: A, D, V = [float(e) for e in line.strip().split('\t')] MSU.sampled_users.append(UserModel(A, D, V, args.lateness_decay)) print >> sys.stderr, 'preloading sessions' user_sessions = {} user_ssn_starts = {} for ui in xrange(args.num_users): user_sim = MSU.sampled_users[ui] uid = ui + 1 ttfile = outfnamepre + "time-trails/time-trail-user-" + str(uid) with open(ttfile) as tf: line = tf.readline() #skip header for line in tf: ssn_durn, time_away = [ float(e) for e in line.strip().split('\t') ] user_sim.session_away_durations.append((ssn_durn, time_away))
def test_update_user(self): username = '******' password = '******' # Read parameters user = UserModel() user.username = username user.email = '*****@*****.**' user.setPassword(password) user.project = 'ReLDI' user.requests_limit = 1000 user.requests_made = 0 user.status = 'active' user.role = 'admin' user.save() dbUser = UserModel.getByUsername(user.username) new_username = '******' dbUser.username = new_username dbUser.project = 'RelDI2' dbUser.requests_limit = 2000 dbUser.requests_made = 4 dbUser.status = 'blocked' dbUser.role = 'user' dbUser.save() dbUser = UserModel.getByUsername(new_username) self.assertEqual(dbUser.username, new_username) self.assertEqual(dbUser.project, 'RelDI2') self.assertEqual(dbUser.requests_limit, 2000) self.assertEqual(dbUser.requests_made, 4) self.assertEqual(dbUser.status, 'blocked') self.assertEqual(dbUser.role, 'user')
level=logging.INFO, datefmt='%m-%d %H:%M:%S' ) if __name__ == '__main__': logging_setup() logging.info('=============== START RUNNING ===============') preprocessor = Preprocessor(Config=Config) preprocessor.load_news_vec_pool() preprocessor.load_datas_for_user_model() preprocessor.build_user_to_news_history_custom() preprocessor.build_sampling_pool(top = 5000, at_least = 10) preprocessor.build_id_pairs_from_history(at_least=3) # preprocessor.build_vec_pairs_from_history(at_least=10) preprocessor.load_datas_for_candidate_pool() preprocessor.build_candidate_pool(top = 2000, at_least = 100) user_model = UserModel(Config=Config) user_model.load_news_history(by_id=True) user_model.model_training_dict_version(start=0, items=10, N=10, epochs=20, batch_size=256, model_type='GRU', init_rnn_by_avg=False) recommender = Recommender(Config=Config) recommender.load_vec_pool() recommender.build_annoy_indexer()
from user_model import UserModel user = UserModel()
def test_create_user(self): username = '******' # Read parameters user = UserModel() user.username = username user.email = '*****@*****.**' user.setPassword('000000') user.project = 'ReLDI' user.requests_limit = 1000 user.requests_made = 0 user.status = 'active' user.role = 'admin' user.logRequest() user.logRequest() user.save() dbUser = UserModel.getByUsername(user.username) self.assertEqual(dbUser.username, username) self.assertEqual(dbUser.project, 'ReLDI') self.assertEqual(dbUser.requests_limit, 1000) self.assertEqual(dbUser.requests_made, 2) self.assertEqual(dbUser.status, 'active') self.assertEqual(dbUser.role, 'admin')
def delete(self, id): user = UserModel.find_by_id(id) db.session.delete(user) db.session.commit() return user.json()
cv2.rectangle(s_copy, (int(cl[0] * w) - 2, int(cl[1] * h) - 2), (int(cl[0] * w) + 2, int(cl[1] * h) + 2), (0, 255, 0), -1, 8) return s_copy if __name__ == '__main__': wd = os.getcwd() if len(sys.argv) < 4: print("Must have filename argument!", file=sys.stderr) sys.exit(1) input_dir = sys.argv[1] file = input_dir + "/user_model.mdl" user_model = UserModel() output = "." if len(sys.argv) > 4: output = sys.argv[4] with open(file, "rb") as f: user_model = pickle.load(f) screenshot_file = sys.argv[2] screenshot = cv2.imread(screenshot_file) window_title = sys.argv[3]
def post(self): data = request.get_json() user = UserModel(username=data["username"], password=data["password"]) db.session.add(user) db.session.commit() return user.json(), 201
def test_login_user_invalid(self): username = '******' password = '******' # Read parameters user = UserModel() user.username = username user.email = '*****@*****.**' user.setPassword(password) user.project = 'ReLDI' user.requests_limit = 1000 user.requests_made = 0 user.status = 'active' user.role = 'admin' user.save() dbUser = UserModel.getByUsername(user.username) with self.assertRaises(ValueError): token = dbUser.generateToken('not this password')
def test_login_user(self): username = '******' password = '******' # Read parameters user = UserModel() user.username = username user.email = '*****@*****.**' user.setPassword(password) user.project = 'ReLDI' user.requests_limit = 1000 user.requests_made = 0 user.status = 'active' user.role = 'admin' user.save() dbUser = UserModel.getByUsername(user.username) token = dbUser.generateToken(password) self.assertIsNotNone(token)
def test_delete_user(self): username = '******' # Read parameters user = UserModel() user.username = username user.email = '*****@*****.**' user.setPassword('000000') user.project = 'ReLDI' user.requests_limit = 1000 user.requests_made = 0 user.status = 'active' user.role = 'admin' user.save() dbUser = UserModel.getByUsername(user.username) self.assertIsNotNone(dbUser) dbUser.delete() dbUser = UserModel.getByUsername(user.username) self.assertIsNone(dbUser)
if __name__ == '__main__': # Collect events until released with keyboard.Listener(on_release=on_release) as klistener: wd = os.getcwd() event_constructor = EventConstructor() if len(sys.argv) < 4: print("Must have filename argument!", file=sys.stderr) sys.exit(1) input_dir = sys.argv[4] file = input_dir + "/user_model.mdl" user_model = UserModel() with open(file, "rb") as f: user_model = pickle.load(f) output_dir = input_dir + "/tests/" + str(int(time.time())) out_folder = output_dir if not os.path.isdir(output_dir): os.makedirs(output_dir, exist_ok=True) try: os.mkdir(output_dir) except OSError as e: #folder exists pass
def process_inputs(input_dirs, output_dir): event_constructor = EventConstructor() events = [] window_event = default_window_event for f in input_dirs: file = f + "/" + "events.evnt" if not os.path.isfile(file): print("Cannot find file: " + file) continue with open(file, "r") as f: for line in f: events.append(event_constructor.construct(line)) events.append(Event(events[-1].timestamp+1, EventType.NONE)) events = sorted(events, key=lambda x : x.timestamp) proc_events = {} windowed_events = {} for e in events: key = window_event.filename_string() if isinstance(e, WindowChangeEvent): window_event = e windows.append(e) if not key in proc_events: proc_events[key] = {} if not e.event_type in proc_events[key]: proc_events[key][e.event_type] = [] if not None in proc_events: proc_events[None] = {} if not e.event_type in proc_events[None]: proc_events[None][e.event_type] = [] if normalise and not window_event == default_window_event: e.change_window(window_event) proc_events[key][e.event_type].append(e) proc_events[None][e.event_type].append(e) if not isinstance(e, WindowChangeEvent) or window_event.filename_string() != key: if not None in windowed_events: windowed_events[None] = [] windowed_events[None].append(e) if not key in windowed_events: windowed_events[key] = [] windowed_events[key].append(e) user_model = UserModel() for window_event in proc_events: window_model = gen_window_model(window_event, proc_events, windowed_events) if window_model is None: continue user_model.add_window_model(window_event, window_model) if not window_event is None: for input_file_num in range(len(input_dirs)): screenshot = find(window_event, input_dirs[input_file_num] + "/images/") cols = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (0, 255, 255), (255, 0, 255), (0, 0, 0)] if not screenshot is None and os.path.isfile(screenshot): img = cv2.imread(screenshot) if not img is None: ci = 0 h, w = img.shape[:2] for c in window_model.clusters: if c[-4:] == "DOWN": for cl in window_model.clusters[c]: print(cl) cv2.rectangle(img, (int(cl[0]*w)-2, int(cl[1]*h)-2), (int(cl[0]*w)+2, int(cl[1]*h)+2), cols[ci], -1, 8) ci += 1 imgout = output_dir + "/imgs/" if not os.path.isdir(imgout): os.makedirs(imgout, exist_ok=True) try: os.mkdir(imgout) except OSError as e: #folder exists pass cv2.imwrite(imgout + window_event + ".png", img) continue default_model = gen_window_model(None, proc_events, windowed_events) user_model.set_default_model(default_model) for i in range(100): print("Gen:", default_model.next()) if not os.path.isdir(output_dir): os.makedirs(output_dir, exist_ok=True) try: os.mkdir(output_dir) except OSError as e: #folder exists pass with open(output_dir + "/user_model.mdl", "bw+") as f: pickle.dump(user_model, f)
def get(self, id): user = UserModel.find_by_id(id) return user.json()
def test_status_block_user(self): username = '******' password = '******' user = UserModel() user.username = username user.email = '*****@*****.**' user.setPassword(password) user.project = 'ReLDI' user.requests_limit = 1000 user.requests_made = 0 user.status = 'active' user.role = 'admin' user.save() dbUser = UserModel.getByUsername(user.username) dbUser.block() self.assertEqual(dbUser.status, 'blocked')
r'/index', 'Index', r'/login', 'Login', r'/list', 'List', r'/delete_all', 'Delete_All', r'/register', 'Register', r'/user_info', 'User_info', r'/list_dir', 'List_dir', r'/group_by', 'Group_by', ) app = web.application(urls, globals()) connection = pymongo.MongoClient('localhost', 27017) db = connection.test model = UserModel(db) #Default page class Index(): def GET(self): return render.index() #Get status of the mongo server class Status: def GET(self): try: res = db.last_status() res['location'] = 'localhost' res['port'] = 27017