Geoffrey-Django/views.py

87 lines
2.3 KiB
Python

from django.shortcuts import render
from django.views.generic import View
from GeoffreyApp.models import *
from django.views import generic
from django.db.models import Q
# Create your views here.
class Home(View):
def get(self, request):
base_num = Base.objects.count()
shop_num = Shop.objects.count()
player_num = Player.objects.count()
item_num = ItemListing.objects.count()
context = {
"num_players": player_num,
"num_bases": base_num,
"num_shops": shop_num,
"num_items": item_num,
"current_page": "Home",
}
return render(request, 'GeoffreyApp/home.html', context=context)
class SearchList(View):
def get(self, request):
context = {}
query = request.GET.get('search')
context["search"] = query
context["player_list"] = Player.objects.filter(Q(name__icontains=query)).all()
context["base_list"] = Base.objects.filter(Q(name__icontains=query) | Q(owner__name__icontains=query)).all()
context["shop_list"] = Shop.objects.filter(Q(name__icontains=query) | Q(owner__name__icontains=query)).all()
return render(request, 'GeoffreyApp/search.html', context=context)
class PlayerList(generic.ListView):
model = Player
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['current_page'] = "Players"
return context
class ShopList(generic.ListView):
model = Shop
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['current_page'] = "Shops"
return context
def get_queryset(self):
qs = Shop.objects.all()
search = self.request.GET.get('q')
if search:
qs = qs.filter(Q(name__icontains=search) | Q(owner__name__icontains=search))
return qs
class BaseList(generic.ListView):
model = Base
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['current_page'] = "Bases"
return context
class ItemListingList(generic.ListView):
model = ItemListing
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['current_page'] = "Item Listings"
return context