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

Feature Panel - Backend #283

Merged
merged 54 commits into from Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
d09b5fd
add models to db
LiranCaduri Feb 1, 2021
013c6d5
figure out delete.
LiranCaduri Feb 2, 2021
894f234
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
LiranCaduri Feb 2, 2021
cb7947c
first structure of route with middlewere
LiranCaduri Feb 3, 2021
523c01e
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
LiranCaduri Feb 6, 2021
572036e
try middleware
LiranCaduri Feb 7, 2021
397cee3
add middleware filtering requests
LiranCaduri Feb 8, 2021
1da1dda
add: get_user_disabled_features and get_user_enabled_features
LiranCaduri Feb 9, 2021
d160605
modify middleware
LiranCaduri Feb 10, 2021
b51cd21
replace testclient, add on startup event, feature folder, is_feature_…
LiranCaduri Feb 10, 2021
9df777b
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
LiranCaduri Feb 11, 2021
e4a817a
improved is_feature_enabled and middleware code.
LiranCaduri Feb 11, 2021
0514121
add_feature_to_user function
LiranCaduri Feb 11, 2021
c715299
add checking for duplicates in association table
LiranCaduri Feb 13, 2021
6380a8c
renaming some files, and adding on and off routes
LiranCaduri Feb 13, 2021
7f50cc0
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
LiranCaduri Feb 13, 2021
2ea61cb
.example
LiranCaduri Feb 13, 2021
b2b839f
split to internal
LiranCaduri Feb 14, 2021
36af9b7
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
LiranCaduri Feb 14, 2021
8c4929c
add config
LiranCaduri Feb 14, 2021
b999bc6
add tests
LiranCaduri Feb 14, 2021
e97f29b
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
LiranCaduri Feb 16, 2021
6220ad0
access decorator, back to fastapi testclient, fix tests, new document…
LiranCaduri Feb 16, 2021
f6a6630
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
LiranCaduri Feb 16, 2021
0b27713
fix flake8 issue
LiranCaduri Feb 16, 2021
2572153
requested changes
LiranCaduri Feb 18, 2021
10c7d88
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
LiranCaduri Feb 18, 2021
7f0ab27
.example
LiranCaduri Feb 18, 2021
1c1bbe7
requested changes
LiranCaduri Feb 19, 2021
a8e8725
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
LiranCaduri Feb 19, 2021
dfe02c7
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
LiranCaduri Feb 20, 2021
2081329
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
LiranCaduri Feb 20, 2021
107d246
changes
LiranCaduri Feb 20, 2021
182b6be
before cache
LiranCaduri Feb 21, 2021
d6b26a8
remove redundant things
LiranCaduri Feb 21, 2021
4618b06
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
LiranCaduri Feb 21, 2021
c4f6cbe
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
LiranCaduri Feb 21, 2021
3c88d35
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
LiranCaduri Feb 22, 2021
096d6fa
remove redundant things
LiranCaduri Feb 22, 2021
4696df2
pre-commit change some stuff
LiranCaduri Feb 23, 2021
105d253
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
LiranCaduri Feb 23, 2021
dc9fcce
remove blank lines
LiranCaduri Feb 23, 2021
d677ccd
requested changes
LiranCaduri Feb 24, 2021
7712b07
update .gitignore
LiranCaduri Feb 24, 2021
ecd5cb8
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
LiranCaduri Feb 24, 2021
a9f9001
fix semantics
LiranCaduri Feb 24, 2021
5b92634
fix logic
LiranCaduri Feb 24, 2021
572e992
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
LiranCaduri Feb 24, 2021
571fe1a
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
LiranCaduri Feb 24, 2021
36f841b
requested changes
LiranCaduri Feb 25, 2021
c181946
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
LiranCaduri Feb 25, 2021
93b0b02
use pre-commit on google-connect
LiranCaduri Feb 25, 2021
3bd43b2
requested changes
LiranCaduri Feb 25, 2021
69ed31b
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
LiranCaduri Feb 25, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.md
Expand Up @@ -33,6 +33,7 @@
* PureDreamer - Developer
* ShiZinDle - Developer
* YairEn - Developer
* LiranCaduri - Developer

# Special thanks to

Expand Down
23 changes: 23 additions & 0 deletions app/database/models.py
Expand Up @@ -32,6 +32,16 @@
Base: DeclarativeMeta = declarative_base()


class UserFeature(Base):
__tablename__ = "user_feature"

id = Column(Integer, primary_key=True, index=True)
feature_id = Column('feature_id', Integer, ForeignKey('features.id'))
user_id = Column('user_id', Integer, ForeignKey('users.id'))

is_enable = Column(Boolean, default=False)


class User(Base):
__tablename__ = "users"

Expand Down Expand Up @@ -67,6 +77,7 @@ class User(Base):
)
comments = relationship("Comment", back_populates="user")

