<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

# General imports
import re
from django.shortcuts import render, redirect
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from django.views import generic, View
from django.contrib import messages  

# Import Objects
from .models import OldResort, TrailPage
from .forms import UserAddressForm, CompareOrFavoriteForm

# Import get_resort_list function for resort display
from resorts.views.resort_list import get_resort_list

def index(request):
    """ Home page for Skidom.

    On a GET request: Displays form for getting user trip details. 
    If user logged in and has favorite resorts, displays information
    about the five of them with the most 24h snow.
    Else, displays 5 resorts with the most 24h snow.

    On a POST request: Processes user trip detail form.
    Renders matching resorts and information to comparison template.

    Args:
        request: Page request.

    Returns:
        render: Renders valid resorts and info to comparison template
                if successful. Else, reloads the page.
    
    """
    resorts_list = OldResort.objects.all() 

    if request.method == 'POST':
        form = UserAddressForm(request.POST, pass_type = request.POST['pass_type'], starting_from = request.POST['user_address'])

        if form.is_valid():
            if form.cleaned_data['user_address'] not in ["", "Let\'s go!"]:
                resorts_list = process_form(form)
                return render(request, 'resorthub/compare_options.html', {'resorts_list': resorts_list})

            else:
                messages.warning(request, "Please enter valid address!")
                return redirect('/resorthub/')

    else:
        header_message = "Where we\'d ski this weekend:"

        if request.user.is_authenticated():
            address = request.user.address
            pass_type = request.user.pass_type            
            if len(request.user.favorite_resorts.all()) > 0:
                header_message = "What\'s up with your favorite resorts:"
                resorts_list = request.user.favorite_resorts.all()

        else:
            address = 'Let\'s go!'
            pass_type = "NON"

        resorts_list = get_resort_list(resorts_list, order_on = 'snow_in_past_24h')
        form = UserAddressForm(pass_type = pass_type, starting_from=address)
        return render(request, 'resorthub/index.html', {'form': form, 'header_message': header_message, 'resorts_list': resorts_list})


def process_form(form):
    """ Processes trip information form to get resort list for display.

    Args:
        form (Form): Posted trip information form <commit_msg>Added error and redirect if no resorts matching criteria found.<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

# General imports
import re
from django.shortcuts import render, redirect
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from django.views import generic, View
from django.contrib import messages  

# Import Objects
from .models import OldResort, TrailPage
from .forms import UserAddressForm, CompareOrFavoriteForm

# Import get_resort_list function for resort display
from resorts.views.resort_list import get_resort_list

def index(request):
    """ Home page for Skidom.

    On a GET request: Displays form for getting user trip details. 
    If user logged in and has favorite resorts, displays information
    about the five of them with the most 24h snow.
    Else, displays 5 resorts with the most 24h snow.

    On a POST request: Processes user trip detail form.
    Renders matching resorts and information to comparison template.

    Args:
        request: Page request.

    Returns:
        render: Renders valid resorts and info to comparison template
                if successful. Else, reloads the page.
    
    """
    resorts_list = OldResort.objects.all() 

    if request.method == 'POST':
        form = UserAddressForm(request.POST, pass_type = request.POST['pass_type'], starting_from = request.POST['user_address'])

        if form.is_valid():
            if form.cleaned_data['user_address'] not in ["", "Let\'s go!"]:
                resorts_list = process_form(form)
                if resorts_list:
                    return render(request, 'resorthub/compare_options.html', {'resorts_list': resorts_list})
                else:
                    messages.warning(request, "No resorts matching criteria found. Please try again!")
                    return redirect('/resorthub/')
                

            else:
                messages.warning(request, "Please enter valid address!")
                return redirect('/resorthub/')

    else:
        header_message = "Where we\'d ski this weekend:"

        if request.user.is_authenticated():
            address = request.user.address
            pass_type = <PASSWORD>            
            if len(request.user.favorite_resorts.all()) > 0:
                header_message = "What\'s up with your favorite resorts:"
                resorts_list = request.user.favorite_resorts.all()

        else:
            address = 'Let\'s go!'
            pass_type = "<PASSWORD>"

        resorts_list = get_resort_list(resorts_list, order_on = 'snow_in_past_24h')
        form = UserAddressForm(pass_type = pass_type, starting_from=address)
        return render(request, 'resorthub/index.html', {'form': form, 'header_message': header_message, 'resorts_list': resorts_list})


def process_form(form):
    """ Processes trip information form to get resort list for display.

    Args:
        form (Form): Posted trip information form 
{'fraction_non_alphanumeric': 0.08311552535284893, 'fraction_numerical': 0.0031364349189754314, 'mean_word_length': 2.7516339869281046, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2596673', 'n_tokens_mistral': 1589, 'n_tokens_neox': 1487, 'n_words': 503}