Skip to content

Commit d69517b

Browse files
author
Mominur Rahman
authored
Merge pull request #15 from app-generator/restructure
Codebase restructured
2 parents b541ee7 + 5343ec1 commit d69517b

File tree

343 files changed

+304
-28081
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

343 files changed

+304
-28081
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ apps/static/assets/.temp
3333

3434

3535
#migrations
36+
37+
node_modules/

apps/__init__.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,24 @@ def register_blueprints(app):
2222
module = import_module('apps.{}.routes'.format(module_name))
2323
app.register_blueprint(module.blueprint)
2424

25-
from apps.authentication.oauth import github_blueprint
25+
from apps.authentication.oauth import github_blueprint, google_blueprint
2626

2727
def create_app(config):
28-
app = Flask(__name__)
28+
29+
# Contextual
30+
static_prefix = '/static'
31+
templates_dir = os.path.dirname(config.BASE_DIR)
32+
33+
TEMPLATES_FOLDER = os.path.join(templates_dir,'templates')
34+
STATIC_FOLDER = os.path.join(templates_dir,'static')
35+
36+
print(' > TEMPLATES_FOLDER: ' + TEMPLATES_FOLDER)
37+
38+
app = Flask(__name__, static_url_path=static_prefix, template_folder=TEMPLATES_FOLDER, static_folder=STATIC_FOLDER)
39+
2940
app.config.from_object(config)
3041
register_extensions(app)
3142
register_blueprints(app)
3243
app.register_blueprint(github_blueprint, url_prefix="/login")
44+
app.register_blueprint(google_blueprint, url_prefix="/login")
3345
return app

apps/authentication/models.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from flask_login import UserMixin
77

8-
from sqlalchemy.orm import relationship
8+
from sqlalchemy.exc import SQLAlchemyError, IntegrityError
99
from flask_dance.consumer.storage.sqla import OAuthConsumerMixin
1010

1111
from apps import db, login_manager
@@ -26,6 +26,7 @@ class Users(db.Model, UserMixin):
2626
password = db.Column(db.LargeBinary)
2727

2828
oauth_github = db.Column(db.String(100), nullable=True)
29+
oauth_google = db.Column(db.String(100), nullable=True)
2930

3031
def __init__(self, **kwargs):
3132
for property, value in kwargs.items():
@@ -65,7 +66,7 @@ def save(self) -> None:
6566
db.session.rollback()
6667
db.session.close()
6768
error = str(e.__dict__['orig'])
68-
raise InvalidUsage(error, 422)
69+
raise IntegrityError(error, 422)
6970

7071
def delete_from_db(self) -> None:
7172
try:
@@ -75,7 +76,7 @@ def delete_from_db(self) -> None:
7576
db.session.rollback()
7677
db.session.close()
7778
error = str(e.__dict__['orig'])
78-
raise InvalidUsage(error, 422)
79+
raise IntegrityError(error, 422)
7980
return
8081

8182
@login_manager.user_loader

apps/authentication/oauth.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from flask_login import current_user, login_user
99
from flask_dance.consumer import oauth_authorized
1010
from flask_dance.contrib.github import github, make_github_blueprint
11+
from flask_dance.contrib.google import google, make_google_blueprint
1112
from flask_dance.consumer.storage.sqla import SQLAlchemyStorage
1213
from sqlalchemy.orm.exc import NoResultFound
1314
from apps.config import Config
@@ -56,3 +57,53 @@ def github_logged_in(blueprint, token):
5657

5758
login_user(user)
5859

60+
61+
62+
63+
# Google
64+
65+
google_blueprint = make_google_blueprint(
66+
client_id=Config.GOOGLE_ID,
67+
client_secret=Config.GOOGLE_SECRET,
68+
scope=[
69+
"openid",
70+
"https://www.googleapis.com/auth/userinfo.email",
71+
"https://www.googleapis.com/auth/userinfo.profile",
72+
],
73+
storage=SQLAlchemyStorage(
74+
OAuth,
75+
db.session,
76+
user=current_user,
77+
user_required=False,
78+
),
79+
80+
)
81+
82+
@oauth_authorized.connect_via(google_blueprint)
83+
def google_logged_in(blueprint, token):
84+
info = google.get("/oauth2/v1/userinfo")
85+
86+
if info.ok:
87+
account_info = info.json()
88+
username = account_info["given_name"]
89+
email = account_info["email"]
90+
91+
query = Users.query.filter_by(oauth_google=username)
92+
try:
93+
94+
user = query.one()
95+
login_user(user)
96+
97+
except NoResultFound:
98+
# Save to db
99+
user = Users()
100+
user.username = '(google)' + username
101+
user.oauth_google = username
102+
user.email = email
103+
104+
# Save current user
105+
db.session.add(user)
106+
db.session.commit()
107+
108+
login_user(user)
109+

