import from kontor-flask
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
"""
|
||||
Define routing rules for comic related information
|
||||
"""
|
||||
@@ -0,0 +1,31 @@
|
||||
"""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')
|
||||
@@ -0,0 +1,51 @@
|
||||
"""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,222 @@
|
||||
"""
|
||||
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