Commit 541db55e authored by ='s avatar =

add display movies functionallity and create movie

parent c5ab97ce
def generate_id():
id = 1
while True:
yield id
id += 1
id = generate_id()
class Movie:
def __init__(self, name, img_url, description="", rating=0):
self.id = next(id)
self.name = name
self.img_url = img_url
self.description = description
self.rating = rating
def __str__(self):
return f'{self.id} {self.name}'
class DBMovie:
def __init__(self):
self.movies = []
def get(self):
return self.movies
def find_by_id(self, id):
for movie in self.movies:
if movie.id == id:
return movie
def delete_by_id(self, id):
movie_for_delete = self.find_by_id(id)
self.movies.remove(movie_for_delete)
def save(self, movie):
self.movies.append(movie)
def filter_by_categories(self, category):
pass
......@@ -15,7 +15,6 @@ class Response:
HTTP_REDIRECT_STATUS: "Redirect"
}
PROTOCOL = "HTTP/1.1"
def __init__(self, file):
......
......@@ -2,6 +2,9 @@ from socketserver import StreamRequestHandler, TCPServer, ThreadingMixIn
from request import Request
from response import Response
from StaticResponder import StaticResponder
import views
from random import randint
class HelloTCPServer(StreamRequestHandler):
......@@ -16,7 +19,13 @@ class HelloTCPServer(StreamRequestHandler):
else:
response.add_header("Content-Type", "text/html")
response.add_header("Connection", "close")
response.set_body('<h1>Hello, world!</h1>')
route_func = views.routes.get(request.uri, "404")
if "/movies/" in request.uri:
views.show_movie(request, response)
elif route_func == "404":
response.set_status(Response.HTTP_NOT_FOUND)
else:
route_func(request, response)
response.send()
......@@ -29,4 +38,5 @@ HOST, PORT = "127.0.0.1", 8000
TCPServer.allow_reuse_address = True
with TCPServer((HOST, PORT), HelloTCPServer) as s:
print(f'Server on port: {PORT}')
s.serve_forever()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<title>Title</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a class="navbar-brand" href="/">Kinopoisk</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="/">Movies</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/create_movies.html">Create</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<h1>Add movie</h1>
<form action="/create" method="post">
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">Movie name</label>
<input type="text" name="name" class="form-control" id="exampleFormControlInput1" placeholder="name">
</div>
<div class="mb-3">
<label for="exampleFormControlTextarea1" class="form-label">Movie description</label>
<textarea name="description" class="form-control" id="exampleFormControlTextarea1" rows="3"></textarea>
</div>
<div class="mb-3">
<label class="form-label">Movie img url</label>
<input name="img_url" type="text" class="form-control" placeholder="img url">
</div>
<button type="submit" class="btn btn-info">
Save
</button>
</form>
</div>
</body>
</html>
\ No newline at end of file
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a class="navbar-brand" href="/">Kinopoisk</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" aria-current="page" href="/">Movies</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="/create_movie.html">Create</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
{{movie}}
</div>
</body>
</html>
\ No newline at end of file
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a class="navbar-brand" href="/">Kinopoisk</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" aria-current="page" href="/">Movies</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="/create_movie.html">Create</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="row mt-3">
{{movies}}
</div>
</div>
</body>
</html>
\ No newline at end of file
from models import Movie, DBMovie
from urllib.parse import parse_qs
from response import Response
db_movie = DBMovie()
db_movie.save(
Movie("Movie1", "https://image.api.playstation.com/vulcan/img/rnd/202010/2621/H9v5o8vP6RKkQtR77LIGrGDE.png"))
db_movie.save(
Movie("Movie2", "https://image.api.playstation.com/vulcan/img/rnd/202010/2621/H9v5o8vP6RKkQtR77LIGrGDE.png"))
db_movie.save(
Movie("Movie3", "https://image.api.playstation.com/vulcan/img/rnd/202010/2621/H9v5o8vP6RKkQtR77LIGrGDE.png"))
def show_movies(request, response):
movies = db_movie.get()
html_movies = ""
for movie in movies:
html_movies += f'''
<div class="col">
<div class="card">
<img src="{movie.img_url}" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">{movie.id} {movie.name}</h5>
<p class="card-text">{movie.description}</p>
<a href="/movies/{movie.id}" class="btn btn-primary">Detail</a>
</div>
</div>
</div>
'''
with open("templates/index.html", "r") as file:
html = file.read()
result_html = html.replace("{{movies}}", html_movies)
response.set_body(result_html)
def show_movie(request, response):
id = int(request.uri[-1])
movie = db_movie.find_by_id(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)
def create_movie(request, response):
decoded_body = request.body.decode()
body = parse_qs(decoded_body)
movie = Movie(body['name'][0], body['img_url'][0], body['description'][0])
db_movie.save(movie)
response.set_status(Response.HTTP_REDIRECT_STATUS)
response.add_header('Location', "/")
routes = {
"/": show_movies,
"/create": create_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