コード例 #1
0
def new_game():
    # get group_name from form & initiating user's turn
    if request.method == "POST":
        group_name = request.form.get("group")

        # check that no game started with group where one of members is currently in an active game
        activeusers = query_db(
            "SELECT users.user_id FROM games INNER JOIN groups ON groups.group_name=games.group_name INNER JOIN users ON users.user_id=groups.user_id WHERE games.active=?",
            [
                True,
            ],
            one=False)
        members = query_db("SELECT user_id FROM groups WHERE group_name=?",
                           [group_name],
                           one=False)
        if len(members) < 3:
            flash(
                group_name +
                " does not have enough players. Add at least 3 to start a game."
            )
            return redirect("/")
        intersection = [value for value in activeusers if value in members]
        if intersection:
            flash("One or more members of selected group is busy playing.")
            return redirect("/")

        row = query_db("SELECT * FROM groups WHERE group_name=? AND user_id=?",
                       [group_name, session["user_id"]],
                       one=True)
        # new DB entry for new game
        get_db().execute(
            "INSERT INTO games (active, turn, group_name) VALUES (:active, :turn, :group_name)",
            {
                "active": 1,
                "turn": row["turn"],
                "group_name": row["group_name"]
            })
        get_db().commit()
        session["userrow"] = query_db(
            "SELECT * FROM games INNER JOIN groups ON groups.group_name=games.group_name INNER JOIN users ON users.user_id=groups.user_id WHERE users.user_id=? AND games.active=?",
            [session["user_id"], 1],
            one=True)
        session["gamerow"] = query_db(
            "SELECT * FROM groups INNER JOIN games ON games.group_name=groups.group_name INNER JOIN users ON groups.user_id=users.user_id WHERE groups.user_id=? AND games.active=?",
            [session["user_id"], 1],
            one=True)
        session["round"] = 1
        return render_template("live_game.html",
                               turn=row["turn"],
                               round=session["round"],
                               isturn=True,
                               group_name=group_name)
    else:
        groups = query_db("SELECT group_name FROM groups WHERE user_id=?",
                          [session["user_id"]],
                          one=False)
        return render_template("new_game.html", groups=groups)
コード例 #2
0
def leave_group(group):
    # Disallow leaving active group
    if session["gamerow"]["group_name"] == group:
        flash(
            group +
            " is currently playing with you. End game before leaving the group."
        )
        return redirect("/groups")
    get_db().execute("DELETE FROM groups WHERE user_id=? AND group_name=?",
                     (session["user_id"], group))
    get_db().commit()
    flash("You left " + group + "!")
    return redirect("/groups")
コード例 #3
0
ファイル: data.py プロジェクト: nmktextiles/nmk_datawarehouse
 def get_max_datetime(self, table_name):
     """ Calculates and returns the maximum date up to which data is already present in DB
         Parameter: name of table
     """
     db = get_db()
     query = '''select {} from {}'''.format(self.column_check_date,
                                            table_name)
     cursor = db.cursor()
     try:
         cursor.execute(query)
         result = cursor.fetchall()
         dates = list()
         for item in result:
             dates.append(self.__get_datetime(item[0]))
         db.close()
         return max(dates)
     except Exception as e:
         try:
             db.close()
         except:
             pass
         print "Exception while generating max date :", e
         Logger.log('error',
                    "Exception while generating max date : {}.".format(e))
         return None
コード例 #4
0
ファイル: data.py プロジェクト: nmktextiles/nmk_datawarehouse
 def truncate_table(self, table_name):
     """ Truncates a given table
         Parameter: table name
     """
     db = get_db()
     query = "truncate table {}".format(table_name)
     cursor = db.cursor()
     cursor.execute(query)
     db.close()
コード例 #5
0
def print_tail(db, lines, filter):
    database = get_db(db)
    with cd(database['home']):
        latest_log = ""
        if len(filter) > 0:
            latest_log = run(
                "ls -ltr | grep %s | awk 'END{print}' | awk {'print $9'};" %
                filter)
        else:
            latest_log = run("ls -ltr | awk 'END{print}' | awk {'print $9'};")
        if len(latest_log) > 0:
            print run('tail -n-%s %s' % (lines, latest_log))
