Example #1
0
def modify_org(args):
    connect_db(get_database_url())

    try:
        with session_scope() as session:
            try:
                org = session.query(Organisation).filter_by(
                    name=args.name).one()
                is_new = True
            except sqlalchemy.orm.exc.NoResultFound:
                is_new = False
                org = Organisation(name=args.name)
                session.add(org)

            if args.region is not None:
                org.region = args.region
            if args.url is not None:
                org.url = args.url
            if args.customers is not None:
                org.number_of_customers = args.customers

            session.flush()
            session.expunge(org)

    except sqlalchemy.exc.IntegrityError as e:
        print('Failed to create organisation %s' % args.name)
        print('\n'.join(e.orig.args))
        sys.exit(1)

    if is_new:
        print('Added organisation %s' % org.name)
    else:
        print('Updated organisation %s' % org.name)
    print('ID: %s' % org.id)
Example #2
0
 def __init__(self, params):
     self.params = params
     # load configuration
     model.load_config(params.config, basepath=params.path)
     # connect to database
     model.connect_db()
     # clear db
     model.ue.UE.drop_collection()
     model.location.Location.drop_collection()
     model.accesspoint.AccessPoint.drop_collection()
     # setup zero mq receiver
     self.setup_zmq()
     # load access point definitions from configuration file
     # and from remote AP manager component.
     # Deletes all current AP definitions at first.
     # If connection to remote AP manage API is not possible,
     # the system will run without any AP object and thus
     # no APs are assigned to UEs.
     model.accesspoint.AccessPoint.refresh(
         model.CONFIG["accesspoints"], ap_manager_client.get_accesspoints())
     # load management algorithm
     plugin.load_algorithms(
         model.CONFIG["algorithm"]["available"],
         model.CONFIG["algorithm"]["default"])
     # kick of AP state fetcher thread
     self.apFetcherThread = updater.AccessPointStateFetcher()
     self.apFetcherThread.start()
     # enable "always_on_macs" on all access points
     if "always_on_macs" in model.CONFIG:
         for mac in model.CONFIG["always_on_macs"].itervalues():
             ap_manager_client.set_mac_list(
                 mac, [ap.uuid for ap in model.accesspoint.AccessPoint.objects], [])
             logging.info("Enabled MAC %s on all APs." % str(mac))
Example #3
0
    def setUp(self):
        # This runs before each of the tests in this class.
        # Requires you to set up testing client

        self.client = app.test_client()
        app.config['TESTING'] = True
        connect_db(app)
Example #4
0
 def __init__(self, params):
     self.params = params
     # load configuration
     model.load_config(params.config, basepath=params.path)
     # connect to database
     model.connect_db()
     # setup zero mq sender
     api.setup_zmq()
Example #5
0
def modify_user(args):
    connect_db(get_database_url())

    password = getpass.getpass(prompt='Enter new password: '******'Re-enter password: '******'Failed to add user %s' % args.name)
        print('Passwords do not match')
        sys.exit(1)

    try:
        with session_scope() as session:
            if args.organisation is not None:
                organisation = session.query(Organisation) \
                    .filter_by(name=args.organisation).one()
            else:
                organisation = None

            try:
                user = session.query(AppUser).filter_by(email=args.email).one()
                is_new = False
                if password != "":
                    user.password = password
            except sqlalchemy.orm.exc.NoResultFound:
                is_new = True
                user = AppUser(email=args.email)
                session.add(user)
                if password != "":
                    user.password = password
                else:
                    print(
                        "Not setting a password. User will not be able to log in."
                    )
                    user.password = "******"

            if args.name is not None:
                user.name = args.name
            if args.role is not None:
                user.role = args.role
            if organisation is not None:
                user.organisation_id = str(organisation.id)
            session.flush()
            session.expunge(user)

    except sqlalchemy.exc.IntegrityError as e:
        print('Failed to add user %s' % args.email)
        print('\n'.join(e.orig.args))
        sys.exit(1)

    if is_new:
        print('Added user %s' % user.email)
    else:
        print('Updated user %s' % user.email)
    print('ID: %s' % user.id)
