Added a way to get the child object from a location object

+ get_loc_child returns the Town, Base, Shop... object
+ link gets the url for the location
+ the info command now returns the child object instead of the Location
object
+ Added Town navbar
doc_update
Joey Hines 2019-02-06 14:54:50 -06:00
parent a668ade97d
commit b88bfbda43
9 changed files with 84 additions and 11 deletions

View File

@ -44,7 +44,7 @@ def parse_help(func):
return match.group(0).partition(":help: ")[-1] return match.group(0).partition(":help: ")[-1]
except: except:
return '' return ' '
def get_player(discord_uuid=None, mc_uuid=None): def get_player(discord_uuid=None, mc_uuid=None):
@ -348,7 +348,7 @@ def info(location_name):
location = Location.objects.filter(name__iregex=".*{}.*".format(location_name)).first() location = Location.objects.filter(name__iregex=".*{}.*".format(location_name)).first()
if location is not None: if location is not None:
return location.json return location.loc_child_obj.json
else: else:
raise LocationLookUpError raise LocationLookUpError
@ -602,6 +602,7 @@ def remove_resident(resident_name, town_name, discord_uuid=None, mc_uuid=None):
:param mc_uuid: Owner mc uuid :param mc_uuid: Owner mc uuid
:raises: PlayerNotFound, LocationLookupError, IsResidentError, ResidentNotFoundError :raises: PlayerNotFound, LocationLookupError, IsResidentError, ResidentNotFoundError
:return: Updated town :return: Updated town
:help: Removes a resident from a town
''' '''
owner = get_player(discord_uuid, mc_uuid) owner = get_player(discord_uuid, mc_uuid)

View File

@ -1,5 +1,6 @@
from django.db import models from django.db import models
from django.conf import settings from django.conf import settings
from django.urls import reverse
from sys import maxsize from sys import maxsize
from GeoffreyApp.util import create_token, objects_list_to_json from GeoffreyApp.util import create_token, objects_list_to_json
@ -111,6 +112,10 @@ class Location(models.Model):
return owner_list return owner_list
@property
def link(self):
return ""
@property @property
def json(self): def json(self):
return {"type": self.__class__.__name__, return {"type": self.__class__.__name__,
@ -120,19 +125,30 @@ class Location(models.Model):
"dimension": self.dimension, "dimension": self.dimension,
"owner": self.get_owners, "owner": self.get_owners,
"location": self.location, "location": self.location,
"tunnel": None if self.tunnel is None else self.tunnel.tunnel_str "tunnel": None if self.tunnel is None else self.tunnel.tunnel_str,
"link": self.link
} }
@property @property
def loc_type(self): def loc_type(self):
str = self.loc_child_obj.__class__.__name__
return str
@property
def link(self):
return self.loc_child_obj.link()
@property
def loc_child_obj(self):
if hasattr(self, "shop"): if hasattr(self, "shop"):
return "shop" return self.shop
elif hasattr(self, "base"): elif hasattr(self, "base"):
return "base" return self.base
elif hasattr(self, "town"): elif hasattr(self, "town"):
return "town" return self.town
else: else:
return "location" return self
@property @property
def dynmap_url(self): def dynmap_url(self):
@ -153,11 +169,19 @@ class Shop(Location):
def __str__(self): def __str__(self):
return "Shop: %s" % self.name return "Shop: %s" % self.name
@property
def link(self):
return reverse("GeoffreyShopInfo", kwargs={"id": self.id})
class Base(Location): class Base(Location):
def __str__(self): def __str__(self):
return "Base: %s" % self.name return "Base: %s" % self.name
@property
def link(self):
return reverse("GeoffreyBaseInfo", kwargs={"id": self.id})
class Town(Location): class Town(Location):
residents = models.ManyToManyField(Player) residents = models.ManyToManyField(Player)
@ -175,10 +199,14 @@ class Town(Location):
def json(self): def json(self):
json = super().json json = super().json
json["residents"] = self.residents json["residents"] = self.get_residents
return json return json
@property
def link(self):
return reverse("GeoffreyTownInfo", kwargs={"id": self.id})
class ItemListing(models.Model): class ItemListing(models.Model):
item_name = models.CharField(max_length=128) item_name = models.CharField(max_length=128)

View File