コード例 #6
0
def sign_up():
    # Clear session
    session.clear()
    #Ensure all fields filled out also if JS disabled
    if request.method == "POST":
        name = request.form.get("username")
        if not name:
            return apology("Please provide username")
        if not request.form.get("password") or not request.form.get(
                "confirmation"):
            return apology("Please provide password and confirm it")

        hashp = generate_password_hash(request.form.get("password"),
                                       method='pbkdf2:sha256',
                                       salt_length=8)
        if not check_password_hash(hashp, request.form.get("confirmation")):
            return apology("Password does not match confirmation.")

        try:
            cur = get_db().execute(
                "INSERT INTO users (name, hash) VALUES (:name, :hash)", {
                    "name": name,
                    "hash": hashp
                })
            get_db().commit()
        except sqlite3.IntegrityError:
            return apology("Username already exists")

        # Login user automatically, storing their id in session, then layout will also show index.html? &menu?
        session["user_id"] = query_db("SELECT user_id FROM users WHERE name=?",
                                      [name],
                                      one=True)["user_id"]
        session["name"] = query_db("SELECT name FROM users WHERE user_id=?",
                                   [session["user_id"]],
                                   one=True)["name"]
        return render_template("index.html")
    else:
        return render_template("sign_up.html")
コード例 #7
0
def _ycsb_execution_status(db):
    """ Shows whether the job is:q complete"""
    database = get_db(db)
    with cd(database['home']):
        latest_out_log = run(
            "ls -ltr | grep *.out | awk 'END{print}' | awk {'print $9'};")
        if len(latest_out_log) > 0:

            # Check for errors
            latest_err_log = run(
                "ls -ltr | grep .err | awk 'END{print}' | awk {'print $9'};")

            permissible_errors = ''
            if 'permissablerunerrors' in get_db(db):
                permissible_errors = " ".join([
                    '| grep -v ' + s
                    for s in get_db(db)['permissablerunerrors']
                ])
                print permissible_errors

            lines_in_error_log = run(
                'cat %s %s | grep -v "^ [0-9].*sec" | wc -l' %
                (latest_err_log, permissible_errors))
            if int(lines_in_error_log) > 8:
                ycsb.do_get_log(db)
                run('tail -n 100 %s ' % latest_err_log)
                raise Exception(
                    'Looks like there is something in the error log: %s : %s lines'
                    % (latest_err_log, int(lines_in_error_log)))

            # Check for completion
            complete = run('cat %s | grep "\[OVERALL\]" | wc -l' %
                           latest_out_log)
            run('tail -n-4 %s' % latest_out_log)
            if int(complete) > 0:
                print 'YCSB has completed on %s' % env.host
                return 1
    return 0
コード例 #8
0
async def main():
    db = get_db()

    try:
        ps = Queue()
        for ep in db._all():
            while ps.qsize() > limit_concurrent_requests:
                logging.debug('Queue too big! Waiting for some to finish')
                await ps.get()
            ps.put(asyncio.create_task(refresh_ep(ep, db)))

        while not ps.empty():
            await ps.get()
    except:
        pass
    finally:
        db.close()
コード例 #9
0
ファイル: sell_helpers.py プロジェクト: nkochath/NikhinStocks
def sell_stock(username, symbol, amount):
    data = lookup(symbol)
    price = data['price']
    cash_to_add = price * amount


    db = get_db()
    with db:
        current_shares = db.cursor().execute('SELECT amount FROM holdings WHERE username = ? and symbol = ?', (username, symbol)).fetchone()
        current_shares = current_shares[0]

        if amount == current_shares:
            db.cursor().execute('DELETE FROM holdings WHERE symbol = ?', (symbol, ))
        else:
            remaining_shares = current_shares - amount
            db.cursor().execute('UPDATE holdings SET amount = ? WHERE username = ? AND symbol = ?', (remaining_shares, username, symbol))


        db.cursor().execute('UPDATE users SET cash = ? WHERE username = ?', (cash_to_add + get_user_cash(), username))
        sql = 'INSERT INTO transactions (username, company, symbol, price, amount, value, type, date) VALUES(?,?,?,?,?,?,?,?)'
        date = str(datetime.now())
        tuple = (username, data['company'], data['symbol'], data['price'], amount, cash_to_add, 'sell', date)
        db.cursor().execute(sql, tuple)
