소스 검색

Added practices and examples routes

joel
Simon Bowie 3 년 전
부모
커밋
c34e86e1eb
6개의 변경된 파일232개의 추가작업 그리고 0개의 파일을 삭제
  1. +8
    -0
      web/app/__init__.py
  2. +67
    -0
      web/app/example.py
  3. +67
    -0
      web/app/practice.py
  4. +10
    -0
      web/app/templates/base.html
  5. +40
    -0
      web/app/templates/examples.html
  6. +40
    -0
      web/app/templates/practices.html

+ 8
- 0
web/app/__init__.py 파일 보기

@@ -56,6 +56,14 @@ def create_app():
from .tool import tool as tool_blueprint
app.register_blueprint(tool_blueprint)

# blueprint for example parts of app
from .example import example as example_blueprint
app.register_blueprint(example_blueprint)

# blueprint for practice parts of app
from .practice import practice as practice_blueprint
app.register_blueprint(practice_blueprint)

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

+ 67
- 0
web/app/example.py 파일 보기

@@ -0,0 +1,67 @@
# @name: example.py
# @version: 0.1
# @creation_date: 2021-11-03
# @license: The MIT License <https://opensource.org/licenses/MIT>
# @author: Simon Bowie <ad7588@coventry.ac.uk>
# @purpose: example route for example-related functions and pages
# @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 Example
from werkzeug.exceptions import abort
from . import db

example = Blueprint('example', __name__)

# function to retrieve data about a single example from the database
def get_example(example_id):
example = Example.query.filter_by(id=example_id).first()
if example is None:
abort(404)
return example

# route for displaying all examples in database
@example.route('/examples')
def get_examples():
examples = Example.query
return render_template('examples.html', examples=examples)

# route for displaying a single example based on the ID in the database
@example.route('/examples/<int:example_id>')
def show_example(example_id):
example = get_example(example_id)
return render_template('example.html', example=example)

# route for editing a single example based on the ID in the database
@example.route('/examples/<int:example_id>/edit', methods=('GET', 'POST'))
@login_required
def edit_example(example_id):
example = get_example(example_id)

if request.method == 'POST':
name = request.form['name']
description = request.form['description']

if not name:
flash('Name is required!')
else:
example = Example.query.get(example_id)
example.name = name
example.description = description
db.session.commit()
return redirect(url_for('example.get_examples'))

return render_template('edit.html', example=example)

# route for function to delete a single example from the edit page
@example.route('/examples/<int:example_id>/delete', methods=('POST',))
@login_required
def delete_example(example_id):
example = get_example(example_id)
deletion = Example.query.get(example_id)
db.session.delete(deletion)
db.session.commit()
flash('Successfully deleted!')
return redirect(url_for('example.get_examples'))

+ 67
- 0
web/app/practice.py 파일 보기

@@ -0,0 +1,67 @@
# @name: practice.py
# @version: 0.1
# @creation_date: 2021-11-03
# @license: The MIT License <https://opensource.org/licenses/MIT>
# @author: Simon Bowie <ad7588@coventry.ac.uk>
# @purpose: practice route for practice-related functions and pages
# @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 Practice
from werkzeug.exceptions import abort
from . import db

practice = Blueprint('practice', __name__)

# function to retrieve data about a single practice from the database
def get_practice(practice_id):
practice = Practice.query.filter_by(id=practice_id).first()
if practice is None:
abort(404)
return practice

# route for displaying all practices in database
@practice.route('/practices')
def get_practices():
practices = Practice.query
return render_template('practices.html', practices=practices)

# route for displaying a single practice based on the ID in the database
@practice.route('/practices/<int:practice_id>')
def show_practice(practice_id):
practice = get_practice(practice_id)
return render_template('practice.html', practice=practice)

# route for editing a single practice based on the ID in the database
@practice.route('/practices/<int:practice_id>/edit', methods=('GET', 'POST'))
@login_required
def edit_practice(practice_id):
practice = get_practice(practice_id)

if request.method == 'POST':
name = request.form['name']
description = request.form['description']

