Kevin Veroneau avatar Kevin Veroneau committed 7c21c72 Draft

Added game list page and filtering.

Comments (0)

Files changed (8)

-'''
-Created on May 10, 2013
+from django import forms
+from gamedb.models import Game
 
-@author: kveroneau
-'''
+class FilterForm(forms.Form):
+    game_type = forms.ChoiceField(choices=Game.TYPE_LIST, required=False)
+    genre = forms.ChoiceField(choices=Game.GENRE_LIST, required=False)
+    style = forms.ChoiceField(choices=Game.STYLE_LIST, required=False)
         return super(Game, self).save(**kwargs)
     def first_screenshot(self):
         return self.screenshots.all()[0].image.url
+    @models.permalink
+    def get_absolute_url(self):
+        return ('game_detail', [self.slug])

gamedb/templates/gamedb/fragments/game_table.html

+{% load ohrrpgce %}<table class="table table-striped">
+  <thead><tr><th>Game Title</th><th>Author</th><th>Type</th><th>Genre</th></tr></thead>
+  <tbody>
+    {% for game in game_list %}
+    <tr><td>{{game|link}}</td><td>{{game.owner}}</td><td><a href="?game_type={{game.game_type}}">{{game.get_game_type_display}}</a></td><td><a href="?genre={{game.genre}}">{{game.get_genre_display}}</a></td></tr>
+    {% endfor %}
+  </tbody>
+</table>

gamedb/templates/gamedb/game_detail.html

     <div class="alert alert-info">Game Title</div>
     {{game.title}}
     <div class="alert alert-info">Game Type</div>
-    {{game.get_game_type_display}}
+    <a href="{% url 'game_list' %}?game_type={{game.game_type}}">{{game.get_game_type_display}}</a>
     <div class="alert alert-info">Game Genre</div>
-    {{game.get_genre_display}}
+    <a href="{% url 'game_list' %}?genre={{game.genre}}">{{game.get_genre_display}}</a>
     <div class="alert alert-info">Game Style</div>
-    {{game.get_style_display}}
+    <a href="{% url 'game_list' %}?style={{game.style}}">{{game.get_style_display}}</a>
     <div class="alert alert-info">Engine Version used</div>
     {{game.engine}}
     <div class="alert alert-info">Download this game</div>

gamedb/templates/gamedb/game_list.html

+{% extends "base.html" %}
+
+{% block content %}
+<form class="form-horizontal" action="{% url 'game_list' %}" method="get">
+  <fieldset>
+    <legend>Filter games</legend>
+    {% include "bootstrap_form.html" with form=filter_form %}
+    <button type="submit" class="btn btn-primary">Apply Filter</button>
+  </fieldset>
+</form>
+{% include "gamedb/fragments/game_table.html" %}
+{% endblock %}

gamedb/templatetags/ohrrpgce.py

+from django import template
+from django.utils.safestring import mark_safe
+
+register = template.Library()
+
+@register.filter
+def yesnoicon(value):
+    icon = "ok" if value else "remove"
+    return mark_safe('<i class="icon-%s"></i>' % icon)
+
+@register.filter
+def ratingicon(value):
+    return mark_safe('<i class="icon-star"></i>' * value)
+
+@register.filter
+def link(value):
+    try:
+        return mark_safe('<a href="%s">%s</a>' % (value.get_absolute_url(), value))
+    except:
+        return value
 from django.conf.urls import patterns, url
 from django.views.generic.base import TemplateView
-from gamedb.views import ViewGame
+from gamedb.views import ViewGame, GameList
 
 urlpatterns = patterns('',
-    url(r'^$', ViewGame.as_view()),
+    url(r'^Games/$', GameList.as_view(), name='game_list'),
+    url(r'^Games/(?P<slug>[\w-]+).html$', ViewGame.as_view(), name='game_detail'),
 )
 from django.views.generic.detail import DetailView
 from gamedb.models import Game
+from django.views.generic.list import ListView
+from gamedb.forms import FilterForm
 
 class ViewGame(DetailView):
     model = Game
-    def get_object(self, queryset=None):
-        return Game.objects.get(pk=1)
+
+class GameList(ListView):
+    model = Game
+    def get_context_data(self, **kwargs):
+        ctx = super(GameList, self).get_context_data(**kwargs)
+        ctx.update({'filter_form':self.filter_form})
+        return ctx
+    def get_queryset(self):
+        qs = super(GameList, self).get_queryset()
+        self.filter_form = FilterForm(self.request.GET)
+        if self.filter_form.is_valid():
+            if 'game_type' in self.request.GET:
+                qs=qs.filter(game_type=self.filter_form.cleaned_data['game_type'])
+            if 'genre' in self.request.GET:
+                qs=qs.filter(genre=self.filter_form.cleaned_data['genre'])
+            if 'style' in self.request.GET:
+                qs=qs.filter(style=self.filter_form.cleaned_data['style'])
+        return qs
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.