features = relationship("Feature", secondary=UserFeature.__tablename__)
oauth_credentials = relationship(
"OAuthCredentials",
cascade="all, delete",
Expand All @@ -83,6 +94,18 @@ async def get_by_username(db: Session, username: str) -> User:
return db.query(User).filter(User.username == username).first()


class Feature(Base):
__tablename__ = "features"

id = Column(Integer, primary_key=True, index=True)
name = Column(String, nullable=False)
route = Column(String, nullable=False)
creator = Column(String, nullable=True)
description = Column(String, nullable=False)

users = relationship("User", secondary=UserFeature.__tablename__)


class Event(Base):
__tablename__ = "events"

Expand Down
169 changes: 169 additions & 0 deletions app/internal/features.py
@@ -0,0 +1,169 @@
from fastapi import Depends
from functools import wraps
from starlette.responses import RedirectResponse
LiranCaduri marked this conversation as resolved.
Show resolved Hide resolved
from typing import List

from app.database.models import UserFeature, Feature
from app.dependencies import get_db, SessionLocal
from app.internal.features_index import features
from app.internal.utils import create_model, get_current_user


def feature_access_filter(call_next):

@wraps(call_next)
async def wrapper(*args, **kwargs):
request = kwargs['request']

if request.headers['user-agent'] == 'testclient':
# in case it's a unit test.
return await call_next(*args, **kwargs)

# getting the url route path for matching with the database.
route = '/' + str(request.url).replace(str(request.base_url), '')

# getting access status.
is_enabled = is_access_allowd(route=route)

if is_enabled:
# in case the feature is enabled or access is allowed.
return await call_next(*args, **kwargs)

elif 'referer' not in request.headers:
# in case request come straight from address bar in browser.
return RedirectResponse(url='/')

# in case the feature is disabled or access isn't allowed.
return RedirectResponse(url=request.headers['referer'])

return wrapper


LiranCaduri marked this conversation as resolved.
Show resolved Hide resolved
def create_features_at_startup(session: SessionLocal) -> bool:
for feat in features:
if not is_feature_exists(feature=feat, session=session):
create_feature(**feat, db=session)

return True


def is_association_exists_in_db(form: dict, session: SessionLocal) -> bool:
db_association = session.query(UserFeature).filter_by(
feature_id=form['feature_id'],
user_id=get_current_user(session=session).id
).first()

return db_association is not None


def delete_feature(
feature: Feature, session: SessionLocal = Depends(get_db)
) -> None:
session.query(UserFeature).filter_by(feature_id=feature.id).delete()
session.query(Feature).filter_by(id=feature.id).delete()
session.commit()


def is_feature_exists(feature: dict, session: SessionLocal) -> bool:
LiranCaduri marked this conversation as resolved.
Show resolved Hide resolved
db_feature = session.query(Feature).filter(
(Feature.name == feature['name']) |
(Feature.route == feature['route'])).first()

if db_feature is None:
return False

# Update if found
update_feature(
feature=db_feature, new_feature_obj=feature, session=session)
LiranCaduri marked this conversation as resolved.
Show resolved Hide resolved
return True


LiranCaduri marked this conversation as resolved.
Show resolved Hide resolved
def update_feature(feature: Feature, new_feature_obj: dict,
session: SessionLocal = Depends(get_db)) -> Feature:
feature.name = new_feature_obj['name']
feature.route = new_feature_obj['route']
feature.description = new_feature_obj['description']
feature.creator = new_feature_obj['creator']
session.commit()
return feature


def is_feature_enabled(
feature: Feature, session: SessionLocal = Depends(get_db)
) -> bool:
enabled_features = get_user_enabled_features(session=session)
return any(ef.id == feature.id for ef in enabled_features)


def is_access_allowd(route: str) -> bool:
session = SessionLocal()
user = get_current_user(session=session)
feature = session.query(Feature).filter_by(route=route).first()

if feature is None:
# in case there is no feature exists in the database that match the
# route that gived by to the request.
return True

user_pref = session.query(UserFeature).filter_by(
feature_id=feature.id,
user_id=user.id
).first()

return user_pref is not None and user_pref.is_enable


def create_feature(name: str, route: str,
description: str,
creator: str = None,
db: SessionLocal = Depends()) -> Feature:
"""Creates a feature."""
db = SessionLocal()
return create_model(
db, Feature,
name=name,
route=route,
creator=creator,
description=description,
)


def create_user_feature_association(
db: SessionLocal, feature_id: int, user_id: int, is_enable: bool
) -> UserFeature:
"""Creates an association."""
return create_model(
db, UserFeature,
user_id=user_id,
feature_id=feature_id,
is_enable=is_enable
)


def get_user_enabled_features(session: SessionLocal = Depends(get_db)) -> List:
user = get_current_user(session=session)
enabled = []
user_prefs = session.query(UserFeature).filter_by(user_id=user.id).all()

for pref in user_prefs:
if pref.is_enable:
feature = session.query(Feature).filter_by(
id=pref.feature_id).first()
enabled.append(feature)

return enabled


def get_user_uninstalled_features(session: SessionLocal) -> List:
uninstalled = []
all_features = session.query(Feature).all()

for feat in all_features:
in_enabled = is_feature_enabled(
feature=feat, session=session
)

if not in_enabled:
uninstalled.append(feat)

return uninstalled
51 changes: 51 additions & 0 deletions app/internal/features_index.py
@@ -0,0 +1,51 @@
'''
This file purpose is for developers to add their features to the database
in one convenient place, every time the system loads up it's adding and
updating the features in the features table in the database.

To update a feature, The developer needs to change the name or the route
and let the system load, but not change both at the same time otherwise
it will create junk and unnecessary duplicates.

* IMPORTANT - To enable features panel functionlity the developer must *
* add the feature_access_filter decorator to ALL the feature routes *
* Please see the example below. *

Enjoy and good luck :)
'''

'''
Example to feature stracture:

{
"name": "<feature name - str>",
"route": "/<the route like: /features - str>",
"description": "<description - str>",
"creator": "<creator name or nickname - str>"
}
'''

'''
* IMPORTANT *

Example to decorator placement:

@router.get("/<my-route>")
@feature_access_filter <---- just above def keyword!
def my_cool_feature_route():
....
...
some code.
..
.

'''

features = [
{
"name": "Google Sync",
"route": "/google/sync",
"description": "Sync Google Calendar events with Pylender",
"creator": "Liran Caduri"
},
]
11 changes: 11 additions & 0 deletions app/main.py
Expand Up @@ -15,8 +15,10 @@
SOUNDS_PATH,
STATIC_PATH,
templates,
SessionLocal,
)
from app.internal import daily_quotes, json_data_loader
import app.internal.features as internal_features
from app.internal.languages import set_ui_language
from app.internal.security.ouath2 import auth_exception_handler
from app.routers.salary import routes as salary
Expand Down Expand Up @@ -60,6 +62,7 @@ def create_tables(engine, psql_environment):
email,
event,
export,
features,
four_o_four,
friendview,
google_connect,
Expand Down Expand Up @@ -109,6 +112,7 @@ async def swagger_ui_redirect():
email.router,
event.router,
export.router,
features.router,
four_o_four.router,
friendview.router,
google_connect.router,
Expand All @@ -131,6 +135,13 @@ async def swagger_ui_redirect():
app.include_router(router)


