Commit 32c3a6b9 authored by yevgeniy's avatar yevgeniy

inital

parent 541db55e
...@@ -5,6 +5,7 @@ class Request: ...@@ -5,6 +5,7 @@ class Request:
self.uri = "" self.uri = ""
self.protocol = "" self.protocol = ""
self.body = "" self.body = ""
self.query = ""
self.headers = {} self.headers = {}
self.parse_request_line() self.parse_request_line()
self.parse_headers() self.parse_headers()
......
...@@ -6,7 +6,6 @@ import views ...@@ -6,7 +6,6 @@ import views
from random import randint from random import randint
class HelloTCPServer(StreamRequestHandler): class HelloTCPServer(StreamRequestHandler):
def handle(self): def handle(self):
request = Request(self.rfile) request = Request(self.rfile)
...@@ -19,10 +18,12 @@ class HelloTCPServer(StreamRequestHandler): ...@@ -19,10 +18,12 @@ class HelloTCPServer(StreamRequestHandler):
else: else:
response.add_header("Content-Type", "text/html") response.add_header("Content-Type", "text/html")
response.add_header("Connection", "close") response.add_header("Connection", "close")
if '?' in request.uri:
query_split = request.uri.split("?")
request.uri = query_split[0]
request.query = query_split[1]
route_func = views.routes.get(request.uri, "404") route_func = views.routes.get(request.uri, "404")
if "/movies/" in request.uri: if route_func == "404":
views.show_movie(request, response)
elif route_func == "404":
response.set_status(Response.HTTP_NOT_FOUND) response.set_status(Response.HTTP_NOT_FOUND)
else: else:
route_func(request, response) route_func(request, response)
...@@ -34,7 +35,7 @@ class ThreadedTCPServe(ThreadingMixIn, TCPServer): ...@@ -34,7 +35,7 @@ class ThreadedTCPServe(ThreadingMixIn, TCPServer):
pass pass
HOST, PORT = "127.0.0.1", 8000 HOST, PORT = "127.0.0.1", 8001
TCPServer.allow_reuse_address = True TCPServer.allow_reuse_address = True
with TCPServer((HOST, PORT), HelloTCPServer) as s: with TCPServer((HOST, PORT), HelloTCPServer) as s:
......
...@@ -22,7 +22,7 @@ def show_movies(request, response): ...@@ -22,7 +22,7 @@ def show_movies(request, response):
<div class="card-body"> <div class="card-body">
<h5 class="card-title">{movie.id} {movie.name}</h5> <h5 class="card-title">{movie.id} {movie.name}</h5>
<p class="card-text">{movie.description}</p> <p class="card-text">{movie.description}</p>
<a href="/movies/{movie.id}" class="btn btn-primary">Detail</a> <a href="/movies/find?id={movie.id}" class="btn btn-primary">Detail</a>
</div> </div>
</div> </div>
</div> </div>
...@@ -61,7 +61,31 @@ def create_movie(request, response): ...@@ -61,7 +61,31 @@ def create_movie(request, response):
response.add_header('Location', "/") response.add_header('Location', "/")
def find_one_movie(request, response):
id_split = request.query.split("=")
if len(id_split) > 1:
id = id_split[1]
movie = db_movie.find_by_id(int(id))
html_movie = f'''
<div class="card" >
<img src="{movie.img_url}" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">Name: {movie.name}</h5>
<p class="card-text">Description: {movie.description}.</p>
<p class="card-text">Rating: {movie.rating}.</p>
</div>
</div>
'''
with open("templates/detail.html", "r") as file:
html = file.read()
result_html = html.replace("{{movie}}", html_movie)
response.set_body(result_html)
else:
pass
routes = { routes = {
"/": show_movies, "/": show_movies,
"/create": create_movie "/create": create_movie,
"/movies/find": find_one_movie
} }
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