def get_suggestions(self):
        terms = self.request.GET.get('term')

        terms = terms.split("-")
        terms = [el.strip(" '\"-") for el in terms]
        all_songs = []
        if len(terms) == 1:
            all_songs = db.query(Song).filter(
                or_(Song.title.ilike("%" + terms[0] + "%"),
                    Song.artist_title.ilike("%" + terms[0] +
                                            "%"))).limit(5).all()

        if not all_songs and len(terms) == 2:
            print "first"
            all_songs = db.query(Song).filter(
                and_(Song.title.ilike("%" + terms[0] + "%"),
                     Song.artist_title.ilike("%" + terms[1] +
                                             "%"))).limit(5).all()

        if not all_songs and len(terms) == 2:
            print "sec"
            all_songs = db.query(Song).filter(
                and_(Song.title.ilike("%" + terms[1] + "%"),
                     Song.artist_title.ilike("%" + terms[0] +
                                             "%"))).limit(5).all()

        titles = [song.title + " - " + song.artist_title for song in all_songs]
        print json.dumps(titles)

        return {'suggestions': json.dumps(titles)}
 def reset_vehicle(index, driver_id):
     _sql = """
               UPDATE vehicles
               SET vehicles.driver_id = NULL
               WHERE vehicles.id = (
                 SELECT users.vehicle_id_""" + str(index) +"""
                 FROM users
                 WHERE users.id = %(id)s );
             """
     db.query(_sql, params={"id" : driver_id}, fetch=False)
Exemple #3
0
def stripe_charge(cid, charge_amount, charge_descripton):
    insufficient_message = 'Your transaction was declined due to insufficient. Please try a different card.'
    issuer_not_available = 'Please wait a few minutes and try this order again. If you still have trouble with your transaction' \
                           ', please try a different card or calling your bank.'
    generic_decline = 'Please try a different card or call your bank to find out why out why this transaction was declined.'

    customer_info = db.query(Customer).filter({"id": cid})
    if customer_info:
        if customer_info.stripe_customer_id:
            try:
                charge = stripe.Charge.create(
                    customer=customer_info.stripe_customer_id,
                    amount=charge_amount,
                    currency='usd',
                    description=charge_descripton)
                if charge.outcome.network_status == "approved_by_network":
                    return True, charge.receipt_number
                elif charge.outcome.network_status == 'issuer_not_available':
                    return False, issuer_not_available
                elif charge.outcome.network_status == "insufficient_funds":
                    return False, insufficient_message
                else:
                    return False,
            except stripe.CardError as e:
                return False, e
        else:
            e = "User Does Not Have Payment Method on File"
            return False, e
    else:
        e = "Customer not found"
        return False, e
Exemple #4
0
    def test_predict(self):
        with self.app.app_context():
            offer = db.query(Offer).first()
            tags = Classifier().predict_category(offer)
            print(offer.title, offer.url)
            print(tags)

        assert True
def set_vehicle(driver_id:int):

    def reset_vehicle(index, driver_id):
        _sql = """
                  UPDATE vehicles
                  SET vehicles.driver_id = NULL
                  WHERE vehicles.id = (
                    SELECT users.vehicle_id_""" + str(index) +"""
                    FROM users
                    WHERE users.id = %(id)s );
                """
        db.query(_sql, params={"id" : driver_id}, fetch=False)


    company_id = session["user"]["company_id"]
    if not db.is_existing(table="users",conditions={"id":driver_id, "type":"driver", "company_id": company_id}):
        return jsonify(info="Driver not found"), 404

    vehicles = request.get_json(force=True)
    vehicles = Driver.parse(vehicles, "set_vehicle")

    if "errors" in vehicles:
        return jsonify(errors=vehicles["errors"]),400

    vehicles = vehicles["vehicles"]

    for k,v in vehicles.items():

        if v is not None:

            if not db.is_existing(table="vehicles", conditions={"company_id": company_id, "id":v}):
                return jsonify(info="Vehicle " + str(k) + " not found"), 404

            _sql = """ SELECT 1 FROM vehicles WHERE company_id=%(company_id)s AND id= %(v_id)s AND (driver_id IS NULL OR driver_id=%(id)s); """

            # if wished vehicle is available
            if db.query(_sql,params={"company_id" : company_id, "id": driver_id, "v_id" : v}, multiple=False) is not None:

                # reset old vehicle
                reset_vehicle(k[1], driver_id)

                # update vehicle
                db.update(table="vehicles", params={"driver_id": driver_id}, conditions={"id":v})

            else :
                return jsonify(info="Vehicle " + str(k) + " already taken"), 400

        else :
            # reset old vehicle
            reset_vehicle(k[1], driver_id)

        # update user
        db.update(table="users", params={"vehicle_id_"+k[1]: v}, conditions={"id" : driver_id})

    return jsonify(info="Vehicles set successfully"), 200
