Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development #15

Merged
merged 20 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM python:3.8

ENV FLASK_APP=app.py

ENV FLASK_RUN_HOST=0.0.0.0

WORKDIR /app

COPY . .

# Install packages
RUN pip3 install -r requirements.txt

RUN rm site.db

EXPOSE 5000

CMD ["python3", "app.py"]
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@


### How to run the project :horse_racing:

##### To run locally
```sh
* Clone the repository
git clone https://github.com/DiptoChakrabarty/flask-online-store.git
Expand Down Expand Up @@ -46,6 +48,23 @@
(it is preferable if you use something as postman as most are post requests)


```

##### To run in docker
```sh
* Set environment variables
cp .env.example .env

* Fill the required parameters

* If you have removed site.db then remove the following line in Dockerfile
RUN rm site.db

* Run using
docker-compose up -d to start as background process

* Head over to http://localhost:5000

```

## Repository Structure :deciduous_tree:
Expand Down
5 changes: 5 additions & 0 deletions database.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
POSTGRES_USER=test
POSTGRES_PASSWORD=password
POSTGRES_HOST=db
POSTGRES_PORT=5432
POSTGRES_DB=flask_online_store
27 changes: 27 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: '3.8'
services:
db:
image: postgres:latest
env_file: database.conf
ports:
- 5432:5432
volumes:
- ./db_volume:/var/lib/db
app:
build:
context: .
dockerfile: Dockerfile
env_file:
- .env
volumes:
- ./:/app
environment:
FLASK_ENV: development
FLASK_APP: ./app.py
ports:
- 5000:5000
restart: always
depends_on:
- db
volumes:
data-volume:
Binary file modified model/__pycache__/users.cpython-36.pyc
Binary file not shown.
11 changes: 6 additions & 5 deletions model/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ class UserModel(db.Model):
__tablename__="users"
id = db.Column(db.Integer,primary_key=True)
username = db.Column(db.String(20),nullable=False,unique=True)
password = db.Column(db.String(20))
password = db.Column(db.String(20),nullable=True)
email = db.Column(db.String(40),nullable=False,unique=True)
activated = db.Column(db.Boolean,default=False) #set default as False
activated = db.Column(db.Boolean,default=False) #set default as False , if you do not email verification set as True
seller = db.Column(db.Boolean, default=False)

def __init__(self,username,password,email,activated=True):
def __init__(self,username,password,email,activated=False,seller=False): # if you do not email verification set activated as True
self.username=username
self.password=password
self.email = email
self.activated = activated
self.seller = seller

def save_to_db(self):
db.session.add(self)
Expand All @@ -37,7 +39,6 @@ def delete_from_db(self):
db.session.delete(self)
db.session.commit()


def generate_mail(self):
serializer = URLSafeTimedSerializer("secrettoken",1800)
token = serializer.dumps({"email":self.email},salt="flask-email-confirmation")
Expand All @@ -63,7 +64,7 @@ def find_by_username(cls,username):
@classmethod
def find_by_email(cls,email):
return cls.query.filter_by(email=email).first()

@classmethod
def check_password(cls,username,password):
user=cls.query.filter_by(username=username).first()
Expand Down
16 changes: 14 additions & 2 deletions resource/github_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,25 @@
from outh import github
from model.users import UserModel
from flask_jwt_extended import create_access_token,create_refresh_token
import secrets
import string

class Github(Resource):
@classmethod
def get(cls):
return github.authorize(url_for("github.authorize",_external=True))

class GithubAuthorize(Resource):


#generate a sample password for github oauth users
@classmethod
def generate_sample_password(cls):
alpha = string.ascii_letters + string.digits
random = ''.join(secrets.choice(alpha) for i in range(20))
return random


@classmethod
def get(cls):
response = github.authorized_response()
Expand All @@ -28,9 +40,9 @@ def get(cls):

#if UserModel.find_by_username(github_username):
# return {"msg": "User with username exists"}

#add user to database
user = UserModel(username=github_username,password=None,activated=True,email=github_email)
user = UserModel(username=github_username,password=GithubAuthorize.generate_sample_password(),activated=True,email=github_email)
user.save_to_db()

#create jwt tokens
Expand Down
9 changes: 5 additions & 4 deletions resource/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ class Order(Resource):
def post(cls):
data= request.get_json()
items=[]
item_quantity = Counter(data["items"])
ordered_list = data['items'] # list of dictionaries

for name,count in item_quantity.most_common():
for ordered_item in data['items']:
name = ordered_item['name']
count = ordered_item['qty']
res = ItemModel.find_by_name(name)
if not res:
return {"msg": "Item not present {}".format(name)},404
items.append(ItemsInOrder(item_id=ItemModel.find_id(name),quantity=count))
print(items)



order = OrderModel(items=items,status="pending")
order.save_to_db() #save orders to database

Expand Down
16 changes: 14 additions & 2 deletions resource/stores.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from flask_restful import Resource
from model.store import StoreModel
from model.users import UserModel
from flask import request
from schemas.stores import StoreSchema
from flask_jwt_extended import jwt_required,fresh_jwt_required
from flask_jwt_extended import jwt_required,fresh_jwt_required, get_jwt_identity

store_schema = StoreSchema()
store_list_schema = StoreSchema(many=True)
Expand All @@ -19,10 +20,16 @@ def get(self):

@jwt_required
def post(self):
user = UserModel.find_by_id(get_jwt_identity())

if not user.seller:
return {"msg": "User is not a seller"}, 403

data=request.get_json()
name=data["name"]

store = StoreModel.find_by_name(name)

if store:
return {"msg": "Store exists already"},400

Expand All @@ -36,6 +43,11 @@ def post(self):

@fresh_jwt_required
def delete(self):
user = UserModel.find_by_id(get_jwt_identity())

if not user.seller:
return {"msg": "User is not a seller"}, 403

data=request.get_json()
name=data["name"]

Expand Down
3 changes: 2 additions & 1 deletion resource/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def post(self):
username = data.username
passwd = data.password
email = data.email
seller = data.seller

print(username,passwd,email)
hashed = bcrypt.hashpw(passwd.encode('utf-8'),bcrypt.gensalt())
Expand All @@ -32,7 +33,7 @@ def post(self):
if UserModel.find_by_email(email):
return {"msg": "user with email id exists"}

user = UserModel(username,hashed,email)
user = UserModel(username,hashed,email,seller=seller)
user.save_to_db()

user.generate_mail() #send emails to new users
Expand Down
Binary file modified site.db
Binary file not shown.