@ -1,5 +1,5 @@
{% if loc.loc_type == "base" %} {% if loc.loc_type == "Base" %}
<a href="{% url 'GeoffreyBaseInfo' loc.id %}">{{ loc.name }}</a> <a href="{% url 'GeoffreyBaseInfo' loc.id %}">{{ loc.name }}</a>
{% elif loc.loc_type == "shop" %} {% elif loc.loc_type == "Shop" %}
<a href="{% url 'GeoffreyShopInfo' loc.id %}">{{ loc.name }}</a> <a href="{% url 'GeoffreyShopInfo' loc.id %}">{{ loc.name }}</a>
{% endif %} {% endif %}

View File

@ -12,7 +12,7 @@
<tbody> <tbody>
{% for loc in loc_list %} {% for loc in loc_list %}
<tr> <tr>
<td>{% include "GeoffreyApp/location_link.html" with loc=loc %}</td> <td><a href="{{ loc.link }}">{{ loc.name }}</a></td>
<td>{{ loc.location }}</td> <td>{{ loc.location }}</td>
{% if show_owner %} {% if show_owner %}
<td> <td>

View File

@ -0,0 +1,7 @@
{% extends "GeoffreyApp/location.html" %}
{% block info %}
<h2>Residents:</h2>
{% include "GeoffreyApp/player_table.html" with itemlisting_list=residents %}
<hr class="my-4">
{% endblock %}

View File

@ -0,0 +1,9 @@
{% extends "GeoffreyApp/base.html" %}
{% block header %}
Towns
{% endblock %}
{% block content %}
{% include "GeoffreyApp/location_table.html" with loc_list=town_list show_owner=True %}
{% endblock %}

View File

@ -8,6 +8,7 @@ navbar_options = [
("Players", reverse("GeoffreyPlayers")), ("Players", reverse("GeoffreyPlayers")),
("Shops", reverse("GeoffreyShops")), ("Shops", reverse("GeoffreyShops")),
("Bases", reverse("GeoffreyBases")), ("Bases", reverse("GeoffreyBases")),
("Towns", reverse("GeoffreyTowns")),
("Item Listings", reverse("GeoffreyItems")) ("Item Listings", reverse("GeoffreyItems"))
] ]

View File

@ -9,6 +9,8 @@ urlpatterns = [
url(r'^shops/(?P<id>[0-9]{1,9})/$', views.ShopInfo.as_view(), name='GeoffreyShopInfo'), url(r'^shops/(?P<id>[0-9]{1,9})/$', views.ShopInfo.as_view(), name='GeoffreyShopInfo'),
url(r'^bases/$', views.BaseList.as_view(), name='GeoffreyBases'), url(r'^bases/$', views.BaseList.as_view(), name='GeoffreyBases'),
url(r'^bases/(?P<id>[0-9]{1,9})/$', views.BaseInfo.as_view(), name='GeoffreyBaseInfo'), url(r'^bases/(?P<id>[0-9]{1,9})/$', views.BaseInfo.as_view(), name='GeoffreyBaseInfo'),
url(r'^towns/$', views.TownList.as_view(), name='GeoffreyTowns'),
url(r'^towns/(?P<id>[0-9]{1,9})/$', views.TownInfo.as_view(), name='GeoffreyTownInfo'),
url(r'^items/$', views.ItemListingList.as_view(), name='GeoffreyItems'), url(r'^items/$', views.ItemListingList.as_view(), name='GeoffreyItems'),
url(r'^search/$', views.SearchList.as_view(), name='GeoffreySearch'), url(r'^search/$', views.SearchList.as_view(), name='GeoffreySearch'),
] ]

View File

@ -72,6 +72,15 @@ class BaseList(generic.ListView):
return context return context
class TownList(generic.ListView):
model = Town
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['current_page'] = "Towns"
return context
class ItemListingList(generic.ListView): class ItemListingList(generic.ListView):
model = ItemListing model = ItemListing
@ -122,3 +131,19 @@ class BaseInfo(View):
return render(request, 'GeoffreyApp/location.html', context=context) return render(request, 'GeoffreyApp/location.html', context=context)
except Player.DoesNotExist: except Player.DoesNotExist:
return render(request, 'GeoffreyApp/error.html') return render(request, 'GeoffreyApp/error.html')
class TownInfo(View):
def get(self, request, id):
try:
town = Town.objects.get(pk=id)
residents = town.residents.all()
context = {
"loc": town,
"residents": residents
}
return render(request, 'GeoffreyApp/town.html', context=context)
except Player.DoesNotExist:
return render(request, 'GeoffreyApp/error.html')