* Clone the repository
git clone https://github.com/DiptoChakrabarty/flask-online-store.git
* Enter directory
cd flask-online-store
* Activate virtual environment
source venv/bin/activate
* Install packages
pip3 install -r requirements.txt
* Set environment variables
cp .env.example .env
* Fill the parameters
CLIENT_ID= {Github Client Id}
CILENT_SECRET= {Github Client Secret}
STRIPE_API= {Stripe token}
MAIL_USERNAME = {Email Id }
MAIL_PASSWORD = {Email Password}
* Remove site.db to start from fresh database
rm site.db
* Start app
python3 app.py
* Head over to http://localhost:5000
(it is preferable if you use something as postman as most are post requests)
* 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
Based on the directories present
Model directory contains all the database model class and methods associated
Resource directory contains api resource classes
Schemas is for the marshmallow schemas
Split contains sample code which can help you understand the code base
Static is the directory where user images are uploaded
Templates conatins html templates
There are mainly four schemas
* Users - which contains details about the users
* Store - which contains details about the store
* Items - which contains details about the items
* Order - which is for ordering stuff
### DataBase Architecture
- store and item : one to many
- item and order: many to many
flask jwt extended allows us to generate access and refresh tokens
access_token = create_access_token(identity=user.id,fresh=True)
refresh_token = create_refresh_token(user.id)
return {
"access_token": access_token,
"refresh_token": refresh_token
},200
For all post and put methods jwt token is required
Deletion requires a fresh jwt token so might prompt you to sign in again
Used for creating resource class
You can define the endpoints using flask restful
class Item(Resource):
def get(self):
try:
data = item_schema.load(request.get_json())
print(data.name)
except ValidationError as err:
return err.messages,400
print(data)
name = data.name
item = ItemModel.find_by_name(name)
if item:
return item_schema.dump(item)
return {
"msg": "Item not found"
}
flask marshmallow allows us to easily seralize and desarlize data
Check marsh_app.py file under split directory which contains simple example of using flask marshmallow
class RewardSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = Reward
load_instance = True
class MarshSchema(ma.SQLAlchemyAutoSchema):
rewards = ma.Nested(RewardSchema,many=True)
class Meta:
model = Marsh
load_instance = True
The example used above has been similarly implemented in the project
flask marshmallow latest version has syantax different from previous versions
marshamllow integrates well with SQLAlchemy
For OAuth we are now using GitHub
The relevant methods are in github_login file in resource directory
Go to Github -> Settings -> Developer settings -> OAuth Apps and get your own client id and secret
Populate those values in .env to use github oauth
flask migrate can be used to migrate the database easily
app.py under split folder contains sample code
Command used for migration
flask db migrate
This does the initial migration and creates the relevant files
flask db upgrade
This finally upgrades your database
Command used for downgrade
flask db downgrade
flask mail allows us to integrate mailing service with flask easily
Check mail.py file under split directory which contains simple example to use flask mail
msg= Message("Confirm Email",recipients=[email])
link = url_for("token_verify",token=tok,_external=True)
msg.body = "Verify email address by clicking here {}".format(link)
mail.send(msg)
This is used for user confirmation during signup
Payments using stripe is integrated within the app
Create an account in stripe and generate stripe api keys and add the secret key in .env
Test token to make payements is generated using the function
token=stripe.Token.create(
card={
"number": "4242424242424242",
"exp_month": 9,
"exp_year": 2021,
"cvc": "314",
}, )
The values given here are all sample values
For performing Indian payments we have to provide the following parameters
stripe.Charge.create(
amount=self.amount,
currency=CURRENCY,
description=self.description,
source=token["id"],
shipping={
'name': "John",
'address': {
'line1': '510 Townsend St',
'postal_code': '98140',
'city': 'Kolkata',
'state': 'WB',
'country': 'India',
}
})
If you check you payments page in stripe dashboard you should receive fake payments
When contributing to this repository, please first discuss the change you wish to make via issue, email, or any other method with the owners of this repository before making a change.
If anyone wants to take up an issuse they are free to do so .
development: If you are making a contribution make sure to send your Pull Request to this branch . All developments goes in this branch.
master: After significant features/bug-fixes are accumulated in development branch we merge it with the master branch.