[PYTHON] Play with Pyramid

I played with the Pyramid tutorial. http://docs.pylonsproject.org/projects/pyramid_tutorials/en/latest/single_file_tasks/single_file_tasks.html It is boring to copy sutras as it is, so I rewrote the DB to Mongodb. The template and stylesheet files will work if you arrange them in the same way as in the tutorial.

tasks.py


# -*- coding: utf-8

from paste.httpserver import serve
from pyramid.configuration import Configurator
from pyramid.response import Response
from pyramid.events import subscriber, ApplicationCreated, NewRequest
from pyramid.session import UnencryptedCookieSessionFactoryConfig
from pyramid.exceptions import NotFound
from pyramid.httpexceptions import HTTPFound
from mongoengine import connect, Document, StringField, \
     BooleanField
from pyramid.view import view_config
import os

MONGODB_SERVER_HOST = 'localhost'
MONGODB_SERVER_PORT = 27017
MONGODB_SERVER_DATABASE = 'db'

here = os.path.dirname(os.path.abspath(__file__))

#
# Models
#

class Task(Document):
    name = StringField(required=True, max_length=100)
    closed = BooleanField(required=True, default=False)

#
# Views
#

@view_config(route_name='list', renderer='list.mako')
def list_view(request):
    tasks = Task.objects(closed__ne=True)
    return {'tasks': tasks}

@view_config(route_name='new', renderer='new.mako')
def new_view(request):
    if request.method == 'POST':
        if request.POST.get('name'):
            task = Task()
            task.name = request.POST['name']
            task.save()
            request.session.flash('New task was successfully added!')
            return HTTPFound(location=request.route_url('list'))
        else:
            request.session.flash('Please enter a name for the task!')
    return {}

@view_config(route_name='close')
def close_view(request):
    task_id = unicode(request.matchdict['id'])
    Task.objects(id=task_id).update(set__closed=True)
    request.session.flash('Task was successfully closed!')
    return HTTPFound(location=request.route_url('list'))

@view_config(context='pyramid.exceptions.NotFound', renderer='notfound.mako')
def notfound_view(self):
    return {}

@subscriber(ApplicationCreated)
def application_created_subscriber(event):
    connect(MONGODB_SERVER_DATABASE, host=MONGODB_SERVER_HOST, port=MONGODB_SERVER_PORT)

if __name__ == '__main__':
    settings = {
        'reload_all': True,
        'debug_all': True,
        'mako.directories': os.path.join(here, 'templates'),
        }
    session_factory = UnencryptedCookieSessionFactoryConfig('itsaseekreet')
    config = Configurator(settings=settings, session_factory=session_factory)
    config.add_route('list', '/')
    config.add_route('new', '/new')
    config.add_route('close', '/close/{id}')
    config.add_static_view('static', os.path.join(here, 'static'))
    config.scan()
    app = config.make_wsgi_app()
    serve(app, host='0.0.0.0')

Recommended Posts

Play with Pyramid
Play with Prophet
Play with PyTorch
Play with 2016-Python
Play with CentOS 8
Play with Fathom
Play with Othello (Reversi)
Let's play with 4D 4th
Let's play with Amedas data-Part 1
Play with push notifications with imap4lib
Play around with Linux partitions
Use sqlite load_extensions with Pyramid
Let's play with Amedas data-Part 4
Play with Jupyter Notebook (IPython Notebook)
[Python] Play with Discord's Webhook.
pyramid
Play RocketChat with API / Python
Let's play with Amedas data-Part 3
Let's play with Amedas data-Part 2
Play with ASE MD module
Play with A3RT (Text Suggest)
Play with numerical calculation of magnetohydrodynamics
Play with a turtle with turtle graphics (Part 1)
Let's play with Excel with Python [Beginner]
Play with Pythonista UI implementation [Action implementation]
Play with PIR sensor module [DSUN-PIR]
Play around with Linux partitions ~ Continued ~
Spark play with WSL anaconda jupyter (2)
Play with Turtle on Google Colab
Play with demons because it's setsubun
Play video with sound with python !! (tkinter / imageio)
[Introduction to WordCloud] Let's play with scraping ♬
Play handwritten numbers with python Part 2 (identify)
[Complement] [PySide] Let's play with Qt Designer
Fractal to make and play with Python
I want to play with aws with python
Play with MoleculeNet's PDBBind and DeepChem's RDKitGridFeaturizer
Play audio files with interrupts using PyAudio
Play with puns using the COTOHA API
Load csv with pandas and play with Index
Introduced Pyramid
Python hand play (let's get started with AtCoder?)
[REAPER] How to play with Reascript in Python
Play with Google Spread Sheets in python (OAuth)
[Piyopiyokai # 1] Let's play with Lambda: Creating a Lambda function
Play with custom image notebook on Kubeflow v0.71
Play with Lambda layer (python) for about 5 minutes
I wanted to play with the Bezier curve
Understand grid points and play with contour lines.
Play with your Ubuntu desktop on your Raspberry Pi 4