-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from hexlet-components/cookies
Cookies
- Loading branch information
Showing
7 changed files
with
75 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,23 @@ | ||
FROM python:3.12-slim | ||
|
||
RUN apt-get update && apt-get install make -yq | ||
RUN apt-get update && apt-get install make -yqq \ | ||
make \ | ||
postgresql-15 \ | ||
sudo | ||
|
||
RUN pip install poetry | ||
|
||
ENV POETRY_VIRTUALENVS_IN_PROJECT=true | ||
|
||
COPY ./pg_hba.conf /etc/postgresql/15/main/pg_hba.conf | ||
|
||
WORKDIR /app | ||
|
||
COPY poetry.lock . | ||
COPY . . | ||
|
||
RUN poetry install | ||
|
||
COPY . . | ||
#USER postgres | ||
|
||
CMD ["bash", "-c", "make prod"] | ||
#COPY ./run.sh . | ||
#CMD ./run.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
services: | ||
app: | ||
build: | ||
context: . | ||
ports: | ||
- 8000:8000 | ||
command: make prod | ||
environment: | ||
DATABASE_URL: postgres://postgres:password@db:5432/postgres |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
CREATE TABLE IF NOT EXISTS users ( | ||
id SERIAL PRIMARY KEY, | ||
name VARCHAR(255) NOT NULL, | ||
email VARCHAR(255) NOT NULL | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/bash | ||
|
||
set -m | ||
|
||
/usr/lib/postgresql/15/bin/postgres \ | ||
-D /var/lib/postgresql/15/main \ | ||
-c config_file=/etc/postgresql/15/main/postgresql.conf & | ||
|
||
sleep 5 && psql -a -f init.sql && make run | ||
|
||
fg %1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,32 @@ | ||
import json | ||
import sys | ||
import uuid | ||
from flask import session | ||
|
||
class UserRepository: | ||
|
||
class UserRepository(): | ||
def __init__(self): | ||
self.users = json.load(open("./users.json", 'r')) | ||
if 'user' not in session: | ||
session['user'] = {} | ||
|
||
def get_content(self): | ||
return self.users | ||
return session['user'].values() | ||
|
||
def find(self, id): | ||
try: | ||
for user in self.users: | ||
if str(id) == str(user['id']): | ||
return user | ||
return session['user'][id] | ||
except KeyError: | ||
sys.stderr.write(f'Wrong post id: {id}') | ||
sys.stderr.write(f'Wrong user id: {id}') | ||
raise | ||
|
||
def save(self, user_data): | ||
# repository should know nothing about validation in outer layer | ||
if not (user_data.get('name') and user_data.get('email')): | ||
raise Exception(f'Wrong data: {json.dumps(user_data)}') | ||
|
||
if 'id' not in user_data: | ||
new_user = user_data.copy() | ||
new_user['id'] = str(uuid.uuid4()) | ||
self.users.append(new_user) | ||
else: | ||
for i, existing_user in enumerate(self.users): | ||
if str(user_data['id']) == str(existing_user['id']): | ||
self.users[i] = user_data | ||
break | ||
else: | ||
raise Exception(f"User with id {user_data['id']} not found") | ||
|
||
with open("./users.json", "w") as f: | ||
json.dump(self.users, f) | ||
|
||
return user_data['id'] | ||
|
||
def destroy(self, id): | ||
current_user = self.find(id) | ||
self.users.remove(current_user) | ||
with open("./users.json", "w") as f: | ||
json.dump(self.users, f) | ||
del session['user'][id] | ||
|
||
def save(self, user): | ||
if not (user.get('name') and user.get('email')): | ||
raise Exception(f'Wrong data: {json.loads(user)}') | ||
if not user.get('id'): | ||
user['id'] = str(uuid.uuid4()) | ||
session['user'][user['id']] = user | ||
session['user'] = session['user'] | ||
return user['id'] |
This file was deleted.
Oops, something went wrong.