Geoffrey-Django/GeoffreyApp/views.py

205 lines
4.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):
stats = []
stats.append(("Players", Player.objects.count()))
stats.append(("Bases", Base.objects.count()))
stats.append(("Shops", Shop.objects.count()))
stats.append(("Items for Sale", ItemListing.objects.count()))
context = {
"stat_list": stats,
"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["loc_list"] = Location.objects.filter(Q(name__icontains=query) | Q(owner__name__icontains=query)).all()
return render(request, 'GeoffreyApp/search.html', context=context)
class GeoffreyModelList(generic.ListView):
model = None
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['current_page'] = self.model.__name__
return context
class PlayerList(GeoffreyModelList):
model = Player
class ShopList(GeoffreyModelList):
model = Shop
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(GeoffreyModelList):
model = Base
class TownList(GeoffreyModelList):
model = Town
class PublicFarmList(GeoffreyModelList):
model = PublicFarm
class ItemListingList(GeoffreyModelList):
model = ItemListing
class MarketList(GeoffreyModelList):
model = Market
class PointOfInterestList(GeoffreyModelList):
model = PointOfInterest
class ModelInfoView(View):
template = None
def get_context(self, id):
return None
def get(self, request, id):
try:
return render(request, 'GeoffreyApp/%s' % self.template, context=self.get_context(id))
except Player.DoesNotExist:
return render(request, 'GeoffreyApp/error.html')
class PlayerInfo(ModelInfoView):
template = 'player.html'
def get_context(self, id):
player = Player.objects.get(pk=id)
loc_list = Location.objects.filter(owner=player).all()
context = {
"player": player,
"loc_list": loc_list
}
return context
class ShopInfo(ModelInfoView):
template = 'shop.html'
def get_context(self, id):
shop = Shop.objects.get(pk=id)
inventory = ItemListing.objects.filter(shop=shop).all()
context = {
"loc": shop,
"inventory": inventory
}
return context
class BaseInfo(ModelInfoView):
template = "location.html"
def get_context(self, id):
base = Base.objects.get(pk=id)
context = {
"loc": base,
}
return context
class TownInfo(ModelInfoView):
template = "town.html"
def get_context(self, id):
town = Town.objects.get(pk=id)
residents = town.residents.all()
context = {
"loc": town,
"residents": residents
}
return context
class PublicFarmInfo(ModelInfoView):
template = "publicfarm.html"
def get_context(self, id):
public_farm = PublicFarm.objects.get(pk=id)
resources = public_farm.resource.all()
context = {
"loc": public_farm,
"resources": resources
}
return context
class MarketInfo(ModelInfoView):
template = "market.html"
def get_context(self, id):
market = Market.objects.get(pk=id)
shops = market.get_shops(limit=100)
context = {
"loc": market,
"shops": shops
}
return context
class PointOfInterestInfo(ModelInfoView):
template = "location.html"
def get_context(self, id):
point_of_interest = PointOfInterest.objects.get(pk=id)
context = {
"loc": point_of_interest,
}
return context