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.response import Response
class BaseController:
def __init__(self, request: Request, response: Response):
def __init__(
self,
request: Request,
response: Response,
):
self.request = request
self.response = response
self.session: Session = get_session()
import utils
import sqlalchemy as sa
from core import utils
from controllers.base import BaseController
from db.conf import get_session
from db.database import Database
from db.models.post import PostModel
from template_engine.jinja import env
from utils import id_gen
class PostsController(BaseController):
def get_list(self):
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.set_body(body)
......@@ -21,7 +27,14 @@ class PostsController(BaseController):
self.response.set_body(body)
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.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:
......
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.posts import PostsController
from db.conf import create_tables, current_session
from src.router import Router
from src.server import runserver
......@@ -16,4 +17,8 @@ router.get('/posts', PostsController, 'get_list')
router.post('/posts', PostsController, 'create')
router.get('/posts/new', PostsController, 'new')
create_tables()
# current_session.close_all()
runserver(router, config)
......@@ -40,10 +40,10 @@ class Router:
if not route:
return errors.not_found(request, response)
try:
ctrl = route['ctrl'](request, response)
getattr(ctrl, route['ctrl_method'])()
# try:
ctrl = route['ctrl'](request, response)
getattr(ctrl, route['ctrl_method'])()
except BaseException as e:
print(e)
return errors.internal_server_error(request, response)
# except BaseException as e:
# print(e)
# return errors.internal_server_error(request, response)
......@@ -3,15 +3,15 @@
{% block content %}
<form action="/posts" method="post">
<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">
<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 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">
<input type="text" class="form-control" id="inputPassword" name="title">
<input type="text" class="form-control" id="inputDescription" name="description">
</div>
</div>
<button class="btn btn-success">Submit</button>
......
......@@ -4,6 +4,7 @@
<h1>This is the posts page</h1>
<div class="row">
<a href="/posts/new" class="btn btn-primary">Создать</a>
{% for post in posts %}
<div class="col-4">
<div class="card">
......@@ -12,7 +13,7 @@
</div>
<div class="card-body">
<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>
</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