def test_local_time(self): self.chennai.hours_offset = 5 self.chennai.minutes_offset = 30 self.chennai.put() now = datetime.datetime.now() perf = Performance(show=self.hamlet, venue=self.lady_andal, utc_date_time=now) self.assertEqual(now + self.chennai.get_timedelta(), perf.get_local_time()) perf.set_local_time(now + self.chennai.get_timedelta()) self.assertEqual(now, perf.utc_date_time) self.assertEqual(now + self.chennai.get_timedelta(), perf.get_local_time())
def performance_dataSource(): try: if request.method == "POST": raise Exception("method must be get") retrieve_list = ["performance_id", "corporation_id", "user_id", "hr_id", "value", "description", "registerdate", "department", "post"] querylist = Performance.get_obj(retrieve_list) msg = db.session.query(*querylist, Person.user_id, Person.name).\ filter(Performance.user_id == Person.user_id) return_msg = [] return_list = ["performance_id", "corporation_id", "user_id", "hr_id", "value", "description", "registerdate", "department", "post", "name"] for line in msg: line = list(line) line[6] = line[6].strftime('%Y-%m-%d') del line[9] temp = zip(return_list, line) return_msg.append(dict(temp)) return dict( status=1, message="success", data=return_msg ) except Exception as e: return dict( status=0, message=str(e), data="none" )
def get_session(self, user_key: str, session_key: str) -> Session: # Sets up a document reference to a specified session. ref = db.document(f'users/{user_key}/sessions/{session_key}') # Converts the entries stored within the referenced document # into a dictionary. session_dict: dict = ref.get().to_dict() # Builds up a reference to the performance document of a # specified session. ref = db.document( f'users/{user_key}/sessions/{session_key}/data/performance') # Adds the performance entries to the session dictionary, # enabling the convertion from a dictionary to a Session # object. performance: Performance = Performance.from_dict(ref.get().to_dict()) # Creates a session which is based on the entries within the # previously updated dictionary. session: Session = Session.from_dict(session_dict, performance) # Return the newly created session. return session
def generate_session(self, user_key: str, level: Level, difficulty_score: float) -> str: # Variable intended to store the key # that will be created within the function. session_id: int = 0 # Fetches the ids of all previously played sessions. session_ids: [int] = self.get_session_ids(user_key) # Checks whether at least a single session has been # stored before for a given user. if len(session_ids) > 0: # Sets the id for the session that should be # created next to one higher than previous # session id. session_id = max(session_ids) + 1 # Generates a key that corresponds to the previously # created id like 'session_042' if the session's id # is '42' session_key: str = Helper().generate_key('session', session_id) # Defines a new performance object which values are # initialized with zeros. performance: Performance = Performance(0, 0, 0, 0, 0, 0, 0, difficulty_score) # Creates a new session object based on the previously # defined key and id pair. session: Session = Session(session_key, session_id, 'created', server_timestamp, performance) # Defines a new database reference pointing towards # the place at which the new session sould be stored. ref = db.document(f'users/{user_key}/sessions/{session_key}') # Writes the generated session to Firestore. ref.set(session.to_dict()) # Defines a new database reference at which the session's # performance should be stored. ref = db.document( f'users/{user_key}/sessions/{session_key}/data/performance') # Stores the session's performance. ref.set(performance.to_dict()) # Defines a new database reference pointing to the place at # which the level for the generated session should be stored. ref = db.document( f'users/{user_key}/sessions/{session_key}/data/level') # Stores the session's level at Firestore. ref.set(level.to_dict()) # Returns the key of the generated session. return session_key
def set_average_performance(self, userKey: str, average_performance: Performance) -> None: # Creates a reference to the 'average_performance' Firestore document. ref = db.document(f'users/{userKey}/properties/average_performance') # Writes the average performance with all # correspoinding attributes to Firestore. ref.set(average_performance.to_dict())
def test_from_dict(self): # Should create a new Performance object result: Performance = Performance.from_dict( Samples.sample_performance_dict) # Expected Result expected_result: Performance = Samples.sample_performance # self.assertEqual(result.to_dict(), expected_result.to_dict())
def get_average_performance(self, user_key: str) -> Performance: # Creates a reference to the 'performance' Firestore document. ref = db.document(f'users/{user_key}/properties/average_performance') # Stores all datapoints stored at the document # to a dictionary. dict = ref.get().to_dict() # Returns a Performance object created from the # values stored in the dictionary. return Performance.from_dict(dict)
def newdataSource_dataSource(): try: if request.method == "POST": raise Exception("method must be get") corporation_id = request.values.get("corporation_id") if not corporation_id: raise Exception("corporation_id must not be empty") retrieve_list = ["performance_id", "corporation_id", "user_id", "hr_id", "value", "description", "registerdate", "department", "post"] querylist = Performance.get_obj(retrieve_list) msg = db.session.query(*querylist, Person.user_id, Person.name).\ filter(Performance.corporation_id == corporation_id).\ filter(Performance.user_id == Person.user_id) tempmessage = dict() for line in msg: user_id = line[retrieve_list.index("user_id")] if tempmessage.get(user_id, 0) == 0: tempmessage[user_id] = line else: history_date = tempmessage[user_id][retrieve_list.index("registerdate")] new_date = line[retrieve_list.index("registerdate")] if new_date > history_date: tempmessage[user_id] = line return_msg = [] return_list = ["performance_id", "corporation_id", "user_id", "hr_id", "value", "description", "registerdate", "department", "post", "name"] for line in tempmessage.values(): line = list(line) line[6] = line[6].strftime('%Y-%m-%d') del line[9] temp = zip(return_list, line) return_msg.append(dict(temp)) return dict( status=1, message="success", data=return_msg ) except Exception as e: return dict( status=0, message=str(e), data="none" )
def test_performance_creation(self): performance_route = '/_admin/performance' self.admin_app.post(performance_route, {'action':'create'}, status=403) # tells blank posts to bugger off old_count = self.lady_andal.get_performances().count() name = self.random() url = self.random() performance_data = dict(action='create', show_key=self.hamlet.key(), venue_key=self.lady_andal.key(), year=self.two_days_later.date().year, month=self.two_days_later.date().month, day=self.two_days_later.date().day, hour=self.two_days_later.time().hour, minute=self.two_days_later.time().minute) self.admin_app.post(performance_route, performance_data) self.assertEqual(old_count + 1, self.lady_andal.get_performances().count()) result = Performance.all().filter('show =', self.hamlet).filter('venue =', self.lady_andal).filter('utc_date_time =', self.two_days_later).fetch(1)[0] self.assertTrue(result)
def store_cluster(self, cluster_id: int, centroid: [float]) -> None: # Creates a new player performance object from # the 'centroid' parameter that should be of # the form of an array of Float values. This in- # between step is not strictly necessary, but keeps # the amount of boilerplate could to a minimum since # a 'Performance' object can easily converted to a # dictionary that can directly be stored at Firebase. performance: Performance = Performance.from_array(centroid) # Creates a new reference to a cluster document # at Firestore which is used to store a cluster's # centroid. ref = db.document(f'clusters/cluster_00{str(cluster_id)}') # Stores the priviously created performance object # to Firestore by converting the obejct to a # dictionary. ref.set(performance.to_dict())
def get_sessions(self) -> [Performance]: performances: [Performance] = [] for doc in db.collection(f'performances').get(): d = doc.to_dict() user_key = d['user_key'] performance: Performance = Performance( d['defeated_by_gaps'], d['defeated_by_opponent_type_1'], d['defeated_by_opponent_type_2'], d['defeated_by_opponent_type_3'], d['score'], d['time'], d['progress'], d['difficulty']) if user_key != 'user_001': performances.append(performance) return performances
def jixiaoCheck(): try: if request.method == "GET": raise Exception("method must be post") user_id = request.form.get('uid') if not all([user_id]): raise Exception("uid must not be empty") retrieve_list = [ "performance_id", "corporation_id", "user_id", "hr_id", "value", "description", "registerdate", "department", "post" ] querylist = Performance.get_obj(retrieve_list) msg = db.session.query(*querylist, Hr.hr_id, Hr.name, Corporation.corporation_id, Corporation.name). \ filter(Performance.user_id == user_id). \ filter(Performance.hr_id == Hr.hr_id). \ filter(Performance.corporation_id == Corporation.corporation_id). \ order_by(Performance.registerdate.desc()) retrieve_list = [ "performance_id", "corporation_id", "user_id", "hr_id", "value", "description", "registerdate", "department", "post", "hr_name", "corporation_id" ] return_msg = [] for line in msg: line = list(line) line[6] = line[6].strftime('%Y-%m-%d') del line[9] del line[10] temp = zip(retrieve_list, line) return_msg.append(dict(temp)) return dict(status=1, message="success", data=return_msg) except Exception as e: return dict(status=0, message=str(e), data="none")
def delete(self, key = None): performance = Performance.get(self.read('key')) if performance : performance.delete() self.get()
def test_cache(self): now = datetime.datetime.now() perf = Performance(show=self.hamlet, venue=self.lady_andal, utc_date_time=now) perf.put() self.assertEqual(self.chennai, perf.cached_city) self.assertEqual(self.evam, perf.cached_company)
class Samples: ############### ## Variables ## ############### db: Database = Database() # sample_user: User = User('user_042', 42, 'german') sample_performance_dict = { "defeated_by_gaps": 1, "defeated_by_opponent_type_1": 1, "defeated_by_opponent_type_2": 1, "defeated_by_opponent_type_3": 0, "score": 600, "time": 45, "progress": 15, "difficulty": 80, } sample_performance: Performance = Performance( defeated_by_gaps=1, defeated_by_opponent_type_1=1, defeated_by_opponent_type_2=1, defeated_by_opponent_type_3=0, score=600, time=45, progress=15, difficulty=80, ) sample_session: Session = Session('session_001', 1, 'finished', db.generate_timestamp(), sample_performance) sample_level: Level = Level.from_dict({ 'key': 'level_01', 'id': 1, 'line_00': [ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' ], 'line_01': [ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' ], 'line_02': [ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' ], 'line_03': [ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'D', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' ], 'line_04': [ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'B' ], 'line_05': [ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'D' ], 'line_06': [ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'S', 'S', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'D' ], 'line_07': [ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'D' ], 'line_08': [ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'S', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'C', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'D' ], 'line_09': [ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'B', 'B', 'B', 'B', '.', '.', '.', '.', '.', '.', '.', '.', 'D', '.', '.', '.', '.', '.', '.', 'D' ], 'line_10': [ '.', '.', 'X', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'C', '.', '.', '.', '.', '.', 'C', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'D', 'D', '.', '.', '.', '.', '.', '.', 'D' ], 'line_11': [ '.', '.', 'X', '.', '.', '.', '.', '.', '.', '.', 'B', '.', 'B', 'B', 'B', 'B', 'B', 'B', '.', '.', '.', 'D', 'D', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'D', 'D', 'D', '.', '.', '.', '.', '.', '.', 'D' ], 'line_12': [ '.', '.', 'X', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'D', 'D', '.', '.', '.', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', '.', '.', 'D', 'D', 'D', 'D', '.', '.', '.', '.', '.', '.', 'D' ], 'line_13': [ '.', '.', 'X', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'D', 'D', '.', '.', '.', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', '.', 'D', 'D', 'D', 'D', 'D', '.', '.', '.', '.', '.', '.', 'D' ], 'line_14': [ 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', '.', '.', '.', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', '.', '.', 'Z', 'Z', 'Z', 'D', 'D' ] }) sample_user_1: User = User('user_001', 1, 'english') sample_user_2: User = User('user_002', 2, 'english') sample_user_3: User = User('user_003', 3, 'english') sample_user_4: User = User('user_004', 4, 'english') sample_session_1: Session = Session( 'session_001', 1, 'finished', db.generate_timestamp(), Performance(0, 1, 0, 2, 1500, 48, 200, 80)) sample_session_2: Session = Session( 'session_002', 2, 'finished', db.generate_timestamp(), Performance(0, 2, 0, 2, 3000, 130, 156, 100)) sample_session_3: Session = Session( 'session_003', 3, 'finished', db.generate_timestamp(), Performance(2, 0, 0, 2, 4500, 38, 137, 110)) sample_session_4: Session = Session( 'session_001', 1, 'finished', db.generate_timestamp(), Performance(1, 0, 0, 1, 4300, 48, 149, 90)) sample_session_5: Session = Session( 'session_002', 2, 'finished', db.generate_timestamp(), Performance(0, 0, 0, 0, 4300, 243, 200, 180)) sample_session_6: Session = Session( 'session_001', 1, 'finished', db.generate_timestamp(), Performance(0, 3, 1, 0, 2200, 38, 113, 65)) sample_session_7: Session = Session( 'session_002', 2, 'finished', db.generate_timestamp(), Performance(0, 2, 2, 0, 1100, 52, 200, 79)) sample_session_8: Session = Session( 'session_001', 1, 'finished', db.generate_timestamp(), Performance(2, 0, 0, 1, 500, 74, 31, 103))
def make_performance(self, show, venue, dt): perf = Performance(show=show, venue=venue, utc_date_time=dt) perf.put() return perf
def jixiao_insert(): status = 0 try: if request.method == "GET": raise Exception("method must be post") corporation_id = request.form.get('corporation_id') value = request.form.get('value') user_id = request.form.get('user_id') post = request.form.get('post') description = request.form.get('description') department = request.form.get('department') hr_id = request.form.get('hr_id') if not all([value, post, description, department, hr_id]): raise Exception("all values must not be empty: value," "post, description, department, hr_id") if not user_id: user_id = session.get("user_id", "") if user_id == "": raise Exception("user id must be set as: 1, through log in 2, through form post. " "while the form have fhe first priority") if not corporation_id: corporation_id = session.get("corporation_id", "") if corporation_id == "": raise Exception("corporation_id id must be set as: 1, through log in 2, through form post. " "while the form have fhe first priority") try: corporation_id = int(corporation_id) value = int(value) user_id = int(user_id) hr_id = int(hr_id) except: raise Exception("corporation_id, value, user_id, hr_id must be int type of value") if value < 0 or value > 10: status = 2 raise Exception("value must in between 0 and 10, include") have_corporation_id = db.session.query(Corporation.corporation_id). \ filter(Corporation.corporation_id == corporation_id).first() if not have_corporation_id: status = 3 raise Exception("corporation id is not valid") have_user_id = db.session.query(Person.user_id). \ filter(Person.user_id == user_id).first() if not have_user_id: status = 3 raise Exception("user id is not valid") have_hr_id = db.session.query(Hr.hr_id). \ filter(Hr.hr_id == hr_id).first() if not have_hr_id: status = 3 raise Exception("hr id is not valid") performance = Performance(corporation_id=corporation_id, user_id=user_id, hr_id=hr_id, value=value, description=description, department=department, post=post) performance.save() return dict( status=1, message="success", data="none" ) except Exception as e: return dict( status=status, message=str(e), data="none" )
def test_cascading_deletes(self): self.make_performance(self.hamlet, self.lady_andal, self.one_day_later) self.make_performance(self.hamlet, self.lady_andal, self.two_days_later) perf_count = Performance.all().count() self.lady_andal.delete() self.assertEqual(perf_count-2,Performance.all().count())
def get(self, url = None): self.render('admin/performance.html', dict(performances=Performance.all(), shows=Show.all(), venues=Venue.all()))