Exemple #6
0
def create_stripe_customer(cid, email, stripe_token):
    """

    :rtype : object
    """
    customer_info = db.query(Customer).filter({"id": cid})
    user = Customer.query.filter_by(id=cid).first()
    if user:
        stripe_customer = stripe.Customer.create(email=email,
                                                 source=stripe_token)
        if stripe_customer.id:
            return stripe_customer.id
        else:
            return None
    else:
        return None
Exemple #7
0
def get_total_suggestions(all_intersections, required_suggestions, required_unique_bands):
    total_suggestions = 0
    already_visited = []
    for chunk in all_intersections:
        for band in chunk:
            if total_suggestions > required_suggestions and len(already_visited) >= required_unique_bands:
                return already_visited, total_suggestions
            if band.get_title() not in already_visited:
                ss = time.time()
                # print db.query(Artist).filter(Artist.id == band.id).first().similar_artists.count()
                total_suggestions += db.query(Artist).filter(Artist.id == band.id).first().similar_artists.count()
                ee = time.time()
                print "simi arti", ee - ss
                already_visited.append(band.get_title())

    return already_visited, total_suggestions
    def get_preview_urls(self, song_ids):
        token = self.request.session.get('spotify_token')
        code_payload = {
            "grant_type": "client_credentials",
        }

        encoded_code_payload = urllib.urlencode(code_payload)
        base64encoded = base64.b64encode("{}:{}".format(
            CLIENT_ID_SPOTIFY, CLIENT_SECRETS_SPOTIFY))
        spotify_auth_headers = {
            "Authorization": "Basic {}".format(base64encoded)
        }

        if not token:
            self.request.session['oauth_redirect'] = 'get_preview_url'

            post_request = urllib2.Request(SPOTIFY_TOKEN_URL,
                                           data=encoded_code_payload,
                                           headers=spotify_auth_headers)
            response = urllib2.urlopen(post_request)
            response_data = json.loads(response.read())

            self.request.session["spotify_token"] = response_data[
                "access_token"]
        print "len before exe", len(song_ids)

        for song_id in song_ids:
            song = db.query(Song).filter(Song.id == song_id).first()
            if song.preview_url:
                print "already have preview url and will be skipping"
                continue

            for query_type in [' - ', 'by', '-']:
                song_title = song.get_full_title().replace(" - ", query_type)
                search_results = self.make_search_query(song_title)

                tracks = search_results.get('tracks')
                items = tracks.get('items')
                if len(items) == 0:
                    continue

                if items[0].get('preview_url'):
                    song.preview_url = items[0].get('preview_url')
                    db.commit()
                    break