コード例 #10
0
def next():
    if request.method == "POST":
        # insert written sentence into group's game
        group_name = session["gamerow"]["group_name"]
        newsentence = request.form.get("newsentence")

        if number_sentences(newsentence) > 1:
            flash("Please write only one sentence.")
            return redirect("/live_game")

        get_db().execute(
            "INSERT INTO sentences (game_id, sentence, group_name, user_id, time) VALUES (:game_id, :sentence, :group_name, :user_id, :timestamp)",
            {
                "game_id": session["gamerow"]["game_id"],
                "sentence": newsentence,
                "group_name": group_name,
                "user_id": session["gamerow"]["user_id"],
                "timestamp": datetime.now()
            })

        # get current turn, max turn (# of players), increment & insert to DB
        turn = session["userrow"]["turn"]
        maxturn = query_db("SELECT MAX(turn) FROM groups WHERE group_name=?",
                           [group_name],
                           one=True)["MAX(turn)"]
        turn = 1 if turn == maxturn else turn + 1
        get_db().execute("UPDATE games SET turn=? WHERE game_id=?",
                         (turn, session["userrow"]["game_id"]))
        get_db().commit()

        player_id = query_db(
            "SELECT user_id FROM groups WHERE group_name=? AND turn=?",
            [session["gamerow"]["group_name"], turn],
            one=True)["user_id"]
        player = query_db("SELECT name FROM users WHERE user_id=?",
                          [player_id],
                          one=True)["name"]
        session["round"] += 1
    return render_template("live_game.html",
                           round=session["round"],
                           group_name=group_name,
                           player=player)
コード例 #11
0
def end_game():
    if request.method == "POST":
        group_name = session["gamerow"]["group_name"]
        newsentence = request.form.get("newsentence")
        get_db().execute(
            "INSERT INTO sentences (game_id, sentence, group_name, user_id, time) VALUES (:game_id, :sentence, :group_name, :user_id, :timestamp)",
            {
                "game_id": session["gamerow"]["game_id"],
                "sentence": newsentence,
                "group_name": group_name,
                "user_id": session["gamerow"]["user_id"],
                "timestamp": datetime.now()
            })
        get_db().execute("UPDATE games SET active=? WHERE group_name=?",
                         (0, group_name))
        get_db().commit()
        session["round"] = None
        session["userrow"] = None
        session["gamerow"] = None
        return redirect("/archive")
コード例 #12
0
from helpers import get_db
import json

conn = get_db()
cur = conn.cursor()
cur.execute("select features from products where product_id=45")
product = cur.fetchone()[0]
for (k, v) in product.items():
    print(k)
    print(v)
    print("=============")