Example #6
0
def connect_db():
    global SessionStaging, SessionUpstream
    engine_source = model.connect_db(
        'postgresql://*****:*****@%s:5432/postgres' %
        os.environ['SOURCE'])
    engine_target = model.connect_db(
        'postgresql://*****:*****@%s:5432/postgres' %
        os.environ['TARGET'])

    SessionStaging = sessionmaker(bind=engine_source)
    SessionUpstream = sessionmaker(bind=engine_target)
Example #7
0
def populate_items(item_list, level):

    db = model.connect_db()

    successfully_entered = 0.0
    no_file_error = 0.0

    for item_id, item_seo_title in item_list:

        try:

            model.enter_new_item(db, item_id, level)
            print item_id
            successfully_entered += 1

        except:

            print "++++++++++++++++++++++++++"
            print "couldn't find file"
            print item_id
            print item_seo_title
            print "++++++++++++++++++++++++++"

            no_file_error += 1

    print "Successfully entered: " + str(successfully_entered)
    print "Errors: " + str(no_file_error)
    print "Error rate: " + str(round(no_file_error / successfully_entered * 100, 2)) + "%"

    db.close()
Example #8
0
def new_user():
    user_name= request.form['user_name']
    email= request.form['user_email']
    db = model.connect_db()
    #new_user(db, email, password, name)
    user_id = model.new_user(db, email, None, user_name)
    return redirect("/new_task")
Example #9
0
def sets_per_item():

    db = model.connect_db()
    query = """SELECT COUNT(set_id) from Items_Sets GROUP BY item_id"""
    result = db.execute(query)

    result_list = [  row[0] for row in result.fetchall()  ]

    # Create a histogram
    histogram = [0] * 23
    print histogram

    for num in result_list:
        if num <= 22:
            histogram[num] += 1
        elif num > 22:
            histogram[22] += 1

    print histogram

    # Print out the histogram
    for bucket in range(1, len(histogram)):
        if bucket <= 21:
            print "%d items <-----> belong to %d sets" % (histogram[bucket], bucket)
        elif bucket == 22:
            print "%d items <-----> belong to 22 or more sets" % histogram[bucket]

    # Check against total # of Items_Sets records by doing SumProduct
    sum_p = 0
    for bucket in range(1, len(histogram)):
        sum_p += bucket * histogram[bucket]

    print sum_p

    db.close()
Example #10
0
def save_task():
	task_title = request.form['task_title']
	user_id = request.form['user_id']
	db = model.connect_db()
# assume all tasks are attached to user 1 :(
	task_id = model.new_task(db, task_title, user_id)
	return redirect("/tasks")
Example #11
0
def save_task():
	title = request.form['title']
	db = model.connect_db()
	#Assume that all tasks are attached to user Christian Fernandez
	#task ID below
	task_id = model.new_task(db, title, 'Christian', 'Fernandez')
	return redirect("/tasks")
def populate_sets_fans(batch):
    
    db = model.connect_db()

    for set_id, seo_title in batch:

        model.enter_new_sets_fans(db, set_id)
def list_of_unique_items(batch):

    # Make a tuple of the relevant sets to pass into the query
    relevant_sets = tuple([ b[0] for b in batch ])
    question_marks = ["?"] * len(relevant_sets)
    question_marks_string = ", ".join(question_marks)

    # Connecting to database
    db = model.connect_db()

    # Format and execute the SQL query
    query_template = """SELECT * FROM Sets_Items WHERE set_id IN (%s)"""
    query = query_template % (question_marks_string)
    result = db.execute(query, relevant_sets)
    
    # Get a list of records, where each item is a fan name
    list_of_item_ids = []
    list_of_item_seo_titles = []

    for row in result.fetchall():

        if not row[2] in list_of_item_ids:  # Check for duplicates
                                            # We need to delay zipping until the end b/c of this.

            list_of_item_ids.append(row[2])
            list_of_item_seo_titles.append(row[3])

    list_of_items = zip(list_of_item_ids, list_of_item_seo_titles)

    return list_of_items
