def main(): try: app = SESSION.query(App).filter(App.name == "CloudControl").one() if app.status == "stop": app.start_time = datetime.datetime.now() app.end_time = None app.status = "running" except sqlalchemy.orm.exc.NoResultFound: app = App( name="CloudControl", start_time=datetime.datetime.now(), status="running", platform=1 ) SESSION.add(app) except sqlalchemy.orm.exc.MultipleResultsFound: pass SESSION.commit() ioloop.IOLoop.current().spawn_callback(get_code) yield generate_case() yield Q.join() app.end_time = datetime.datetime.now() app.status = "stop" global DONE_COUNT, CASE_COUNT DONE_COUNT = 0 CASE_COUNT = 0 SESSION.commit()
def wx_delete(): """ """ _id = request.form.get('id') App.delete_app(g._db, _id) return ''
def _csv_to_db(csvfile): """Read a CSV file and create App models from row/fields. Store empty values as '' to avoid storing None which renders ugly in template and cannot be unicoded on export. We are not doing smart things like foreign keys to controlled vocabulary tables, nor converting string values to Integer, Boolean, Date, etc. """ num_apps = 0 warnings = [] infos = [] App.objects.all().delete() # DANGER: Delete existing apps info reader = csv.DictReader(csvfile) for field in reader.fieldnames: if field not in MOGRIFIELDS.keys(): msg = "Ignoring unrecognized field name '%s' for all rows in CSV" % field warnings.append(msg) logging.warning(msg) for rid, row in enumerate(reader): acronym = row["Acronym"].strip() version = row["Version Number"].strip() if acronym == "": msg = "Ignoring row=%d without Acronym" % rid warnings.append(msg) logging.warning(msg) continue if len(acronym) < 2: msg = "Ignoring row=%d with too-short Acronym=%s" % (rid, acronym) warnings.append(msg) logging.warning(msg) continue existing = App.objects.filter(acronym__iexact=acronym, version_number=version) if existing: msg = "Ignoring extant acronym=%s version=%s" % (acronym, version) warnigns.append(msg) logging.warning(msg) continue app = App() app.save() # save for M2M for k, v in row.items(): if k not in MOGRIFIELDS.keys(): continue # ignore unrecognized field names v = v.strip() try: setattr(app, MOGRIFIELDS[k], v) except (TypeError, ValidationError), e: logging.error("SETATTR: %s", e) import pdb pdb.set_trace() try: app.save() except (TypeError, ValidationError), e: logging.error("SAVE: %s", e) import pdb pdb.set_trace()
def setUp(self): """ user_a -> admin user_b -> user2 user_a uploads shared file. We authenticate to the API with user_b. user_b subscribes to user_a's shake.] """ super(APIResourceRequests, self).setUp() self.user_a = User( name='admin', email='*****@*****.**', email_confirmed=1, is_paid=1, about="admin", website='https://mltshp.com') self.user_a.set_password('asdfasdf') self.user_a.save() self.sid = self.sign_in('admin', 'asdfasdf') self.xsrf = self.get_xsrf() self.test_file1_path = os.path.abspath("test/files/1.png") self.test_file1_sha1 = Sourcefile.get_sha1_file_key(self.test_file1_path) self.test_file1_content_type = "image/png" response = self.upload_file(file_path=self.test_file1_path, sha1=self.test_file1_sha1, content_type=self.test_file1_content_type, user_id=self.user_a.id, sid=self.sid, xsrf=self.xsrf) self.user_b = User(name='user2', email='*****@*****.**', email_confirmed=1, is_paid=1) self.user_b.set_password('asdfasdf') self.user_b.save() self.group_shake = self.user_b.create_group_shake(title='Group Shake', name='groupshake', description='This is a group shake.') self.group_shake_2 = self.user_a.create_group_shake(title='Another Group', name='anothergroup') # Add user_b to user_a's group shake, so we get it in user_b's /shakes endpoint. shake_manager = ShakeManager(user_id=self.user_b.id, shake_id=self.group_shake_2.id) shake_manager.save() self.app = App(user_id=self.user_a.id, title='An App', description='Nothing yet.', redirect_url='http://client.example.com/return') self.app.save() self.authorization = Authorizationcode.generate(self.app.id, self.app.redirect_url, self.user_b.id) self.access_token = Accesstoken.generate(self.authorization.id) extra_authorization = Authorizationcode.generate(self.app.id, self.app.redirect_url, self.user_b.id) self.ratelimited_access_token = Accesstoken.generate(extra_authorization.id) now_hour = datetime.utcnow().strftime('%Y-%m-%d %H:00:00') ratelimit = Apihit(accesstoken_id=self.ratelimited_access_token.id, hits=options.api_hits_per_hour - 2, hour_start=now_hour) ratelimit.save() #subscription self.user_b.subscribe(self.user_a.shake())
async def api_save_app_info(*, name, size, count): app = App(name=name, size=size, status=1, surplus_count=count, add_time=get_current_time()) app.id = 'adhasjdhasjdhasjdhajkdhajkhd' # await app.save() create_mobile_config(app.id, app.name) return dict(app=app)
def populate_datastore(): client_key_name1='client01' client1 = Client(key_name=client_key_name1, id='12345678', name='Organic Oranges LLC', domain=db.Link("http://client1.clickin-tech.appspot.com")) client1.put() client_key_name2='client02' client2 = Client(key_name=client_key_name2, id='87654321', name='Green Trees Inc.', domain=db.Link("http://client2.clickin-tech.appspot.com")) client2.put() admin_key_name1='admin001' admin1 = Client_User(key_name=admin_key_name1, client=client1, id='001002', name='Daniel Alves', email='*****@*****.**') admin1.put() admin_key_name2='admin002' admin2 = Client_User(key_name=admin_key_name2, client=client1, id='001005', name='Andres Iniesta', email='*****@*****.**') admin2.put() admin_key_name5='admin05' admin5 = Client_User(key_name=admin_key_name5, client=client2, id='0010011', name='Josep Guardiola', email='*****@*****.**') admin5.put() admin_key_name6='admin06' admin6 = Client_User(key_name=admin_key_name6, client=client2, id='0010016', name='Lionel Messi', email='*****@*****.**') admin6.put() #ClickIn People Search app_key_name1='app1' app1 = App(key_name=app_key_name1, client=client1, app_id='163185560402939', api_key='807c2277abe596cfe542927858105306', app_secret='aae6abb4d6bf0b41f066c387ab36e486', name = 'ClickIn People Search', domain=db.Link("http://app1.client1.clickin-tech.appspot.com/")) app1.put() #Click In People Search 2 app_key_name2='app2' app2 = App(key_name=app_key_name2, client=client1, app_id='114549068628127', api_key='7f15ffb2b72ff6c4a6d08daebca21a52', app_secret='61545bcd8a3d9fc6a8107eaed5cbe4de', name = 'ClickIn People Search 2', domain=db.Link("http://app2.client1.clickin-tech.appspot.com/")) app2.put() #Cool Running App app_key_name3='app3' app3 = App(key_name=app_key_name3, client=client2, app_id='107411582680918', api_key='7a55f39fb4e0371aad78e1bd8dd517af', app_secret='c12b89b5f815cebe27636cd8c50a6264', name = 'Cool Running App', domain=db.Link("http://app1.client2.clickin-tech.appspot.com/")) app3.put()
def get_apps( *, page = '1' ): page_index = get_page_index( page ) num = yield from App.findNumber( 'count(id)' ) page = Page( num, page_index ) if num == 0: apps = [] else: apps = yield from App.findAll( orderBy = 'created_at desc', limit = ( page.offset, page.limit ) ) logging.info( 'Page: %s, BlogsNum: %s.' % ( page, len( apps ) ) ) return { '__template__' : 'apps.html', 'page' : page, 'apps' : apps }
def post(self, user, account, **kwargs): app_id = (kwargs['app_id'] if 'app_id' in kwargs else 'new') body_json = self.request.body body = json.loads(body_json) if 'name' not in body: logging.exception(exception) self.response.set_status(400) self.response.write('Invalid JSON data') # return BadRequest("Invalid JSON data") if app_id == 'new': app = App(name=body['name'], created_by=account.key(), editors=[account.key()]) else: app = App.get_by_id(int(app_id)) if app is None: return render_json_response(self, {'error': 'app-not-found'}) if account.key() not in app.editors: return render_json_response(self, {'error': 'access-denied'}) app.name = body['name'] app.body = db.Text(body_json.decode('utf-8')) app.put() return render_json_response(self, {'id': app.key().id()})
def setUp(self): super(APIResourceOwnerPasswordCredentials, self).setUp() self.user_a = User(name='admin', email='*****@*****.**', email_confirmed=1, is_paid=1) self.user_a.set_password('asdfasdf') self.user_a.save() self.sid = self.sign_in('admin', 'asdfasdf') self.xsrf = self.get_xsrf() self.user_b = User(name='user2', email='*****@*****.**', email_confirmed=1, is_paid=1) self.user_b.set_password('asdfasdf') self.user_b.save() self.app = App(user_id=self.user_a.id, title='An App', description='Nothing yet.', redirect_url='http://client.example.com/return') self.app.save()
def get(self): account_count = Account.all().count(1000) app_count = App.all().count(1000) html = """<?xml version="1.0" encoding="UTF-8"?> <root> <item> <value>%(app_count)d</value> <text>Apps</text> </item> <item> <value>0</value> <text></text> </item> <item> <value>%(account_count)d</value> <text>Accounts</text> </item> </root> """ % dict( account_count=account_count, app_count=app_count ) # return Response(html, content_type="text/xml") self.response.headers["Content-Type"] = "text/xml" self.response.write(html)
def get(self, user, account, **kwargs): users_info = {} app_query = App.all() if not users.is_current_user_admin(): app_query = app_query.filter("editors", account.key()) apps = app_query.fetch(100) accounts_by_key = {} for acc in Account.get(set([app._created_by for app in apps] + [account.key()])): accounts_by_key[acc.key()] = acc users_info[acc.user.user_id()] = { "apps": [], "full_name": acc.full_name or "", "email": acc.user.email(), "created_at": time.mktime(acc.created_at.timetuple()), } for app in apps: users_info[accounts_by_key[app._created_by].user.user_id()]["apps"].append( {"id": app.key().id(), "body": app.body} ) return render_json_response( self, {"users": users_info, "current_user": account.user.user_id() if account else None} )
def get(self, user, account, **kwargs): users_info = {} app_query = App.all() if not users.is_current_user_admin(): app_query = app_query.filter('editors', account.key()) apps = app_query.fetch(100) accounts_by_key = {} for acc in Account.get( set([app._created_by for app in apps] + [account.key()])): accounts_by_key[acc.key()] = acc users_info[acc.user.user_id()] = { 'apps': [], 'full_name': acc.full_name or '', 'email': acc.user.email(), 'created_at': time.mktime(acc.created_at.timetuple()), } for app in apps: users_info[accounts_by_key[ app._created_by].user.user_id()]['apps'].append({ 'id': app.key().id(), 'body': app.body, }) return render_json_response( self, { 'users': users_info, 'current_user': account.user.user_id() if account else None })
def clean_subdomain(self): subdomain = self.cleaned_data['subdomain'] subdomain = App.provision_subdomain(subdomain) self.cleaned_data['subdomain'] = subdomain return subdomain
def send_stats(): """Runs forever in a socket.io background task and sends updates on all monitored apps. Updates are checked and sent on the configured interval `scaling.monitor_interval`. """ while True: global track_stats if track_stats: try: apps = sockets.rooms_with_members('apps/') print ('getting stats for apps', apps) for room in apps: app_id = room.split('/')[-1] try: app = App.find_by_id(app_id) stats = app.get_current_stats( include_instance_stats=True) update_app_watchers(app_id, 'stats', stats.to_dict()) except Exception as e: import traceback print(traceback.format_exc()) print(type(e), e.message) except Exception as e: print (e) # else: # print ('No clients active, sleeping.') socketio.sleep(config.scaling_monitor_interval)
def on_connect(): """Verifies that the user is logged in and that the app they want to monitor exists. Joins them to a "room" for that app. Emits an event containing a history of activity for the past 900 seconds. Starts polling for real time stats on the app's performance. """ print('connecting') app_id = request.args.get('app_id', None) if not app_id: print ('app_id is required') return disconnect() app = App.find_by_id(app_id) if not app: print ('app not found') return disconnect() print ('watching app', app_id) sockets.join_room(get_app_room_id(app_id)) interval = request.args.get('history_interval', 900) app_stats = AppStats.get_history(app_id, interval=interval) socketio.emit('history', util.to_dict(app_stats), room=request.sid, namespace=namespace) resume_stats()
def apps(self, attributes=None): """The apps endpoint provides information about a customer's mobile applications. The apps endpoint returns a list of apps with links to additional metric endpoints. Information requested in the attributes parameter is made available as a series of key-value pairs inside the object representing each app. Required keyword arguments: <None> Optional keyword arguments: attributes -- list of attributes desired as part of app response data Possible contents: "appName", "appType", "appVersions", "crashPercent", "dau", "latency", "latestAppStoreReleaseDate", "latestVersionString", "linkToAppStore", "iconURL", "mau", "rating", "role" """ url_suffix = 'apps?attributes=%s' % '%2C'.join( attributes or list(self.APP_ATTRIBUTE_CHOICES)) content = self.__request('GET', url_suffix, {}) response = [ App(app_id, app_data) for app_id, app_data in content.items() ] return response
def get(self, app_id): current_user = self.get_current_user_object() api_app = App.get('id = %s and user_id = %s', int(app_id), current_user.id) if not api_app: return self.redirect("/developers") return self.render("developers/edit-app.html", app=api_app)
def post(self): app = App.instance() app.total_posts += 1 app.put() ip = self.request.get('ip') bin = self.request.get('bin') size = int(self.request.get('size')) day = datetime.datetime.now().day daily_ip_key = 'usage-%s-%s' % (day, ip) daily_ip_usage = memcache.get(daily_ip_key) or 0 memcache.set(daily_ip_key, int(daily_ip_usage) + size, time=24 * 3600) if daily_ip_usage > 500000000: # about 500MB mail.send_mail(sender="*****@*****.**", to="*****@*****.**", subject="PostBin user IP over quota", body=ip) daily_bin_key = 'usage-%s-%s' % (day, bin) daily_bin_usage = memcache.get(daily_bin_key) or 0 memcache.set(daily_bin_key, int(daily_bin_usage) + size, time=24 * 3600) if daily_bin_usage > 10485760: # 10MB obj = Bin.get_by_name(bin) obj.delete()
def post(): args = parser.parse_args(strict=True) try: app = App.query.filter_by(name=args['app']).first() except Exception as e: print(e) if app is not None: return Response(response=json.dumps( dict( error='App exist, you are already registered with this name' )), status=400, mimetype='application/json') try: new_app = App(name=args['app'], path_folder=args['path'], route=args['route']) except Exception as e: print(e) db.session.add(new_app) db.session.commit() return Response(response=json.dumps( dict(info='you are successfully registered')), status=200, mimetype='application/json')
def setUp(self): super(APITokenTests, self).setUp() self.user_a = User(name='admin', email='*****@*****.**', email_confirmed=1, is_paid=1) self.user_a.set_password('asdfasdf') self.user_a.save() self.sid = self.sign_in('admin', 'asdfasdf') self.xsrf = self.get_xsrf() self.user_b = User(name='user2', email='*****@*****.**', email_confirmed=1, is_paid=1) self.user_b.set_password('asdfasdf') self.user_b.save() self.app = App(user_id=self.user_a.id, title='An App', description='Nothing yet.', redirect_url='http://client.example.com/return') self.app.save() self.authorization = Authorizationcode.generate(self.app.id, self.app.redirect_url, self.user_b.id)
def post(self): current_user = self.get_current_user_object() title = self.get_argument('title', '').strip() description = self.get_argument('description', '').strip() redirect_url = self.get_argument('redirect_url', '').strip() new_app = App(user_id=current_user.id, title=title, description=description, redirect_url=redirect_url) if not new_app.save(): self.add_errors(new_app.errors) return self.render("developers/new-api-application.html", title=title, description=description, redirect_url=redirect_url) return self.redirect('/developers/view-app/%s' % (new_app.id))
def get(self, play_store_id): start_time = time.time() app = App.for_play_store_id(play_store_id).first() if not app: try: res = requests.get( 'https://play.google.com/store/apps/details?id={}'.format( play_store_id)) if (res.status_code != 200): return Response.error( 'No app found for app id {}'.format(play_store_id), 404) except requests.exceptions.ConnectionError: return Response.error( 'Unable to fetch app from playstore, please try again after sometime', 500) app = App() app.parse_html_and_save(res.text, play_store_id) if not app.reviews.count() > 0: app.parse_and_save_reviews() args = reqparse.RequestParser().add_argument( 'page_num', type=int, required=False).parse_args() return Response.pagination( 'Reviews Delivered in {} seconds'.format( round(time.time() - start_time, 2)), Review.for_play_store_id(play_store_id).with_author(), args.get('page_num') or 1, 40)
def wx_detail(appid): """ store 详情 """ db = g._db app = App.get_wx_app(db, appid) return render_template('wx/detail.html', data={'info': app})
def app_detail(appid): """ store 详情 """ db = g._db app = App.get_app(db, appid) return render_template('app/detail.html', app=app)
def app_complete(app_id): """ 游戏创建完成 """ db = g._db app = App.get_app(db, app_id) return render_template('app/complete.html', app=app)
def test_disconnecting_connection(self): """ Sending a post request to /settings/connections/{{app_id}}/disconnect should mark an access token as deleted. """ # app created by Torrez. app = App(user_id=self.user.id,title='Title') app.save() # another user. new_user = User(name='newuser', email='*****@*****.**', email_confirmed=1, is_paid=1) new_user.set_password('asdfasdf') new_user.save() #connect another user and app token = Accesstoken(user_id=new_user.id, app_id=app.id, deleted=0) token.save() # If torrez hits URL, which he should never be able to find, nothing happens # to token. self.post_url("/account/settings/connections/%s/disconnect" % app.id) token_reloaded = Accesstoken.get('id = %s', token.id) self.assertEqual(0, token_reloaded.deleted) # but if the user it's about hits it, the token deletes. self.sign_in('newuser', 'asdfasdf') self.post_url("/account/settings/connections/%s/disconnect" % app.id) token_reloaded = Accesstoken.get('id = %s', token.id) self.assertEqual(1, token_reloaded.deleted) # Even if there are several undeleted access tokens, they all end up deleted. token_two = Accesstoken(user_id=new_user.id, app_id=app.id, deleted=0) token_two.save() token_three = Accesstoken(user_id=new_user.id, app_id=app.id, deleted=0) token_three.save() self.post_url("/account/settings/connections/%s/disconnect" % app.id) token_reloaded = Accesstoken.get('id = %s', token.id) token_two_reloaded = Accesstoken.get('id = %s', token_two.id) token_three_reloaded = Accesstoken.get('id = %s', token_three.id) self.assertEqual(1, token_reloaded.deleted) # still self.assertEqual(1, token_two_reloaded.deleted) self.assertEqual(1, token_three_reloaded.deleted)
def delete(self, user, account, **kwargs): app_id = kwargs["app_id"] app = App.get_by_id(int(app_id)) if app is None: return render_json_response(self, {"error": "app-not-found"}) if account.key() not in app.editors: return render_json_response(self, {"error": "access-denied"}) app.delete() return render_json_response(self, {"status": "ok"})
def delete(self, user, account, **kwargs): app_id = kwargs['app_id'] app = App.get_by_id(int(app_id)) if app is None: return render_json_response(self, {'error': 'app-not-found'}) if account.key() not in app.editors: return render_json_response(self, {'error': 'access-denied'}) app.delete() return render_json_response(self, {'status': 'ok'})
def post(self, app_id=None): """ Handles AJAX post request to disconnect an app from a user. """ user = self.get_current_user_object() app = App.get("id = %s", app_id) if not app: return {'error': 'Invalid request.'} app.disconnect_for_user(user) return {'result': 'ok'}
def list_available_apps(): """Checks with Cloud Foundry to display a list of all apps within the configured organization and space that have not yet been imported (hence available). """ apps = App.list_available_apps() if config.app_id is not None: apps = [app for app in apps if app.app_id != config.app_id] return render_template('apps/list.html', apps=apps, is_available=True, title='Available Apps')
def app_index(): """ store 模块首页 """ uid = session['user']['id'] store_id = session['user']['store_id'] db = g._imdb allApps = App.get_apps(db, store_id) wxs = App.get_wxs(db, store_id) #移除wx公众号 apps = [] for app in allApps: r = [wx for wx in wxs if wx['id'] == app['id']] if not r: apps.append(app) return render_template('app/index.html', data={'list':apps})
def get(self, play_store_id): app_found = App.for_play_store_id(play_store_id).first() if (app_found): return Response.model('Delivered', app_found) try: res = requests.get( 'https://play.google.com/store/apps/details?id={}'.format( play_store_id)) if (res.status_code != 200): return Response.error( 'No app found for app id {}'.format(play_store_id), 404) except requests.exceptions.ConnectionError: return Response.error( 'Unable to fetch app from playstore, please try again after sometime', 500) new_app = App() new_app.parse_html_and_save(res.text, play_store_id) return Response.model('Delivered', new_app)
def wx_index(): """ store 模块首页 """ uid = session['user']['id'] store_id = session['user']['store_id'] db = g._imdb wxs = App.get_wxs(db, store_id) return render_template('wx/index.html', data={'list': wxs})
def post(self): app = App.by_key(self.get_argument('client_id', '-')) current_user = self.get_current_user_object() redirect_url = self.get_argument('redirect_uri', app.redirect_url) agree_flag = self.get_argument('agree', None) if agree_flag == None or agree_flag == '0': return self.redirect_with_params(redirect_url, error='access_denied') else: auth_code = Authorizationcode.generate(app.id, redirect_url, current_user.id) return self.redirect_with_params(redirect_url, code=auth_code.code)
def api_new_app(): try: file = request.files['file'] app = App.from_file(file) except Exception as e: web.logger.error('%r', e) res = jsonify({'message': e.message}) res.status_code = 400 return res else: db.session.add(app) db.session.commit() return jsonify(simple_serialize(app))
def get(self): admin = authorizedAdminClient() if admin: app_id = cgi.escape(self.request.get("app")) encoded_app_id = base64.b64encode(app_id) apps = App.all() apps.filter('app_id =', app_id) app = apps.get() self.render(u'app_menu', app=app, encoded_app_id=encoded_app_id) else: self.render(u'unauthorized', user=users.get_current_user(), login_url=users.create_login_url("/"), logout_url=users.create_logout_url("/"))
def main(): app_context = ApplicationContext() parser = argparse.ArgumentParser() parser.add_argument("--apps", default="config/apps.json") args = parser.parse_args() app = App(args) window = MainWindow(app) window.show() app_context.app.setStyle("windowsvista") return app_context.app.exec_()
def post(self, app_id): current_user = self.get_current_user_object() api_app = App.get('id = %s and user_id = %s', int(app_id), current_user.id) if not api_app: return self.redirect("/developers") api_app.title = self.get_argument('title', '').strip() api_app.description = self.get_argument('description', '').strip() api_app.redirect_url = self.get_argument('redirect_url', '').strip() if not api_app.save(): self.add_errors(api_app.errors) return self.render('developers/edit-app.html', app=api_app) return self.redirect("/developers/view-app/%s" % (api_app.id))
def list_my_apps(): """Show a list of the user's apps imported for autoscaling. If no apps have been imported, then the user will be redirected to the /apps/available page to import apps. """ my_apps = App.list_my_apps() if not my_apps: return redirect('/apps/available') if config.app_id is not None: my_apps = [app for app in my_apps if app.app_id != config.app_id] return render_template('apps/list.html', apps=my_apps, is_available=False, title='Imported Apps')
def get(self): account_count = Account.all().count(1000) app_count = App.all().count(1000) html = """ <pre> Mockko statistics: Users: %(account_count)d Applications: %(app_count)d """ % dict( account_count=account_count, app_count=app_count ) # return Response(html, content_type="text/html") self.response.headers["Content-Type"] = "text/html" self.response.write(html)
def get_access_token(rds, db, wx_appid): access_token = WX.get_access_token(rds, wx_appid) if not access_token: app = App.get_wx(db, wx_appid) if not app: return None refresh_token = app['refresh_token'] component_token = get_component_access_token(rds) if not component_token: return None wx = WXOpenAPI(APPID, APPSECRET, component_token) r = wx.refresh_auth(wx_appid, refresh_token) if r.get('errcode'): logging.error("refresh auto error:%s %s", r['errcode'], r['errmsg']) return None token = r['authorizer_access_token'] expires = r['expires_in'] authorizer_refresh_token = r['authorizer_refresh_token'] #提前10分钟过期 if expires > 20*60: expires = expires - 10*60 if authorizer_refresh_token != refresh_token: logging.error("old refresh token:%s new refresh token:%s", refresh_token, authorizer_refresh_token) else: logging.debug("refresh token is unchanged") WX.set_access_token(rds, wx_appid, token, expires) access_token = token return access_token
def post(self, user, account, **kwargs): app_id = kwargs["app_id"] if "app_id" in kwargs else "new" body_json = self.request.body body = json.loads(body_json) if "name" not in body: logging.exception(exception) self.response.set_status(400) self.response.write("Invalid JSON data") # return BadRequest("Invalid JSON data") if app_id == "new": app = App(name=body["name"], created_by=account.key(), editors=[account.key()]) else: app = App.get_by_id(int(app_id)) if app is None: return render_json_response(self, {"error": "app-not-found"}) if account.key() not in app.editors: return render_json_response(self, {"error": "access-denied"}) app.name = body["name"] app.body = db.Text(body_json.decode("utf-8")) app.put() return render_json_response(self, {"id": app.key().id()})
def app_add_post(): """ 创建应用 """ db = g._db store_id = session['user']['store_id'] name = request.form.get('name') key = random_ascii_string(32) secret = random_ascii_string(32) android, ios = _get_clients() developer_id = 0 db.begin() appid = App.gen_id(db) App.create_app(db, appid, name, developer_id, key, secret, store_id) if android: push_types = request.form.getlist('push_type') xinge_access_id = request.form.get('xinge_access_id', '') xinge_secret_key = request.form.get('xinge_secret_key', '') mi_appid = request.form.get('mi_appid', '') mi_secret_key = request.form.get('mi_secret_key', '') hw_appid = request.form.get('hw_appid', '') hw_secret_key = request.form.get('hw_secret_key', '') gcm_sender_id = request.form.get('gcm_sender_id', '') gcm_api_key = request.form.get('gcm_api_key', '') client_id = Client.gen_id(db) Client.create_client(db, client_id, appid, developer_id, PlatformType.ANDROID, android['platform_identity']) Client.create_android(db, client_id, xinge_access_id, xinge_secret_key, mi_appid, mi_secret_key, hw_appid, hw_secret_key, gcm_sender_id, gcm_api_key) if ios: sandbox_key_file = request.files.get('sandbox_key') if sandbox_key_file: filename = sandbox_key_file.filename ext = os.path.splitext(filename)[1] if ext == '.p12': sandbox_key = sandbox_key_file.read() sandbox_key_secret = request.form.get('sandbox_key_secret') else: sandbox_key = "" sandbox_key_secret = "" else: sandbox_key = "" sandbox_key_secret = "" production_key_file = request.files.get('production_key') if production_key_file: filename = production_key_file.filename ext = os.path.splitext(filename)[1] if ext == '.p12': production_key = production_key_file.read() production_key_secret = request.form.get('production_key_secret') else: production_key = "" production_key_secret = "" else: production_key = "" production_key_secret = "" client_id = Client.gen_id(db) Client.create_client(db, client_id, appid, developer_id, PlatformType.IOS, ios['platform_identity']) Client.create_ios(db, client_id, sandbox_key, sandbox_key_secret, production_key, production_key_secret) db.commit() return redirect(url_for('.app_complete', app_id=appid))
def get_user(rds, db, gh_id, openid): now = int(time.time()) u = WXUser.get_wx_user(rds, gh_id, openid) if not u: wx = App.get_wx_by_ghid(db, gh_id) if not wx: logging.error("invalid gh_id:%s", gh_id) return None store_id = wx['store_id'] if not store_id: logging.error("can't find store id with gh_id:%s", gh_id) return None access_token = get_access_token(rds, db, wx['wx_app_id']) if not access_token: logging.error("can't get access token") return None mp = WXMPAPI(access_token) uid = WXUser.gen_id(rds) u = WXUser() u.gh_id = gh_id u.openid = openid u.appid = wx['appid'] u.wx_appid = wx['wx_app_id'] u.uid = uid u.store_id = store_id u.timestamp = now u.seller_id = 0 WXUser.save_wx_user(rds, u) WXUser.bind_openid(rds, u.appid, u.uid, openid) logging.debug("bind openid:%s %s %s", u.appid, u.uid, openid) r = mp.get_user_by_openid(openid) if r.get('errcode'): logging.error("get user error:%s %s", r['errcode'], r['errmsg']) else: avatar = r.get('headimgurl', '') name = r.get('nickname', '') logging.debug("gh_id:%s openid:%s name:%s avatar:%s", gh_id, openid, name, avatar) WXUser.set_user_name(rds, u.appid, u.uid, name, avatar) elif now - u.timestamp > 4*3600: #更新用户信息 wx = App.get_wx_by_ghid(db, gh_id) if not wx: logging.error("invalid gh_id:%s", gh_id) return None store_id = wx.get('store_id') if not store_id: logging.error("gh_id:%s is unauth", gh_id) return None appid = wx['appid'] #切换到其它账号或者删除之后重新授权 if u.store_id != store_id or u.appid != appid: logging.debug("store/app changed, gh_id:%s openid:%s store id:%s -> %s appid:%s -> %s", gh_id, openid, u.store_id, store_id, u.appid, appid) #生成新的用户id uid = WXUser.gen_id(rds) u = WXUser() u.gh_id = gh_id u.openid = openid u.appid = wx['appid'] u.wx_appid = wx['wx_app_id'] u.uid = uid u.store_id = store_id u.timestamp = now u.seller_id = 0 WXUser.save_wx_user(rds, u) WXUser.bind_openid(rds, u.appid, u.uid, openid) logging.debug("bind openid:%s %s %s", u.appid, u.uid, openid) if wx['is_app']: #小程序获取不到微信的用户信息 WXUser.set_timestamp(rds, gh_id, openid, now) else: access_token = get_access_token(rds, db, wx['wx_app_id']) if not access_token: logging.error("can't get access token") return None mp = WXMPAPI(access_token) r = mp.get_user_by_openid(openid) if r.get('errcode'): logging.error("get user error:%s %s", r['errcode'], r['errmsg']) else: avatar = r.get('headimgurl', '') name = r.get('nickname', '') logging.debug("gh_id:%s openid:%s name:%s avatar:%s", gh_id, openid, name, avatar) WXUser.set_user_name(rds, u.appid, u.uid, name, avatar) WXUser.set_timestamp(rds, gh_id, openid, now) return u
def get(self, app_id): # user = users.get_current_user() # if user is None: # return render_json_response(self, { 'error': 'signed-out' }) # else: # account = Account.all().filter('user', user).get() # if account is None: # account = Account(user=user) # account.put() app = App.get_by_id(int(app_id)) if app is None: return render_json_response(self, {"error": "app-not-found"}) # if account.key() not in app.editors: # return render_json_response(self, { 'error': 'access-denied' }) body_json = app.body content = json.loads(body_json) body = "\n".join([s["html"] for s in content["screens"]]) body = body.replace('"static', '"/static') body = body.replace('"images', '"/images') html = """ <!DOCTYPE html> <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="apple-mobile-web-app-status-bar-style" content="default" /> <link charset='utf-8' href='/static/iphone/iphone.css' media='screen' rel='stylesheet' title='no title' type='text/css' /> <script charset='utf-8' src='/static/lib/jquery.js' type='text/javascript'></script> <style> .c-background { display: none; }</style> <script> jQuery(function($) { $('.c-background:first').show(); $('.has-action').click(function() { var action = $(this).attr('action'); var m; if (m = action.match(/^screen:(.*)$/)) { var screenId = "screen-" + m[1]; console.log("Jumping to #" + screenId); $('.c-background').hide(); $('#' + screenId).show(); } }); }); </script> <title> %(title)s </title> </head> <body class="run"> %(content)s </body> </html> """ % dict( title=content["name"], content=body ) # return Response(html, content_type="text/html") self.response.headers["Content-Type"] = "text/html" self.response.write(html)
def handle_authorized(data): authorizer_appid = data.get('AuthorizerAppid') authorization_code = data.get('AuthorizationCode') code_expire = data.get('AuthorizationCodeExpiredTime') logging.debug("authorized appid:%s code:%s expire:%s", authorizer_appid, authorization_code, code_expire) rds = g.im_rds db = g._db auth_code = authorization_code store_id = 0 component_token = get_component_access_token(rds) if not component_token: return "授权失败" wx = WXOpenAPI(APPID, APPSECRET, component_token) r = wx.request_auth(auth_code) if r: info = r['authorization_info'] wx_appid = info['authorizer_appid'] access_token = info['authorizer_access_token'] expires_in = info['expires_in'] #提前10分钟过期 if expires_in > 20*60: expires_in = expires_in - 10*60 refresh_token = info['authorizer_refresh_token'] funcs = info['func_info'] fids = [] for f in funcs: fid = f['funcscope_category']['id'] fids.append(fid) is_app = False AUTHORIZATION_MESSAGE = 1 AUTHORIZATION_CONTACT = 19 if AUTHORIZATION_MESSAGE in fids: #公众号 is_app = False elif AUTHORIZATION_CONTACT in fids: #小程序 is_app = True else: logging.warning("no message authorization") return "没有消息权限" app_info = wx.request_info(wx_appid) if not app_info: logging.warning("request app info fail") return "获取公众号信息失败" name = app_info['authorizer_info']['nick_name'] gh_id = app_info['authorizer_info']['user_name'] app = App.get_wx(db, wx_appid) if app: Client.update_wx(db, wx_appid, refresh_token, 1) if app['store_id'] != 0 and app['store_id'] != store_id: return "已被其它账号授权" else: App.create_wx(db, name, gh_id, wx_appid, refresh_token, store_id, is_app) WX.set_access_token(rds, wx_appid, access_token, expires_in) return "授权成功" else: return "获取令牌失败"
def get(self): app = App.instance() self.response.out.write(template.render('templates/main.html', locals()))
def app_edit(appid): """ 创建游戏 """ app = App.get_app(g._db, appid) return render_template('app/edit.html', data={'game': app, 'method': 'edit'})
def post(self): app = App.instance() app.total_posts += 1 app.put()
def auth_callback(uid): rds = g.im_rds db = g._db auth_code = request.args.get('auth_code') expires_in = request.args.get('expires_in') if not auth_code or not expires_in: return "非法调用" seller = Seller.get_seller(db, uid) store_id = seller['store_id'] logging.debug("auth callback code:%s uid:%s store_id:%s", auth_code, uid, store_id) component_token = get_component_access_token(rds) if not component_token: return "授权失败" wx = WXOpenAPI(APPID, APPSECRET, component_token) r = wx.request_auth(auth_code) if r: info = r['authorization_info'] logging.debug("auth callback info:%s", info) wx_appid = info['authorizer_appid'] access_token = info['authorizer_access_token'] expires_in = info['expires_in'] #提前10分钟过期 if expires_in > 20*60: expires_in = expires_in - 10*60 refresh_token = info['authorizer_refresh_token'] funcs = info['func_info'] fids = [] for f in funcs: fid = f['funcscope_category']['id'] fids.append(fid) is_app = False AUTHORIZATION_MESSAGE = 1 AUTHORIZATION_CONTACT = 19 if AUTHORIZATION_MESSAGE in fids: #公众号 is_app = False elif AUTHORIZATION_CONTACT in fids: #小程序 is_app = True else: logging.warning("no message authorization") return "没有消息权限" app_info = wx.request_info(wx_appid) if not app_info: logging.warning("request app info fail") return "获取公众号信息失败" name = app_info['authorizer_info']['nick_name'] gh_id = app_info['authorizer_info']['user_name'] app = App.get_wx(db, wx_appid) if app: Client.update_wx(db, wx_appid, refresh_token, 1) if app['store_id'] != 0 and app['store_id'] != store_id: return "已被其它账号授权" if app['store_id'] == 0: App.set_store_id(db, app['id'], store_id) else: App.create_wx(db, name, gh_id, wx_appid, refresh_token, store_id, is_app) WX.set_access_token(rds, wx_appid, access_token, expires_in) return "授权成功" else: return "获取令牌失败"
def update_all(): for app in App.all(): update_app(app)
def get(self): app_id = self.request.get("app") apps = App.all() apps.filter('app_id =', app_id) app = apps.get() self.render(u'permissions', app=app)