def get_all_drivers(company_id:int, return_obj=False, vehicles=False):
    _sql = """
            select users.id,
            users.name,
            users.location_lat,
            users.location_lng,
            users.vehicle_id_1,
            users.vehicle_id_2,
         v1.max_area AS v1_max_area,
         v1.max_weight AS v1_max_weight,
         v1.area AS v1_area,
         v1.weight AS v1_weight,
         v2.max_area AS v2_max_area,
         v2.max_weight AS v2_max_weight,
         v2.area AS v2_area,
         v2.weight AS v2_weight
         from users
        INNER JOIN vehicles AS v1
        ON users.vehicle_id_1 = v1.id
        LEFT JOIN vehicles AS v2
        ON users.vehicle_id_2 = v2.id
        WHERE users.type='driver' and users.company_id = %(company_id)s ;
        """

    if vehicles is True:
        drivers_raw = db.query(_sql, {"company_id" : company_id}, multiple=True)
    else :
        drivers_raw= db.select(table="users", conditions={"type":"driver", "company_id": company_id}, multiple=True)

    drivers = []
    if return_obj:
        for driver in drivers_raw:
            drivers.append(Driver(driver))

    else :
        for driver in drivers_raw:
            d = {"driver": Driver(driver).__dict__}
            drivers.append(d)

    return drivers
def get_all_deliveries(company_id: int, conditions: dict, return_obj=False, get_locations=False, driver_id: int = None):
    conditions = Delivery.parse(conditions, "getAll")
    if "errors" in conditions:
        return conditions

    cond = ""
    conditions = conditions["conditions"] if "conditions" in conditions else conditions

    if "start" in conditions and "end" in conditions:
        cond = (
            "(deliveries.date_due BETWEEN '"
            + conditions["start"].strftime("%Y-%m-%d %H:%M:%S")
            + "' AND '"
            + conditions["end"].strftime("%Y-%m-%d %H:%M:%S")
            + "') "
        )

    if "customer_id" in conditions:
        if cond != "":
            cond += " AND "
        cond += "deliveries.customer_id = " + str(conditions["customer_id"])

    if "state" in conditions:
        if cond != "":
            cond += " AND "
        cond += "deliveries.state='" + str(conditions["state"]) + "'"

    if cond != "":
        cond += " AND "

        # Driver session
    cond += "deliveries.company_id=" + str(company_id) + " "

    if driver_id is not None:
        cond += "AND deliveries.driver_id=" + str(driver_id) + " "

    query = """ SELECT deliveries.* , delivery_orders.num_order, customers.name AS customer_name"""

    extra_inner = ""
    if get_locations is True:
        query += """ , senders.location_lng as sender_lng,
                  senders.location_lat as sender_lat,
                  receivers.location_lng as receiver_lng,
                  receivers.location_lat as receiver_lat """
        extra_inner = """
          INNER JOIN customers as senders
          ON deliveries.sender_id=senders.id
          INNER JOIN customers as receivers
          ON deliveries.receiver_id=receivers.id
      """

    query += (
        """ FROM deliveries
      INNER JOIN customers
      ON deliveries.customer_id=customers.id
      INNER JOIN delivery_orders
      ON deliveries.id=delivery_orders.delivery_id """
        + extra_inner
        + """
      WHERE """
        + cond
        + " ;"
    )

    deliveries_raw = db.query(query)

    deliveries = []

    if return_obj:
        for delivery in deliveries_raw:
            deliveries.append(Delivery(delivery))

    else:
        for delivery in deliveries_raw:
            d = {"delivery": Delivery(delivery).to_dict()}
            deliveries.append(d)

    return deliveries
 def get_valid_delivery(delivery_id, company_id):
     sql = "SELECT date_due FROM deliveries WHERE id=%(id)s AND company_id=%(company_id)s AND (state='not taken' OR state='not assigned');"
     return db.query(sql, params={"id": delivery_id, "company_id": company_id}, multiple=False)
Exemple #12
0
import logging
from geoip import geolite2
import requests
from sqlalchemy import func
from application import db
from application.models import User, Student, Mentor, Course, UserCourse
from .config import MOODLE_API_URL

logging.basicConfig(filename='cron.log',
                    filemode='w',
                    format='%(asctime)s - %(message)s',
                    level=logging.DEBUG)

