def GET(self, request, story_id): story = models.story.Story.get(story_id) if not story: story = models.story.Story.active_story() accepted_posts = sorted([ models.post.Post.get(post['objectId']) for post in story.accepted_posts ], cmp=lambda x, y: 1 if x.createdAt > y.createdAt else -1) snippets = sorted([ models.post.Post.get(post['objectId']) for post in story.snippets ], cmp=models.post.comparePosts) initials = ":)" if self.session(request): with SessionToken(self.session(request)): user = User.current_user() initials = user.first_name[0] + user.last_name[0] return flask.render_template( "story.html", story=story, accepted_posts=accepted_posts, snippets=snippets, initials=initials, session=self.session(request), max_vote_count=models.post.Post.max_vote_count(), user_voted_story=story.user_voted(self.session(request)), text_length=models.post.Post.text_length)
def refreshWaitedTime(self): with SessionToken(self.token): self.patients = self.patientClass.Query.all().order_by("-waitedTime") for patient in self.patients: currentTime = datetime.datetime.utcnow() patient.waitedTime = (currentTime-patient.createdAt)/datetime.timedelta(minutes=1) patient.save()
def project_reads(objectId): try: token = request.args.get('sessionToken') with SessionToken(token): return _project_reads(objectId) except Exception as e: print(traceback.format_exc(), file=sys.stderr) return jsonify({'error': str(e)})
def user_calleft(token): if request.method == 'GET': session_token = token with SessionToken(session_token): me = User.current_user() me.calleft = user_calories(token) - me.feed['calories'] me.save() return me.calleft
def user_calories(token): if request.method == 'GET': session_token = token with SessionToken(session_token): me = User.current_user() me.calories = str(float(me.weight) * 33) me.save() return me.calories
def sample_initialize(projId, objectId): try: token = request.args.get('sessionToken') with SessionToken(token): name = request.args.get('name') return _sample_initialize(projId, objectId, name) except Exception as e: print(traceback.format_exc(), file=sys.stderr) return jsonify({'error': str(e)})
def func_wrapper(self, request, *args): try: session = self.session(request) with SessionToken(session): return func(self, request, *args) except ResourceRequestNotFound: pass return flask.redirect('/login')
def project_get(objectId, code, name): try: token = request.args.get('sessionToken') # Check session token. with SessionToken(token): return _project_get(objectId, code, name) except Exception as e: print(traceback.format_exc(), file=sys.stderr) return jsonify({'error': str(e)})
def user_voted(self, session): with SessionToken(session): for snippet in self.snippets: post = Post.get(snippet['objectId']) user_voted = bool([ user for user in post.voted_users if user['objectId'] == User.current_user().objectId ]) if user_voted: return True return False
def user_feed(token): if request.method == 'GET': session_token = token with SessionToken(session_token): me = User.current_user() me.calconsumed = 0 print(me.feed) for i in me.feed: j = json.loads(i) #print(j) me.calconsumed = str(int(j['calories']) + int(me.calconsumed)) me.save() return me.calconsumed
def project_compile(objectId): try: token = request.args.get('sessionToken') with SessionToken(token): if objectId in compiling and compiling[objectId].is_alive(): raise Exception( '{} is already being compiled'.format(objectId)) Project = Object.factory('Project') project = Project.Query.get(objectId=objectId) project.progress = 'compiling' project.save() t = Thread(target=_project_compile, args=(project, )) t.daemon = True compiling[objectId] = t t.start() return jsonify({'result': 'compiling'}) except Exception as e: print(traceback.format_exc(), file=sys.stderr) return jsonify({'error': str(e)})
def project_upload(objectId): try: token = request.args.get('sessionToken') with SessionToken(token): data = request.get_json() host = data['host'] username = data['username'] password = data['password'] geo_username = data['geo_username'] Project = Object.factory('Project') project = Project.Query.get(objectId=objectId) project.progress = 'uploading' project.save() if objectId in uploading and uploading[objectId].is_alive(): raise Exception( '{} is already being uploaded'.format(objectId)) t = Thread(target=_project_upload, args=( project, host, username, password, geo_username, )) t.daemon = True uploading[objectId] = t t.start() return jsonify({'result': 'uploading'}) except Exception as e: print(traceback.format_exc(), file=sys.stderr) return jsonify({'error': str(e)})
def testWithSessionToken(self): with SessionToken(token='asdf'): self.assertIn('session_token', self.get_access_keys()) self.assertNotIn('session_token', self.get_access_keys())
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] ] } class Unit(Object): pass class Facility(Object): pass updateGrid = Function('updateGrid') facility = Facility(objectId=facilityId) with SessionToken(u.sessionToken): units = Unit.Query.filter(facility=facility) total_count = len(units) * len(shifts) index = 0 for unit in units: for staffTitle in shifts: percent = int((index * 100)/total_count) index += 1 print('{}%\t Updating {} for {}'.format(percent, staffTitle, unit.name)) json = { 'staffTitle': staffTitle, 'unitId': unit.objectId, 'grids': shifts[staffTitle]
def testWithSessionToken(self): with SessionToken(token='asdf'): self.assertIn('session_token', self.get_connection()) self.assertNotIn('session_token', self.get_connection())
def user_voted(self, session): with SessionToken(session): return bool([ user for user in self.voted_users if user['objectId'] == User.current_user().objectId ])