def location_search_form(request): return render( request, 'form_pages/search_by_location.html', { 'search_form': EstablishmentSearchForm, 'location_search_form': EstablishmentSearchByLocationForm, 'mobile': is_mobile(request) })
def browse(request, type_filter): # get establishments to list based on user selection if type_filter == 'all': list_title = 'All:' establishments = Establishment.objects.all().order_by('name') elif type_filter == 'breweries': list_title = 'Breweries:' establishments = Establishment.objects.filter( type=Establishment.BREWERY).order_by('name') elif type_filter == 'wineries': list_title = 'Wineries:' establishments = Establishment.objects.filter( type=Establishment.WINERY).order_by('name') elif type_filter == 'distilleries': list_title = 'Distilleries:' establishments = Establishment.objects.filter( type=Establishment.DISTILLERY).order_by('name') elif type_filter == 'cideries': list_title = 'Cideries:' establishments = Establishment.objects.filter( type=Establishment.CIDERY).order_by('name') else: return redirect( 'home') # redirect home if type_filter is something unexpected return render( request, 'browse_pages/list.html', { 'establishments': establishments, 'list_title': list_title, 'search_form': EstablishmentSearchForm, 'mobile': is_mobile(request) })
def user_profile(request, username): user = User.objects.get_by_natural_key(username) user_data = UserData.objects.get(user=user) places_visited = user_data.user_establishments.all().order_by('name') drinks_added = Drink.objects.filter(user=user).order_by('name') drinks_drunk = user_data.user_drinks.all().order_by('name') # percentages of each category the user has visited visited_percents = { 'all': f'{percent_visited(user):.0f}', # ".0f" rounds to whole percent 'breweries': f'{percent_visited(user, Establishment.BREWERY):.0f}', 'wineries': f'{percent_visited(user, Establishment.WINERY):.0f}', 'distilleries': f'{percent_visited(user, Establishment.DISTILLERY):.0f}', 'cideries': f'{percent_visited(user, Establishment.CIDERY):.0f}' } return render( request, 'account_pages/user_profile.html', { 'user': user, 'search_form': EstablishmentSearchForm, 'places_visited': places_visited, 'visited_percents': visited_percents, 'drinks_drunk': drinks_drunk, 'drinks_added': drinks_added, 'focus_lat': MINNESOTA_COORDINATES[0], 'focus_lon': MINNESOTA_COORDINATES[1], 'zoom_level': WIDE_ZOOM_LEVEL, 'mobile_zoom': WIDE_ZOOM_LEVEL - 1, 'map_establishments': places_visited, 'mapbox_token': mapbox_token, 'mobile': is_mobile(request) })
def new_drink_form(request, establishment_pk): establishment = get_object_or_404(Establishment, pk=establishment_pk) # receive new Drink data from form if request.method == 'POST': drink_form = NewDrinkForm(request.POST, request.FILES) if drink_form.is_valid(): drink = drink_form.save(commit=False) drink.establishment = establishment drink.user = request.user drink.save() return redirect('drink_detail', drink_pk=drink.pk) # show form to receive input else: drink_form = NewDrinkForm() return render( request, 'form_pages/new_drink.html', { 'drink_form': drink_form, 'establishment': establishment, 'search_form': EstablishmentSearchForm, 'mobile': is_mobile(request) })
def establishment_detail(request, establishment_pk): establishment = get_object_or_404(Establishment, pk=establishment_pk) authenticated = request.user.is_authenticated if authenticated: user_data = UserData.objects.get(user=request.user) visited = establishment in user_data.user_establishments.all() else: visited = None drinks = Drink.objects.filter(establishment=establishment).order_by('name') return render( request, 'detail_pages/establishment.html', { 'establishment': establishment, 'drinks': drinks, 'search_form': EstablishmentSearchForm, 'visited': visited, 'authenticated': authenticated, 'mapbox_token': mapbox_token, 'focus_lat': establishment.latitude, 'focus_lon': establishment.longitude, 'zoom_level': LOCATION_ZOOM_LEVEL, 'mobile_zoom': LOCATION_ZOOM_LEVEL - 1, 'map_establishments': [establishment], 'mobile': is_mobile(request) })
def home(request): establishments = Establishment.objects.all() return render( request, 'browse_pages/home.html', { 'search_form': EstablishmentSearchForm, 'focus_lat': MINNESOTA_COORDINATES[0], 'focus_lon': MINNESOTA_COORDINATES[1], 'zoom_level': WIDE_ZOOM_LEVEL, 'mobile_zoom': WIDE_ZOOM_LEVEL - 1, 'map_establishments': establishments, 'mapbox_token': mapbox_token, 'mobile': is_mobile(request) })
def search(request): search_term = request.GET.get('search_term') if search_term: # check that search term is entered list_title = f'Results for "{search_term}":' establishments = Establishment.objects.filter( name__icontains=search_term).order_by('name') return render( request, 'browse_pages/list.html', { 'establishments': establishments, 'list_title': list_title, 'search_form': EstablishmentSearchForm, 'mobile': is_mobile(request) }) return redirect( 'browse', type_filter='all') # show /browse/all if no search term provided
def drink_detail(request, drink_pk): drink = get_object_or_404(Drink, pk=drink_pk) authenticated = request.user.is_authenticated if authenticated: user_data = UserData.objects.get(user=request.user) drunk = drink in user_data.user_drinks.all() else: drunk = None return render( request, 'detail_pages/drink.html', { 'drink': drink, 'search_form': EstablishmentSearchForm, 'drunk': drunk, 'authenticated': authenticated, 'mobile': is_mobile(request) })
def search_by_location(request): location = request.GET.get('location') num_results = int(request.GET.get('num_results')) if location: coordinates = get_coordinates(location) if coordinates: list_title = f'{num_results} results closest to "{location}"' closest = get_closest_establishments(coordinates, num_results) return render( request, 'browse_pages/list.html', { 'establishments': closest, 'list_title': list_title, 'search_form': EstablishmentSearchForm, 'mobile': is_mobile(request) }) return redirect( 'browse', type_filter='all' ) # show /browse/all if no location provided or no coordinates found