def list_of_unique_fans(batch):

    # Make a tuple of the relevant sets to pass into the query
    relevant_sets = tuple([ b[0] for b in batch ])
    question_marks = ["?"] * len(relevant_sets)
    question_marks_string = ", ".join(question_marks)

    # Connecting to database
    db = model.connect_db()

    # Format and execute the SQL query
    query_template = """SELECT * FROM Sets_Fans WHERE set_id IN (%s)"""
    query = query_template % (question_marks_string)
    result = db.execute(query, relevant_sets)
    
    # Get a list of records, where each item is a fan name
    list_of_fans = []

    for row in result.fetchall():

        if not row[3] in list_of_fans:   # Check for duplicates

            if not row[2] == "0":          # If fan_id is 0, means they don't have an actual account

                list_of_fans.append(row[3])

    return list_of_fans
Example #15
0
def print_overlap():

    db = model.connect_db()

    # Creates a table with 2 columns -- (a) fan_name (b) num out of 5 sets in batch_1 that they fanned
    cursor = db.execute("SELECT fan_name, COUNT(fan_name) FROM Sets_Fans GROUP BY fan_name")

    # Doing the rest in Python instead of SQL for now --> need to look up how to query the result of a query
    total_fans = 0.0
    sets_rated_by_each_fan = ["Not Applicable", 0.0, 0.0, 0.0, 0.0, 0.0]

    for row in cursor.fetchall():

        number_rated = row[1]
        sets_rated_by_each_fan[number_rated] += 1

        total_fans += 1

    # Transform frequency counts into percentages

    print "LEVEL OF OVERLAP"
    print "Out of " + str(int(total_fans)) + " total fans who rated 5 'Popular' sets"
    print "--------------------------------------------------"

    for x in range(1,6):
        percentage = int(round(sets_rated_by_each_fan[x]/total_fans*100))
        print str(int(sets_rated_by_each_fan[x])) + " out of " + str(int(total_fans)) + " -- (" + str(percentage) + "%) -- rated " + str(x) + " out of 5 sets"

    db.close()
Example #16
0
def winners():
	db = model.connect_db()
	participants_from_db = model.get_participants(db)
	games_from_db = model.get_games(db)
	print participants_from_db

	entries = []

	for item in participants_from_db:
		entry = item['number_entries']
		person = item['participant_name']
		for i in range(entry):
			entries.append(person)
	random.shuffle(entries)
	
	games_list = []
	for game in games_from_db:
		games_list.append(game["game"])
	random.shuffle(games_list)

	decisions = {}
	a = 0
	while a < len(entries):
		if entries[a] in decisions:
			decisions[entries[a]].append(games_list[a])
		else:
			decisions[entries[a]] = [games_list[a]]
		a +=1
	print decisions



	return render_template("winners.html", winners = decisions)
Example #17
0
def save_participant():
	participant_name = request.form['participant_name']
	number_entries = request.form['number_entries']
	db = model.connect_db()
	participant_id = model.new_participant(db, participant_name, number_entries)
	print participant_id
	return redirect("/the_lottery")
Example #18
0
def get_session():
    # session is stored in application global
    if not hasattr(g, 'session'):
        engine = model.connect_db()
        Session = sessionmaker(bind=engine)
        g.session = Session()
    return g.session
Example #19
0
def login():
	email = request.form['email']
	password = request.form['password']
	db = model.connect_db()
	auth_user = model.authenticate(db, email, password)
	if auth_user != None:
		return redirect("/Tasks")
