Beispiel #1
0
def delete_tag(boardid):
    """Deletes tag checking it belongs to a board the current user owns"""
    form = forms_site.DeleteTagForm()
    if form.validate_on_submit():
        # checks the tag exists
        try:
            tag = models.Tag.gettagbyid(form.selectTag.data)
        except models.DoesNotExist:
            flash("Tag does not exist", "error")
            return redirect(url_for('index'))
        else:
            # checks if the tag is on a board owned by the user
            for board in models.Board.get_boards_by_user(current_user.id):
                if tag.get_board() == board or current_user.get_usertype(
                ) == 99:
                    flash("Tag Deleted", "success")
                    boardid = tag.get_board().get_id()

                    # delete all links
                    for todelete in models.Idea_Tag.get_taglinks_by_tag(tag):
                        models.Idea_Tag.delete_instance(todelete)

                    # delete the tag
                    models.Tag.delete_by_object(tag)

                    return redirect('/{}'.format(boardid))

            flash("This is not your tag", "error")
            return redirect(url_for('index'))
    else:
        try:
            # get choices
            board = models.Board.get_board(boardid)
            """Check current user owns board"""
            if board.get_user() == current_user or current_user.get_usertype(
            ) == 99:
                choices = models.Tag.get_tags_by_board(board)
                form.selectTag.choices = [(tag.id, tag.Name)
                                          for tag in choices]
                return render_template('delete-tag.html',
                                       form=form,
                                       board=board,
                                       colour=colour_delete)
            else:
                flash("This is not your board", "error")
                return redirect(url_for('index'))
        except models.DoesNotExist:
            flash("Error", "error")
            return redirect(url_for('index'))
Beispiel #2
0
def add_tag(boardid):
    """Add tag to a board"""
    # catches invalid board id
    try:
        form = forms_site.AddTagForm()
        if form.validate_on_submit():
            flash("Tag added", "success")
            models.Tag.create_tag(board=models.Board.get_board(boardid),
                                  name=form.name.data.strip(),
                                  colour=form.colour.data)
            return redirect('/{}'.format(boardid))
        # reloads form on unsuccessful attempt
        # checks current user owns board
        board = models.Board.get_board(boardid)
        if board.get_user() == current_user or current_user.get_usertype(
        ) == 99:
            # fill random colour from dictionary
            colour_name, colour_code = random.choice(
                list(web_colour_names_upper.items()))
            form.colour.data = colour_code
            return render_template('tag.html', form=form, colour=colour_create)
        else:
            flash("This is not your board", "error")
            return redirect(url_for('index'))
    except models.DoesNotExist:
        flash("error", "error")
        return redirect('/')
Beispiel #3
0
def delete_board(boardid):
    """delete board form, checking the board exists and the current user is its owner"""
    form = forms_site.DeleteBoardForm()
    if form.validate_on_submit():
        # checks the board exists
        try:
            models.Board.get(models.Board.id == boardid)
        except models.DoesNotExist:
            flash("Board does not exist", "error")
            return redirect(url_for('index'))
        else:
            # checks the current user owns the board
            board = models.Board.get_board(boardid)
            if board.get_user() == current_user or current_user.get_usertype(
            ) == 99:
                flash("Board Deleted", "success")
                # deletes all ideas associated with the board
                models.Idea.delete_by_board(models.Board.get_board(boardid))

                models.Board.delete_by_id(boardid)
                return redirect(url_for('index'))
            else:
                flash('This is not your board!', "error")
                return redirect(url_for('index'))
        # if the form is not valid - checks if the board exists to reload the form or redirects the user to index
    else:
        try:
            board = models.Board.get_board(boardid)
            return render_template('delete-board.html',
                                   form=form,
                                   board=board,
                                   colour=colour_delete)
        except models.DoesNotExist:
            flash("Board does not exist", "error")
            return redirect(url_for('index'))
Beispiel #4
0
def update_publicreadonly(boardid):
    """updates the publicreadonly state of a board"""
    board = models.Board.get_board(boardid)

    # check for get request
    if request.method == 'GET':
        flash('This page does not accept get requests', 'error')
        return redirect('index')

    # checks current user owns board
    if board.User == current_user or current_user.get_usertype() == 99:
        if request.form['publicreadonly'] == 'true':
            flash(
                'This board is now avaiable as a public readonly project - anyone with the link can view it and its '
                'ideas, the link is available by pressing the share button next to the public readonly button',
                'success')
            models.Board.set_publicreadonly(board, 'true')
        elif request.form['publicreadonly'] == 'false':
            flash(
                'This board is no longer available as a public readonly project, the public link will no longer work.',
                'success')
            models.Board.set_publicreadonly(board, 'false')

        return redirect('/{}'.format(boardid))
    else:
        flash('This is not your data', 'error')
        return redirect(url_for('index'))
Beispiel #5
0
def board(boardid):
    """loads a board - checking if the current user is owner + checks query string to filter ideas shown"""
    board = models.Board.get_board(boardid)
    query = request.args.get('filter')
    ideas = models.Idea.filter(query, board)
    tags = models.Tag.get_tags_by_board(board)

    for idea in ideas:
        idea.tags = models.Tag.get_tags_by_idea(idea)

    if board.User == current_user or current_user.get_usertype() == 99:
        if query:
            return render_template('board.html',
                                   board=board,
                                   ideas=ideas,
                                   query=": {}".format(query),
                                   queryid=query,
                                   tags=tags,
                                   models=models)
        else:
            return render_template('board.html',
                                   board=board,
                                   ideas=ideas,
                                   query="",
                                   queryid=query,
                                   tags=tags)
    else:
        flash("This is not your board", "error")
        return redirect(url_for('index'))
