def get_shared(): """Return the records from the shared table.""" user = root.authorized() cid = request.query.cid app = request.query.app or root.active_app() n = request.query.n if not n: n = config.jobs_num_rows else: n = int(n) # sort by descending order of jobs.id result = db((db.jobs.shared=="True") & (db.jobs.uid==users.id)).select(orderby=~jobs.id)[:n] # result = db((db.jobs.shared=="True") & # (jobs.gid == users.gid)).select(orderby=~jobs.id)[:n] # clear notifications users(user=user).update_record(new_shared_jobs=0) db.commit() params = {} params['cid'] = cid params['app'] = app params['user'] = user params['n'] = n params['num_rows'] = config.jobs_num_rows return template('shared', params, rows=result)
def get_all_jobs(): user = root.authorized() if not user == "admin": return template("error", err="must be admin to use this feature") cid = request.query.cid app = request.query.app or root.active_app() n = request.query.n if not n: n = config.jobs_num_rows else: n = int(n) # sort by descending order of jobs.id result = db((db.jobs.uid==users.id)).select(orderby=~jobs.id)[:n] # clear notifications users(user=user).update_record(new_shared_jobs=0) db.commit() params = {} params['cid'] = cid params['app'] = app params['user'] = user params['n'] = n params['num_rows'] = config.jobs_num_rows return template('shared', params, rows=result)
def share_case(): root.authorized() jid = request.forms.jid jobs(id=jid).update_record(shared="True") db.commit() # increase count in database for every user for u in db().select(users.ALL): nmsg = users(user=u.user).new_shared_jobs or 0 users(user=u.user).update_record(new_shared_jobs=nmsg+1) db.commit() redirect('/jobs')
def post_login(): if not config.auth: return "ERROR: authorization disabled. Change auth setting in config.py to enable" s = request.environ.get('beaker.session') row = users(user=request.forms.get('user').lower()) pw = request.forms.passwd err = "<p>Login failed: wrong username or password</p>" # if password matches, set the USER_ID_SESSION_KEY hashpw = hashlib.sha256(pw).hexdigest() try: if hashpw == row.passwd: # set session key s[USER_ID_SESSION_KEY] = row.user.lower() s.save() else: return err except: exc_type, exc_value, exc_traceback = sys.exc_info() print traceback.print_exception(exc_type, exc_value, exc_traceback) return err # if referred to login from another page redirect to referring page referrer = request.forms.referrer if referrer: redirect('/' + referrer) else: redirect('/myapps')
def aws_status(aid): user = root.authorized() cid = request.query.cid app = request.query.app params = {} params['aid'] = aid params['cid'] = cid params['app'] = app params['user'] = user params['port'] = config.port a = aws_conn(aid) if users(user=user).id != aws_instances(aid).uid: return template('error', err="access forbidden") try: astatus = a.status() if astatus['state'] == "running": astatus['uptime'] = a.uptime(astatus['launch_time']) astatus['charge since last boot'] = a.charge(astatus['uptime']) return template('aws_status', params, astatus=astatus) except: exc_type, exc_value, exc_traceback = sys.exc_info() print traceback.print_exception(exc_type, exc_value, exc_traceback) return template( 'error', err= "There was a problem connecting to the AWS machine. Check the credentials and make sure the machine is running." )
def new_order(): user_list = [(user['id'], user['nazwa']) for user in model.users()] form = forms.NewOrderForm.feed_with_users(request.form, user_list) if request.method == 'POST' and form.validate(): model.save_order(form.name.data, form.description.data, form.client_id.data) return redirect('/') return render_template('new_order.html', form=form)
def zipget(): """get zipfile from another machine, save to current machine""" import zipfile user = root.authorized() cid = request.query.cid app = request.query.app try: worker = config.remote_worker_url except: exc_type, exc_value, exc_traceback = sys.exc_info() print traceback.print_exception(exc_type, exc_value, exc_traceback) worker = request.query.url # if config.worker != "remote" or config.remote_worker_url is None: if worker is None: params = { 'app': app, 'err': "worker and remote_worker_url parameters must be set " + " in config.py for this feature to work" } return template('error', params) requests.get(worker + "/zipcase", params={ 'app': app, 'cid': cid, 'user': user }) path = os.path.join(user_dir, user, app, cid) file_path = path + ".zip" url = os.path.join(worker, file_path) print "url is:", url if not os.path.exists(path): os.makedirs(path) print "downloading " + url fh, _ = urllib.urlretrieve(url) z = zipfile.ZipFile(fh, 'r', compression=zipfile.ZIP_DEFLATED) z.extractall() # add case to database uid = users(user=user).id db.jobs.insert(uid=uid, app=app, cid=cid, state="REMOTE", description="", time_submit=time.asctime(), walltime="", np="", priority="") db.commit() # status = "file_downloaded" # redirect(request.headers.get('Referer')) #+ "&status=" + status) redirect("/jobs")
def showapps(): user = root.authorized() q = request.query.q if not q: result = db().select(apps.ALL, orderby=apps.name) else: result = db( db.apps.name.contains(q, case_sensitive=False) | db.apps.category.contains(q, case_sensitive=False) | db.apps.description.contains(q, case_sensitive=False)).select() # find out what apps have already been activated so that a user can't activate twice uid = users(user=user).id activated = db(app_user.uid == uid).select() activated_apps = [] for row in activated: activated_apps.append(row.appid) if user == "admin": configurable = True else: configurable = False params = {'configurable': configurable, 'user': user} return template('apps', params, rows=result, activated=activated_apps)
def execute(): app = request.forms['app'] user = request.forms['user'] cid = request.forms['cid'] desc = request.forms['desc'] np = request.forms['np'] appmod = pickle.loads(request.forms['appmod']) # remove the appmod key del request.forms['appmod'] appmod.write_params(request.forms, user) # if preprocess is set run the preprocessor try: if appmod.preprocess: run_params, _, _ = appmod.read_params(user, cid) base_dir = os.path.join(user_dir, user, app) process.preprocess(run_params, appmod.preprocess, base_dir) if appmod.preprocess == "terra.in": appmod.outfn = "out" + run_params['casenum'] + ".00" except: return template('error', err="There was an error with the preprocessor") # submit job to queue try: priority = db(users.user == user).select( users.priority).first().priority uid = users(user=user).id jid = sched.qsub(app, cid, uid, np, priority, desc) return str(jid) #redirect("http://localhost:"+str(config.port)+"/case?app="+str(app)+"&cid="+str(cid)+"&jid="+str(jid)) except OSError: return "ERROR: a problem occurred"
def post(self): json_data = request.get_json(force=True) user_id=uuid.uuid1() new_user = users(str(user_id),json_data['username'],json_data['passwd'],int(time.strftime('%Y',time.localtime(time.time())))-int(json_data['birth_year']), json_data['gender']) db.session.add(new_user) db.session.commit() flash("Sign up successfully") return json_data,201
def _check_user_passwd(user, passwd): """check password against database""" u = users(user=user) hashpw = _hash_pass(passwd) if hashpw == u.passwd: return True else: return False
def save_theme(): user = root.authorized() uid = users(user=user).id print "saving theme:", request.forms.theme user_meta.update_or_insert(user_meta.uid == uid, uid=uid, theme=request.forms.theme) db.commit()
def useapp(): user = root.authorized() uid = users(user=user).id app = request.forms.app appid = apps(name=app).id print "allowing user", user, uid, "to access app", app, appid app_user.insert(uid=uid, appid=appid) db.commit() redirect('/apps')
def post_aws_creds(): user = root.authorized() a = request.forms.account_id s = request.forms.secret k = request.forms.key uid = users(user=user).id db.aws_creds.insert(account_id=a, secret=s, key=k, uid=uid) db.commit() redirect('/aws')
def removeapp(): user = root.authorized() uid = users(user=user).id app = request.forms.app appid = apps(name=app).id auid = app_user(uid=uid, appid=appid).id del app_user[auid] print "removing user", user, uid, "access to app", app, appid db.commit() redirect('/myapps')
def execute(): user = root.authorized() app = request.forms.app cid = request.forms.cid np = int(request.forms.np) or 1 walltime = request.forms.walltime desc = request.forms.desc #priority = request.forms.priority params = {} # base_dir = os.path.join(user_dir, user, app, cid) inputs, _, _ = root.myapps[app].read_params(user, cid) # in addition to supporting input params, also support case id if "cid" not in inputs: inputs["cid"] = cid # if preprocess is set run the preprocessor # try: # if root.myapps[app].preprocess: # processed_inputs = process.preprocess(inputs, # root.myapps[app].preprocess,base_dir) # except: # exc_type, exc_value, exc_traceback = sys.exc_info() # print traceback.print_exception(exc_type, exc_value, exc_traceback) # return template('error', err="There was an error with the preprocessor") cmd = apps(name=app).command # for parallel runs if np > 1: cmd = config.mpirun + " -np " + str(np) + " " + cmd # this is the relative path to the executable from the case directory where # the simulation files are stored inputs['rel_apps_path'] = (os.pardir + os.sep)*4 + apprw.apps_dir # replace placeholder tags in the command line, e.g. <cid> with appropriate params cmd = replace_tags(cmd, inputs) outfn = app + ".out" # submit job to queue try: params['cid'] = cid params['app'] = app params['user'] = user priority = db(users.user==user).select(users.priority).first().priority uid = users(user=user).id jid = root.sched.qsub(app, cid, uid, cmd, np, priority, walltime, desc) redirect("/case?app="+app+"&cid="+cid+"&jid="+jid) except OSError, e: exc_type, exc_value, exc_traceback = sys.exc_info() print traceback.print_exception(exc_type, exc_value, exc_traceback) print >> sys.stderr, "Execution failed:", e params = { 'cid': cid, 'app': app, 'user': user, 'err': e } return template('error', params)
def GET(self): if session.room and session.username: regd_users = model.users() player_score = model.show_score(session.username) id_ = model.user_id(session.username) piclink = model.piclink(id_) return render.show_room(room=session.room, user=session.username, score=player_score, piclink=piclink, guesses=session.guess) else: # why is there here? do you need it? # yes if someone tries to directly go to the /game url without setting the session # or session.room is None return render.you_died(death=map.death())
def showmyapps(): user = root.authorized() uid = users(user=user).id app = root.active_app() result = db((apps.id == app_user.appid) & (uid == app_user.uid)).select(orderby=apps.name) if user == "admin": configurable = True else: configurable = False params = {'configurable': configurable, 'user': user, 'app': app} return template('myapps', params, rows=result)
def login(): if request.method == 'GET': return render_template('login.html') else: log = model.users() username = request.form['username'] password = request.form['password'] if username == log[0]: if password == log[1]: balance = model.check_balance() return render_template('homepage.html', balance=balance) else: return render_template('login.html', message='Error: Bad Credentials')
def create_instance(): """create instance""" user = root.authorized() instance = request.forms.instance itype = request.forms.itype region = request.forms.region rate = request.forms.rate uid = users(user=user).id db.aws_instances.insert(instance=instance, itype=itype, region=region, rate=rate, uid=uid) db.commit() redirect('/aws')
def elite(update, context): """ Mention the elite """ cid=update.message.chat.id # validate user if not utils._check_user(context, update.effective_user.id, cid): return # telegram user user=model.User(id=update.effective_user.id, username=update.effective_user.username, full_name=update.effective_user.full_name, link=update.effective_user.link, is_bot=update.effective_user.is_bot) # mention users users=model.users() i=0 tmp="" max_cw_level=0 for u in users: if u.cw_level>max_cw_level: max_cw_level=u.cw_level for u in users: if u.id==str(update.effective_user.id) or u.cw_level<max_cw_level-model.get_data("MENTION_ELITE_DELTA", 15): continue tmp+="@{0} ".format(u.username) i+=1 if not i%3: i=0 try: context.bot.send_message(chat_id=cid, text=u"\U0000270A Sir! Yes Sir!\n"+tmp, parse_mode=telegram.ParseMode.HTML, disable_web_page_preview=True) except Exception as e: utils._admin_error(context, "/elite", user=user, error=str(e)) tmp="" if tmp: try: context.bot.send_message(chat_id=cid, text=u"\U0000270A Sir! Yes Sir!\n"+tmp, parse_mode=telegram.ParseMode.HTML, disable_web_page_preview=True) except Exception as e: utils._admin_error(context, "/elite", user=user, error=str(e))
def message(update, context): """ Send a message to each user """ try: if str(update.effective_user.id) == settings.ADMIN_ID: # message hint if update.message.text == "/message": text = 'Use /message <i>text</i> for send the current text to each subscribed user\n\nThe text can be formatted:\n{0} user full name\n{1} admin link' context.bot.send_message(chat_id=settings.ADMIN_ID, text=text, parse_mode=telegram.ParseMode.HTML, disable_web_page_preview=True) # build and send message else: text = update.message.text[9:] text.format("", html.escape(settings.ADMIN_CONTACT)) counter_success = 0 counter_fail = 0 list_fail = [] users = model.users() for user in users: try: context.bot.send_message( chat_id=user.id, text=text.format( html.escape(user.full_name), html.escape(settings.ADMIN_CONTACT)), parse_mode=telegram.ParseMode.HTML, disable_web_page_preview=True) counter_success += 1 except Exception as e: counter_fail += 1 list_fail.append(user.full_name) # send report to the admin report_text = '<b>Message:</b> {0}\n\n<b>Subscribed users:</b> {1}\n<b>Successful deliveries:</b> {2}\n<b>Unsuccessful deliveries:</b> {3}'.format( text.format("USER", html.escape(settings.ADMIN_CONTACT)), len(users), counter_success, counter_fail) if list_fail: report_text += "\n<b>Unsuccessful users:</b> {0}".format( ", ".join(list_fail)) context.bot.send_message(chat_id=settings.ADMIN_ID, text=report_text, parse_mode=telegram.ParseMode.HTML, disable_web_page_preview=True) except Exception as e: utils._admin_error(context, "/message", error=str(e), trace=False)
def tokensignin(): email = request.forms.get('email') s = request.environ.get('beaker.session') user, _ = email.split('@') s[USER_ID_SESSION_KEY] = user if not users(user=user.lower()): # insert a random password that nobody will be able to guess hashpw = _hash_pass(str(uuid.uuid4())[:8]) users.insert(user=user.lower(), email=email, passwd=hashpw, priority=config.default_priority, new_shared_jobs=0) db.commit() return user
def aws_conn(id): """create a connection to the EC2 machine and return the handle""" user = root.authorized() uid = users(user=user).id try: creds = db(db.aws_creds.uid == uid).select().first() account_id = creds['account_id'] secret = creds['secret'] key = creds['key'] instances = db(db.aws_instances.id == id).select().first() instance = instances['instance'] region = instances['region'] rate = instances['rate'] or 0. return EC2(key, secret, account_id, instance, region, rate) except: return template('error', err="problem validating AWS credentials")
def admin_delete_user(): user = root.authorized() if not user == "admin": return template("error", err="must be admin to delete") uid = request.forms.uid if int(uid) == 0: return template("error", err="can't delete admin user") if request.forms.del_files == "True": path = os.path.join(user_dir, users(uid).user) print "deleting files in path:", path if os.path.isdir(path): shutil.rmtree(path) del db.users[uid] db.commit() redirect("/admin/show_users")
def everybody(update, context): """ Mention all subscribed users """ cid=update.message.chat.id # validate user if not utils._check_user(context, update.effective_user.id, cid): return # telegram user user=model.User(id=update.effective_user.id, username=update.effective_user.username, full_name=update.effective_user.full_name, link=update.effective_user.link, is_bot=update.effective_user.is_bot) # mention users users=model.users() i=0 tmp="" for u in users: if u.id==str(update.effective_user.id): continue tmp+="@{0} ".format(u.username) i+=1 if not i%3: i=0 try: context.bot.send_message(chat_id=cid, text=tmp, parse_mode=telegram.ParseMode.HTML, disable_web_page_preview=True) except Exception as e: utils._admin_error(context, "/everybody", user=user, error=str(e)) tmp="" if tmp: try: context.bot.send_message(chat_id=cid, text=tmp, parse_mode=telegram.ParseMode.HTML, disable_web_page_preview=True) except Exception as e: utils._admin_error(context, "/everybody", user=user, error=str(e))
def change_password(): # this is basically the same coding as the register function # needs to be DRY'ed out in the future user = root.authorized() if config.auth and not root.authorized(): redirect('/login') opasswd = request.forms.opasswd pw1 = request.forms.npasswd1 pw2 = request.forms.npasswd2 # check old passwd #user = request.forms.user if _check_user_passwd(user, opasswd) and pw1 == pw2 and len(pw1) > 0: u = users(user=user) u.update_record(passwd=_hash_pass(pw1)) db.commit() else: return template('error', err="problem with password") params = {} params['user'] = user params['alert'] = "SUCCESS: password changed" return template('account', params)
def craft_reset(update, context): """ Reset craft operation """ global CACHE cid=update.message.chat.id # validate user if not utils._check_user(context, update.effective_user.id, cid): return tmp=update.message.text.split(" ") if len(tmp)>1: if tmp[1]=="yes": # reset users for user in model.users(): user.crafting="{}" status=model.update_user(user) # reset guild CACHE["guild"]={"resources": {}, "parts": {}, "recipes": {}} context.bot.send_message(chat_id=cid, text="Crafting operation restarted...", parse_mode=telegram.ParseMode.HTML, disable_web_page_preview=True) else: user=model.user_by_id(tmp[1]) if user: user.crafting="{}" status=model.update_user(user) context.bot.send_message(chat_id=cid, text="Crafting operation restarted [{1}]... for @{0}".format(html.escape(user.username), status), parse_mode=telegram.ParseMode.HTML, disable_web_page_preview=True) else: context.bot.send_message(chat_id=cid, text='Type "/craft_reset <i>yes</i>" to reset all operation and stored data', parse_mode=telegram.ParseMode.HTML, disable_web_page_preview=True)
def register(): if session.get('logged_in'): return redirect(url_for('dashboard')) else: try: if request.method == 'POST': username = request.form['username'] email = request.form['email'] password = sha256_crypt.encrypt( (str(request.form['password']))) try: x = m.users.query.filter_by(username=username).all() if len(x) > 0: flash("Username Taken") return render_template('register.html') else: uid = m.db.session.query(m.users.uid) i_uid = uid.all() i_uid = len(i_uid) + 1 user = m.users(i_uid, username, password, email) m.db.session.add(user) m.db.session.commit() flash("Done") return redirect(url_for('dashboard')) except: print("Error") return render_template('register.html') except Exception as e: return (str(e))
def check_outdata_crafting_list(): while True: time.sleep(int(model.get_data("CRAFT_OUTDATE_INTERVAL_HOURS", 8*3600))) cid=model.get_data("CRAFTING_ROOM_CHAT_ID", None) print("[TIMER] check_outdata_crafting_list:", cid) if cid: today=datetime.datetime.today() try: for u in model.users(): crafting=json.loads(u.crafting) if crafting and len(crafting.keys()): # validate the date t=datetime.datetime.fromisoformat(crafting["datetime"]) tmp=(datetime.datetime.today()-t).total_seconds()/(24.0*3600.0) if tmp>int(model.get_data("CRAFT_OUTDATE_INTERVAL_DAYS", 3)): BOT.send_message(chat_id=cid, text=u'@{0}, your crafting data is outdated. Please, forward it here...'.format(html.escape(u.username)), parse_mode=telegram.ParseMode.HTML, disable_web_page_preview=True) except Exception as e: class Context: bot=BOT context=Context() utils._admin_error(context, "timer.check_outdata_crafting_list", error=str(e))
def resource(update, context): """ Show info about an specific resource """ global CACHE day_range=7 if settings.VERBOSE: print("[REGEX] resource") cid=update.message.chat.id # validate user if not utils._check_user(context, update.effective_user.id, cid): return # list of resources recipes="" parts="" # code r_recipe=utils.item_by_code("r{0}".format(update.message.text.split("@")[0][2:]), "recipes") r_recipe_amount=0 r_part=utils.item_by_code("k{0}".format(update.message.text.split("@")[0][2:]), "parts", adjust_part_code=True) r_part_amount=0 if r_recipe and r_part: # guild if "guild" in CACHE: # recipes tmp=CACHE["guild"]["recipes"].get(r_recipe["name"].lower(), 0) if tmp: r_recipe_amount+=tmp recipes+="\n - {0} x {1}".format(settings.GUILD_NAME.decode('unicode_escape'), tmp) # parts tmp=CACHE["guild"]["parts"].get(r_part["name"].lower(), 0) if tmp: r_part_amount+=tmp parts+="\n - {0} x {1}".format(settings.GUILD_NAME.decode('unicode_escape'), tmp) # users for u in model.users(): crafting=json.loads(u.crafting) if crafting: # validate the date dated="" t=datetime.datetime.fromisoformat(crafting["datetime"]) if (datetime.datetime.today()-t).total_seconds()/(day_range*24.0*60.0*60.0)>1: dated=u' \U0000231B' # recipes tmp=crafting["recipes"].get(r_recipe["name"].lower(), 0) if tmp: r_recipe_amount+=tmp recipes+='\n - <a href="https://t.me/share/url?url=/g_deposit%20{2}%20{1}">{0}</a> x {1}{3}'.format(html.escape(u.username), tmp, r_recipe["code"], dated) # parts tmp=crafting["parts"].get(r_part["name"].lower(), 0) if tmp: r_part_amount+=tmp parts+='\n - <a href="https://t.me/share/url?url=/g_deposit%20{2}%20{1}">{0}</a> x {1}{3}'.format(html.escape(u.username), tmp, r_part["code"], dated) # send message crafteable=u'\U00002705' if utils.item_is_crafteable(r_recipe, r_recipe_amount, r_part_amount) else u'\U0001F17E' text="{1} <b>{0}</b>\n(links are deposit shortcuts)\n\n".format(r_recipe["name"][:-7], crafteable) text+="{0} ({1}){2}\n\n{3} ({4}){5}".format(r_recipe["name"], r_recipe_amount, recipes if recipes else "", r_part["name"], r_part_amount, parts if parts else "") context.bot.send_message(chat_id=cid, text=text, parse_mode=telegram.ParseMode.HTML, disable_web_page_preview=True) else: context.bot.send_message(chat_id=cid, text=settings.MESSAGES["unknow"].format(""), disable_web_page_preview=True)
def GET(self): users = list(model.users()) user_ids = [u['id'] for u in users] return json.dumps(user_ids)