Beispiel #1
0
def quote():

    if 'user_id' not in session:
        return render_template("add-quote.html", islogged=False)

    else:
        user = User.get_by_id(int(session['user_id']))
        username = user.username
        if request.method == 'POST':
            quote = request.form.get('quote')
            if Quote.select().where(Quote.description == quote).exists():
                # handles case that quote already exists in the system.
                return render_template(
                    "add-quote.html",
                    username=username,
                    boards=get_boards(),
                    message="the quote you entered already exists- try again.",
                    islogged='user_id' in session)
            elif len(quote) < 15:
                return render_template(
                    "add-quote.html",
                    username=username,
                    boards=get_boards(),
                    message="the quote you entered is too short- try again.",
                    islogged='user_id' in session)
            else:
                try:
                    new_quote = Quote.create(description=quote,
                                             user_id=session['user_id'])
                    new_quote.save()
                except peewee.DataError:
                    database.rollback()
                    return render_template(
                        "add-quote.html",
                        username=username,
                        boards=get_boards(),
                        message=
                        'quote is too long- try adding a shorter quote.',
                        islogged='user_id' in session)
                quote_id = Quote.select().where(
                    Quote.description == quote).get().id
                boards_matching = request.form.getlist('boards')
                for b in boards_matching:
                    newlink = QuotesBoards.create(quote_id=quote_id,
                                                  board_id=b)
                    newlink.save()
                return render_template(
                    "add-quote.html",
                    username=username,
                    boards=get_boards(),
                    message=
                    f'{username}, your quote was added to our main gallery.',
                    islogged='user_id' in session)
        else:
            return render_template("add-quote.html",
                                   username=username,
                                   boards=get_boards(),
                                   message='',
                                   islogged='user_id' in session)
Beispiel #2
0
def new_board():
    if 'user_id' not in session:
        return render_template("add-quote.html", islogged=False)
    else:
        user = User.get_by_id(int(session['user_id']))
        username = user.username
        if request.method == 'POST':
            title = request.form.get('title')
            description = request.form.get('description')
            if Board.select().where(Board.title == title).exists():
                # handles case when board with this title already exists.
                return render_template(
                    "add-board.html",
                    username=username,
                    quotes=get_quotes(),
                    message='board with this title already exists.',
                    islogged='user_id' in session)
            elif (len(title) < 3):
                return render_template(
                    "add-board.html",
                    username=username,
                    quotes=get_quotes(),
                    message=
                    'title of board must be at least 3 characters long..',
                    islogged='user_id' in session)
            else:
                new_board = Board.create(title=title, description=description)
                new_board.save()
                # adding the quotes related to the board
                board_id = Board.select().where(Board.title == title).get().id
                quotes_in_board = request.form.getlist('quotes')
                # return f'number of qoutes in board- {quotes_in_board}'
                for q in quotes_in_board:
                    newlink = QuotesBoards.create(quote_id=q,
                                                  board_id=board_id)
                    newlink.save()
                return render_template("add-board.html",
                                       username=username,
                                       quotes=get_quotes(),
                                       message='Board created Successfuly!',
                                       islogged='user_id' in session)
        else:
            return render_template("add-board.html",
                                   username=username,
                                   quotes=get_quotes(),
                                   message='',
                                   islogged='user_id' in session)
Beispiel #3
0
def my_quotes():
    if 'user_id' in session:
        my_quotes = User.select(
            peewee.fn.ARRAY_AGG(User.username).alias('username'),
            Quote.description,
            peewee.fn.ARRAY_TO_STRING(peewee.fn.ARRAY_AGG(
                Board.title), ', ').alias('title')).join(Quote).join(
                    QuotesBoards,
                    peewee.JOIN.LEFT_OUTER,
                    on=(QuotesBoards.quote_id == Quote.id)).join(
                        Board, peewee.JOIN.LEFT_OUTER).where(
                            Quote.user_id == User.get_by_id(
                                session['user_id'])).group_by(
                                    User.username, Quote.description)
        my_quotes = list(my_quotes.dicts())
        return render_template("quote_display.html",
                               quotes=my_quotes,
                               user=session['user_id'],
                               islogged='user_id' in session,
                               display='user')
    else:
        return render_template("quote_display.html", islogged=False)