# print(product.keys())
# print(product['اقلام همراه هارد دیسک'])
# print(product.keys())
コード例 #13
0
ファイル: data.py プロジェクト: nmktextiles/nmk_datawarehouse
    def insert_db(self, data, table_name, data_updated_till, id_prefix):
        """ Inserts data to a given table
            Parameters: data from the API
                        name of table
                        maximum datetime up to which data is already update in the table
                        prefix of id of table
        """
        print "inside insert_db method"
        db = get_db()

        # Fetching columns in API
        fieldnames_api = data.next()
        fieldnames_api = [
            item.lower().replace(" : ", "_").replace(" ",
                                                     "_").replace("-", "_")
            for item in fieldnames_api
        ]

        try:
            column_check_date_index = fieldnames_api.index(
                self.column_check_date)
        except:
            print "WARNING !! {} not found in API response, GOING TO INSERT ALL DATA TO DATABASE.".format(
                ' '.join(self.column_check_date.split('_')))
            Logger.log(
                'warning',
                "{} not found in API response, GOING TO INSERT ALL DATA TO DATABASE."
                .format(' '.join(self.column_check_date.split('_'))))
            column_check_date_index = None

        # Fetching columns already present in out DB table
        query = "show columns from {}".format(table_name)
        cursor = db.cursor()
        cursor.execute(query)
        result = cursor.fetchall()
        fieldnames_db = list()
        for item in result[1:]:
            fieldnames_db.append(item[0])

        difference = list(set(fieldnames_api) - set(fieldnames_db))
        if len(difference) > 0:
            print "found new column(s)."
            Logger.log('info', "found new column(s).")
            try:
                self.__alter_table_collumn_add(db, table_name, difference)
            except Exception as e:
                print "Exception during alter table :", e
                Logger.log('error',
                           "Exception during alter table : {}".format(e))
                return None

        # fields structure to build the insert query
        if table_name == "inventory_master_db":
            fields = "%s, " * (len(fieldnames_api) + 2)
        else:
            fields = "%s, " * (len(fieldnames_api) + 1)
        fields = fields[:-2]

        max_id = self.__get_current_max_id(db, id_prefix, table_name)
        max_id = int(max_id)

        # fields to build the query string for building the insert query
        query_str = ''

        for item in fieldnames_api:
            query_str += item + ", "
        query_str = query_str + self.nmk_id_field
        if table_name == "inventory_master_db":
            query_str = query_str + ", last_modified"

        # building the final insert query
        query = '''insert into {} ({}) values ({})'''.format(
            table_name, query_str, fields)

        cursor = db.cursor()

        # Append id in each row of data to be inserted in DB table
        final_data = list()
        for row in data:
            row = [str(item) for item in row]
            if (column_check_date_index is not None) and (data_updated_till
                                                          is not None):
                try:
                    current_row_date_value = row[column_check_date_index]
                    date = self.__get_datetime(current_row_date_value)
                except Exception as e:
                    continue
                if data_updated_till < date:
                    max_id += 1
                    final_data.append(
                        self.__append_id(id_prefix, row, max_id, table_name))
            else:
                max_id += 1
                final_data.append(
                    self.__append_id(id_prefix, row, max_id, table_name))

        if (column_check_date_index is not None) and (data_updated_till
                                                      is not None):
            print "Number of new row(s) found : {}".format(len(final_data))
            Logger.log(
                'info',
                "Number of new row(s) found : {}".format(len(final_data)))

        # If we have values to be inserted in table then we insert all data at once
        if len(final_data):
            try:
                print "inserting data into table '{}'".format(table_name)
                Logger.log('info',
                           "inserting data into table '{}'".format(table_name))
                row_count = cursor.executemany(query, final_data)
                db.commit()
                print "Number of row(s) inserted : {}".format(row_count)
                Logger.log('info',
                           "Number of row(s) inserted : {}".format(row_count))
            except Exception as e:
                print "Database insertion exception :", e
                Logger.log('error',
                           "Database insertion exception : {}".format(e))

        db.close()
