This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
187 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
from app.config import app, jwt | ||
from app.config import app, jwt, socketio | ||
from app.config import db | ||
from app.config import scheduler | ||
from app.controller import * | ||
from app.sockets import * | ||
from app.jobs import * | ||
from app.api import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from functools import wraps | ||
from flask import request | ||
|
||
from flask_jwt_extended import verify_jwt_in_request | ||
from flask_socketio import disconnect | ||
|
||
|
||
def socket_jwt_required( | ||
optional: bool = False, | ||
fresh: bool = False, | ||
refresh: bool = False, | ||
): | ||
def wrapper(fn): | ||
@wraps(fn) | ||
def decorator(*args, **kwargs): | ||
try: | ||
verify_jwt_in_request(optional, fresh, refresh) | ||
except: | ||
disconnect() | ||
return | ||
return fn(*args, **kwargs) | ||
|
||
return decorator | ||
|
||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from marshmallow.exceptions import ValidationError | ||
from app.errors import InvalidUsage | ||
from flask import request | ||
from functools import wraps | ||
|
||
|
||
def validate_socket_args(schema_cls): | ||
def validate(func): | ||
@wraps(func) | ||
def func_wrapper(*args, **kwargs): | ||
if not schema_cls: | ||
raise Exception("Invalid usage. Schema class missing") | ||
|
||
try: | ||
arguments = schema_cls().load(args[0]) | ||
except ValidationError as exc: | ||
raise InvalidUsage('{}'.format(exc)) | ||
|
||
return func(arguments, **kwargs) | ||
|
||
return func_wrapper | ||
|
||
return validate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .shoppinglist_socket import * | ||
from .connection_socket import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from flask_jwt_extended import current_user | ||
from flask_socketio import join_room | ||
|
||
from app.helpers import socket_jwt_required | ||
from app import socketio | ||
|
||
|
||
@socketio.on('connect') | ||
@socket_jwt_required() | ||
def on_connect(): | ||
for household in current_user.households: | ||
join_room(household.household_id) | ||
|
||
|
||
@socketio.on('reconnect') | ||
@socket_jwt_required() | ||
def on_reconnect(): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from marshmallow import Schema, fields | ||
|
||
|
||
class shoppinglist_item_add(Schema): | ||
shoppinglist_id = fields.Integer(required=True) | ||
name = fields.String( | ||
required=True | ||
) | ||
description = fields.String() | ||
|
||
class shoppinglist_item_remove(Schema): | ||
shoppinglist_id = fields.Integer(required=True) | ||
item_id = fields.Integer(required=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from flask_jwt_extended import current_user | ||
from flask_socketio import emit | ||
from app.controller.shoppinglist.shoppinglist_controller import removeShoppinglistItem | ||
from app.errors import NotFoundRequest | ||
|
||
from app.helpers import socket_jwt_required, validate_socket_args | ||
from app.models import Shoppinglist, Item, ShoppinglistItems, History | ||
from app import socketio | ||
from .schemas import shoppinglist_item_add, shoppinglist_item_remove | ||
|
||
|
||
@socketio.on('shoppinglist_item:add') | ||
@socket_jwt_required() | ||
@validate_socket_args(shoppinglist_item_add) | ||
def on_add(args): | ||
shoppinglist = Shoppinglist.find_by_id(args['shoppinglist_id']) | ||
if not shoppinglist: | ||
raise NotFoundRequest() | ||
shoppinglist.checkAuthorized() | ||
|
||
item = Item.find_by_name(shoppinglist.household_id, args['name']) | ||
if not item: | ||
item = Item.create_by_name(shoppinglist.household_id, args['name']) | ||
|
||
con = ShoppinglistItems.find_by_ids(shoppinglist.id, item.id) | ||
if not con: | ||
description = args['description'] if 'description' in args else '' | ||
con = ShoppinglistItems(description=description) | ||
con.created_by = current_user.id | ||
con.item = item | ||
con.shoppinglist = shoppinglist | ||
con.save() | ||
|
||
History.create_added(shoppinglist, item, description) | ||
|
||
emit("shoppinglist_item:add", { | ||
"item": con.obj_to_item_dict(), | ||
"shoppinglist": shoppinglist.obj_to_dict() | ||
}, to=shoppinglist.household_id) | ||
|
||
|
||
@socketio.on('shoppinglist_item:remove') | ||
@socket_jwt_required() | ||
@validate_socket_args(shoppinglist_item_remove) | ||
def on_remove(args): | ||
shoppinglist = Shoppinglist.find_by_id(args['shoppinglist_id']) | ||
if not shoppinglist: | ||
raise NotFoundRequest() | ||
shoppinglist.checkAuthorized() | ||
|
||
con = removeShoppinglistItem(shoppinglist, args['item_id']) | ||
if con: | ||
emit('shoppinglist_item:remove', { | ||
"item": con.obj_to_item_dict(), | ||
"shoppinglist": shoppinglist.obj_to_dict() | ||
}, to=shoppinglist.household_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
from app import app | ||
import gevent.monkey | ||
gevent.monkey.patch_all() | ||
|
||
from app import app, socketio | ||
import os | ||
|
||
from app.config import UPLOAD_FOLDER | ||
|
||
if __name__ == "__main__": | ||
if not os.path.exists(UPLOAD_FOLDER): | ||
os.makedirs(UPLOAD_FOLDER) | ||
app.run(debug=True) | ||
socketio.run(app, debug=True) |