if not name:
flash('Name is required!')
else:
practice = Practice.query.get(practice_id)
practice.name = name
practice.description = description
db.session.commit()
return redirect(url_for('practice.get_practices'))

return render_template('edit.html', practice=practice)

# route for function to delete a single practice from the edit page
@practice.route('/practices/<int:practice_id>/delete', methods=('POST',))
@login_required
def delete_practice(practice_id):
practice = get_practice(practice_id)
deletion = Practice.query.get(practice_id)
db.session.delete(deletion)
db.session.commit()
flash('Successfully deleted!')
return redirect(url_for('practice.get_practices'))

+ 10
- 0
web/app/templates/base.html 파일 보기

@@ -49,6 +49,16 @@
Tools
</a>
</li>
<li class="nav-item">
<a href="{{ url_for('example.get_examples') }}" class="nav-link">
Examples
</a>
</li>
<li class="nav-item">
<a href="{{ url_for('practice.get_practices') }}" class="nav-link">
Practices
</a>
</li>
{% if current_user.is_authenticated %}
<li class="nav-item">
<a href="{{ url_for('create.create_resource') }}" class="nav-link">

+ 40
- 0
web/app/templates/examples.html 파일 보기

@@ -0,0 +1,40 @@
{% extends 'base.html' %}

{% block content %}
<div class="row">
<div class="col-12 text-center">
<h1>{% block title %} Examples {% endblock %}</h1>
</div>
</div>
<div class="row">
<div class="col-12 text-center">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque eget viverra magna. Nam in ante ultricies purus feugiat vestibulum et ac erat. Donec in sagittis ante. Maecenas non mauris et eros commodo fringilla. Integer accumsan ullamcorper diam, non rhoncus tellus molestie ut. Maecenas finibus pretium dolor ac sagittis.
</p>
</div>
</div>
<div class="row">
{% for example in examples %}
<div class="col-md-4 col-sm-6 py-3">
<div class="card">
<div class="card-header">
<a href="{{ url_for('example.show_example', example_id=example['id']) }}">
<h2 class="card-title">{{ example['name'] }}</h2>
</a>
</div>
<div class="card-body">
<p class="card-text">
{{ example['description']|truncate(100) }}
</p>
<span class="badge bg-secondary">{{ example['created'].strftime("%Y-%m-%d %H:%M") }} UTC</span>
{% if current_user.is_authenticated %}
<a href="{{ url_for('example.edit_example', example_id=example['id']) }}">
<span class="badge bg-warning">Edit</span>
</a>
{% endif %}
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock %}

+ 40
- 0
web/app/templates/practices.html 파일 보기

@@ -0,0 +1,40 @@
{% extends 'base.html' %}

{% block content %}
<div class="row">
<div class="col-12 text-center">
<h1>{% block title %} Practices {% endblock %}</h1>
</div>
</div>
<div class="row">
<div class="col-12 text-center">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque eget viverra magna. Nam in ante ultricies purus feugiat vestibulum et ac erat. Donec in sagittis ante. Maecenas non mauris et eros commodo fringilla. Integer accumsan ullamcorper diam, non rhoncus tellus molestie ut. Maecenas finibus pretium dolor ac sagittis.
</p>
</div>
</div>
<div class="row">
{% for practice in practices %}
<div class="col-md-4 col-sm-6 py-3">
<div class="card">
<div class="card-header">
<a href="{{ url_for('practice.show_practice', practice_id=practice['id']) }}">
<h2 class="card-title">{{ practice['name'] }}</h2>
</a>
</div>
<div class="card-body">
<p class="card-text">
{{ practice['description']|truncate(100) }}
</p>
<span class="badge bg-secondary">{{ practice['created'].strftime("%Y-%m-%d %H:%M") }} UTC</span>
{% if current_user.is_authenticated %}
<a href="{{ url_for('practice.edit_practice', practice_id=practice['id']) }}">
<span class="badge bg-warning">Edit</span>
</a>
{% endif %}
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock %}

Loading…
취소
저장