def instruments_create(): form = InstrumentForm() type = form.type.data to_edit = form.to_edit.data if not is_set(type) and not is_set(to_edit): flash('Type cannot be empty!') return redirect(url_for('instruments.instruments')) if not is_set(to_edit): try: cursor.execute("INSERT INTO instruments(type) VALUES(%s::TEXT)", (type, )) except psycopg2.IntegrityError as e: flash('Such an instrument already exists!') except Exception as e: flash('Some required fields were not set!') else: try: if is_set(type): cursor.execute( 'UPDATE instruments SET type = %s::TEXT WHERE type= %s::TEXT', (type, to_edit)) except psycopg2.IntegrityError as e: flash('Such an instrument already exists!') except Exception as e: flash('Modification was not possible!') return redirect(url_for('instruments.instruments'))
def genres_delete(name): try: cursor.execute('DELETE FROM genres WHERE name = %s::TEXT', (name, )) flash('Genre deleted sucessfully!') except: flash('Some albums are of this genre!') return redirect(url_for('genres.genres'))
def award_delete(name): try: cursor.execute("DELETE FROM awards WHERE name = %s::TEXT", [name]) flash("Award deleted successfully!") except: flash("Can't delete this award!") return redirect(url_for('awards.awards'))
def awards_add(): form = AwardForm() name = form.name.data to_edit = form.to_edit.data if not is_set(name) and not is_set(to_edit): flash('Name cannot be empty!') return redirect(url_for('awards.awards')) if not is_set(to_edit): try: cursor.execute("INSERT INTO awards(name)" "VALUES(%s::TEXT)", (name, )) except psycopg2.IntegrityError as e: flash('Such a award already exists!') except Exception as e: flash(Options.fields_not_set) else: try: if is_set(name): cursor.execute( 'UPDATE awardS SET name = %s::TEXT WHERE name= %s::TEXT', (name, to_edit)) except psycopg2.IntegrityError as e: flash('Such an award already exists!') except Exception as e: flash('Modification was not possible!') return redirect(url_for('awards.awards'))
def festivals(query=None): cursor.execute( "SELECT * FROM festivals f JOIN PLACES p ON f.id_place = p.id_place ORDER BY f.date_start DESC" ) incoming = cursor.fetchall() if query: incoming = filter_by_query(incoming, query) query_form = QueryForm() form = FestivalForm() cursor.execute("SELECT * FROM places") places = cursor.fetchall() form.place.choices = BLANK_OPTION + sorted( [(place['id_place'], f'{place["city"]} / {place["name"]}') for place in places], key=lambda x: x[1]) form.to_edit.choices = Options.EMPTY + sorted( [(x['id_festival'], x['festival_name']) for x in incoming], key=lambda x: x[1]) return render_template('festivals.html', incoming=incoming, form=form, query_form=query_form)
def instruments_delete(type): try: cursor.execute('DELETE FROM instruments WHERE type = %s::TEXT', (type, )) flash('Instrument deleted sucessfully!') except: flash('Some musicians are still playing this instrument!') return redirect(url_for('instruments.instruments'))
def places_delete(id_place): try: cursor.execute('DELETE FROM places WHERE id_place = %s::INTEGER', (id_place, )) flash('Place deleted sucessfully!') except: flash('Some events happen at this place!') return redirect(url_for('places.places'))
def band_delete(name): try: cursor.execute("DELETE FROM bands WHERE name = %s::TEXT", [name]) flash("Band deleted successfully!") except Exception as e: flash( "Can't delete this band because it's not empty or is participating!" ) return redirect(url_for('bands.bands'))
def appearances_add(id_festival): form = AppearanceForm() band = form.band.data try: cursor.execute("INSERT INTO appearances (id_festival, band)" "VALUES(%s::INTEGER, %s::TEXT)", (id_festival, band)) except psycopg2.IntegrityError as e: flash('That band is already participating!') except Exception as e: flash(Options.fields_not_set) return redirect(url_for('appearances.appearances', id_festival=id_festival))
def awards(query=None): cursor.execute("SELECT * FROM awards ORDER BY name ASC") awards = cursor.fetchall() if query: awards = filter_by_query(awards, query) query_form = QueryForm() form = AwardForm() form.to_edit.choices = Options.EMPTY + sorted( [(x['name'], x['name']) for x in awards], key=lambda x: x[1]) return render_template('awards.html', awards=awards, form=form, query_form=query_form)
def songs_add(id_album): form = SongForm() name = form.name.data to_edit = form.to_edit.data if not is_set(name) and not is_set(to_edit): flash('Name cannot be empty!') return redirect(url_for('songs.songs')) position = form.position.data length = form.length.data if not is_set(to_edit): try: cursor.execute( "INSERT INTO songs(name, id_album, position)" "VALUES(%s::TEXT, %s::INTEGER, %s::INTEGER)", (name, id_album, position)) if is_set(length): cursor.execute( 'UPDATE SONGS SET length = %s::INTEGER WHERE name= %s::TEXT', (length, name)) except psycopg2.IntegrityError as e: flash('Such a song already exists!') except Exception as e: flash(Options.fields_not_set) else: try: if is_set(name): cursor.execute( 'UPDATE SONGS SET name = %s::TEXT WHERE name= %s::TEXT', (name, to_edit)) if is_set(position): cursor.execute( 'UPDATE SONGS SET position = %s::INTEGER WHERE name= %s::TEXT', (position, to_edit)) if is_set(length): cursor.execute( 'UPDATE SONGS SET length = %s::INTEGER WHERE name= %s::TEXT', (length, to_edit)) except psycopg2.IntegrityError as e: flash('Such a song already exists!') except Exception as e: flash('Modification was not possible!') return redirect(url_for('songs.songs', id_album=id_album))
def instruments(query=None): cursor.execute("SELECT * FROM INSTRUMENTS ORDER BY TYPE ASC") instruments = cursor.fetchall() if query: instruments = filter_by_query(instruments, query) query_form = QueryForm() form = InstrumentForm() form.to_edit.choices = Options.EMPTY + sorted( [(x['type'], x['type']) for x in instruments], key=lambda x: x[1]) return render_template('instruments.html', instruments=instruments, form=form, query_form=query_form)
def bands(query=None): cursor.execute("SELECT * FROM bands ORDER BY name") my_bands = cursor.fetchall() if query: my_bands = filter_by_query(my_bands, query) query_form = QueryForm() form = BandForm() form.to_edit.choices = Options.EMPTY + [(x['name'], x['name']) for x in my_bands] return render_template('bands.html', my_bands=my_bands, form=form, query_form=query_form)
def places(query=None): cursor.execute("SELECT * FROM PLACES ORDER BY CITY ASC, NAME ASC") places = cursor.fetchall() if query: places = filter_by_query(places, query) query_form = QueryForm() form = PlaceForm() form.to_edit.choices = Options.EMPTY + sorted( [(x['id_place'], f'{x["name"]}/{x["city"]}') for x in places], key=lambda x: x[1]) return render_template('places.html', places=places, form=form, query_form=query_form)
def albums_add(): form = AlbumForm() name = form.name.data to_edit = form.to_edit.data if not is_set(name) and not is_set(to_edit): flash('Name cannot be empty!') return redirect(url_for('albums.albums')) band = form.band.data genre = form.genre.data if not is_set(to_edit): try: cursor.execute( "INSERT INTO albums(band, name)" "VALUES(%s::TEXT, %s::TEXT)", (band, name)) if is_set(genre): cursor.execute( 'UPDATE albums SET genre = %s::TEXT WHERE band = %s::TEXT' 'AND name = %s::TEXT', (genre, band, name)) except psycopg2.IntegrityError as e: flash('Such an album already exists!') except Exception as e: flash(Options.fields_not_set) else: try: if is_set(name): cursor.execute( 'UPDATE albums SET name = %s::TEXT WHERE name= %s::TEXT', (name, to_edit)) if is_set(band): cursor.execute( 'UPDATE albums SET band = %s::TEXT WHERE name= %s::TEXT', (band, to_edit)) if is_set(genre): cursor.execute( 'UPDATE albums SET genre = %s::TEXT WHERE name= %s::TEXT', (genre, to_edit)) except psycopg2.IntegrityError as e: flash('Such album already exists!') except Exception as e: flash('Modification was not possible!') return redirect(url_for('albums.albums'))
def genres(query=None): cursor.execute("SELECT * FROM genres ORDER BY name ASC") genres = cursor.fetchall() if query: genres = filter_by_query(genres, query) query_form = QueryForm() form = GenreForm() form.supergenre.choices = Options.BLANK + sorted( [(x['name'], x['name']) for x in genres], key=lambda x: x[1]) form.to_edit.choices = Options.EMPTY + sorted( [(x['name'], x['name']) for x in genres], key=lambda x: x[1]) return render_template('genres.html', genres=genres, form=form, query_form=query_form)
def musicians(query=None): cursor.execute("SELECT * FROM musicians ORDER BY band, name") my_musicians = cursor.fetchall() if query: my_musicians = filter_by_query(my_musicians, query) form = MusicianForm() query_form = QueryForm() cursor.execute("SELECT * FROM bands") bands = cursor.fetchall() form.band.choices = Options.BLANK + sorted( [(band['name'], band['name']) for band in bands], key=lambda x: x[1]) cursor.execute("SELECT * FROM instruments") instruments = cursor.fetchall() form.instrument.choices = Options.BLANK + sorted( [(instrument['type'], instrument['type']) for instrument in instruments], key=lambda x: x[1]) form.to_edit.choices = Options.EMPTY + [(x['name'], x['name']) for x in my_musicians] return render_template('musicians.html', my_musicians=my_musicians, form=form, query_form=query_form)
def albums(query=None): cursor.execute("SELECT * FROM albums ORDER BY band, name") my_albums = cursor.fetchall() if query: my_albums = filter_by_query(my_albums, query) form = AlbumForm() query_form = QueryForm() cursor.execute("SELECT * FROM bands") bands = cursor.fetchall() form.band.choices = Options.BLANK + sorted( [(band['name'], band['name']) for band in bands], key=lambda x: x[1]) cursor.execute("SELECT * FROM genres") genres = cursor.fetchall() form.genre.choices = Options.BLANK + sorted([(genre['name'], genre['name']) for genre in genres], key=lambda x: x[1]) form.to_edit.choices = Options.EMPTY + sorted( [(x['name'], x['name']) for x in my_albums], key=lambda x: x[1]) return render_template('albums.html', my_albums=my_albums, form=form, query_form=query_form)
def songs(id_album): cursor.execute( "SELECT * FROM songs WHERE id_album = %s::INTEGER order by position", [id_album]) my_songs = cursor.fetchall() cursor.execute( "SELECT name, band FROM albums WHERE id_album = %s::INTEGER", [id_album]) album_info = cursor.fetchone() form = SongForm() form.to_edit.choices = Options.EMPTY + sorted( [(x['name'], x['position']) for x in my_songs], key=lambda x: x[1]) return render_template('songs.html', my_songs=my_songs, id_album=id_album, album_name=album_info['name'], band=album_info['band'], form=form)
def festivals_add(): form = FestivalForm() name = form.name.data to_edit = form.to_edit.data if not is_set(name) and not is_set(to_edit): flash('Name cannot be empty!') return redirect(url_for('festivals.festivals')) id_place = form.place.data date_start = form.date_start.data if not is_set(to_edit): try: cursor.execute( "INSERT INTO festivals(festival_name, date_start, id_place)" "VALUES (%s::TEXT, %s::DATE, %s::INTEGER)", (name, date_start, int(id_place))) except psycopg2.IntegrityError as e: start_pos = str(e).find('DETAIL') + 9 flash(str(e)[start_pos:]) else: try: if is_set(name): cursor.execute( 'UPDATE FESTIVALS SET festival_name = %s::TEXT WHERE ID_FESTIVAL = %s::INTEGER', (name, to_edit)) if is_set(id_place): cursor.execute( 'UPDATE FESTIVALS SET id_place = %s::INTEGER WHERE ID_FESTIVAL = %s::INTEGER', (int(id_place), to_edit)) if is_set(date_start): cursor.execute( 'UPDATE FESTIVALS SET date_start = %s::DATE WHERE ID_FESTIVAL = %s::INTEGER', (date_start, to_edit)) except psycopg2.IntegrityError as e: start_pos = str(e).find('DETAIL') + 9 flash(str(e)[start_pos:]) except Exception as e: flash('Modification was not possible!') return redirect(url_for('festivals.festivals'))
def places_create(): form = PlaceForm() name = form.name.data city = form.city.data to_edit = form.to_edit.data if not is_set(name) or not is_set(city) and not is_set(to_edit): flash('Name and city cannot be empty!') return redirect(url_for('places.places')) street = form.street.data if not is_set(to_edit): try: cursor.execute( "INSERT INTO places(name, city, street)" "VALUES(%s::TEXT, %s::TEXT, %s::TEXT)", (name, city, street)) except psycopg2.IntegrityError as e: flash('Such a place already exists!') except Exception as e: flash('Some required fields were not set!') else: try: if is_set(name): cursor.execute( "UPDATE PLACES SET name = %s::TEXT WHERE ID_PLACE = %s::INTEGER", (name, to_edit)) if is_set(city): cursor.execute( "UPDATE PLACES SET city = %s::TEXT WHERE ID_PLACE = %s::INTEGER", (city, to_edit)) if is_set(street): cursor.execute( "UPDATE PLACES SET street = %s::TEXT WHERE ID_PLACE = %s::INTEGER", (street, to_edit)) except psycopg2.IntegrityError as e: flash('Such a place already exists!') except Exception as e: flash('Modification was not possible!') return redirect(url_for('places.places'))
def genres_create(): form = GenreForm() name = form.name.data to_edit = form.to_edit.data if not is_set(name) and not is_set(to_edit): flash('Name cannot be empty!') return redirect(url_for('genres.genres')) supergenre = form.supergenre.data if not is_set(to_edit): try: cursor.execute("INSERT INTO genres(name)" "VALUES(%s::TEXT)", (name, )) if is_set(supergenre): cursor.execute( 'UPDATE genres SET supergenre = %s::TEXT WHERE name= %s::TEXT', (supergenre, name)) except psycopg2.IntegrityError as e: flash('Such a genre already exists!') except Exception as e: flash('Some required fields were not set!') else: try: if is_set(name): cursor.execute( 'UPDATE genres SET name = %s::TEXT WHERE name = %s::TEXT', (name, to_edit)) if is_set(supergenre): cursor.execute( 'UPDATE genres SET supergenre = %s::TEXT WHERE name = %s::TEXT', (supergenre, to_edit)) except psycopg2.IntegrityError as e: flash('Such a genre already exists!') except Exception as e: flash('Modification was not possible!') return redirect(url_for('genres.genres'))
def appearances(id_festival=None, query=None): cursor.execute("SELECT * FROM bands b JOIN appearances a ON b.name = a.band " "WHERE a.id_festival = %s::INTEGER order by b.name", [id_festival]) my_bands = cursor.fetchall() if query: my_bands = filter_by_query(my_bands, query) form = AppearanceForm() query_form = QueryForm() cursor.execute("SELECT * FROM bands") bands = cursor.fetchall() form.band.choices = Options.BLANK + sorted([(band['name'], band['name']) for band in bands], key=lambda x: x[1]) cursor.execute("SELECT festival_name FROM festivals WHERE id_festival = %s::INTEGER", [id_festival]) festival = cursor.fetchone() return render_template('appearances.html', my_bands=my_bands, query_form=query_form, form=form, id_festival=id_festival, festival_name=festival['festival_name'])
def concerts(days=None, query=None): days = days or 30 query = None if query == 'none' else query cursor.execute( "SELECT * FROM getConcertsByDate(days_number := %s::INTEGER) NATURAL JOIN PLACES " "ORDER BY concert_date DESC", (days, )) incoming = cursor.fetchall() if query: incoming = filter_by_query(incoming, query) form = ConcertForm() query_form = ConcertsQueryForm() cursor.execute("SELECT * FROM bands") bands = cursor.fetchall() form.band.choices = Options.BLANK + sorted( [(band['name'], band['name']) for band in bands], key=lambda x: x[1]) cursor.execute("SELECT * FROM places") places = cursor.fetchall() form.place.choices = Options.BLANK + sorted( [(place['id_place'], f'{place["city"]} / {place["name"]}') for place in places], key=lambda x: x[1]) # form.to_edit.choices = Options.EMPTY + sorted([(x['id_concert'], x['id_concert']) for x in incoming], key=lambda x: x[1]) form.to_edit.choices = Options.EMPTY + sorted( [(x['id_concert'], f'{x["concert_date"]} / {x["band"]} / {x["name"]} / {x["city"]}') for x in incoming], key=lambda x: x[1], reverse=True) return render_template('concerts.html', incoming=incoming, form=form, query_form=query_form)
def song_delete(id_song, id_album): cursor.execute("DELETE FROM songs WHERE id_song = %s::INTEGER", [id_song]) flash("Song deleted successfully!") return redirect(url_for('songs.songs', id_album=id_album))
def concert_delete(id_concert): cursor.execute("DELETE FROM CONCERTS WHERE id_concert = %s::INTEGER", [id_concert]) flash("Concert deleted successfully!") return redirect(url_for('concerts.concerts'))
def concerts_add(): form = ConcertForm() to_edit = form.to_edit.data band = form.band.data if not is_set(band) and not is_set(to_edit): flash('Band cannot be empty!') return redirect(url_for('concerts.concerts')) id_place = form.place.data if not is_set(id_place) and not is_set(to_edit): flash('Place cannot be empty!') return redirect(url_for('concerts.concerts')) concert_date = form.concert_date.data if not is_set(concert_date) and not is_set(to_edit): flash('Concert_date cannot be empty!') return redirect(url_for('concerts.concerts')) tour = form.tour.data if not is_set(to_edit): try: cursor.execute( "INSERT INTO concerts(band, concert_date, id_place)" "VALUES(%s::TEXT, %s::DATE, %s::INTEGER)", (band, concert_date, int(id_place))) if is_set(tour): cursor.execute( 'UPDATE CONCERTS SET tour = %s::TEXT' ' WHERE band = %s::TEXT AND concert_date = %s::DATE AND id_place = %s::INTEGER', (tour, band, concert_date, int(id_place))) except psycopg2.IntegrityError as e: flash('Such a concert already exists!') except Exception as e: flash('Some required fields were not set!') else: try: if is_set(band): cursor.execute( 'UPDATE CONCERTS SET band = %s::TEXT WHERE id_concert = %s::INTEGER', (band, to_edit)) if is_set(id_place): cursor.execute( 'UPDATE CONCERTS SET id_place = %s::INTEGER WHERE id_concert = %s::INTEGER', (int(id_place), to_edit)) if is_set(concert_date): cursor.execute( 'UPDATE CONCERTS SET concert_date = %s::DATE WHERE id_concert = %s::INTEGER', (concert_date, to_edit)) if is_set(tour): cursor.execute( 'UPDATE CONCERTS SET tour = %s::TEXT WHERE id_concert = %s::INTEGER', (tour, to_edit)) except psycopg2.IntegrityError as e: flash('Such a concert already exists!') except Exception as e: flash('Modification was not possible!') return redirect(url_for('concerts.concerts'))
def musician_delete(name): cursor.execute("DELETE FROM musicians WHERE name = %s::TEXT", [name]) flash("Musician deleted successfully!") return redirect(url_for('musicians.musicians'))
def musicians_add(): form = MusicianForm() name = form.name.data to_edit = form.to_edit.data if not is_set(name) and not is_set(to_edit): flash('Name cannot be empty!') return redirect(url_for('musicians.musicians')) band = form.band.data if not is_set(band) and not is_set(to_edit): flash('Band cannot be empty!') return redirect(url_for('musicians.musicians')) nationality = form.nationality.data instrument = form.instrument.data if not is_set(instrument) and not is_set(to_edit): flash('Instrument cannot be empty!') return redirect(url_for('musicians.musicians')) if not is_set(to_edit): try: cursor.execute( "INSERT INTO musicians(name, band, instrument)" "VALUES(%s::TEXT, %s::TEXT, %s::TEXT)", (name, band, instrument)) if is_set(nationality): cursor.execute( 'UPDATE MUSICIANS SET nationality = %s::TEXT WHERE name= %s::TEXT', (nationality, name)) except psycopg2.IntegrityError as e: flash('Such a musician already exists!') except Exception as e: flash(Options.fields_not_set) else: try: if is_set(name): cursor.execute( 'UPDATE MUSICIANS SET name = %s::TEXT WHERE name= %s::TEXT', (name, to_edit)) if is_set(band): cursor.execute( 'UPDATE MUSICIANS SET band = %s::TEXT WHERE name= %s::TEXT', (band, to_edit)) if is_set(instrument): cursor.execute( 'UPDATE MUSICIANS SET instrument = %s::TEXT WHERE name= %s::TEXT', (instrument, to_edit)) if is_set(nationality): cursor.execute( 'UPDATE MUSICIANS SET nationality = %s::TEXT WHERE name= %s::TEXT', (nationality, to_edit)) except psycopg2.IntegrityError as e: flash('Such a musician already exists!') except Exception as e: flash('Modification was not possible!') return redirect(url_for('musicians.musicians'))
def festivals_delete(id_festival): cursor.execute('DELETE FROM festivals WHERE id_festival = %s::INTEGER', [id_festival]) flash('Festival delete successfully') return redirect(url_for('festivals.festivals'))