コード例 #14
0
def new_group():
    if request.method == "POST":
        group_name = request.form.get("group_name")
        if not group_name:
            flash("Please choose a group name!")
            return redirect("/new_group")

        if query_db("SELECT * FROM groups WHERE group_name=?", [group_name],
                    one=True) is not None:
            flash("Group name not available.")
            return redirect("/new_group")
        # set turn in group (for game) to 0 before looping
        turn = 0
        # get usernames typed into fields[]
        for field in zip(request.form.getlist("fields[]")):
            if not field[0]:
                flash("Please select player")
                return redirect("/new_group")
            user_id = query_db("SELECT user_id FROM users WHERE name=?",
                               [field[0]],
                               one=True)
            # check that user not duplicated in form AND group
            # if user enters inexistent username, remove all entries
            if user_id is None:
                get_db().execute("DELETE FROM groups WHERE group_name=?",
                                 (group_name, ))
                get_db().commit()
                flash(field[0] + " is not registered")
                return redirect("/new_group")
            # if username entered is already in group, remove all entries
            checkusers = query_db(
                "SELECT user_id FROM groups WHERE group_name=?", [group_name],
                one=False)
            if checkusers:
                for check in checkusers:
                    print("CHECK IS:", check["user_id"])
                    if (turn > 0) and (user_id["user_id"] == check["user_id"]):
                        get_db().execute(
                            "DELETE FROM groups WHERE group_name=?",
                            (group_name, ))
                        get_db().commit()
                        flash(field[0] + " is already added to " + group_name)
                        return redirect("/new_group")

            turn += 1
            try:
                get_db().execute(
                    "INSERT INTO groups (group_name, turn, user_id) VALUES (:group_name, :turn, :user_id)",
                    {
                        "group_name": group_name,
                        "turn": turn,
                        "user_id": user_id["user_id"]
                    })
                get_db().commit()

            except sqlite3.IntegrityError:
                return apology("something went wrong in DB")
        return redirect("/groups")
    else:
        return render_template("new_group.html")
コード例 #15
0
        item_id, {
            'id': item_id,
            'series': series_name,
            'last_title': current_title,
            'checked_since': now,
            'needs_title': needs_title,
            'needs_thumb': needs_thumb
        })


if __name__ == '__main__':
    if len(sys.argv) != 4:
        args = []
        for a in sys.argv:
            args.append(a)
        logging.error('Incorrect number of arguments! ARGS=%s' % args)
        sys.exit()

    item_type = sys.argv[1]
    item_id = sys.argv[2]
    item_isvirtual = sys.argv[3].lower() in ['1', 'true']

    if item_type != 'Episode' or item_isvirtual:  # Exit if item is virtual or is not an episode
        logging.info(
            f'Item is virtual. Exiting. ARGS={item_id};{item_type};{item_isvirtual}'
        )
        sys.exit()
    db = get_db()
    asyncio.run(check_episode(item_id, db))
    db.close()
コード例 #16
0
ファイル: orgAuthTest.py プロジェクト: Akanksha18/eden
import sys
import glob
import unittest
from gluon.globals import Request

# Pre-requisites & settings
# -----------------------------------------------------------------------------
APP_NAME=request.application
sys.path.append("applications/" + APP_NAME + "/tests/unit")
import helpers

auth = db.auth
manager = db.manager
helpers.USE_TEST_DB = True
db = helpers.get_db(DAL, db, clean=True)

if helpers.USE_TEST_DB:
    # run models over test db for zzz_1st_roles, etc
    model_files = glob.glob('applications/'+APP_NAME+'/models/*.py')
    for model_file in model_files:
        execfile(model_file, globals())


# Unit Tests
# -----------------------------------------------------------------------------
class OrgAuthTests(unittest.TestCase):
    def setUp(self):
        #self.test_org_id = db.org_organisation.insert(
                                #name="OrgAuthTest Org 1",
                                #acronym="OATO1")
コード例 #17
0
import sys
import glob
import unittest
from gluon.globals import Request

# Pre-requisites & settings
# -----------------------------------------------------------------------------
APP_NAME=request.application
sys.path.append("applications/" + APP_NAME + "/tests/unit")
import helpers

auth = db.auth
manager = db.manager
helpers.USE_TEST_DB = True
db = helpers.get_db(DAL, db, clean=True)

if helpers.USE_TEST_DB:
    # run models over test db for zzz_1st_roles, etc
    model_files = glob.glob('applications/'+APP_NAME+'/models/*.py')
    for model_file in model_files:
        execfile(model_file, globals())


# Unit Tests
# -----------------------------------------------------------------------------
class OrgAuthTests(unittest.TestCase):
    def setUp(self):
        #self.test_org_id = db.org_organisation.insert(
                                #name="OrgAuthTest Org 1",
                                #acronym="OATO1")
