def addTeam(self, leagueID): # Returns a redirection to /manage/teams/{leagueID} if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser.Permissions[0].teams: RTeam = db.Teams(name=request.params["name"], short=request.params["short"], country=request.params["country"], league=leagueID, link=request.params["link"]) Session.add(RTeam) Session.commit() avatar = request.params["avatar"].file avatar_path = os.path.join('website/public/images/teams', str(RTeam.id) + '.jpg') temp_path = avatar_path + '~' output_file = open(temp_path, 'wb') avatar.seek(0) while True: data = avatar.read(2<<16) if not data: break output_file.write(data) output_file.close() os.rename(temp_path, avatar_path) return redirect("/manage/teams/" + leagueID) else: return redirect("https://saloon.tf/") else: return redirect("https://saloon.tf/")
def matchesList(self, leagueID): # Returns a rendered template if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser.Permissions[0].bets: c.current = "manage" c.managePage = "matches" c.user = RUser.to_dict() RLeague = Session.query(db.Leagues).filter(db.Leagues.id == leagueID).first() RMatches = Session.query(db.Matches, db.Writeups).join(db.Writeups).filter(and_(db.Matches.league == leagueID, db.Matches.status != 5)).order_by(db.Matches.id.desc()).all() RTeams = Session.query(db.Teams).filter(db.Teams.league == leagueID).order_by(db.Teams.id).all() c.league = RLeague.to_dict() c.matches = [] c.writeups = [] for RMatch, RWriteup in RMatches: match = RMatch.to_dict() match["naturalTime"] = RMatch.naturalTime() match["Team1"].update(RMatch.team1Score()) match["Team2"].update(RMatch.team2Score()) match["json"] = json.dumps(match, default = lambda obj: obj.strftime("%d-%m-%Y %H:%M") if isinstance(obj, datetime.datetime) else None) c.matches.append(match) writeup = RWriteup.to_dict() writeup["json"] = json.dumps(writeup) c.writeups.append(writeup) c.teams = [] for RTeam in RTeams: team = RTeam.to_dict() c.teams.append(team) return render('/manage/matches/list.mako') return abort(403)
def tickets(self): c.user = False if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser: if RUser.Permissions[0].bets: c.current = "manage" c.managePage = "tickets" c.user = RUser.to_dict() c.tickets = [] RTickets = Session.query(db.Tickets).filter(db.Tickets.open == True).order_by(db.Tickets.id.desc()).all() for RTicket in RTickets: ticket = RTicket.to_dict() ticket["categoryName"] = RTicket.categoryName() params = {} params["view"] = "cm" params["ui"] = 2 params["tf"] = 0 params["fs"] = 1 params["to"] = ticket["email"] params["su"] = "Saloon.tf_staging %s #%d - %s" % (ticket["categoryName"], ticket["id"], ticket["title"]) params["body"] = "\nBest regards, %s\n\nQuoted:\n" % (RUser.name) for line in ticket["text"].splitlines(): params["body"] += "> %s\n" % (line) ticket["url"] = "https://mail.google.com/mail/u/0/?" + urllib.urlencode(params) c.tickets.append(ticket) return render("/manage/tickets.mako") return redirect("https://saloon.tf/manage/")
def bets(self, matchID, teamID, offset = 0, limit = 10): c.current = "match" c.user = False if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser: c.user = RUser.to_dict() limit = min(int(limit), 10) RMatch = Session.query(db.Matches).filter(db.Matches.id == matchID).first() c.match = RMatch.to_dict() conditions = [ db.Bets.match == matchID, db.Bets.team == teamID, ] if c.user: conditions += [db.Bets.user != RUser.id] c.bets = [] RBets = Session.query(db.Bets) \ .filter(and_(*conditions)) \ .order_by(db.Bets.value.desc()) \ .offset(offset).limit(limit).all() if not RBets: return render('/ajax/bets.mako') betIDs = [RBet.id for RBet in RBets] betItems = defaultdict(list) RItemsBet = Session.query(db.ItemsInstances, db.ItemsBet) \ .distinct(db.ItemsInstances.originID) \ .filter(db.ItemsBet.bet.in_(betIDs), db.ItemsBet.itemInstance == db.ItemsInstances.id) \ .all() RItemsBet = sorted(RItemsBet, key = lambda x: (x[1].origin * -1, x[0].Item.type, x[0].Item.quality, x[0].Item.value * -1)) for RBet in RBets: bet = RBet.to_dict(show=["bets.User", "bets.Team"]) if RBet.team == RMatch.team1: bet["Team"].update(RMatch.team1Score()) else: bet["Team"].update(RMatch.team2Score()) bet["own"] = False bet["items"] = [] for RItemInstance, RItemBet in RItemsBet: if RItemBet.bet != RBet.id: continue item = RItemInstance.to_dict(show=["itemsinstances.Item"]) item["origin"] = RItemBet.origin bet["items"].append(item) c.bets.append(bet) return render('/ajax/bets.mako')
def editBot(self, botID): # Returns a json string if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser.Permissions[0].bots: RBot = Session.query(db.Bots).filter(db.Bots.id == botID).first() if RBot: RBot.name = request.params["name"] RBot.emailAddress = request.params["emailAddress"] if request.params["emailPassword"]: RBot.emailPassword = request.params["emailPassword"] RBot.steamLogin = request.params["steamLogin"] if request.params["steamPassword"]: RBot.steamPassword = request.params["steamPassword"] RBot.steamID = request.params["steamID"] RBot.steamAPI = request.params["steamAPI"] match = re.match(r"http:\/\/steamcommunity\.com\/tradeoffer\/new\/\?partner=[0-9]+&token=([a-zA-Z0-9\-]+)", request.params["tradeoffers"]) if match: RBot.token = match.group(1) RBot.slotsEmpty += int(request.params["slots"]) - RBot.slots RBot.slots = int(request.params["slots"]) Session.commit() success = True message = "Changed selected bot." else: success = False message = "<strong>Oh snap!</strong> Couldn't edit this bot." else: success = False message = "You don't have sufficent privileges to access this page." else: success = False message = "You don't have sufficent privileges to access this page." array = {"success": success, "message": message} return json.dumps(array)
def newOffer(self, botID, userID): if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser.Permissions[0].economy: c.current = "manage" c.managePage = "bots" c.user = RUser.to_dict() RBot = Session.query(db.Bots).filter(db.Bots.id == botID).first() ROtherUser = Session.query(db.Users).filter(db.Users.id == userID).first() if RBot and ROtherUser: botAPI = API(RBot.steamAPI, RBot.steamID) c.bot = RBot.to_dict() c.botItems = botAPI.Friend(RBot.steamID).sortedInventory() c.botItemsCount = 0 for itemsGroup in c.botItems: for item in itemsGroup["items"].values(): c.botItemsCount += 1 c.botPages = int(math.ceil(c.botItemsCount / 24.0)) c.otherUser = ROtherUser.to_dict() inventory = botAPI.Friend(ROtherUser.steamID).inventory() c.otherUserItems = botAPI.Friend(ROtherUser.steamID).sortedInventory() c.otherUserItemsCount = 0 for itemsGroup in c.botItems: for item in itemsGroup["items"].values(): c.otherUserItemsCount += 1 c.otherUserPages = int(math.ceil(c.botItemsCount / 24.0)) return render("/manage/offers/new.mako") return render("/manage/bots/")
def teamsList(self, leagueID): # Returns a rendered template if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser.Permissions[0].teams: c.current = "manage" c.managePage = "teams" c.user = RUser.to_dict() RLeague = Session.query(db.Leagues).filter(db.Leagues.id == leagueID).first() RTeams = Session.query(db.Teams).filter(db.Teams.league == leagueID).order_by(db.Teams.id).all() RCountries = Session.query(db.Countries).order_by(db.Countries.name).all() c.league = RLeague.to_dict() c.teams = [] for RTeam in RTeams: team = RTeam.to_dict() team["json"] = json.dumps(team) c.teams.append(team) c.countries = [] for RCountry in RCountries: country = {} country["id"] = RCountry.id country["name"] = RCountry.name c.countries.append(country) return render('/manage/teams/list.mako') else: return abort(403) else: return abort(403)
def editLeague(self, leagueID): # Returns a json string if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser.Permissions[0].leagues: RLeague = Session.query(db.Leagues).filter(db.Leagues.id == leagueID).first() if RLeague: RLeague.name = request.params["name"] RLeague.type = request.params["type"] RLeague.region = request.params["region"] RLeague.colour = request.params["accentColour"] RLeague.detailsColour = request.params["detailsColour"] RLeague.textColour = request.params["textColour"] Session.commit() success = True message = "Changed selected league." else: success = False message = "<strong>Oh snap!</strong> Couldn't change this league." else: success = False message = "You don't have sufficent privileges to access this page." else: success = False message = "You don't have sufficent privileges to access this page." array = {"success": success, "message": message} return json.dumps(array)
def addLeague(self): # Returns redirection to /manage/leagues/ if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser.Permissions[0].leagues: params = ["name", "type", "region", "avatar", "accentColour"] if all(item in request.params for item in params): if request.params["avatar"].filename[-4:] != ".png": success = False message = "Wrong file type." else: RLeague = db.Leagues(name=request.params["name"], type=request.params["type"], region=request.params["region"], colour=request.params["accentColour"], detailsColour = request.params["detailsColour"], textColour = request.params["textColour"]) Session.add(RLeague) Session.commit() os.makedirs("website/public/images/leagues/" + str(RLeague.id)) avatar = request.params["avatar"].file avatar_path = os.path.join('website/public/images/leagues', str(RLeague.id), 'logo.png') temp_path = avatar_path + '~' output_file = open(temp_path, 'wb') avatar.seek(0) while True: data = avatar.read(2<<16) if not data: break output_file.write(data) output_file.close() os.rename(temp_path, avatar_path) return redirect("https://saloon.tf/manage/leagues/") return redirect("https://saloon.tf/")
def user(self, userID): if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser.Permissions[0].users: ROtherUser = Session.query(db.Users).filter(db.Users.id == userID).first() if ROtherUser: c.current = "manage" c.managePage = "users" c.user = RUser.to_dict() c.otherUser = ROtherUser.to_dict() c.ranks = {} if RUser.Permissions[0].permissions: RUsersRanks = Session.query(db.UsersRanks).all() for RUsersRank in RUsersRanks: c.ranks[RUsersRank.id] = RUsersRank.name c.teams = [] if ROtherUser.teams: RTeams = Session.query(db.Teams).filter(db.Teams.id.in_(ROtherUser.teams)).all() for RTeam in RTeams: c.teams.append(RTeam.to_dict()) return render('/manage/users/user.mako') return redirect("https://saloon.tf/manage/users/") return redirect("https://saloon.tf/manage/")
def editTeam(self, leagueID, teamID): # Returns a json string if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser.Permissions[0].teams: RTeam = Session.query(db.Teams).filter(db.Teams.id == teamID).first() if (RTeam): RTeam.name = request.params["name"] RTeam.short = request.params["short"] RTeam.country = request.params["country"] RTeam.league = leagueID, RTeam.link = request.params["link"] Session.commit() success = True message = "Changed selected team." else: success = False message = "<strong>Oh snap!</strong> Couldn't edit this team." else: success = False message = "You don't have sufficent privileges to access this page." else: success = False message = "You don't have sufficent privileges to access this page." array = {"success": success, "message": message} return json.dumps(array)
def index(self): # Return a rendered template c.current = "home" c.user = False if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser: c.user = RUser.to_dict() RMatches = Session.query(db.Matches)\ .filter(db.Matches.status != 5)\ .order_by( sql.expression.case(( (db.Matches.status == 1, 0), (db.Matches.status == 0, sql.expression.extract('epoch', db.Matches.time)) )).asc(), db.Matches.time.desc(), db.Matches.id.desc() )\ .limit(10).all() c.matches, c.bets, c.offers = ([],[],[]) for RMatch in RMatches: match = RMatch.to_dict(show=["matches.Team1", "matches.Team2"]) match["Team1"].update(RMatch.team1Score()) match["Team2"].update(RMatch.team2Score()) match["naturalTime"] = RMatch.naturalTime() match["json"] = json.dumps(match, default = lambda obj: obj.isoformat() if isinstance(obj, datetime.datetime) else None) match["Team1"]["json"] = json.dumps(match["Team1"]) match["Team2"]["json"] = json.dumps(match["Team2"]) c.matches.append(match) offer = False bet = False if c.user: ROffer = Session.query(db.Offers).filter(and_(db.Offers.status == 2, db.Offers.match == RMatch.id, db.Offers.user == RUser.id, db.Offers.hidden == False)).first() if ROffer: offer = ROffer.team bet = { "team": None, "own": False, "limit": RUser.Rank.slots } RBet = Session.query(db.Bets).filter(and_(db.Bets.user == RUser.id, db.Bets.match == RMatch.id)).first() if RBet: bet["team"] = RBet.team bet["own"] = True RItemsCount = Session.query(func.count(db.ItemsBet.id)).filter(db.ItemsBet.bet == RBet.id).first() bet["limit"] -= RItemsCount[0] c.bets.append(bet) c.offers.append(offer) return render('/home.mako')
def edit(self): if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser: RUser.name = request.params["name"] if request.params["tradeLink"]: match = re.match(r"(http|https):\/\/steamcommunity\.com\/tradeoffer\/new\/\?partner=[0-9]+&token=([a-zA-Z0-9\-\_]+)", request.params["tradeLink"]) if match: RUser.token = match.group(2) Session.commit() return redirect("https://saloon.tf/settings/")
def closeTicket(self, ticketID): c.user = False if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser: if RUser.Permissions[0].bets: RTicket = Session.query(db.Tickets).filter(db.Tickets.id == ticketID).first() RTicket.open = False Session.commit() return redirect("https://saloon.tf/manage/tickets/") return redirect("https://saloon.tf/manage/")
def index(self): # Returns a rendered template c.user = False if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser: if RUser.Permissions[0].manage: c.current = "manage" c.managePage = "dashboard" c.user = RUser.to_dict() RNotification = Session.query(db.Notifications).first() c.notification = RNotification.to_dict() return render('/manage/dashboard.mako') return abort(403)
def disableNotification(self): c.user = False if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser: if RUser.Permissions[0].manage: RNotification = Session.query(db.Notifications).first() RNotification.active = False Session.commit() try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(("127.0.0.1", 9002)) s.send(json.dumps(["notification", "disable"])) s.close() except Exception, e: pass return redirect("https://saloon.tf/manage/")
def matchesLeagues(self): # Returns a rendered template if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser.Permissions[0].bets: c.current = "manage" c.managePage = "matches" c.user = RUser.to_dict() RLeagues = Session.query(db.Leagues).order_by(db.Leagues.id).all() c.leagues = [] for RLeague in RLeagues: league = RLeague.to_dict() league["json"] = json.dumps(league) c.leagues.append(league) return render('/manage/matches/index.mako') return abort(403)
def removeTeam(self, leagueID, teamID): # Returns a json string if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser.Permissions[0].teams: RTeam = Session.query(db.Teams).filter(db.Teams.id == teamID).first() if RTeam: RMatch = Session.query(db.Matches).filter(db.or_(db.Matches.team1 == teamID, db.Matches.team2 == teamID)).first() if RMatch: success = False message = "<strong>Oh snap!</strong> I can't remove a team with matches." else: Session.delete(RTeam) Session.commit() success = True message = "Removed selected team." else: success = False message = "<strong>Oh snap!</strong> Couldn't remove this team." else: success = False message = "You don't have sufficent privileges to access this page." else: success = False message = "You don't have sufficent privileges to access this page." array = {"success": success, "message": message} return json.dumps(array)
def offersReceived(self, botID): if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser.Permissions[0].economy: c.current = "manage" c.managePage = "bots" c.user = RUser.to_dict() RBot = Session.query(db.Bots).filter(db.Bots.id == botID).first() if RBot: c.bot = RBot.to_dict() c.offers = API(RBot.steamAPI, RBot.steamID).GetOffers("received", False, True, False, True) for offer in c.offers.values(): RUser = Session.query(db.Users).filter(db.Users.steamID == int(offer.Partner.steamID)).first() offer.user = False if RUser: offer.user = RUser.to_dict() return render("/manage/offers/received.mako") return render("/manage/bots/")
def index(self): c.current = "tos" c.user = False if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser: c.user = RUser.to_dict() return render("/tos.mako")
def index(self): # Return a rendered template c.current = "code" c.user = False if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser: c.user = RUser.context() return render('/code.mako')
def users(self): # Returns rendered template if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser.Permissions[0].users: c.current = "manage" c.managePage = "users" c.user = RUser.to_dict() return render('/manage/users/index.mako') return redirect("https://saloon.tf/manage/")
def index(self): # Return a rendered template c.current = "help" c.user = False if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser: c.user = RUser.to_dict() return render("/help.mako")
def bots(self): if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser.Permissions[0].bots: c.current = "manage" c.managePage = "bots" c.user = RUser.to_dict() c.bots = [] RBots = Session.query(db.Bots).order_by(db.Bots.id).all() for RBot in RBots: bot = RBot.to_dict() bot["json"] = json.dumps(bot) c.bots.append(bot) return render('/manage/bots.mako') else: return redirect("https://saloon.tf/manage/") else: return redirect("https://saloon.tf/")
def users(self, name = False, steamid = False, limit = False): # Return a json string if limit: limit = int(limit) if limit > 10: limit = 10 if name: name = name.lower() RUsers = Session.query(db.Users).filter(func.lower(db.Users.name).like(name + "%")).limit(limit).all() else: RUsers = Session.query(db.Users).filter(db.Users.steamid == steamid).limit(limit).all() users = [] for RUser in RUsers: user = {} user["id"] = RUser.id user["name"] = RUser.name user["steamid"] = RUser.steamID users.append(user) return json.dumps(users)
def editUser(self, userID): if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser.Permissions[0].users: ROtherUser = Session.query(db.Users).filter(db.Users.id == userID).first() if ROtherUser: if int(request.POST["steamid"]) != ROtherUser.steamID: url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=%s&steamids=%d" % (config["steamapi"], int(request.POST["steamid"])) data = json.loads(requests.get(url).text)[u"response"][u"players"][0] ROtherUser.avatar = data[u"avatarfull"][67:-9] + "_full.jpg" ROtherUser.name = data[u"personaname"] ROtherUser.steamID = request.POST["steamid"] if RUser.Permissions[0].permissions: for permission in ["manage", "leagues", "teams", "users", "bets", "bots", "economy", "scores"]: if permission not in request.POST.getall("permissions"): setattr(ROtherUser.Permissions[0], permission, False) else: setattr(ROtherUser.Permissions[0], permission, True) ROtherUser.rank = int(request.POST["rank"]) teams = [] for team in request.POST.getall("team"): teams.append(int(team)) ROtherUser.teams = teams Session.commit() success = True message = "Changed selected user." else: success = False message = "<strong>Oh snap!</strong> Couldn't edit this user." else: success = False message = "You don't have sufficent privileges to access this page." else: success = False message = "You don't have sufficent privileges to access this page." array = {"success": success, "message": message} return json.dumps(array)
def teams(self, name = False, leagueID = False, limit = False): # Return a json string if limit: limit = int(limit) if limit > 10: limit = 10 if name: name = name.lower() RTeams = Session.query(db.Teams).filter(func.lower(db.Teams.name).like(name + "%")).limit(limit).all() else: RTeams = Session.query(db.Teams).filter(db.Teams.leagueID == leagueID).limit(limit).all() teams = [] for RTeam in RTeams: team = {} team["id"] = RTeam.id team["name"] = RTeam.name team["short"] = RTeam.short team["league"] = RTeam.League.to_dict() teams.append(team) return json.dumps(teams)
def new(self): # Return a json array if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser: category = int(request.params["category"]) if category in range(4): RTicket = db.Tickets(user=RUser.id, category=category, title=request.params["title"], email=request.params["email"], text=request.params["text"]) Session.add(RTicket) Session.commit() success = True message = "Ticket sent successfully. Check your email soon for the reponse!" else: success = False message = "<strong>Oh snap!</strong> Couldn't send the ticket." else: success = False message = "You don't have sufficent priviledges to access this page." else: success = False message = "You don't have sufficent priviledges to access this page." array = {"success": success, "message": message} return json.dumps(array)
def index(self): # Return a rendered template c.current = "settings" c.user = False if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser: c.user = RUser.context() if not c.user: return redirect("https://saloon.tf/home/") return render("settings.mako")
def removeMatch(self, leagueID, matchID): # Returns a json string if "steamid" in session: RUser = Session.query(db.Users).filter(db.Users.steamID == session["steamid"]).first() if RUser.Permissions[0].bets: RMatch = Session.query(db.Matches).filter(db.Matches.id == matchID).first() if RMatch: RMatch.status = 5 Session.commit() success = True message = "Removed selected match." else: success = False message = "<strong>Oh snap!</strong> Couldn't remove this match." else: success = False message = "You don't have sufficent privileges to access this page." else: success = False message = "You don't have sufficent privileges to access this page." array = {"success": success, "message": message} return json.dumps(array)