Ejemplo n.º 1
0
def new_record():
    new_artist = request.form.get('artist')
    new_title = request.form.get('title')
    # sprawdzanie czy ktoś zostawił puste pole
    if new_artist == '' or new_title == '':
        flash("Wypełnij pola!", 'danger')
        records = Record.query.all()
        return render_template('record-list.html', records=records)

    new_record = Record(title=new_title, artist=new_artist)
    api_check = new_record.get_additional()
    # sprawdzanie czy dany artysta/płyta są w zewnętrznym API
    if 'error' in api_check:
        flash("Nie znaleziono takiej pozycji. Podaj prawidłowe dane!",
              'warning')
        records = Record.query.all()
        return render_template('record-list.html', records=records)

    # sprawdzanie czy juz istnieje w naszej bazie
    record_check = Record.query.filter_by(title=new_title,
                                          artist=new_artist).first()
    if record_check is not None:
        flash("Dana pozycja już istnieje w bazie", 'warning')
        records = Record.query.all()
        return render_template('record-list.html', records=records)

    #pobieranie pozostałych danych
    genres = api_check['genres']
    styles = api_check['styles']
    country = api_check['country']
    year = api_check['year']

    genresString = ''
    if (genres is not None):
        for genre in genres:
            genresString += str(genre) + ';'

    stylesString = ''
    if (styles is not None):
        for style in styles:
            stylesString += str(style) + ';'

    new_record.genres = genresString
    new_record.styles = stylesString
    new_record.country = country
    new_record.year = year

    db.session.add(new_record)
    db.session.commit()
    return redirect('/records/')