Example #20
0
def connect_db():
    db_url = model.get_database_url()
    model.connect_db(db_url)
    try:
        model.connect_db_ro(db_url)
    except model.MissingUser:
        # This shouldn't happen because the user is created as part of the
        # schema. However if the user is accidentally deleted - or if the
        # database is restored from backup before the app is started for the
        # first time - then this is the easiest way to get the user back.
        model.create_analyst_user()
        model.connect_db_ro(db_url)
    except model.WrongPassword:
        # If the database is restored from a backup, the analyst password might
        # be wrong.
        model.reset_analyst_password()
        model.connect_db_ro(db_url)
Example #21
0
def authenticate():
    db = model.connect_db()
    email = request.form['email']
    password = request.form['password']
    user_info = model.authenticate(db, email, password)
    if user_info == None:
        return redirect('/')
    else:
        return redirect('/tasks')
def list_tasks():
	db = model.connect_db()
	tasks_from_db = model.get_tasks(db, None)
	completed_items = request.form.getlist("completed_t_f", type=int)
	if completed_items:
		for task in completed_items:
			model.complete_task(db, task)

	return render_template("tasks.html", tasks_field = tasks_from_db)
Example #23
0
def list_tasks():
  db = model.connect_db()
  tasks_from_db = model.get_tasks(db, None)
  user_id_list = []
  for dictionary in tasks_from_db:
    user_id_list.append(dictionary["user_id"])
  for user_id in user_id_list:
    users_from_db = model.get_user(db, user_id)
  return render_template("list_tasks.html", tasks=tasks_from_db,users=users_from_db)
Example #24
0
def list_tasks():
    db = model.connect_db()
    tasks_from_db = model.get_tasks(db, None)
    completed_items = request.form.getlist("completed_t_f", type=int)
    if completed_items:
        for task in completed_items:
            model.complete_task(db, task)

    return render_template("tasks.html", tasks_field=tasks_from_db)
Example #25
0
def populate_items_sets(list_of_ids):

    db = model.connect_db()

    for item_id in list_of_ids:

        model.enter_new_items_sets(db, item_id)

    db.close()
Example #26
0
def unique_records_twocol(table, col1, col2, level):

    # Format and execute the SQL query
    db = model.connect_db()
    query_template = """SELECT DISTINCT (%s), (%s) FROM (%s) WHERE level = (%d)""" 
    query = query_template % (col1, col2, table, level)
    result = db.execute(query)

    # Get a list of records and return it
    list_of_items = [ row for row in result.fetchall() ]
    db.close()
    return list_of_items
Example #27
0
def get_list_1():

    list_of_tups = []

    db = model.connect_db()
    query = """SELECT DISTINCT set_id, set_seo_title, set_imgurl FROM Items_Sets"""
    result = db.execute(query)

    result_list = [  row for row in result.fetchall()  ]
    
    db.close()

    return result_list
Example #28
0
def list_of_unique_sets(level):

    """ Return a list of unique sets as (set_id, set_seo_title) which will be passed to create_file function """

    # Format and execute the SQL query
    db = model.connect_db()
    query = """SELECT DISTINCT set_id, set_seo_title FROM Items_Sets WHERE level = ?"""
    result = db.execute(query, (level,))

    # Get a list of records and return it
    list_of_sets = [ row for row in result.fetchall() ]
    db.close()
    return list_of_sets
Example #29
0
def save_task():
    # print request.form
    task_title = request.form['task_title']
    category = request.form["category"]
    priority = request.form["priority"]
    due_date = request.form["due_date"]

    #due_date = datetime.datetime.now()
    db = model.connect_db()
    created_at = datetime.datetime.now()
    task_id = model.new_task(db, task_title, 1, created_at, 0, category, None,
                             priority, due_date)
    # return "Success!"
    return redirect("/tasks")
def save_task():
	# print request.form
	task_title = request.form['task_title']
	category = request.form["category"]
	priority = request.form["priority"]
	due_date = request.form["due_date"]
	
	#due_date = datetime.datetime.now()
	db = model.connect_db()
	created_at = datetime.datetime.now()
	task_id = model.new_task(db, task_title, 1, 
							 created_at, 0, 
							 category, None, priority, due_date)
	# return "Success!"
	return redirect("/tasks")
