import from kontor-flask branch develop/0.1.0
This commit is contained in:
@@ -1,3 +1,9 @@
|
||||
"""
|
||||
Define routing rules for comic related information
|
||||
"""
|
||||
from flask import Blueprint
|
||||
|
||||
comics_bp = Blueprint('comics_bp', __name__,
|
||||
template_folder='templates',
|
||||
static_folder='static', static_url_path='assets')
|
||||
comics_api = Blueprint('comics_api', __name__)
|
||||
|
||||
from kontor.comics import routes
|
||||
from kontor.comics import api
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
from flask import Blueprint, render_template, jsonify
|
||||
|
||||
from kontor.comics import comics_api
|
||||
from kontor.models import Comic, comics_schema, Publisher, comic_schema, publisher_schema, publishers_schema
|
||||
|
||||
|
||||
@comics_api.route('/')
|
||||
def index():
|
||||
comics = Comic.query.all()
|
||||
return comics_schema.dump(comics)
|
||||
|
||||
|
||||
@comics_api.route('/comic/<string:comic_id>')
|
||||
def view(comic_id):
|
||||
comic = Comic.query.get(comic_id)
|
||||
return comic_schema.dump(comic)
|
||||
|
||||
|
||||
@comics_api.route('/publisher/')
|
||||
def publisher_all():
|
||||
publishers = Publisher.query.all()
|
||||
return publishers_schema.dump(publishers)
|
||||
|
||||
|
||||
@comics_api.route('/publisher/<string:id>')
|
||||
def publisher_detail(publisher_id):
|
||||
publisher = Publisher.query.get(publisher_id)
|
||||
return publisher_schema.dump(publisher)
|
||||
@@ -1,31 +0,0 @@
|
||||
"""Define form to edit publisher, artists and comics"""
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import StringField, SubmitField, BooleanField, SelectField
|
||||
from wtforms.validators import DataRequired
|
||||
from bson import ObjectId
|
||||
|
||||
|
||||
class PublisherForm(FlaskForm):
|
||||
"""
|
||||
Form to add and edit a Comic publisher
|
||||
"""
|
||||
name = StringField('Name', validators=[DataRequired()])
|
||||
submit = SubmitField('Submit')
|
||||
|
||||
|
||||
class ArtistForm(FlaskForm):
|
||||
"""
|
||||
Form to add and edit a Comic publisher
|
||||
"""
|
||||
name = StringField('Name', validators=[DataRequired()])
|
||||
submit = SubmitField('Submit')
|
||||
|
||||
|
||||
class ComicForm(FlaskForm):
|
||||
"""
|
||||
Form to add and edit Comics
|
||||
"""
|
||||
title = StringField('Title', validators=[DataRequired()])
|
||||
publisher = SelectField('Publisher', coerce=ObjectId)
|
||||
current_order = BooleanField('Current Order')
|
||||
submit = SubmitField('Submit')
|
||||
@@ -1,51 +0,0 @@
|
||||
"""This modules declares the model for Comic related information."""
|
||||
|
||||
from flask import current_app
|
||||
from pymongo.write_concern import WriteConcern
|
||||
from pymodm import MongoModel, fields
|
||||
|
||||
|
||||
class Publisher(MongoModel):
|
||||
"""Class Publisher represents a publisher of a comic."""
|
||||
name = fields.CharField()
|
||||
|
||||
def __str__(self):
|
||||
return "Publisher({})".format(self.name)
|
||||
|
||||
@property
|
||||
def comics(self):
|
||||
"""
|
||||
Return list of comics which has reference to this publisher
|
||||
:return:
|
||||
"""
|
||||
comics = Comic.objects.raw({'publisher': self.pk})
|
||||
current_app.logger.debug(comics)
|
||||
return comics
|
||||
|
||||
class Meta:
|
||||
"""Sets the connection and connections details."""
|
||||
connection_alias = 'kontor'
|
||||
write_concern = WriteConcern(j=True)
|
||||
|
||||
|
||||
class Artist(MongoModel):
|
||||
"""Class Artist represents a comic artist."""
|
||||
name = fields.CharField()
|
||||
|
||||
class Meta:
|
||||
"""Sets the connection and connections details."""
|
||||
connection_alias = 'kontor'
|
||||
write_concern = WriteConcern(j=True)
|
||||
|
||||
|
||||
class Comic(MongoModel):
|
||||
"""Class Comic represents a comic."""
|
||||
title = fields.CharField()
|
||||
publisher = fields.ReferenceField(Publisher)
|
||||
current_order = fields.BooleanField(default=False)
|
||||
completed = fields.BooleanField(default=False)
|
||||
|
||||
class Meta:
|
||||
"""Sets the connection and connections details."""
|
||||
connection_alias = 'kontor'
|
||||
write_concern = WriteConcern(j=True)
|
||||
@@ -0,0 +1,16 @@
|
||||
from flask import Blueprint, render_template
|
||||
|
||||
from kontor.comics import comics_bp
|
||||
from kontor.models import Comic
|
||||
|
||||
|
||||
@comics_bp.route('/')
|
||||
def index():
|
||||
comics = Comic.query.all()
|
||||
return render_template('comics/list.html', comics=comics)
|
||||
|
||||
|
||||
@comics_bp.route('/comic/<string:comic_id>')
|
||||
def view(comic_id):
|
||||
comic = Comic.query.get(comic_id)
|
||||
return render_template('comics/view.html', comic=comic)
|
||||
@@ -0,0 +1,28 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block content %}
|
||||
<span class="title">
|
||||
<h1>{% block title %} Comics {% endblock %}</h1>
|
||||
</span>
|
||||
<div class="comics">
|
||||
<h2>Comics Blueprint</h2>
|
||||
{% for comic in comics %}
|
||||
<div class="comic">
|
||||
<b>
|
||||
<p class="name">
|
||||
<a href="{{ url_for('comics_bp.view', comic_id=comic.id)}}">{{ comic.title }}</a>
|
||||
</p>
|
||||
</b>
|
||||
<b><p class="identifier">{{ comic.publisher.name }}</p></b>
|
||||
<div class="date">
|
||||
<h4>Created</h4>
|
||||
<p>{{ comic.created_date }}</p>
|
||||
<h4>Modified</h4>
|
||||
<p>{{ comic.last_modified_date }}</p>
|
||||
</div>
|
||||
<p>Version: {{ comic.version }}</p>
|
||||
<p>Review: {{ comic.review }}</p>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,27 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block content %}
|
||||
<span class="title">
|
||||
<h1>{% block title %} Comics {% endblock %}</h1>
|
||||
</span>
|
||||
<div class="comics">
|
||||
<h2>Comics Blueprint</h2>
|
||||
<div class="comic">
|
||||
<b>
|
||||
<p class="name">
|
||||
<a href="{{ url_for('comics_bp.view', comic_id=comic.id)}}">{{ comic.title }}</a>
|
||||
</p>
|
||||
</b>
|
||||
<b>
|
||||
<p class="identifier">{{ comic.id }}</p>
|
||||
</b>
|
||||
<div class="date">
|
||||
<h4>Created</h4>
|
||||
<p>{{ comic.created_date }}</p>
|
||||
<h4>Modified</h4>
|
||||
<p>{{ comic.last_modified_date }}</p>
|
||||
</div>
|
||||
<p>Version: {{ comic.version }}</p>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -1,222 +0,0 @@
|
||||
"""
|
||||
Define routing rules for comics, publisher and artists
|
||||
"""
|
||||
from flask import Blueprint, flash, redirect, render_template, url_for
|
||||
from flask_login import login_required
|
||||
from bson import ObjectId
|
||||
from pymongo.errors import PyMongoError
|
||||
from .forms import ComicForm, PublisherForm, ArtistForm
|
||||
from .models import Comic, Publisher, Artist
|
||||
|
||||
|
||||
COMIC = Blueprint('comic', __name__)
|
||||
|
||||
|
||||
@COMIC.route('/artists')
|
||||
@login_required
|
||||
def list_artists():
|
||||
"""
|
||||
List all artists
|
||||
:return:
|
||||
"""
|
||||
artists = Artist.objects.all()
|
||||
return render_template('comics/artists.html',
|
||||
artists=artists, title="Artists")
|
||||
|
||||
|
||||
@COMIC.route('/artists/edit/<artist_id>', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def edit_artist(artist_id):
|
||||
"""
|
||||
Edit a comic artist
|
||||
"""
|
||||
artist = Artist.objects.get({'_id': ObjectId(artist_id)})
|
||||
form = ArtistForm(obj=artist)
|
||||
if form.validate_on_submit():
|
||||
artist.name = form.name.data
|
||||
artist.save()
|
||||
flash('You have successfully edited the artist.')
|
||||
return redirect(url_for('comic.list_artists'))
|
||||
form.name.data = artist.name
|
||||
return render_template('simpleform.html', action="Edit",
|
||||
form=form, title="Edit Artist")
|
||||
|
||||
|
||||
@COMIC.route('/artists/add', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def add_artist():
|
||||
"""
|
||||
Add a artist
|
||||
:return:
|
||||
"""
|
||||
form = ArtistForm()
|
||||
if form.validate_on_submit():
|
||||
artist = Artist()
|
||||
artist.name = form.name.data
|
||||
try:
|
||||
# add publisher to the database
|
||||
artist.save()
|
||||
flash('You have successfully added a new artist.')
|
||||
except PyMongoError:
|
||||
# in case publisher name already exists
|
||||
flash('Error: artist name already exists.')
|
||||
return redirect(url_for('comic.list_artists'))
|
||||
return render_template('simpleform.html', action="Add",
|
||||
form=form, title="Add Artist")
|
||||
|
||||
|
||||
@COMIC.route('/artists/delete/<artist_id>', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def delete_artist(artist_id):
|
||||
"""
|
||||
Delete a comic artist
|
||||
:param artist_id:
|
||||
:return:
|
||||
"""
|
||||
artist = Artist.objects.raw({'_id': ObjectId(artist_id)})
|
||||
if artist:
|
||||
artist.delete()
|
||||
flash('You have successfully deleted the comic artist.')
|
||||
return redirect(url_for('comic.list_artists'))
|
||||
|
||||
|
||||
@COMIC.route('/publishers')
|
||||
@login_required
|
||||
def list_publishers():
|
||||
"""
|
||||
List all publishers
|
||||
:return:
|
||||
"""
|
||||
publishers = Publisher.objects.all()
|
||||
return render_template('comics/publishers.html',
|
||||
publishers=publishers, title="Publishers")
|
||||
|
||||
|
||||
@COMIC.route('/publishers/add', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def add_publisher():
|
||||
"""
|
||||
Add a publisher to the database
|
||||
:return:
|
||||
"""
|
||||
form = PublisherForm()
|
||||
if form.validate_on_submit():
|
||||
publisher = Publisher()
|
||||
publisher.name = form.name.data
|
||||
try:
|
||||
# add publisher to the database
|
||||
publisher.save()
|
||||
flash('You have successfully added a new publisher.')
|
||||
except PyMongoError:
|
||||
# in case publisher name already exists
|
||||
flash('Error: publisher name already exists.')
|
||||
return redirect(url_for('comic.list_publishers'))
|
||||
return render_template('simpleform.html', action="Add",
|
||||
form=form, title="Add Publisher")
|
||||
|
||||
|
||||
@COMIC.route('/publishers/edit/<publisher_id>', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def edit_publisher(publisher_id):
|
||||
"""
|
||||
Edit a publisher
|
||||
"""
|
||||
publisher = Publisher.objects.get({'_id': ObjectId(publisher_id)})
|
||||
form = PublisherForm(obj=publisher)
|
||||
if form.validate_on_submit():
|
||||
publisher.name = form.name.data
|
||||
publisher.save()
|
||||
flash('You have successfully edited the publisher.')
|
||||
return redirect(url_for('comic.list_publishers'))
|
||||
form.name.data = publisher.name
|
||||
return render_template('simpleform.html', action="Edit",
|
||||
form=form, title="Edit Publisher")
|
||||
|
||||
|
||||
@COMIC.route('/publishers/delete/<publisher_id>', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def delete_publisher(publisher_id):
|
||||
"""
|
||||
Delete a publisher
|
||||
:param publisher_id: ObjectId of publisher
|
||||
:return:
|
||||
"""
|
||||
publisher = Publisher.objects.raw({'_id': ObjectId(publisher_id)})
|
||||
if publisher:
|
||||
publisher.delete()
|
||||
flash('You have successfully deleted the publisher.')
|
||||
return redirect(url_for('comic.list_publishers'))
|
||||
|
||||
|
||||
@COMIC.route('/comics')
|
||||
@login_required
|
||||
def list_comics():
|
||||
"""
|
||||
List all comics
|
||||
:return:
|
||||
"""
|
||||
comics = Comic.objects.all()
|
||||
return render_template('comics/comics.html',
|
||||
comics=comics, title="Comics")
|
||||
|
||||
|
||||
@COMIC.route('/comics/edit/<comic_id>', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def edit_comic(comic_id):
|
||||
"""
|
||||
Edit a comic
|
||||
"""
|
||||
comic = Comic.objects.get({'_id': ObjectId(comic_id)})
|
||||
form = ComicForm(obj=comic)
|
||||
form.publisher.choices = [(p.pk, p.name) for p in Publisher.objects.all()]
|
||||
form.publisher.default = comic.publisher.pk
|
||||
form.publisher.process_data(comic.publisher.pk)
|
||||
if form.validate_on_submit():
|
||||
comic.title = form.title.data
|
||||
comic.current_order = form.current_order.data
|
||||
comic.publisher = form.publisher.data
|
||||
comic.save()
|
||||
flash('You have successfully edited the comic.')
|
||||
return redirect(url_for('comic.list_comics'))
|
||||
form.title.data = comic.title
|
||||
form.current_order.data = comic.current_order
|
||||
return render_template('simpleform.html', action="Edit",
|
||||
form=form, title="Edit Comic")
|
||||
|
||||
|
||||
@COMIC.route('/comics/add', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def add_comic():
|
||||
"""
|
||||
Add a comic
|
||||
:return:
|
||||
"""
|
||||
form = ComicForm()
|
||||
form.publisher.choices = [(p.pk, p.name) for p in Publisher.objects.all()]
|
||||
if form.validate_on_submit():
|
||||
comic = Comic()
|
||||
comic.title = form.title.data
|
||||
comic.publisher = form.publisher.data
|
||||
try:
|
||||
comic.save()
|
||||
flash('You have successfully added a new comic.')
|
||||
except PyMongoError:
|
||||
flash('Error: comic title already exists.')
|
||||
return redirect(url_for('comic.list_comics'))
|
||||
return render_template('simpleform.html', action="Add",
|
||||
form=form, title="Add Comic")
|
||||
|
||||
|
||||
@COMIC.route('/comics/delete/<comic_id>', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def delete_comic(comic_id):
|
||||
"""
|
||||
Delete a comic
|
||||
:param comic_id:
|
||||
:return:
|
||||
"""
|
||||
comic = Comic.objects.raw({'_id': ObjectId(comic_id)})
|
||||
if comic:
|
||||
comic.delete()
|
||||
flash('You have successfully deleted the comic.')
|
||||
return redirect(url_for('comic.list_comics'))
|
||||
Reference in New Issue
Block a user