Commit e6a2f8af authored by Давид Ли's avatar Давид Ли

completed webinar 19

parent 6dc9565b
File added
from sqlalchemy.orm import Session
from db.conf import get_session
from src.request import Request from src.request import Request
from src.response import Response from src.response import Response
class BaseController: class BaseController:
def __init__(self, request: Request, response: Response): def __init__(
self,
request: Request,
response: Response,
):
self.request = request self.request = request
self.response = response self.response = response
self.session: Session = get_session()
import utils import sqlalchemy as sa
from core import utils
from controllers.base import BaseController from controllers.base import BaseController
from db.conf import get_session
from db.database import Database from db.database import Database
from db.models.post import PostModel
from template_engine.jinja import env from template_engine.jinja import env
from utils import id_gen
class PostsController(BaseController): class PostsController(BaseController):
def get_list(self): def get_list(self):
tmp = env.get_template('posts/list.html') tmp = env.get_template('posts/list.html')
body = tmp.render(posts=Database.all()) posts = self.session.query(PostModel).all()
body = tmp.render(posts=posts)
self.response.add_header('Content-Type', 'text/html') self.response.add_header('Content-Type', 'text/html')
self.response.set_body(body) self.response.set_body(body)
...@@ -21,7 +27,14 @@ class PostsController(BaseController): ...@@ -21,7 +27,14 @@ class PostsController(BaseController):
self.response.set_body(body) self.response.set_body(body)
def create(self): def create(self):
Database.add(self.request.body) # Database.add(self.request.body)
# INSERT INTO posts values ()
# stmt = sa.insert(PostModel).values([self.request.body])
# resp = self.session.execute(stmt)
post = PostModel(**self.request.body)
self.session.add(post)
self.session.commit()
self.response.add_header('Content-Type', 'text/html') self.response.add_header('Content-Type', 'text/html')
self.response.set_status(301) self.response.set_status(301)
......
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine("sqlite:///articles.db", echo=True)
session = sessionmaker(bind=engine)
current_session = scoped_session(session)
BaseModel = declarative_base()
def create_tables():
BaseModel.metadata.create_all(engine)
def get_session():
try:
return current_session
finally:
current_session.close()
from utils import id_gen from core.utils import id_gen
class Database: class Database:
......
import sqlalchemy as sa
from db.conf import BaseModel
class PostModel(BaseModel):
__tablename__ = 'posts'
id = sa.Column(sa.Integer, primary_key=True)
title = sa.Column(sa.String(150), nullable=False)
description = sa.Column(sa.Text(), nullable=True)
def __repr__(self):
return f'{self.id} | {self.title}'
from controllers.pages import PagesController from controllers.pages import PagesController
from controllers.posts import PostsController from controllers.posts import PostsController
from db.conf import create_tables, current_session
from src.router import Router from src.router import Router
from src.server import runserver from src.server import runserver
...@@ -16,4 +17,8 @@ router.get('/posts', PostsController, 'get_list') ...@@ -16,4 +17,8 @@ router.get('/posts', PostsController, 'get_list')
router.post('/posts', PostsController, 'create') router.post('/posts', PostsController, 'create')
router.get('/posts/new', PostsController, 'new') router.get('/posts/new', PostsController, 'new')
create_tables()
# current_session.close_all()
runserver(router, config) runserver(router, config)
...@@ -40,10 +40,10 @@ class Router: ...@@ -40,10 +40,10 @@ class Router:
if not route: if not route:
return errors.not_found(request, response) return errors.not_found(request, response)
try: # try:
ctrl = route['ctrl'](request, response) ctrl = route['ctrl'](request, response)
getattr(ctrl, route['ctrl_method'])() getattr(ctrl, route['ctrl_method'])()
except BaseException as e: # except BaseException as e:
print(e) # print(e)
return errors.internal_server_error(request, response) # return errors.internal_server_error(request, response)
...@@ -3,15 +3,15 @@ ...@@ -3,15 +3,15 @@
{% block content %} {% block content %}
<form action="/posts" method="post"> <form action="/posts" method="post">
<div class="mb-3 row"> <div class="mb-3 row">
<label for="staticEmail" class="col-sm-2 col-form-label">Id</label> <label for="inputPassword" class="col-sm-2 col-form-label">Title</label>
<div class="col-sm-10"> <div class="col-sm-10">
<input type="text" readonly class="form-control-plaintext" id="staticEmail" name="id" value="{{ id }}"> <input type="text" class="form-control" id="inputPassword" name="title">
</div> </div>
</div> </div>
<div class="mb-3 row"> <div class="mb-3 row">
<label for="inputPassword" class="col-sm-2 col-form-label">Title</label> <label for="inputDescription" class="col-sm-2 col-form-label">Description</label>
<div class="col-sm-10"> <div class="col-sm-10">
<input type="text" class="form-control" id="inputPassword" name="title"> <input type="text" class="form-control" id="inputDescription" name="description">
</div> </div>
</div> </div>
<button class="btn btn-success">Submit</button> <button class="btn btn-success">Submit</button>
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
<h1>This is the posts page</h1> <h1>This is the posts page</h1>
<div class="row"> <div class="row">
<a href="/posts/new" class="btn btn-primary">Создать</a>
{% for post in posts %} {% for post in posts %}
<div class="col-4"> <div class="col-4">
<div class="card"> <div class="card">
...@@ -12,7 +13,7 @@ ...@@ -12,7 +13,7 @@
</div> </div>
<div class="card-body"> <div class="card-body">
<h5 class="card-title">{{ post.title }}</h5> <h5 class="card-title">{{ post.title }}</h5>
<p class="card-text">With supporting text below as a natural lead-in to additional content.</p> <p class="card-text">{{ post.description }}</p>
<a href="#" class="btn btn-primary">Go somewhere</a> <a href="#" class="btn btn-primary">Go somewhere</a>
</div> </div>
</div> </div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment