def decorated_view(*args, **kwargs): data = request.get_json(silent=True) or {} data = Validator.validate(data, fields, optional) if not data: return bad_request(101, _l('must include all required fields')) request.passed_data = data return fn(*args, **kwargs)
def organization_description(): organization = Organization.get_organization( request.passed_data['organization_id']) if not organization: return bad_request(103, _l('organization is not found')) response = jsonify({'organization': organization.serialize()}) response.status_code = 200 return response
def product_assign_device(): product = Product.get_product(request.passed_data['product_id']) if not product: return bad_request(103, _l('product is not found')) product.assign_tracking_device(request.passed_data['tracking_device_id']) response = jsonify({'product': product.serialize(detailed=True)}) response.status_code = 200 return response
def product_description(): product = Product.get_product(request.passed_data['product_id']) if not product: return bad_request(103, _l('product is not found')) response = jsonify( {'product': product.serialize(detailed=True, include_statuses=True)}) response.status_code = 200 return response
def login_device(): device = TrackingDevice.get_device(request.passed_data['key']) if not device or not device.check_password( request.passed_data['password']): return bad_request(103, _l('device is not found')) token = device.get_token() return jsonify({'token': token})
def product_type_description(): product_type = ProductType.get_product_type( request.passed_data['product_type_id']) if not product_type: return bad_request(103, _l('product type is not found')) response = jsonify({'product_type': product_type.serialize()}) response.status_code = 200 return response
def register_device(): if TrackingDevice.get_device(request.passed_data['key']): return bad_request(102, _l('device is already registered')) TrackingDevice.add_device(request.passed_data) response = jsonify({'success': True}) response.status_code = 200 return response
def change_status(): user = User.get_user(request.passed_data['user_id']) if not user: return bad_request(103, _l('user is not found')) user.disabled = request.passed_data['active'] == False db.session.commit() response = jsonify({'user': user.serialize()}) response.status_code = 200 return response
def organization_change_status(): organization = Organization.get_organization( request.passed_data['organization_id']) if not organization: return bad_request(103, _l('organization is not found')) organization.disabled = request.passed_data['active'] == False db.session.commit() response = jsonify({'organization': organization.serialize()}) response.status_code = 200 return response
def product_tracks(): from_date = None if 'from_date' in request.passed_data and request.passed_data['from_date']: from_date = parser.parse(request.passed_data['from_date']) product = Product.get_product(request.passed_data['product_id']) if not product: return bad_request(103, _l('product is not found')) response = jsonify({'product': product.get_statuses(from_date)}) response.status_code = 200 return response
def assign_device(): device = TrackingDevice.get_device(request.passed_data['key']) if not device or not device.check_password( request.passed_data['password']): return bad_request(103, _l('device is not found')) product = Product.get_product(request.passed_data['product_id']) product.assign_tracking_device(device.id) response = jsonify({'success': True}) response.status_code = 200 return response
def signup(): if User.get_by_email(request.passed_data['email']): return bad_request(102, _l('please use a different email address')) user = User() user.from_dict(request.passed_data, new_user=True) user.role = 'admin' db.session.add(user) db.session.commit() response = jsonify(user.serialize()) response.status_code = 200 return response
def organization_register(): if User.query.filter_by(email=request.passed_data['email']).first(): return bad_request(102, _l('please use a different email address')) request.passed_data.setdefault('role', 'manager') user = User() user.from_dict(request.passed_data, new_user=True) db.session.add(user) db.session.commit() response = jsonify(user.serialize()) response.status_code = 200 return response
def register(): if User.get_by_email(request.passed_data['email']): return bad_request(102, _l('please use a different email address')) request.passed_data.setdefault('role', 'client') user = User() user.from_dict(request.passed_data, new_user=True) db.session.add(user) db.session.commit() response = jsonify(user.serialize()) response.status_code = 200 return response
def login(): user = User.get_by_email(request.passed_data['email']) if not user or not user.check_password(request.passed_data['password']): return bad_request(103, _l('user is not found')) g.current_user = user token = user.get_token() db.session.commit() return jsonify({ 'token': token, 'role': user.role, 'email': user.email, 'organization_id': user.organization_id })
def track_statuses(): tracks = [] for track in request.passed_data['tracks']: track_data = Validator.validate( track, ['value', 'condition_id', 'date_recordered'], []) if not track_data: return bad_request(101, _l('must include all required fields')) tracks.append(track_data) for track in tracks: track.setdefault('tracking_device_id', g.current_device.id) TrackingStatus.add_status(track) response = jsonify({'success': True}) response.status_code = 200 return response