Esempio n. 1
0
def index():
    """
        Home Page
        :return the bands that the user can join into as JSON
    """
    try:
        s = sessionmaker(bind=engine)
        s = s()
        data = request.get_json()
        u = get_user.by_username(s, data["username"])
        users_in_band = []
        offers = []
        users_id = []

        # filter the bands
        for band in get_band.all(s):
            users_band = get_userband.users_by_band(s, band.id)
            for userband in users_band:
                users_id.append(userband.user_id)
            if u.id not in users_id and band.genre == u.genre: # user is not in band and band's genre is the user's genre
                # if user is not in that band already
                current_instruments = get_userband.users_by_band_and_instrument(s, band.id, u.instrument)
                    count_instruments = get_band_instrument.count_instrument_in_band(s, band.id, u.instrument)
                if count_instruments > 0 and current_instruments < count_instruments:
                    for userband in get_userband.users_by_band(s, band.id):
                        user = get_user.by_id(s, userband.user_id)
                        users_in_band.append(
                            {"name": user.username, "instrument": user.instrument})  # create list of users in band
                    offers.append(
                        {'name': band.name, 'genre': band.genre, 'admin': get_user.by_id(s, band.admin_id).username,
                         'users': users_in_band})  # append the offer
        print(offers)
        return {"Status": "OK", "Method": "Index", "Offers": offers}  # return the information
Esempio n. 2
0
def new_band():
    """
    :return: {"Status": 'Status', "Method": "New Band", "Band": {"name": 'name', "genre": 'genre', "instruments": lst(Instrument)}}
    """
    try:
        s = sessionmaker(bind=engine)
        s = s()
        data = request.get_json()
        print(data)
        print(data["bandInfo"])
        print(data["instruments"])

        user = get_user.by_username(s, data["bandInfo"]["adminUser"])
        band = Band(name=data["bandInfo"]["name"], genre=data["bandInfo"]["genre"], admin_id=user.id)
        create_band.create(s, band)
        userband = UserBand(user_id=user.id, band_id=band.id)
        create_userband.create(s, userband)
        for instrument in data["instruments"]:
            bi = BandInstrument(band_id=band.id, instrument=instrument, count=data["instruments"][instrument])
            create_band_instrument.create(s, bi)

        return {"Status": "OK", "Method": "New Band",
                "Band": {"name": band.name, "genre": band.genre,
                         "instruments": get_band_instrument.instruments(s, band.id)}}
    except Exception as e:
        print(e)
        return {"Status": "NOT OK", "Method": "New Band", "Detail": str(e)}
Esempio n. 3
0
def login():
    """
    :return: {"Status": 'Status', "Method": "Login",
                        "User": {"ID": id, "Username": '******', "Email": 'email', "Number": 'number',
                                 "Instrument": 'instrument', "Genre": 'genre', "Bands": lst(Band)}}
    """
    data = request.get_json()
    print(data)
    try:
        s = sessionmaker(bind=engine)
        s = s()
        u = get_user.by_username(s, data["username"])
        if u:
            u_bands = get_band.convert_userband_list(s, get_userband.bands_by_user(s, u.id))  # get all the user's bands
            if u.password == data["password"]:
                return {"Status": "OK", "Method": "Login",
                        "User": {"ID": u.id, "Username": u.username, "Email": u.email,
                                 "Number": u.phone_number,
                                 "Instrument": u.instrument,
                                 "Genre": u.genre,
                                 "Bands": u_bands}}
            else:
                return {"Status": "NOT OK", "Method": "Login", "Detail": "USERNAME AND PASSWORD ARE NOT MATCHED"}
        else:
            return {"Status": "NOT OK", "Method": "Login", "Detail": "NO SUCH USER"}
    except Exception as e:
        print(e)
        return {"Status": "NOT OK", "Method": "Login", "Detail": str(e)}
Esempio n. 4
0
def decline_joining_band():
    """
    :return: {"Status": 'Status', "Method": "Decline", "Info": {"user": '******', "band": 'bandname'}
    """
    try:
        s = sessionmaker(bind=engine)
        s = s()
        data = request.get_json()
        userband = UserBand(get_user.by_username(s, data["username"]).id, get_band.by_bandname(s, data["bandname"]).id,
                            True)
        create_userband.create(s, userband)
        return {"Status": "OK", "Method": "Decline", "Info": {"user": data["username"], "band": data["bandname"]}}
    except Exception as e:
        print(e)
        return {"Status": "NOT OK", "Method": "Decline", "Detail": str(e)}
Esempio n. 5
0
def accept_joining_band():
    """
    :return: the Username and Bandname
    """
    try:
        s = sessionmaker(bind=engine)
        s = s()
        data = request.get_json()
        user = get_user.by_username(s, data["username"])
        band = get_band.by_bandname(s, data["bandname"])
        userband = UserBand(user_id=user.id, band_id=band.id, declined=False)
        create_userband.create(s, userband)
        return {"Status": "OK", "Method": "Accept",
                "Info": {"user": get_user.to_json(user), "band": get_band.to_json(band)}}
    except Exception as e:
        print(e)
        return {"Status": "NOT OK", "Method": "Accept", "Detail": str(e)}
Esempio n. 6
0
def bands():
    """
    :return: {"Status": 'Status', "Method": "GetBands", "bands": lst(Band)}
    """
    try:
        s = sessionmaker(bind=engine)
        s = s()
        data = request.get_json()
        print(data)
        user = get_user.by_username(s, data["username"])
        print(user)
        bands = []
        for userband in get_userband.bands_by_user(s, user_id=user.id):
            bands.append(get_band.to_json(band=get_band.by_id(s, userband.band_id)))
        return {"Status": "OK", "Method": "GetBands", "bands": bands}
    except Exception as e:
        print(e)
        return {"Status": "NOT OK", "Method": "GetBands", "Detail": str(e)}
Esempio n. 7
0
def register():
    """
    :return: {"Status": 'Status', "Method": "Register", "User": {"ID": id}}
    """
    s = sessionmaker(bind=engine)
    s = s()
    data = request.get_json()
    try:
        print(data)
        u = User(username=data["username"], password=data["password"], email=data["email"],
                 phone_number=data["number"], instrument=data["instrument"], genre=data["genre"])
        create_user.create(s, u)
        print(u)
        return {"Status": "OK", "Method": "Register", "User": {"ID": u.id}}
    except Exception as e:
        delete_user.delete(s, get_user.by_username(s, data["username"]))
        print(e)
        return {"Status": "NOT OK", "Method": "Register", "Detail": str(e)}