def change_schedule(group_id, activity_id, start_time, end_time, authoriser_token): ''' change scheduled time of an activity ''' if not group.is_leader_of_group(group_id, authoriser_token): raise AccessError('Only group owner can change schedule.') data = database.get_schedule(group_id) new_start = convert_str_to_datetime(start_time) new_end = convert_str_to_datetime(end_time) if new_end - timedelta(minutes=15) < new_start: raise ValueError("Each activity must be at least 15 minutes") for line in data: (a_id, a_name, a_start, a_end, a_type) = line curr_start = convert_time_to_datetime(a_start) curr_end = convert_time_to_datetime(a_end) if a_id == activity_id: continue if (new_start >= curr_start) and (new_start < curr_end): raise ValueError( "Chosen time is conflict with other activities in the schedule" ) elif (new_end > curr_start) and (new_end <= curr_end): raise ValueError( "Chosen time is conflict with other activities in the schedule" ) database.change_schedule(activity_id, start_time, end_time)
def unvote(activity_id, access_token): ''' remove user's vote to an activity ''' email = user.get_email_by_token(access_token) if database.exist_vote(activity_id, email): database.remove_vote(activity_id, email) else: raise ValueError('User has not voted for this activity')
def is_leader_of_group(group_id, access_token): ''' check if the user with given token is the leader of this group ''' email = user.get_email_by_token(access_token) leader_email = database.find_group_leader(group_id) if not leader_email: raise ValueError(f"Group '{group_id}' does not exist") return email == leader_email
def vote(activity_id, access_token): ''' add user's vote to an activity ''' email = user.get_email_by_token(access_token) if database.exist_vote(activity_id, email): raise ValueError('User has voted for this activity') else: database.add_vote(activity_id, email)
def get_location(group_id): ''' get trip location of the group ''' result = {} if not database.exist_group(group_id): raise ValueError(f"Group '{group_id}' does not exist") result['location'] = database.get_group_location(group_id) return result
def create_group(): ''' create a new group ''' body = request.get_json() leader_token = body['token'] group_name = body['group_name'] trip_date = body['trip_date'] location = body['location'] if not (re.match('^[a-zA-Z0-9\-\ ]*$', group_name)): raise ValueError('Invalid group name. \ Group name may contain alphanumeric characters, spaces and hyphens only.' ) try: date_format_check = datetime.datetime.strptime(trip_date, '%Y-%m-%d') except Exception: raise ValueError('Trip date must be in ISO format') group.create_group(group_name, leader_token, trip_date, location) return dumps({})
def add_activity_to_database(group_id, google_places_id, activity_name, rating, photo_reference, category, activity_type): if database.exist_activity(group_id, google_places_id): raise ValueError('Activity has been nominated') photo_content = database.get_photo(photo_reference) if not photo_content: photo_content = place.get_photo_content(photo_reference, 500) database.add_photo(photo_reference, photo_content) activity_id = database.add_activity(group_id, google_places_id, activity_name, rating, photo_reference, category, activity_type) return activity_id
def create_group(group_name, leader_token, trip_date, location): ''' create a group ''' group_id = group_name + '_' + trip_date if database.exist_group(group_id): raise ValueError('Group id not unique') leader_email = database.find_email_by_token(leader_token) if not leader_email: raise AuthorizationError('Invalid access token') database.add_group(leader_email, group_id, location)
def get_main_activity_names(group_id): ''' get activity names of all nominated activities ''' if not database.exist_group(group_id): raise ValueError(f"Group '{group_id}' does not exist") main_activities = [] data = database.get_main_activity_names(group_id) for line in data: (activity_name,) = line main_activities.append(activity_name) return main_activities
def add_schedule(group_id, activity_id, authoriser_token): ''' add activity to schedule ''' if not group.is_leader_of_group(group_id, authoriser_token): raise AccessError('Only group owner can add activity to schedule.') data = database.get_schedule(group_id) if len(data) == 0: database.add_schedule(activity_id, '07:00:00', '08:00:00') return (a_id, a_name, earliest, end_time, a_type) = data[0] (a_id, a_name, start_time, latest, a_type) = data[-1] earliest_time = convert_time_to_datetime(earliest) latest_time = convert_time_to_datetime(latest) start_time = None end_time = None if latest_time <= convert_str_to_datetime('19:00:00'): start_time = convert_datetime_to_str(latest_time + timedelta(hours=1)) end_time = convert_datetime_to_str(latest_time + timedelta(hours=2)) elif earliest_time >= convert_str_to_datetime('09:00:00'): start_time = convert_datetime_to_str(latest_time - timedelta(hours=2)) end_time = convert_datetime_to_str(latest_time - timedelta(hours=1)) elif earliest_time >= convert_str_to_datetime('07:30:00'): start_time = '07:00:00' if earliest_time < convert_str_to_datetime('08:00:00'): end_time = '08:00:00' else: end_time = convert_datetime_to_str(earliest_time) elif latest_time <= convert_str_to_datetime('20:30:00'): if latest_time <= convert_str_to_datetime('20:00:00'): start_time = '20:00:00' else: start_time = convert_datetime_to_str(latest_time) end_time = '21:00:00' else: for i in range(0, len(data) - 1): (a_id, a_name, curr_start, curr_end, a_type) = data[i] (a_id, a_name, next_start, next_end, a_type) = data[i + 1] curr_end_time = convert_time_to_datetime(curr_end) next_start_time = convert_time_to_datetime(next_start) if curr_end_time + timedelta(hours == 1) <= next_start_time: start_time = convert_datetime_to_str(curr_end_time) end_time = convert_datetime_to_str(curr_end_time + timedelta(hours == 1)) break elif curr_end_time + timedelta(minutes=15) <= next_start_time: start_time = convert_datetime_to_str(curr_end_time) end_time = convert_datetime_to_str(next_start_time) break if not start_time or not end_time: raise ValueError('Failed to add activity to a full schedule') database.add_schedule(activity_id, start_time, end_time) return
def get_nominations_from_database(group_id): ''' get basic information of nominated activities of given group from database ''' if not database.exist_group(group_id): raise ValueError(f"Group '{group_id}' does not exist") activities = [] for activity_info in database.get_nominations(group_id): (activity_id, activity_name, rating, photo_reference, category) = activity_info activities.append( { 'activity_id': activity_id, 'activity_name': activity_name, 'rating': rating, 'photo_reference': photo_reference, 'category': category } ) return activities
def get_activity_details(activity_name): ''' get all information needed for activity details page of an activity ''' response = place.search_place(activity_name) if len(response['candidates']) == 0: raise ValueError(f"Location '{activity_name}' not found") place_info = response['candidates'][0] place_name = place_info.get('name') category = 'miscellaneous' if 'types' in place_info: category = determine_category(place_info.get('types')) google_places_id = place_info.get('place_id') result = place.search_details(google_places_id) address = result.get('formatted_address') phone_number = result.get('formatted_phone_number') rating = None if 'rating' in result: rating = int(result.get('rating')) website = result.get('website') photos = [] if 'photos' in result: photo_references = get_photo_references(result.get('photos')) opening_hours = None if 'opening_hours' in result: opening_hours = result['opening_hours']['weekday_text'] reviews = result.get('reviews') return { 'google_places_id': google_places_id, 'place_name': place_name, 'rating': rating, 'photo_references': photo_references, 'address': address, 'opening_hours': opening_hours, 'phone_number': phone_number, 'website': website, 'category': category, 'reviews': reviews }
def nominate(group_id, google_places_id, activity_name, rating, photo_reference, category): ''' nominate an activity ''' if not database.exist_group(group_id): raise ValueError(f"Group '{group_id}' does not exist") activity_id = add_activity_to_database(group_id, google_places_id, activity_name, rating, photo_reference, category, 'main') cache.add_activity(group_id, activity_id, activity_name, rating, photo_reference, category)