Browse Source

Added models for examples and practices

joel
Simon Bowie 3 years ago
parent
commit
4861f6cf10
7 changed files with 122 additions and 32 deletions
  1. +5
    -0
      README.md
  2. +4
    -0
      web/app/__init__.py
  3. +85
    -0
      web/app/create.py
  4. +14
    -0
      web/app/models.py
  5. +2
    -2
      web/app/templates/base.html
  6. +12
    -4
      web/app/templates/create.html
  7. +0
    -26
      web/app/tool.py

+ 5
- 0
README.md View File

To build the database run:

`docker exec -it python python`
`from project import db, create_app, models`
`db.create_all(app=create_app())`

+ 4
- 0
web/app/__init__.py View File

from .tool import tool as tool_blueprint from .tool import tool as tool_blueprint
app.register_blueprint(tool_blueprint) app.register_blueprint(tool_blueprint)


# blueprint for create parts of app
from .create import create as create_blueprint
app.register_blueprint(create_blueprint)

return app return app

+ 85
- 0
web/app/create.py View File

# @name: create.py
# @version: 0.1
# @creation_date: 2021-10-25
# @license: The MIT License <https://opensource.org/licenses/MIT>
# @author: Simon Bowie <ad7588@coventry.ac.uk>
# @purpose: create route for creating tools, examples, and practices
# @acknowledgements:
# https://www.digitalocean.com/community/tutorials/how-to-make-a-web-application-using-flask-in-python-3

from flask import Blueprint, render_template, request, flash, redirect, url_for
from flask_login import login_required, current_user
from .models import Tool
from .models import Example
from .models import Practice
from werkzeug.exceptions import abort
from . import db

create = Blueprint('create', __name__)

# route for creating a new resource
@create.route('/create', methods=('GET', 'POST'))
@login_required
def create_resource():
if request.method == 'POST':
if request.form.get('type') == 'tool':
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('create.create'))

# create a new tool with the form data
new_tool = Tool(name=name, description=description)

# add the new tool to the database
db.session.add(new_tool)
db.session.commit()

elif request.form.get('type') == 'example':
name = request.form.get('name')
description = request.form.get('description')

if not name:
flash('Name is required!')
else:
example = Example.query.filter_by(name=name).first() # if this returns an example, then the name already exists in database

if example: # if an example is found, we want to redirect back to create page
flash('Example with same name already exists')
return redirect(url_for('create.create'))

# create a new example with the form data
new_example = Example(name=name, description=description)

# add the new example to the database
db.session.add(new_example)
db.session.commit()

elif request.form.get('type') == 'practice':
name = request.form.get('name')
description = request.form.get('description')

if not name:
flash('Name is required!')
else:
practice = Practice.query.filter_by(name=name).first() # if this returns a practice, then the name already exists in database

if practice: # if a practice is found, we want to redirect back to create page
flash('Practice with same name already exists')
return redirect(url_for('create.create'))

# create a new practice with the form data
new_practice = Practice(name=name, description=description)

# add the new practice to the database
db.session.add(new_practice)
db.session.commit()

return render_template('create.html')

+ 14
- 0
web/app/models.py View File

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)
description = db.Column(db.Text) description = db.Column(db.Text)

# table for examples
class Example(db.Model):
id = db.Column(db.Integer, primary_key=True) # primary keys are required by SQLAlchemy
created = db.Column(db.DateTime, default=datetime.utcnow)
name = db.Column(db.Text)
description = db.Column(db.Text)

# table for examples
class Practice(db.Model):
id = db.Column(db.Integer, primary_key=True) # primary keys are required by SQLAlchemy
created = db.Column(db.DateTime, default=datetime.utcnow)
name = db.Column(db.Text)
description = db.Column(db.Text)

+ 2
- 2
web/app/templates/base.html View File

</li> </li>
{% if current_user.is_authenticated %} {% if current_user.is_authenticated %}
<li class="nav-item"> <li class="nav-item">
<a href="{{ url_for('tool.create_tool') }}" class="nav-link">
Add tool
<a href="{{ url_for('create.create_resource') }}" class="nav-link">
Add resource
</a> </a>
</li> </li>
{% endif %} {% endif %}

+ 12
- 4
web/app/templates/create.html View File

{% extends "base.html" %} {% extends "base.html" %}


{% block content %} {% block content %}
<h1>{% block title %} Add a New Tool {% endblock %}</h1>
<form method="POST" action="/create">
<h1>{% block title %} Add a New Tool, Example, or Practice {% endblock %}</h1>
<form method="POST" action="/create" id="resource">
<div class="form-group">
<label for="cars">Type</label>
<select id="type" name="type" form="resource">
<option value="tool">Tool</option>
<option value="example">Example</option>
<option value="practice">Practice</option>
</select>
</div>
<div class="form-group"> <div class="form-group">
<label for="name">Name</label> <label for="name">Name</label>
<input class="form-control" type="text" name="name" placeholder="Tool name" autofocus="">
<input class="form-control" type="text" name="name" placeholder="Name" autofocus="">
</div> </div>


<div class="form-group"> <div class="form-group">
<label for="description">Description</label> <label for="description">Description</label>
<textarea class="form-control" type="text" name="description" placeholder="Tool description" autofocus=""></textarea>
<textarea class="form-control" type="text" name="description" placeholder="Description" autofocus=""></textarea>
</div> </div>


<button type="submit" class="btn btn-primary">Submit</button> <button type="submit" class="btn btn-primary">Submit</button>

+ 0
- 26
web/app/tool.py View File

db.session.commit() db.session.commit()
flash('Successfully deleted!') flash('Successfully deleted!')
return redirect(url_for('tool.get_tools')) return redirect(url_for('tool.get_tools'))

# route for creating a new tool
@tool.route('/tools/create', methods=('GET', 'POST'))
@login_required
def create_tool():
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')

Loading…
Cancel
Save