コード例 #18
0
async def main():
    if not api_token or not base_url:
        logging.critical('Either your api_token or base_url is blank!')
        sys.exit()
    api_json = {"X-Emby-Token": api_token}
    headers = {
        "user-agent":
        "mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/81.0.4044.138 safari/537.36"
    }
    headers.update(api_json)

    years = datetime.now().year if len(
        sys.argv) == 1 else None if sys.argv[1].lower() in ['all', 'none'
                                                            ] else sys.argv[1]
    logging.warning(f'Years={years}')
    raw_data = {
        'IncludeItemTypes': 'Episode',
        'Years': years,
        'Recursive': True,
        'IsMissing': False,
    }

    def get_items(params):
        res = requests.get(f'{base_url}/Items', params=params, headers=headers)
        try:
            data = json.loads(res.text)
        except json.decoder.JSONDecodeError:
            print(res.text)
            return []
        items = []
        for item in data.get('Items'):
            id = item.get("Id")
            items.append(id)
        return items

    ids = []
    if not check_thumbs:
        for q in ['Episode ', 'TBA']:
            raw_data.update({'NameStartsWith': q})
            ids += get_items(raw_data)

    if check_thumbs:
        ids = get_items(raw_data)

    logging.warning(f'Checking {len(ids)} ids!')
    with alive_bar(len(ids), bar='blocks', spinner='dots_waves2') as bar:
        db = get_db()

        async def run_with_progress(id):
            await check_episode(id, db)
            bar()

        # Optimal qsize limit might be 8
        # 3 = 59.9
        # 5 = 61.4
        # 7 = 58.5
        # 8 = 64
        # 9 = 59.5
        # 10 = 57.6

        ps = Queue()
        for id in ids:
            while ps.qsize() > limit_concurrent_requests:
                await ps.get()
            ps.put(asyncio.create_task(run_with_progress(id)))

        while not ps.empty():
            await ps.get()

    db.close()
コード例 #19
0
def add(group):
    if request.method == "POST":
        # set turn in group (for game) to highest before looping to add new members
        turn = query_db("SELECT MAX(turn) FROM groups WHERE group_name=?",
                        [group],
                        one=True)["MAX(turn)"]
        old_size = turn
        users = []
        # get usernames typed into fields[]
        for field in zip(request.form.getlist("fields[]")):
            if not field[0]:
                flash("Please select player(s)")
                return render_template("add.html", group=group)
            user = query_db("SELECT user_id FROM users WHERE name=?",
                            [field[0]],
                            one=True)
            # store users added in list to flash
            users.append(field[0])

            # check that user not duplicated in form AND group
            # if user enters inexistent username, remove all entries
            if user is None:
                get_db().execute(
                    "DELETE FROM groups WHERE group_name=? AND turn>?",
                    (group, old_size))
                get_db().commit()
                flash(field[0] + " is not registered")
                return redirect("/groups")
        # if username entered is already in group, remove all entries
        checkusers = query_db("SELECT user_id FROM groups WHERE group_name=?",
                              [group],
                              one=False)
        if checkusers:
            for check in checkusers:
                print("CHECK IS:", check["user_id"])
                if (turn >= old_size) and (user["user_id"]
                                           == check["user_id"]):
                    get_db().execute(
                        "DELETE FROM groups WHERE group_name=? AND turn>?",
                        (group, old_size))
                    get_db().commit()
                    flash(field[0] + " is already added to " + group)
                    return redirect("/groups")
        turn += 1

        print("USERS ARE: ", users)
        try:
            get_db().execute(
                "INSERT INTO groups (group_name, turn, user_id) VALUES (:group_name, :turn, :user_id)",
                {
                    "group_name": group,
                    "turn": turn,
                    "user_id": user["user_id"]
                })
            get_db().commit()

        except sqlite3.IntegrityError:
            return apology("something went wrong in DB")
        flash(", ".join(users) + " added!")
        return render_template("group.html", group=group)
    else:
        return render_template("add.html", group=group)