apps/authentication/routes.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
logout_user
1111
)
1212
from flask_dance.contrib.github import github
13+
from flask_dance.contrib.google import google
1314

1415
from apps import db, login_manager
1516
from apps.authentication import blueprint
@@ -31,6 +32,15 @@ def login_github():
3132
return redirect(url_for('home_blueprint.index'))
3233

3334

35+
@blueprint.route("/google")
36+
def login_google():
37+
""" Google login """
38+
if not google.authorized:
39+
return redirect(url_for("google.login"))
40+
41+
res = google.get("/oauth2/v1/userinfo")
42+
return redirect(url_for('home_blueprint.index'))
43+
3444
@blueprint.route('/login', methods=['GET', 'POST'])
3545
def login():
3646
login_form = LoginForm(request.form)
@@ -120,10 +130,12 @@ def logout():
120130

121131
@blueprint.context_processor
122132
def has_github():
123-
if Config.GITHUB_ID and Config.GITHUB_SECRET:
124-
return {'has_github': True}
125-
126-
return {'has_github': False}
133+
return {'has_github': bool(Config.GITHUB_ID) and bool(Config.GITHUB_SECRET)}
134+
135+
@blueprint.context_processor
136+
def has_google():
137+
return {'has_google': bool(Config.GOOGLE_ID) and bool(Config.GOOGLE_SECRET)}
138+
127139

128140
@login_manager.unauthorized_handler
129141
def unauthorized_handler():

apps/config.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,15 @@
44
"""
55

66
import os
7+
from pathlib import Path
78

89
class Config(object):
910

10-
basedir = os.path.abspath(os.path.dirname(__file__))
11-
12-
# for Product model
13-
CURRENCY = { 'usd' : 'usd' , 'eur' : 'eur' }
14-
STATE = { 'completed' : 1 , 'pending' : 2, 'refunded' : 3 }
15-
PAYMENT_TYPE = { 'cc' : 1 , 'paypal' : 2, 'wire' : 3 }
11+
BASE_DIR = Path(__file__).resolve().parent
1612

1713
USERS_ROLES = { 'ADMIN' :1 , 'USER' : 2 }
1814
USERS_STATUS = { 'ACTIVE' :1 , 'SUSPENDED' : 2 }
1915

20-
# Assets Management
21-
ASSETS_ROOT = os.getenv('ASSETS_ROOT', '/static/assets')
22-
2316
# celery
2417
CELERY_BROKER_URL = "redis://localhost:6379"
2518
CELERY_RESULT_BACKEND = "redis://localhost:6379"
@@ -36,7 +29,14 @@ class Config(object):
3629

3730
# Enable/Disable Github Social Login
3831
if GITHUB_ID and GITHUB_SECRET:
39-
SOCIAL_AUTH_GITHUB = True
32+
SOCIAL_AUTH_GITHUB = True
33+
34+
GOOGLE_ID = os.getenv('GOOGLE_ID' , None)
35+
GOOGLE_SECRET = os.getenv('GOOGLE_SECRET', None)
36+
37+
# Enable/Disable Google Social Login
38+
if GOOGLE_ID and GOOGLE_SECRET:
39+
SOCIAL_AUTH_GOOGLE = True
4040

4141
SQLALCHEMY_TRACK_MODIFICATIONS = False
4242

@@ -74,12 +74,15 @@ class Config(object):
7474
if USE_SQLITE:
7575

7676
# This will create a file in <app> FOLDER
77-
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'db.sqlite3')
77+
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(BASE_DIR, 'db.sqlite3')
7878

7979
DYNAMIC_DATATB = {
8080
"products": "apps.models.Product"
8181
}
8282

83+
CDN_DOMAIN = os.getenv('CDN_DOMAIN')
84+
CDN_HTTPS = os.getenv('CDN_HTTPS', True)
85+
8386
class ProductionConfig(Config):
8487
DEBUG = False
8588

apps/db.sqlite3

4 KB
Binary file not shown.

0 commit comments

Comments
 (0)