| from .main import main as main_blueprint | from .main import main as main_blueprint | ||||
| app.register_blueprint(main_blueprint) | app.register_blueprint(main_blueprint) | ||||
| # blueprint for tools parts of app | |||||
| from .tool import tool as tool_blueprint | |||||
| app.register_blueprint(tool_blueprint) | |||||
| return app | return app |
| password = db.Column(db.String(100)) | password = db.Column(db.String(100)) | ||||
| name = db.Column(db.String(1000)) | name = db.Column(db.String(1000)) | ||||
| class Tools(db.Model): | |||||
| class Tool(db.Model): | |||||
| id = db.Column(db.Integer, primary_key=True) # primary keys are required by SQLAlchemy | id = db.Column(db.Integer, primary_key=True) # primary keys are required by SQLAlchemy | ||||
| created = db.Column(db.DateTime, default=datetime.utcnow) | created = db.Column(db.DateTime, default=datetime.utcnow) | ||||
| name = db.Column(db.Text) | name = db.Column(db.Text) |
| <meta charset="utf-8"> | <meta charset="utf-8"> | ||||
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> | <meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||||
| <meta name="viewport" content="width=device-width, initial-scale=1"> | <meta name="viewport" content="width=device-width, initial-scale=1"> | ||||
| <title>Flask Auth Example</title> | |||||
| <title>COPIM online toolkit</title> | |||||
| <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.2/css/bulma.min.css" /> | <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.2/css/bulma.min.css" /> | ||||
| </head> | </head> | ||||
| Home | Home | ||||
| </a> | </a> | ||||
| {% if current_user.is_authenticated %} | {% if current_user.is_authenticated %} | ||||
| <a href="{{ url_for('tool.create') }}" class="navbar-item"> | |||||
| Add tool | |||||
| </a> | |||||
| {% endif %} | |||||
| {% if current_user.is_authenticated %} | |||||
| <a href="{{ url_for('main.profile') }}" class="navbar-item"> | <a href="{{ url_for('main.profile') }}" class="navbar-item"> | ||||
| Profile | Profile | ||||
| </a> | </a> |
| {% extends "base.html" %} | |||||
| {% block content %} | |||||
| <div class="column is-4 is-offset-4"> | |||||
| <h3 class="title">Add a new tool</h3> | |||||
| <div class="box"> | |||||
| {% with messages = get_flashed_messages() %} | |||||
| {% if messages %} | |||||
| <div class="notification is-danger"> | |||||
| {{ messages[0] }} | |||||
| </div> | |||||
| {% endif %} | |||||
| {% endwith %} | |||||
| <form method="POST" action="/create"> | |||||
| <div class="field"> | |||||
| <div class="control"> | |||||
| <input class="input is-large" type="text" name="name" placeholder="Tool name" autofocus=""> | |||||
| </div> | |||||
| </div> | |||||
| <div class="field"> | |||||
| <div class="control"> | |||||
| <textarea class="input is-large" type="text" name="description" placeholder="Tool description" autofocus=""></textarea> | |||||
| </div> | |||||
| </div> | |||||
| <button class="button is-block is-info is-large is-fullwidth">Submit</button> | |||||
| </form> | |||||
| </div> | |||||
| </div> | |||||
| {% endblock %} |
| {% extends "base.html" %} | |||||
| {% block content %} | |||||
| <h1 class="title"> | |||||
| Welcome, {{ name }}! | |||||
| </h1> | |||||
| {% endblock %} |
| from flask import Blueprint, render_template, request, flash | |||||
| from flask_login import login_required, current_user | |||||
| from .models import Tool | |||||
| from . import db | |||||
| tool = Blueprint('tool', __name__) | |||||
| @tool.route('/create', methods=('GET', 'POST')) | |||||
| @login_required | |||||
| def create(): | |||||
| if request.method == 'POST': | |||||
| name = request.form.get('name') | |||||
| description = request.form.get('description') | |||||
| if not name: | |||||
| flash('Name is required!') | |||||
| else: | |||||
| tool = Tool.query.filter_by(name=name).first() # if this returns a tool, then the name already exists in database | |||||
| if tool: # if a tool is found, we want to redirect back to create page | |||||
| flash('Tool with same name already exists') | |||||
| return redirect(url_for('tool.create')) | |||||
| # create a new tool with the form data | |||||
| new_tool = Tool(name=name, description=description) | |||||
| # add the new user to the database | |||||
| db.session.add(new_tool) | |||||
| db.session.commit() | |||||
| return render_template('create.html') |