@app.on_event("startup")
async def startup_event():
session = SessionLocal()
internal_features.create_features_at_startup(session=session)
session.close()


# TODO: I add the quote day to the home page
# until the relevant calendar view will be developed.
@app.get("/", include_in_schema=False)
Expand Down
74 changes: 74 additions & 0 deletions app/routers/features.py
@@ -0,0 +1,74 @@
from app.internal.utils import get_current_user
from fastapi import APIRouter, Request, Depends
from typing import List

from app.dependencies import get_db, SessionLocal
from app.database.models import UserFeature, Feature
from app.internal.features import (
create_user_feature_association,
is_association_exists_in_db,
)

router = APIRouter(
prefix="/features",
tags=["features"],
responses={404: {"description": "Not found"}},
)


@router.get('/')
async def index(
request: Request, session: SessionLocal = Depends(get_db)
LiranCaduri marked this conversation as resolved.
Show resolved Hide resolved
) -> List:
LiranCaduri marked this conversation as resolved.
Show resolved Hide resolved
features = session.query(Feature).all()
return features


@router.post('/add')
async def add_feature_to_user(
request: Request, session: SessionLocal = Depends(get_db)
) -> UserFeature:
LiranCaduri marked this conversation as resolved.
Show resolved Hide resolved
form = await request.form()

user = get_current_user(session=session)
feat = session.query(Feature).filter_by(id=form['feature_id']).first()

is_exist = is_association_exists_in_db(form=form, session=session)

if feat is None or is_exist:
# in case there is no feature in the database with that same id
# and or the association is exist
return False

association = create_user_feature_association(
db=session,
feature_id=feat.id,
user_id=user.id,
is_enable=True
)

return session.query(UserFeature).filter_by(id=association.id).first()


@router.post('/delete')
async def delete_user_feature_association(
request: Request,
session: SessionLocal = Depends(get_db)
) -> bool:
form = await request.form()

user = get_current_user(session=session)
LiranCaduri marked this conversation as resolved.
Show resolved Hide resolved
feature_id = form['feature_id']

is_exist = is_association_exists_in_db(form=form, session=session)

if not is_exist:
return False

session.query(UserFeature).filter_by(
feature_id=feature_id,
user_id=user.id
).delete()
session.commit()

return True