Example #31
0
def get_good_items():

    db = model.connect_db()
    query = """SELECT item_id, seo_title FROM Items WHERE usd_price > 0"""
    result = db.execute(query)

    result_list = [ row for row in result.fetchall() ]

    result_urls = []

    for tup in result_list:
        url = polyvore.get_item_url(tup[0], tup[1])
        result_urls.append(url)

    db.close()
    return result_urls
Example #32
0
def whatever2():

    db = model.connect_db()

    for set_id, set_seo_title in batch_1:
        try:
            model.enter_new_set(db, set_id, 1)
        except IOError:
            print "No file exists (likely Unicode Error)"

    for set_id, set_seo_title in level_3:
        try:
            model.enter_new_set(db, set_id, 3)
        except IOError:
            print "No file exists (likely Unicode Error)"

    db.close()
Example #33
0
def enter_list_1():

    list_to_enter = get_list_1()

    db = model.connect_db()
    c = db.cursor()
    query = """INSERT INTO Sets VALUES(NULL, ?, ?, NULL, NULL, NULL, NULL, NULL, NULL, ?, NULL, NULL, NULL, NULL, NULL, 4)"""

    counter = 0

    for tup in list_to_enter:
        # print tup[0]
        c.execute(query, (tup[0], tup[1], tup[2]))
        print counter
        counter += 1
        
    db.commit()
    db.close()
Example #34
0
def list_of_unique_items(batch):

    """ Return a list of unique items as (item_id, item_seo_title) which will be passed to create_file function """

    # Make a tuple of the relevant sets to pass into the query
    relevant_sets = tuple([  b[0] for b in batch  ])
    question_marks = ["?"] * len(relevant_sets)
    question_marks_string = ", ".join(question_marks)

    # Format and execute the SQL query
    db = model.connect_db()
    query_template = """SELECT DISTINCT item_id, item_seo_title FROM Sets_Items WHERE set_id IN (%s)""" 
    query = query_template % (question_marks_string)
    result = db.execute(query, relevant_sets)

    # Get a list of records and return it
    list_of_items = [ row for row in result.fetchall() ]
    db.close()
    return list_of_items
Example #35
0
def show_setup():
	db = model.connect_db()
	participants_from_db = model.get_participants(db)
	games_from_db = model.get_games(db)

	participant_list = []
	number_entry_list = []

	for participant in participants_from_db:
	 	participant_list.append(participant["participant_name"])
		number_entry_list.append(participant["number_entries"])
	
	participants = zip(participant_list, number_entry_list)

	games_list = []
	for game in games_from_db:
		games_list.append(game["game"])

	return render_template("show_setup.html", participants=participants, games=games_list)
Example #36
0
def grab_these_relationships():

    db = model.connect_db()

    query1 = """SELECT DISTINCT user_id FROM Users WHERE level = 2.1"""
    query2 = """SELECT DISTINCT user_id FROM Users WHERE level = 2.2"""
    query3 = """SELECT DISTINCT user_id FROM Users WHERE level = 2.3"""
    query4 = """SELECT DISTINCT user_id FROM Users WHERE level = 2.4"""

    result1 = db.execute(query1)
    result2 = db.execute(query2)
    result3 = db.execute(query3)
    result4 = db.execute(query4)

    list_fans_1 = [  row[0] for row in result1.fetchall()  ]
    list_fans_2 = [  row[0] for row in result2.fetchall()  ]
    list_fans_3 = [  row[0] for row in result3.fetchall()  ]
    list_fans_4 = [  row[0] for row in result4.fetchall()  ]

    db.close()
Example #37
0
def whatever():

    db = model.connect_db()

    for i in range(0, 300):
        print list_of_fan_names[i]
        model.enter_new_user(db, list_of_fan_names[i], 2.1)

    for i in range(300, 600):
        print list_of_fan_names[i]
        model.enter_new_user(db, list_of_fan_names[i], 2.2)

    for i in range(600, 900):
        print list_of_fan_names[i]
        model.enter_new_user(db, list_of_fan_names[i], 2.3)

    for i in range(900, len(list_of_fan_names)):
        print list_of_fan_names[i]
        model.enter_new_user(db, list_of_fan_names[i], 2.4)

    db.close()
