Skip to content

Instantly share code, notes, and snippets.

@scmmishra
Last active March 8, 2019 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scmmishra/50aef8f51b2885d3e26e1f4663979772 to your computer and use it in GitHub Desktop.
Save scmmishra/50aef8f51b2885d3e26e1f4663979772 to your computer and use it in GitHub Desktop.
Flask Workshop
import os
import datetime
from flask import (Flask, flash, redirect, render_template, request,
Response, url_for, session)
from peewee import *
from playhouse.flask_utils import FlaskDB, get_object_or_404, object_list
from playhouse.sqlite_ext import *
SECRET_KEY = "secret"
APP_DIR = os.path.dirname(os.path.realpath(__file__))
DEBUG = True
DATABASE = 'sqliteext:///%s' % os.path.join(APP_DIR, 'app_data.db')
app = Flask(__name__)
app.config.from_object(__name__)
flask_db = FlaskDB(app)
database = flask_db.database
class BaseModel(flask_db.Model):
class Meta:
database = database
class Author(BaseModel):
id = AutoField()
name = CharField(null = False)
bio = TextField(null = False)
avatar = CharField()
class Book(BaseModel):
id = AutoField()
title = CharField(null = False)
description = TextField()
author = ForeignKeyField(Author)
publish_date = DateTimeField()
cover = TextField()
def _create_or_edit(book, template):
if request.method == 'POST':
book.title = request.form.get('title') or ''
book.description = request.form.get('description') or ''
book.author = Author.get(Author.id == request.form.get('author') or 1)
book.publish_date = request.form.get('publish_date') or datetime.datetime.now()
book.cover = request.form.get('cover') or ''
if not(book.title and book.description and book.author and book.publish_date and book.cover):
flash('All fields are required.', 'danger')
else:
try:
with database.atomic():
book.save()
except IntegrityError:
flash('Error ID Conflicting', 'danger')
else:
flash('book saved successfully.', 'success')
return redirect(url_for('index'))
return render_template(template, book=book)
@app.route('/book/new/', methods=['GET', 'POST'])
def create():
return _create_or_edit(Book, 'create.html')
@app.route('/edit_book/<id>', methods=['GET', 'POST'])
def edit_book(id):
return "Edit Book"
@app.route('/book/<id>')
def get_book(id):
query = Book.select()
book = get_object_or_404(Book, Book.id == id)
return render_template('book.html', book=book)
@app.route('/authors')
def authors():
query = Author.select()
return object_list('author.html', query, check_bounds=False)
@app.route('/')
def index():
query = Book.select()
return object_list('index.html', query, check_bounds=False)
@app.route('/create')
def create():
return "Hello create"
if __name__ == '__main__':
database.create_tables([Author, Book], safe=True)
app.run(debug=True)
app.Author.create(name="Chinmay", bio="He writes", avatar="https://images.gr-assets.com/authors/1362814142p8/3389.jpg")
{% extends "template.html" %}
{% block head %} {{ fist_name }}{% endblock %}
{% block content %}
<h1> This is home.html </h1>
<h2> Welcome {{ fist_name }}</h2>
<ul>
{% for item in list_of_items %}
<li> {{ item }}</li>
{% endfor %}
</ul>
{% endblock %}
<!-- https://gistof.com/workshop -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block head %} {% endblock %}</title>
</head>
<body>
<h1>This is template.html</h1>
{% block content %} {% endblock %}
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment