Skip to content

Instantly share code, notes, and snippets.

View gajanan0707's full-sized avatar
🤲
Focusing

Gajanan Rajput gajanan0707

🤲
Focusing
View GitHub Profile
@gajanan0707
gajanan0707 / helper.py
Last active October 5, 2022 12:13
FlASK_DEMO_REST/users/helper.py
import os
from flask_mail import Message
from utils.common import TokenGenerator
from server import mail
def send_forgot_password_email(request, user):
"""
It sends an email to the user with a link to reset their password
@gajanan0707
gajanan0707 / routes.py
Last active October 5, 2022 12:10
FLASK_DEMO_REST/user
from flask_restful import Api
from users.views import LoginApi, ForgotPassword, SignUpApi, ResetPassword
def create_authentication_routes(api: Api):
"""Adds resources to the api.
:param api: Flask-RESTful Api Object
"""
api.add_resource(SignUpApi, "/api/auth/register/")
api.add_resource(LoginApi, "/api/auth/login/")
@gajanan0707
gajanan0707 / service.py
Last active October 5, 2022 12:12
FLASK_DEMO_REST/user/service,py
import json
import jwt
import datetime
from server import db
from os import environ
from users.helper import send_forgot_password_email
from users.models import User
from flask_bcrypt import generate_password_hash
from utils.common import generate_response, TokenGenerator
from users.validation import (
@gajanan0707
gajanan0707 / validation.py
Last active October 5, 2022 12:12
FLASK_DEMO_REST/users/validation.py
from marshmallow import Schema, fields, validate
# "This class defines the input schema for the CreateSignup mutation. It requires a username, email,
# and password. The username must be at least 4 characters long, and the password must be at least 6
# characters long."
#
# The input schema is used to validate the input data before it is passed to the mutation
class CreateSignupInputSchema(Schema):
# the 'required' argument ensures the field exists
@gajanan0707
gajanan0707 / common.py
Last active September 15, 2022 14:19
common.py
import os
import jwt
from datetime import datetime, timedelta, timezone
from utils.http_code import HTTP_200_OK, HTTP_201_CREATED
def generate_response(data=None, message=None, status=400):
"""
It takes in a data, message, and status, and returns a dictionary with the data, message, and status
@gajanan0707
gajanan0707 / http_code.py
Last active September 15, 2022 14:26
FLASK_DEMO_REST/utils/http_code.py
"""
Descriptive HTTP status codes, for code readability.
"""
def is_informational(code):
return 100 <= code <= 199
@gajanan0707
gajanan0707 / views.py
Last active October 5, 2022 12:12
FLASK_DEMO_REST/users/views.py
from flask import Response
from flask_restful import Resource
from flask import request, make_response
from users.service import create_user, reset_password_email_send, login_user, reset_password
class SignUpApi(Resource):
@staticmethod
def post() -> Response:
"""
"""App entry point."""
"""Initialize Flask app."""
import os
from flask import Flask
from flask_restful import Api
from flask_sqlalchemy import SQLAlchemy
from flask_mail import Mail
db = SQLAlchemy()
mail = Mail()
"""Data models."""
import datetime
from flask_bcrypt import generate_password_hash, check_password_hash
from flask_sqlalchemy import SQLAlchemy
from server import db
# The User class is a data model for user accounts
class User(db.Model):
"""Data model for user accounts."""
"""Initialize Flask app."""
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
def create_app():
"""Construct the core application."""
app = Flask(__name__, instance_relative_config=False)