Skip to content

Commit

Permalink
refactor: docs (#28)
Browse files Browse the repository at this point in the history
* refactor: docs

* fix: echo

---------

Co-authored-by: Marco Mihai Condrache <[email protected]>
  • Loading branch information
GiacomoGuidotto and marcocondrache authored Jun 22, 2024
1 parent 5bd80d2 commit 8a486a1
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 32 deletions.
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# Kepler Shop

This project uses conda as an environment, pip for python packages and invoke to run tasks
This project uses conda as an environment, pip for python packages and invoke to
run tasks

## Requirements

- [Docker](https://docs.docker.com/get-docker/)
- [conda](https://docs.conda.io/projects/conda/en/latest/user-guide/install/index.html)
- [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) (nvm
is recommended)

## Boot up

Expand All @@ -26,18 +34,23 @@ DB_HOST=localhost:5432
DB_NAME=kepler_db
DB_USER=[username]
DB_PASS=[password]
SECRET_KEY=[secret_key]
```

**The username and password needs to match the one defined in
the `docker-compose.yaml` file**

Run the server:

```sh
$ invoke setup dev
```

## Migrations
If you have made changes to the models, you need to generate the migrations and apply them to the database. To do this, run the following commands:
## Contributing

### Migrations

If you have made changes to the models, you need to generate the migrations and
apply them to the database. To do this, run the following commands:

```sh
flask db migrate -m "<migration message>"
Expand All @@ -46,5 +59,5 @@ invoke migrate

The first command generates the migration file,
and the second applies the migration to the database.
To apply the migration to the database,
To apply the migration to the database,
you need to have the database running and the database env variables set.
2 changes: 2 additions & 0 deletions app/modules/orders/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def create_buyer_order(cart: Cart) -> (
for r in invalid_reservations:
r.deleted_at = db.func.now()
# TODO: should create a new reservation?
db.session.commit()
return None, invalid_reservations, OrderCreationErrorReason.INVALID_PRODUCTS

# check locked products
Expand Down Expand Up @@ -161,6 +162,7 @@ def complete_buyer_order(buyer_order: BuyerOrder) -> (BuyerOrder | None, List[Se
for r in reservations:
r.product.locked_stock -= r.quantity
r.product.stock -= r.quantity
r.product.sequence += 1

# create seller orders
seller_orders = []
Expand Down
18 changes: 13 additions & 5 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ class Prod(object):

SECRET_KEY = os.getenv('SECRET_KEY', os.urandom(24))

DB_USER = os.getenv('DB_USER', 'db_admin')
DB_PASS = os.getenv('DB_PASS', 'password')
DB_HOST = os.getenv('DB_HOST', '0.0.0.0')
DB_NAME = os.getenv('DB_NAME', 'kepler_db')
DB_USER = os.getenv('DB_USER')
DB_PASS = os.getenv('DB_PASS')
DB_HOST = os.getenv('DB_HOST')
DB_NAME = os.getenv('DB_NAME')

if not DB_USER or not DB_PASS or not DB_HOST or not DB_NAME:
raise ValueError("Missing database configuration: add DB_USER, DB_PASS, DB_HOST, DB_NAME to the .env file")

SQLALCHEMY_DATABASE_URI = f"postgresql://{DB_USER}:{DB_PASS}@{DB_HOST}/{DB_NAME}"
SQLALCHEMY_ENGINE_OPTIONS = {
"isolation_level": "REPEATABLE READ"
"isolation_level": "REPEATABLE READ",
}

BLUEPRINTS = ["home", "auth", "users", "buyers", "sellers", "products", "carts", "orders", "shipments"]
Expand All @@ -34,4 +37,9 @@ class Prod(object):


class Dev(Prod):
SQLALCHEMY_ENGINE_OPTIONS = {
**super().SQLALCHEMY_ENGINE_OPTIONS,
"echo": True,
}

DEBUG = True
17 changes: 0 additions & 17 deletions docs/UsefulQueris.md

This file was deleted.

File renamed without changes.
File renamed without changes.
Binary file added docs/diagrams/conceptual_scheme.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/diagrams/logic_scheme.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/project_revised.pdf
Binary file not shown.
File renamed without changes.
File renamed without changes.
27 changes: 23 additions & 4 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@ def generate_styles(c):
print("styles generated")


def generate_secret():
secret_entry = "SECRET_KEY="
env_path = ".env"

if not os.path.exists(env_path): # .env file does not exist, create it
with open(env_path, "w") as env_file:
env_file.write(f"{secret_entry}{os.urandom(32)}\n")
print(f"generated .env file with secret key")
else:
with open(env_path, "r") as env_file:
lines = env_file.readlines()

for line in lines: # check if secret key already exists
if line.startswith(secret_entry):
print("secret key already exists in .env file, skipping...")
return

with open(env_path, "a") as env_file: # append secret key to .env file
env_file.write(f"{secret_entry}{os.urandom(32)}\n")
print("appended secret key to .env file")


@task
def populate(c):
migrate(c)
Expand All @@ -33,10 +55,7 @@ def setup(c):
c.run("npm install")

generate_styles(c)

# Generate secret and other env variables
env = open('.env', 'w+')
env.write("SECRET_KEY={secret}".format(secret=os.urandom(32)))
generate_secret()


@task
Expand Down

0 comments on commit 8a486a1

Please sign in to comment.