def update_loyality(): """Loyality update. loyality_type and loyality_param required""" data = get_request_data(request, cls=LoyalityJSONDecoder) host_uid = get_current_host_id() if not host_uid: return jsonify({'message': "Please log in as owner"}), HTTP_403_FORBIDDEN loyality_type, loyality_param = int( data.get(LOYALITY_TYPE)), data.get(LOYALITY_PARAM) loyality_time = data.get(LOYALITY_TIME_PARAM) and int( data[LOYALITY_TIME_PARAM]) loyality_burn = NO_BURN if data.get(LOYALITY_BURN_PARAM) is None else int( data[LOYALITY_BURN_PARAM]) if not Host.check_loyality(loyality_type, loyality_param, loyality_time, loyality_burn): return jsonify({'message': "Loyality is wrong"}), HTTP_400_BAD_REQUEST host = Host(uid=host_uid) if current_user.uid != host.owner_uid: return jsonify({'message': "You are not owner of this host"}), HTTP_403_FORBIDDEN offer = unicode(data[OFFER]) if data.get(OFFER) else None host.change_loyality(loyality_type, loyality_param, loyality_time, loyality_burn, offer=offer) return jsonify(SUCCESS)
def checking(): data = get_request_data(request, cls=LoyalityJSONDecoder) host_uid = get_current_host_id() if not host_uid: return jsonify({'message': "You need to be a staff"}), HTTP_403_FORBIDDEN user_uid = data.get('user_id') if not user_uid: return jsonify({'message': "No user_id provided"}), HTTP_400_BAD_REQUEST score_update = data['score'] if data.get('score') else None if not score_update or not (isinstance(score_update, int) or isinstance(score_update, float)): return jsonify({'message': "Score value is invalid"}), HTTP_400_BAD_REQUEST host = Host(uid=host_uid) if host.uid is None: return jsonify({'message': "No such host"}), HTTP_404_NOT_FOUND if not host.check_loyality(host.loyality_type, host.loyality_param, host.loyality_time_param, host.loyality_burn_param): return jsonify({ 'code': 2, 'message': "Loyality of the host is not set" }) if current_user.uid not in host.staff_uids: return jsonify({'message': "You are not a staff of this place" }), HTTP_403_FORBIDDEN user = User(uid=user_uid) if user.login is None: return jsonify({'message': "No such user"}), HTTP_404_NOT_FOUND score = Score(host.uid, user.uid) return f(host, user, score, score_update)
def get_hosts(): data = get_request_data(request) offset = 0 if data.get('offset'): offset = int(data['offset']) query = data.get('query') client_id = session['user_id'] client = User(uid=client_id) hosts = [ { 'host_id': id, 'title': host.get(TITLE), 'description': host.get(DESCRIPTION), # dynamic offer 'offer': host.get(OFFER), 'address': host.get(ADDRESS), 'latitude': host.get(LATITUDE), 'longitude': host.get(LONGITUDE), 'time_open': host.get(TIME_OPEN), 'time_close': host.get(TIME_CLOSE), 'profile_image': host.get(LOGO), 'points': host.get('score'), 'loyality_type': int(host[LOYALITY_TYPE]), 'loyality_param': host.get(LOYALITY_PARAM) if host.get(LOYALITY_TYPE) in {CUP_LOYALITY, PERCENT_LOYALITY} else None, } for id, host in client.get_list(offset, query=query).items() ] return jsonify({'code': 0, 'hosts': hosts})
def get_host(): host_id = get_request_data(request).get('host_id') if not host_id: return jsonify({'message': "No host id provided"}), HTTP_400_BAD_REQUEST host = Host(uid=host_id) # 404 if there is a host with no title in db. No unnamed hosts allowed. response = host.to_dict() if response is None: return jsonify({'message': "No such host in db"}), HTTP_404_NOT_FOUND score = Score(host_id, session['user_id']) response.update({'score': score.score}) return jsonify(response)
def get_client_score(): data = get_request_data(request) client_id = data.get('client_id') if client_id is None: return jsonify({'message': "client_id required"}), HTTP_400_BAD_REQUEST host_id = session.get('host_id') if host_id is None: return jsonify({'message': "Please login as a staff"}), HTTP_403_FORBIDDEN score = Score(host_id, client_id).score if score is None: return jsonify({'message': "No host with this id"}), HTTP_404_NOT_FOUND return jsonify({'code': 0, 'points': score})
def get_info(): """If host_id provided returns that host info elif host_id in session returns your host info else 400""" if not session.get('host_id'): session['host_id'] = current_user.workplace_uid host_id = get_request_data(request).get('host_id') or session.get( 'host_id') if not host_id: return jsonify({'message': "No host id provided"}), HTTP_400_BAD_REQUEST host = Host(uid=host_id) # 404 if there is a host with no title in db. No unnamed hosts allowed. response = host.to_dict() if response is None: return jsonify({'message': "No such host in db"}), HTTP_404_NOT_FOUND return jsonify(response)
def create_host(): """Required: title""" data = get_request_data(request) if not data.get(TITLE): return jsonify(HOST_CREATION_FAILED), HTTP_400_BAD_REQUEST data[OWNER_UID] = current_user.uid owner = User(uid=current_user.uid) if owner.workplace_uid is not None: return jsonify({'message': "Please retire first"}), HTTP_403_FORBIDDEN host = Host(data) host_uid = host.save() if host_uid is None: return jsonify({ 'message': "Host with this title (and owner) already exists" }), HTTP_409_CONFLICT owner.workplace_uid = session['host_id'] = host_uid owner.save() return jsonify({'code': 0, 'host_id': host_uid, 'message': 'OK'})
def retire(): """worker_id required""" host_uid = get_current_host_id() if host_uid is None: return jsonify({'message': "You need to be an owner"}), HTTP_403_FORBIDDEN data = get_request_data(request) worker_uid = data.get('worker_id') if not worker_uid or not ObjectId.is_valid(worker_uid): return jsonify({'message': "worker_id required"}), HTTP_400_BAD_REQUEST host = Host(uid=host_uid) if current_user.uid != host.owner_uid: return jsonify({'message': "You are not owner of this host"}), HTTP_403_FORBIDDEN if host.owner_uid == ObjectId(worker_uid): return jsonify({'message': "You can't retire yourself"}), HTTP_409_CONFLICT if ObjectId(worker_uid) not in host.staff_uids: return jsonify({'message': "No such worker"}), HTTP_404_NOT_FOUND worker_uid = host.retire(worker_uid) User.retire(worker_uid) return jsonify(SUCCESS)
def update_host(): """All fields updated at a time""" host_uid = get_current_host_id() if not host_uid: return jsonify({'message': "Not logged in"}), HTTP_403_FORBIDDEN data = get_request_data(request) if not data.get(TITLE): return jsonify({'message': "Title required"}), HTTP_400_BAD_REQUEST host = Host(uid=host_uid) if current_user.uid != host.owner_uid: return jsonify({'message': "You are not this host"}), HTTP_403_FORBIDDEN host.title = data[TITLE] host.description = data.get(DESCRIPTION) host.address = data.get(ADDRESS) host.latitude = data.get(LATITUDE) host.longitude = data.get(LONGITUDE) host.time_open = Host.parse_time(data.get(TIME_OPEN)) host.time_close = Host.parse_time(data.get(TIME_CLOSE)) result_uid = host.save() if result_uid is None: return jsonify({'message': "Update failed"}), HTTP_409_CONFLICT return jsonify({'code': 0, 'message': "OK"})
def hire(): """worker_id required""" host_uid = get_current_host_id() if host_uid is None: return jsonify({'message': "You need to be an owner"}), HTTP_403_FORBIDDEN host_uid = ObjectId(host_uid) data = get_request_data(request) worker_uid = data.get('worker_id') if worker_uid is None or not ObjectId.is_valid(worker_uid): return jsonify({'message': "worker_id required"}), HTTP_400_BAD_REQUEST worker = User(uid=worker_uid) if worker.login is None: return jsonify({'message': "No such user"}), HTTP_404_NOT_FOUND if worker.workplace_uid is not None: return jsonify({'message': "This user is occupied"}), HTTP_409_CONFLICT host = Host(uid=host_uid) if current_user.uid != host.owner_uid: return jsonify({'message': "You are not owner of this host"}), HTTP_403_FORBIDDEN host.hire(worker_uid) worker.workplace_uid = host_uid worker.save() return jsonify(SUCCESS)
def _get_creds(request): data = get_request_data(request) return data.get('login', None), data.get('password', None)