logging.info('1. Getting max created_at from the DB')
# Getting the max created_at in the DB
max_date = db.query(func.max(User.created_at)).scalar()
logging.info('1. Successfully extracted max created_at from the DB')

# Getting all users from Jon's endpoint which have been updated since the last user
users = requests.get(f'{MOODLE_API_URL}/api/users?updated={max_date}').json()

# Gets all user_ids from the D
current_users = db.query(User.id).all()

# Gets all courses in the DB
courses = db.query(Course.id).all()

# Inserts on confilct upserts a new user object for each user returned in the API
for id, data in users.items():
    # Creates a new user object
    new_user = User(
Exemple #13
0
def get_suggestions(required_suggestions, required_unique_bands, playlist):
    chunks = chunkify_playlist(playlist, required_unique_bands)
    order = 2
    possible_suggestions = 0
    nu_unique_bands = 0
    all_intersections = []
    start = time.time()
    while possible_suggestions < required_suggestions or nu_unique_bands < required_unique_bands:
        all_intersections = []
        for chunk in chunks:
            s = time.time()

            ss =time.time()
            neighbours = []
            neighbour_rankings = {}
            for artist in chunk:
                source = db.query(Artist).filter(Artist.title == artist).first()
                if source and source.similar_artists.all():
                    sss = time.time()
                    n_similar_artists, rankings = source.get_n_order_similar_artists(order)
                    eee = time.time()
                    print "Chunk--> n order function:", eee-sss
                    neighbours.append(n_similar_artists)
                    neighbour_rankings[source.get_title()] = rankings
            ee=time.time()
            print "Chunk-> n order: ", ee-ss
            if not neighbours:
                continue
            ss=time.time()
            intersection = get_n_list_intersection(neighbours)
            ee=time.time()
            print "Chunk-> intersection: ", ee-ss
            if not intersection:
                continue

            scores = []
            ss=time.time()
            for prospect_suggestion in intersection:
                scores.append(calculate_score(prospect_suggestion, neighbour_rankings))

            scores, ranked_intersection = zip(*sorted(zip(scores, intersection)))
            ranked_intersection = list(ranked_intersection)
            all_intersections.append(ranked_intersection)
            ee=time.time()
            print "Chunk-> scoring: ", ee-ss
            e = time.time()
            print "Chunk time", e - s
        ss = time.time()
        unique_bands, possible_suggestions = get_total_suggestions(all_intersections, required_suggestions,
                                                                   required_unique_bands)
        ee = time.time()
        print "get total sugg time: ", ee - ss

        nu_unique_bands = len(unique_bands)
        order += 1

    end = time.time()
    print "Stage 1: ", (end - start)

    suggestions = []
    possible_suggestions = 0
    nu_bands_included = 0
    bands = []
    start = time.time()
    while nu_bands_included < required_unique_bands or possible_suggestions < required_suggestions:
        for chunk in all_intersections:
            if chunk:
                prospective = chunk.pop(0)
                if prospective not in bands:
                    bands.append(prospective)

        nu_bands_included = len(bands)
        for band in bands:
            if not band.get_songs():
                nu_bands_included -= 1
                bands.remove(band)

        possible_suggestions = get_number_of_songs_from_list_artists(bands)
    end = time.time()
    print "Stage 2: ", (end - start)
    start = time.time()
    all_songs = []
    all_songs_artists = []
    for artist in bands:
        all_songs.append(artist.get_songs())
        all_songs_artists.append(artist)
    i = 0
    while len(suggestions) < required_suggestions:
        for artist_songs, artist in zip(all_songs, all_songs_artists):
            if i < len(artist_songs):
                if artist_songs and (artist_songs[i], artist) not in suggestions:
                    suggestions.append((artist_songs[i], artist))
            else:
                continue
        i += 1

    end = time.time()
    print "Stage 3: ", (end - start)

    return suggestions