|
|
@ -3,28 +3,33 @@ |
|
|
|
|
|
|
|
|
|
|
|
"""Group management.""" |
|
|
|
"""Group management.""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import logging |
|
|
|
|
|
|
|
|
|
|
|
from flask import Blueprint |
|
|
|
from flask import Blueprint |
|
|
|
# from flask import request |
|
|
|
from flask import request |
|
|
|
from flask import jsonify |
|
|
|
from flask import jsonify |
|
|
|
|
|
|
|
|
|
|
|
# from luncho.helpers import ForceJSON |
|
|
|
from sqlalchemy.exc import IntegrityError |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from luncho.helpers import ForceJSON |
|
|
|
from luncho.helpers import JSONError |
|
|
|
from luncho.helpers import JSONError |
|
|
|
|
|
|
|
from luncho.helpers import user_or_error |
|
|
|
|
|
|
|
|
|
|
|
from luncho.server import User |
|
|
|
from luncho.server import User |
|
|
|
# from luncho.server import Group |
|
|
|
from luncho.server import Group |
|
|
|
|
|
|
|
from luncho.server import db |
|
|
|
|
|
|
|
|
|
|
|
groups = Blueprint('groups', __name__) |
|
|
|
groups = Blueprint('groups', __name__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LOG = logging.getLogger('luncho.blueprints.groups') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@groups.route('<token>/', methods=['GET']) |
|
|
|
@groups.route('<token>/', methods=['GET']) |
|
|
|
def user_groups(token): |
|
|
|
def user_groups(token): |
|
|
|
"""Return a list of the groups the user belongs or it's the owner.""" |
|
|
|
"""Return a list of the groups the user belongs or it's the owner.""" |
|
|
|
user = User.query.filter_by(token=token).first() |
|
|
|
(user, error) = user_or_error(token) |
|
|
|
if not user: |
|
|
|
if error: |
|
|
|
return JSONError(404, 'User not found (via token)') |
|
|
|
return error |
|
|
|
|
|
|
|
|
|
|
|
if not user.valid_token(token): |
|
|
|
|
|
|
|
return JSONError(400, 'Invalid token') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
groups = {} |
|
|
|
groups = {} |
|
|
|
for group in user.groups: |
|
|
|
for group in user.groups: |
|
|
@ -34,3 +39,31 @@ def user_groups(token): |
|
|
|
|
|
|
|
|
|
|
|
return jsonify(status='OK', |
|
|
|
return jsonify(status='OK', |
|
|
|
groups=groups.values()) |
|
|
|
groups=groups.values()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@groups.route('<token>/', methods=['PUT']) |
|
|
|
|
|
|
|
@ForceJSON(required=['name']) |
|
|
|
|
|
|
|
def create_group(token): |
|
|
|
|
|
|
|
"""Create a new group belonging to the user.""" |
|
|
|
|
|
|
|
(user, error) = user_or_error(token) |
|
|
|
|
|
|
|
if error: |
|
|
|
|
|
|
|
return error |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LOG.debug('User status: {verified}'.format(verified=user.verified)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not user.verified: |
|
|
|
|
|
|
|
return JSONError(412, 'Account not verified') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
json = request.get_json(force=True) |
|
|
|
|
|
|
|
try: |
|
|
|
|
|
|
|
new_group = Group(name=json['name'], |
|
|
|
|
|
|
|
owner=user.username) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
user.groups.append(new_group) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db.session.add(new_group) |
|
|
|
|
|
|
|
db.session.commit() |
|
|
|
|
|
|
|
except IntegrityError: |
|
|
|
|
|
|
|
return JSONError(500, 'Unknown error') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return jsonify(status='OK', |
|
|
|
|
|
|
|
id=new_group.id) |
|
|
|