def get(self, method, lookup, rdb): ''' Lookup the user by the uid ''' if method == 'uid': uid = lookup elif method == 'username': uid = self.getUID(lookup, rdb) results = r.table('users').get(uid).run(rdb) data = results if data: self.email = results['email'] self.uid = results['id'] self.username = results['username'] self.status = results['status'] self.company = results['company'] self.contact = results['contact'] self.acttype = results['acttype'] self.stripeid = results['stripeid'] self.stripe = results['stripe'] self.subplans = results['subplans'] self.payments = results['payments'] self.subscription = results['subscription'] self.creation_time = results['creation_time'] self.confirmed = results['confirmed'] ## Identify number of monitors and reactions monitor = Monitor() reaction = Reaction() self.monitorCount = monitor.count(self.uid, rdb) self.reactionCount = reaction.count(self.uid, rdb) return self else: return None
def get(self, method, lookup, rdb): """ Lookup the user by the uid """ if method == "uid": uid = lookup elif method == "username": uid = self.getUID(lookup, rdb) results = r.table("users").get(uid).run(rdb) data = results if data: self.email = results["email"] self.uid = results["id"] self.username = results["username"] self.status = results["status"] self.company = results["company"] self.contact = results["contact"] self.acttype = results["acttype"] self.stripeid = results["stripeid"] self.stripe = results["stripe"] self.subplans = results["subplans"] self.payments = results["payments"] self.subscription = results["subscription"] self.creation_time = results["creation_time"] self.confirmed = results["confirmed"] ## Identify number of monitors and reactions monitor = Monitor() reaction = Reaction() self.monitorCount = monitor.count(self.uid, rdb) self.reactionCount = reaction.count(self.uid, rdb) return self else: return None
def addcheck_page(cname): verify = verifyLogin(app.config['SECRET_KEY'], app.config['COOKIE_TIMEOUT'], request.cookies) if verify: user = User() user.get('uid', verify, g.rdb_conn) data = startData(user) data['active'] = 'dashboard' data['url'] = '/dashboard/monitors/' + cname tmpl = 'monitors/' + cname + '.html' data['js_bottom'] = [ 'monitors/monitorlist.js', 'monitors/base.js', 'monitors/' + cname + '.js' ] # Check Users Status if user.status != "active": data['url'] = '/dashboard/mod-subscription' tmpl = 'member/mod-subscription.html' else: # Get list of reactions and validate that there are some data['reactions'] = user.getReactions(g.rdb_conn) # Proces the form cform = __import__("monitorforms." + cname, globals(), locals(), ['CheckForm'], -1) form = cform.CheckForm(request.form) if form.__contains__("timer"): form.timer.choices = data['choices'] reactchoices = [] for key in data['reactions'].keys(): reactchoices.append((data['reactions'][key]['id'], data['reactions'][key]['name'])) form.reactions.choices = reactchoices if request.method == 'POST': if form.validate(): monitor = Monitor() monitor.name = form.name.data monitor.ctype = cname monitor.uid = user.uid monitor.status = "queued" monitor.url = None tmpdata = {} for item in form.__iter__(): tmpdata[item.name] = item.data monitor.data = tmpdata # Check if the user already exceeds their limit if monitor.count(user.uid, g.rdb_conn) < data['limit']: # Create the monitor if all checks out results = monitor.createMonitor(g.rdb_conn) else: results = "toomany" if results == "exists": print("/dashboard/monitors/{0} - \ Monitor already exists".format(cname)) flash( '{0} seems to already exist. \ Try using a different name.'.format( monitor.name), 'danger') elif results is False: print("/dashboard/monitors/{0} - \ Monitor creation failed".format(cname)) flash('Could not create monitor.', 'danger') elif results == 'toomany': stathat.ez_count( app.config['STATHAT_EZ_KEY'], app.config['ENVNAME'] + ' Too many health checks', 1) flash( 'You have too many monitors. \ Please upgrade your plan or clean \ up old ones.', 'danger') print("/dashboard/monitors/{0} - \ Monitor creation failed: toomany".format(cname)) else: stathat.ez_count( app.config['STATHAT_EZ_KEY'], app.config['ENVNAME'] + ' Monitor Added', 1) print("/dashboard/monitors/%s - \ Monitor creation successful") % cname flash( 'Monitor "{0}" successfully added.'.format( monitor.name), 'success') newmonitor = Monitor() newmonitor.get(results, g.rdb_conn) if newmonitor.uid == user.uid: data['monitor'] = { 'cid': newmonitor.cid, 'name': newmonitor.name, 'uid': newmonitor.uid, 'ctype': newmonitor.ctype, 'url': newmonitor.url, 'data': newmonitor.data } else: print("/dashboard/monitors/{0} - \ Monitor creation failed: Form invalid".format(cname)) flash('Form is not valid.', 'danger') page = render_template(tmpl, data=data, form=form) return page else: flash('Please Login.', 'warning') return redirect(url_for('user.login_page'))
def modsub_page(): '''Dashboard Modify Subscription: This will allow a user to modify their subscription and account plan ''' verify = verifyLogin(app.config['SECRET_KEY'], app.config['COOKIE_TIMEOUT'], request.cookies) if verify: user = User() user.get('uid', verify, g.rdb_conn) data = startData(user) data['active'] = 'dashboard' data['url'] = '/dashboard/mod-subscription' data['uid'] = user.uid tmpl = 'member/mod-subscription.html' data['js_bottom'].append('forms/subscribe.js') form = [] headers = { "content-type": "application/json", "Authorization": app.config['ASSEMBLY_PRIVATE_KEY'] } from generalforms import subscribe if data['acttype'] == "Lite": # Upgrade Users if request.method == "POST" and \ "stripeToken" in request.form and "plan" in request.form: stripeToken = request.form['stripeToken'] plan = request.form['plan'] if stripeToken: result = None monitor = Monitor() payload = { 'email': user.email, 'quantity': monitor.count(user.uid, g.rdb_conn), 'card': stripeToken, 'plan': plan } json_payload = json.dumps(payload) url = app.config['ASSEMBLY_PAYMENTS_URL'] + "/customers" print("Making request to %s") % url try: # Send Request to Assembly to create user and subscribe # them to desired plan result = requests.post(url=url, headers=headers, data=json_payload, verify=True) except: print("Critical Error making request to ASM Payments") flash( 'There was an error processing \ your card details.', 'danger') print("Got {0} status code from Assembly".format( result.status_code)) if result.status_code >= 200 and result.status_code <= 299: rdata = json.loads(result.text) user.stripeid = rdata['id'] user.stripe = stripeToken user.subplans = payload['quantity'] user.subscription = payload['plan'] user.acttype = "pro" print("Setting UID %s Subscription to: %s") % ( user.uid, user.acttype) subres = user.setSubscription(g.rdb_conn) stathat.ez_count( app.config['STATHAT_EZ_KEY'], app.config['ENVNAME'] + ' User Upgrades', 1) if subres: newdata = startData(user) data['limit'] = newdata['limit'] data['rlimit'] = newdata['rlimit'] data['acttype'] = newdata['acttype'] data['subplans'] = newdata['subplans'] data['cost'] = newdata['cost'] data['subscription'] = newdata['subscription'] flash('Subscription successfully created.', 'success') else: flash('Subscription not successfully created.', 'danger') # Increase subscription if data['acttype'] != "Lite": form = subscribe.AddPackForm(request.form) if request.method == "POST" and "stripeToken" not in request.form: if form.validate(): add_packs = int(form.add_packs.data) # Set subscription quantity to desired monitor count payload = {'quantity': add_packs} json_payload = json.dumps(payload) url = app.config[ 'ASSEMBLY_PAYMENTS_URL'] + "/customers/" + user.stripeid print("Making request to %s") % url try: # Get Subscription ID result = requests.get(url=url, headers=headers, verify=True) if result.status_code == 200: rdata = json.loads(result.text) subsid = rdata['subscriptions']['data'][0]['id'] url = url + "/subscriptions/" + subsid print("Making request to %s") % url # Set Quantity try: result = requests.put(url=url, headers=headers, data=json_payload, verify=True) except: print("Critical Error making \ request to ASM Payments") flash( 'An error occured while \ processing the form.', 'danger') else: flash( 'An error occured while \ processing the form.', 'danger') except: print("Critical Error making request to ASM Payments") flash( 'An error occured \ while processing the form.', 'danger') print("Got {0} status code from Assembly".format( result.status_code)) if result.status_code >= 200 and result.status_code <= 299: user.subplans = add_packs # Save user config print("Setting subscription count to \ {0} for user {1}".format(add_packs, user.uid)) subres = user.setSubscription(g.rdb_conn) if subres: newdata = startData(user) data['limit'] = newdata['limit'] data['rlimit'] = newdata['rlimit'] data['acttype'] = newdata['acttype'] data['subplans'] = newdata['subplans'] data['cost'] = newdata['cost'] flash('Subscription successfully modified.', 'success') else: flash('Unknown error modifing subscription.', 'danger') page = render_template(tmpl, data=data, form=form) return page else: flash('Please Login.', 'warning') return redirect(url_for('user.login_page'))
def modsub_page(): '''Dashboard Modify Subscription: This will allow a user to modify their subscription and account plan ''' verify = verifyLogin( app.config['SECRET_KEY'], app.config['COOKIE_TIMEOUT'], request.cookies) if verify: user = User() user.get('uid', verify, g.rdb_conn) data = startData(user) data['active'] = 'dashboard' data['url'] = '/dashboard/mod-subscription' data['uid'] = user.uid tmpl = 'member/mod-subscription.html' data['js_bottom'].append('forms/subscribe.js') form = [] # Stripe vs ASM stuff if user.payments == "ASM": headers = { "content-type": "application/json", "Authorization": app.config['ASSEMBLY_PRIVATE_KEY'] } paymenturl = app.config['ASSEMBLY_PAYMENTS_URL'] else: from base64 import b64encode api_key = b64encode(app.config['STRIPE_PRIVATE_KEY']).decode("ascii") headers = { "Authorization": "Basic " + api_key, } paymenturl = app.config['STRIPE_PAYMENTS_URL'] from generalforms import subscribe if data['upgraded'] is False: # Upgrade Users if request.method == "POST" and \ "stripeToken" in request.form and "plan" in request.form: stripeToken = request.form['stripeToken'] plan = request.form['plan'] if stripeToken: result = None monitor = Monitor() payload = { 'email': user.email, 'quantity': monitor.count(user.uid, g.rdb_conn), 'source': stripeToken, 'plan': plan } json_payload = json.dumps(payload) url = paymenturl + "/customers" print ("Making request to %s") % url try: # Send Request to Payment system to create user and subscribe # them to desired plan result = requests.post( url=url, headers=headers, params=payload, verify=True) except: print("Critical Error making request to Payments") flash('There was an error processing \ your card details.', 'danger') print("Got {0} status code from Payments".format( result.status_code)) if result.status_code >= 200 and result.status_code <= 299: rdata = json.loads(result.text) user.stripeid = rdata['id'] user.stripe = stripeToken user.subplans = payload['quantity'] user.subscription = payload['plan'] if "pro_plus" in plan: user.acttype = "proplus" else: user.acttype = "pro" print("Setting UID %s Subscription to: %s") % ( user.uid, user.acttype) subres = user.setSubscription(g.rdb_conn) stathat.ez_count( app.config['STATHAT_EZ_KEY'], app.config['ENVNAME'] + ' User Upgrades', 1) if subres: newdata = startData(user) data['limit'] = newdata['limit'] data['rlimit'] = newdata['rlimit'] data['acttype'] = newdata['acttype'] data['subplans'] = newdata['subplans'] data['cost'] = newdata['cost'] data['subscription'] = newdata['subscription'] flash('Subscription successfully created.', 'success') else: flash('Subscription not successfully created.', 'danger') else: flash('Subscription not created got status code: %d' % result.status_code, 'danger') # Increase subscription if data['upgraded']: form = subscribe.AddPackForm(request.form) if request.method == "POST" and "stripeToken" not in request.form: if form.validate(): add_packs = int(form.add_packs.data) # Set subscription quantity to desired monitor count payload = {'quantity': add_packs} json_payload = json.dumps(payload) url = paymenturl + "/customers/" + user.stripeid print("Making request to %s") % url try: # Get Subscription ID result = requests.get( url=url, headers=headers, verify=True) if result.status_code == 200: rdata = json.loads(result.text) subsid = rdata['subscriptions']['data'][0]['id'] url = url + "/subscriptions/" + subsid print("Making request to %s") % url # Set Quantity try: if user.payments == "ASM": result = requests.put( url=url, headers=headers, data=json_payload, verify=True) else: result = requests.post( url=url, headers=headers, params=payload, verify=True) except: print("Critical Error making \ request to ASM Payments") flash('An error occured while \ requesting update to %s.' % url, 'danger') else: flash('An error occured while \ pulling subscription details - %d.' % result.status_code, 'danger') except: print("Critical Error making request to ASM Payments") flash('An error occured \ while processing the form.', 'danger') print("Got {0} status code from Assembly".format( result.status_code)) if result.status_code >= 200 and result.status_code <= 299: user.subplans = add_packs # Save user config print("Setting subscription count to \ {0} for user {1}".format(add_packs, user.uid)) subres = user.setSubscription(g.rdb_conn) if subres: newdata = startData(user) data['limit'] = newdata['limit'] data['rlimit'] = newdata['rlimit'] data['acttype'] = newdata['acttype'] data['subplans'] = newdata['subplans'] data['cost'] = newdata['cost'] flash('Subscription successfully modified.', 'success') else: flash('Unknown error modifing subscription.', 'danger') page = render_template(tmpl, data=data, form=form) return page else: flash('Please Login.', 'warning') return redirect(url_for('user.login_page'))
def addcheck_page(cname): verify = verifyLogin( app.config['SECRET_KEY'], app.config['COOKIE_TIMEOUT'], request.cookies) if verify: user = User() user.get('uid', verify, g.rdb_conn) data = startData(user) data['active'] = 'dashboard' data['url'] = '/dashboard/monitors/' + cname tmpl = 'monitors/' + cname + '.html' data['js_bottom'] = ['monitors/monitorlist.js', 'monitors/base.js', 'monitors/' + cname + '.js'] # Check Users Status if user.status != "active": data['url'] = '/dashboard/mod-subscription' tmpl = 'member/mod-subscription.html' else: # Get list of reactions and validate that there are some data['reactions'] = user.getReactions(g.rdb_conn) # Proces the form cform = __import__( "monitorforms." + cname, globals(), locals(), ['CheckForm'], -1) form = cform.CheckForm(request.form) if form.__contains__("timer"): form.timer.choices = data['choices'] reactchoices = [] for key in data['reactions'].keys(): reactchoices.append( (data['reactions'][key]['id'], data['reactions'][key]['name'])) form.reactions.choices = reactchoices if request.method == 'POST': if form.validate(): monitor = Monitor() monitor.name = form.name.data monitor.ctype = cname monitor.uid = user.uid monitor.status = "queued" monitor.url = None tmpdata = {} for item in form.__iter__(): tmpdata[item.name] = item.data monitor.data = tmpdata # Check if the user already exceeds their limit if monitor.count(user.uid, g.rdb_conn) < data['limit']: # Create the monitor if all checks out results = monitor.createMonitor(g.rdb_conn) else: results = "toomany" if results == "exists": print("/dashboard/monitors/{0} - \ Monitor already exists".format(cname)) flash('{0} seems to already exist. \ Try using a different name.'.format( monitor.name), 'danger') elif results is False: print("/dashboard/monitors/{0} - \ Monitor creation failed".format(cname)) flash('Could not create monitor.', 'danger') elif results == 'toomany': stathat.ez_count( app.config['STATHAT_EZ_KEY'], app.config['ENVNAME'] + ' Too many health checks', 1) flash('You have too many monitors. \ Please upgrade your plan or clean \ up old ones.', 'danger') print("/dashboard/monitors/{0} - \ Monitor creation failed: toomany".format(cname)) else: stathat.ez_count( app.config['STATHAT_EZ_KEY'], app.config['ENVNAME'] + ' Monitor Added', 1) print("/dashboard/monitors/%s - \ Monitor creation successful") % cname flash('Monitor "{0}" successfully added.'.format( monitor.name), 'success') newmonitor = Monitor() newmonitor.get(results, g.rdb_conn) if newmonitor.uid == user.uid: data['monitor'] = { 'cid': newmonitor.cid, 'name': newmonitor.name, 'uid': newmonitor.uid, 'ctype': newmonitor.ctype, 'url': newmonitor.url, 'data': newmonitor.data } else: print("/dashboard/monitors/{0} - \ Monitor creation failed: Form invalid".format(cname)) flash('Form is not valid.', 'danger') page = render_template(tmpl, data=data, form=form) return page else: flash('Please Login.', 'warning') return redirect(url_for('user.login_page'))
def modsub_page(): '''Dashboard Modify Subscription: This will allow a user to modify their subscription and account plan ''' verify = verifyLogin( app.config['SECRET_KEY'], app.config['COOKIE_TIMEOUT'], request.cookies) if verify: user = User() user.get('uid', verify, g.rdb_conn) data = startData(user) data['active'] = 'dashboard' data['url'] = '/dashboard/mod-subscription' data['uid'] = user.uid tmpl = 'member/mod-subscription.html' data['js_bottom'].append('forms/subscribe.js') form = [] headers = { "content-type": "application/json", "Authorization": app.config['ASSEMBLY_PRIVATE_KEY'] } from generalforms import subscribe if data['acttype'] == "Lite": # Upgrade Users if request.method == "POST" and \ "stripeToken" in request.form and "plan" in request.form: stripeToken = request.form['stripeToken'] plan = request.form['plan'] if stripeToken: result = None monitor = Monitor() payload = { 'email': user.email, 'quantity': monitor.count(user.uid, g.rdb_conn), 'card': stripeToken, 'plan': plan } json_payload = json.dumps(payload) url = app.config['ASSEMBLY_PAYMENTS_URL'] + "/customers" print ("Making request to %s") % url try: # Send Request to Assembly to create user and subscribe # them to desired plan result = requests.post( url=url, headers=headers, data=json_payload, verify=True) except: print("Critical Error making request to ASM Payments") data['msg'] = "There was an error processing your card details" data['error'] = True print( "Got %d status code from Assembly") % result.status_code if result.status_code >= 200 and result.status_code <= 299: rdata = json.loads(result.text) user.stripeid = rdata['id'] user.stripe = stripeToken user.subplans = payload['quantity'] user.subscription = payload['plan'] user.acttype = "pro" print("Setting UID %s Subscription to: %s") % ( user.uid, user.acttype) subres = user.setSubscription(g.rdb_conn) stathat.ez_count( app.config['STATHAT_EZ_KEY'], app.config['ENVNAME'] + ' User Upgrades', 1) if subres: newdata = startData(user) data['limit'] = newdata['limit'] data['rlimit'] = newdata['rlimit'] data['acttype'] = newdata['acttype'] data['subplans'] = newdata['subplans'] data['cost'] = newdata['cost'] data['subscription'] = newdata['subscription'] data['msg'] = "Subscription successfully created" data['error'] = False else: data[ 'msg'] = "Subscription not successfully created" data['error'] = True # Increase subscription if data['acttype'] != "Lite": form = subscribe.AddPackForm(request.form) if request.method == "POST" and "stripeToken" not in request.form: if form.validate(): add_packs = int(form.add_packs.data) # Set subscription quantity to desired monitor count payload = {'quantity': add_packs} json_payload = json.dumps(payload) url = app.config[ 'ASSEMBLY_PAYMENTS_URL'] + "/customers/" + user.stripeid print("Making request to %s") % url try: # Get Subscription ID result = requests.get( url=url, headers=headers, verify=True) if result.status_code == 200: rdata = json.loads(result.text) subsid = rdata['subscriptions']['data'][0]['id'] url = url + "/subscriptions/" + subsid print("Making request to %s") % url # Set Quantity try: result = requests.put( url=url, headers=headers, data=json_payload, verify=True) except: data['msg'] = "An error occured while processing the form" data['error'] = True print("Critical Error making request to ASM Payments") else: data['msg'] = "An error occured while processing the form" data['error'] = True except: data['msg'] = "An error occured while processing the form" data['error'] = True print("Critical Error making request to ASM Payments") print( "Got %d status code from Assembly") % result.status_code if result.status_code >= 200 and result.status_code <= 299: user.subplans = add_packs # Save user config print("Setting subscription count to %d for user %s") % (add_packs, user.uid) subres = user.setSubscription(g.rdb_conn) if subres: newdata = startData(user) data['limit'] = newdata['limit'] data['rlimit'] = newdata['rlimit'] data['acttype'] = newdata['acttype'] data['subplans'] = newdata['subplans'] data['cost'] = newdata['cost'] data['msg'] = "Subscription successfully modified" data['error'] = False else: data['msg'] = "Unknown error modifing subscription" data['error'] = True page = render_template(tmpl, data=data, form=form) return page else: return redirect(url_for('user.login_page'))