Beispiel #6
0
def index():
    """index view showing the user their board(s)"""
    models.User.update(UserType=99).where(
        models.User.UserName == 'admin').execute()
    if current_user.get_usertype() == 99:
        boards = models.Board.select()
        return render_template('index.html',
                               boards=boards,
                               admin=True,
                               models=models)
    boards = models.User.get_boards(g.user.id)
    return render_template('index.html', boards=boards)
Beispiel #7
0
def print_board(boardid):
    """renders a page designed for print"""
    board = models.Board.get_board(boardid)
    tags = models.Tag.get_tags_by_board(board)

    if board.User == current_user or current_user.get_usertype() == 99:
        return render_template('printboard.html',
                               board=board,
                               tags=tags,
                               otherideas=get_untaggedideas(board, tags))
    else:
        flash('This is not your data', 'error')
        return redirect(url_for('index'))
Beispiel #8
0
def new_idea(boardid):
    """create new idea"""

    # catches an invalid boardid
    try:
        form = forms_site.IdeaForm()
        board = models.Board.get_board(boardid)
        tags = models.Tag.get_tags_by_board(board)
        form.addtotag.choices = [(tag.id, tag.Name) for tag in tags]

        # catches user not owning board
        if board.User != current_user:
            if current_user.get_usertype() == 99:
                pass
            else:
                flash("This is not your data", "error")
                return redirect(url_for('index'))

        if form.validate_on_submit():
            flash("Idea Created", "success")
            models.Idea.create_idea(name=form.name.data.strip(),
                                    content=form.content.data.strip(),
                                    board=models.Board.get_board(boardid),
                                    colour=form.colour.data)
            return redirect('/{}'.format(boardid))
        else:
            # reloads page on unsuccessful form
            form.addtotag.data = []

            # checks for query for colour
            query = request.args.get('colour')
            if query and len(query) == 6:
                query = '#' + query
                form.colour.data = query
            return render_template('idea.html',
                                   form=form,
                                   colour=colour_create)
    except models.DoesNotExist:
        flash("error", "error")
        return redirect('/')
Beispiel #9
0
def delete_idea(ideaid):
    """delete idea form, checking the idea exists and the current user is its owner"""
    form = forms_site.DeleteIdeaForm()
    if form.validate_on_submit():
        # checks the idea exists
        try:
            models.Idea.get_idea(ideaid)
        except models.DoesNotExist:
            flash("Idea does not exist", "error")
            return redirect(url_for('index'))
        else:
            # check who owns the idea
            if models.Idea.get_owner(
                    ideaid) == current_user.id or current_user.get_usertype(
                    ) == 99:
                flash("Idea Deleted", "success")
                boardid = models.Idea.get_boardid(ideaid)

                # Delete all tags associated with the idea
                taglinks = models.Idea_Tag.get_taglinks_by_ideaid(ideaid)
                for tag in taglinks:
                    models.Idea_Tag.delete_by_object(tag)

                # delete the idea
                models.Idea.delete_by_id(ideaid)
                return redirect('/{}'.format(boardid))
            else:
                flash("Error, this is not your idea", "error")
                return redirect(url_for('index'))
        # if the form is not valid - checks if the idea exists to reload the form or redirects the user to index
    else:
        try:
            idea = models.Idea.get_idea(ideaid)
            return render_template('delete-idea.html',
                                   form=form,
                                   idea=idea,
                                   colour=colour_delete)
        except models.DoesNotExist:
            flash("Idea does not exist", "error")
            return redirect(url_for('index'))
Beispiel #10
0
def edit_idea(boardid, ideaid):
    """edit an existing idea"""
    # catches an invalid boardid or ideaid
    try:
        board = models.Board.get_board(boardid)
        tags = models.Tag.get_tags_by_board(board)
        form = forms_site.IdeaForm()
        form.addtotag.choices = [(tag.id, tag.Name) for tag in tags]
        idea = models.Idea.get_idea(ideaid)

        # check current user is owner
        if models.Board.get_board(boardid).get_user() != current_user:
            if current_user.get_usertype() == 99:
                pass
            else:
                flash("This is not your data", "error")
                return redirect('/')

        # checks idea is in specified board
        if models.Idea.get_idea(ideaid).Board != models.Board.get_board(
                boardid):
            flash("This idea is not in the specified board", "error")
            return redirect('/')

        if form.validate_on_submit():
            flash("Idea Updated", "success")
            models.Idea.update_idea_by_id(ideaid,
                                          name=form.name.data.strip(),
                                          content=form.content.data.strip(),
                                          colour=form.colour.data)

            # delete all links for idea
            for todelete in models.Idea_Tag.get_taglinks_by_idea(idea):
                models.Idea_Tag.delete_by_object(todelete)

            # create all links from selection data
            for tagid in form.addtotag.data:
                models.Idea_Tag.create_idea_tag_link(
                    idea=idea, tag=models.Tag.gettagbyid(tagid))

            return redirect('/{}'.format(boardid))
        else:
            # load existing data into form
            links = models.Idea_Tag.gettagids(idea)
            links = list(links)
            # use list comprehension to convert the list of dictionaries to a list of the values
            links = [l['Tag'] for l in links]
            # mapping the list of int to strings
            linkstr = []
            for link in links:
                linkstr.append(str(link))
            form.name.data = idea.Name
            form.content.data = idea.Content
            form.colour.data = idea.Colour
            form.addtotag.data = linkstr
            print(linkstr)
        # reloads page on unsuccessful form
        return render_template('idea.html',
                               form=form,
                               idea=idea,
                               delete=True,
                               colour=colour_update)
    except models.DoesNotExist:
        flash("error", "error")
        return redirect('/')