All pastes #974755 Raw Edit

chartjes

public python v1 · immutable
·published 2008-04-07 00:50 UTC
rendered paste body
Form class StadiumForm(forms.Form):    location = forms.BooleanField()    def __init__(self, locId, *args, **kwargs):        super(StadiumForm, self).__init__(*args, **kwargs)        self.fields['location'].choices = locIdviewsdef search_results(request):    if request.method == 'POST':        FORMS_COUNT = 5        date_form = DateForm(request.POST)        team_forms = [TeamForm(request.POST, prefix="team%d" %(x)) for x in range(FORMS_COUNT)]        showResults = True        stadium_forms = []        if date_form.is_valid():            games = False                        for tf in team_forms:                if tf.is_valid():                    searchTeam = Team.objects.get(name = tf.cleaned_data['name'])                                        # Using a neat trick to chain together multiple filter sets because we don't always                    # know how many teams we will be searching for                    if games == False:                        if tf.cleaned_data['homeOrAway'] == u'Home':                            games = Game.objects.filter(home_team = searchTeam, game_date__gte = date_form.cleaned_data['startDate']).order_by('game_date', 'start_time') & Game.objects.filter(home_team = searchTeam, game_date__lte = date_form.cleaned_data['endDate']).order_by('game_date', 'start_time')                        else:                            games = Game.objects.filter(away_team = searchTeam, game_date__gte = date_form.cleaned_data['startDate']).order_by('game_date', 'start_time') & Game.objects.filter(away_team = searchTeam, game_date__lte = date_form.cleaned_data['endDate']).order_by('game_date', 'start_time')                    else:                        if tf.cleaned_data['homeOrAway'] == u'Home':                            games = Game.objects.filter(home_team = searchTeam, game_date__gte = date_form.cleaned_data['startDate']).order_by('game_date', 'start_time') & Game.objects.filter(home_team = searchTeam, game_date__lte = date_form.cleaned_data['endDate']).order_by('game_date', 'start_time') | games                        else:                            games = Game.objects.filter(away_team = searchTeam, game_date__gte = date_form.cleaned_data['startDate']).order_by('game_date', 'start_time') & Game.objects.filter(away_team = searchTeam, game_date__lte = date_form.cleaned_data['endDate']).order_by('game_date', 'start_time') | games                       for game in games:                stadium_forms.insert(int(game.location.id), StadiumForm(int(game.location.id), prefix=int(game.location.id)))        else:            games = 'Hoopa'                return render_to_response('search/results.html', {'games': games, 'showResults': showResults, 'stadium_forms': stadium_forms})    else:        return HttpResponseRedirect('/')templateThis is where the search results would go<br /><br />{% if showResults %}<form id = 'mapForm' action="/map" method = "post">    <table>        <tr>            <th>Date</th>            <th>Game</th>        </tr>    {% if games %}        {% for game in games %}            <tr>                <td>                    {% ifchanged game.game_date %}                    {{ game.game_date }}                    {% endifchanged %}                </td>                <td align='right'>{{game.away_team}}</td><td> @ </td><td align='left'>{{game.home_team}}</td>                <td>This is where I want to display the StadiumForm instance</td>                <td>Buy tickets</td>            </tr>        {% endfor %}    {% else %}      <tr>          <td colspan = 5>No ganes found in your search, please go back and refine your search</td>      </tr>      {% endif %}      <tr>          <td colspan=5><input type='submit' value = 'Show Map'></td>      </tr>    </table></form>{% else %}	You have too many games, please reduce your search parameters{% endif %}