Example #38
0
import model
import telegram
from sqlalchemy.orm import sessionmaker

engine = model.connect_db()
Session = sessionmaker(bind=engine)
session = Session()


def analyse_activity(owner_id, activity_id):
    athlete = session.query(model.Athlete).get(owner_id)

    telegram.newActivity(athlete.firstname, athlete.lastname, activity_id)
Example #39
0
def set_up_db():
    g.db = model.connect_db()
import model
import datetime

db = model.connect_db()
user_id = model.new_user(db, "*****@*****.**", "securepassword", "Christian")
task = model.new_task(db, "A task for Christian", user_id, datetime.datetime.now(),
					  None, "category3", None, 4, "Tuesday")
task = model.new_task(db, "The best task OH MY FREAKING GOD", user_id, datetime.datetime.now(),
					  None, "category3", None, 5, "Next year")
user_id = model.new_user(db, "*****@*****.**", "awesomepassword", "Gulnara")
task = model.new_task(db, "let's drink", user_id, datetime.datetime.now(),
					  None, "category3", None, 1, "in an hour")
task = model.new_task(db, "go to sleep", user_id, datetime.datetime.now(),
					  None, "category4", None, 2, "in 2 hours")
user_id = model.new_user(db, "*****@*****.**", "yeahyeahyeah", "Lindsay")
task = model.new_task(db, "go hiking", user_id, datetime.datetime.now(),
					  None, "category78", None, 3, "sunday")
task = model.new_task(db, "read a few books", user_id, datetime.datetime.now(),
					  None, "category54", None, 4, "this weekend")
Example #41
0
def connect_db():
    url = model.get_database_url()
    model.connect_db(url)
Example #42
0
from flask import Flask, render_template, redirect, request
from model import db, connect_db, Pet
from forms import PetForm
app = Flask(__name__)

app.config[
    'SQLALCHEMY_DATABASE_URI'] = 'postgresql://*****:*****@localhost/adoptpets_db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = True
app.config['SECRET_KEY'] = 'adoptpets'

connect_db(app)
db.create_all()


@app.route('/')
def home_page():
    pet_all = Pet.query.all()
    return render_template("home.html", pets=pet_all)


@app.route('/add', methods=['GET', 'POST'])
def add_pets():
    form = PetForm()
    if form.validate_on_submit():
        new = Pet(name=form.name.data,
                  species=form.species.data,
                  photo_url=form.photo_url.data,
                  age=form.age.data,
                  notes=form.notes.data)
        db.session.add(new)
Example #43
0
        if not getattr(ride, attr):
            setattr(ride, attr, None)

    return ride


if __name__ == '__main__':
    # Debug for DebugToolbarExtension. Also so server restarts when changes made
    # app.debug = True

    # # Connection function from model.py
    # connect_db(app)

    # # Debug Toolbar
    # DebugToolbarExtension(app)

    # # Allows redirects
    # app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False

    # app.run(host="0.0.0.0", port=5000)
    # app.run(debug=True, host="0.0.0.0")

    connect_db(app, os.environ.get("DATABASE_URL"))

    db.create_all(app=app)

    DEBUG = "NO_DEBUG" not in os.environ

    PORT = int(os.environ.get("PORT", 5000))

    app.run(host="0.0.0.0", port=PORT, debug=DEBUG)
Example #44
0
def before_request():
    g.db = model.connect_db()
Example #45
0
def set_up_db():
	g.db = model.connect_db() # global variable, db
Example #46
0
 def setUpClass(cls):
     url = model.get_database_url()
     cls._engine = model.connect_db(url)
     cls.destroy_schema()
     model.drop_analyst_user()
     model.Base.metadata.create_all(cls._engine)