diff --git a/admin.py b/admin.py index 2572f1d..8e61638 100644 --- a/admin.py +++ b/admin.py @@ -9,3 +9,5 @@ admin.site.register(Tunnel) admin.site.register(ItemListing) admin.site.register(APIToken) admin.site.register(Town) +admin.site.register(PublicFarm) +admin.site.register(Resource) diff --git a/api/commands.py b/api/commands.py index c0f379c..2ea6a80 100644 --- a/api/commands.py +++ b/api/commands.py @@ -175,6 +175,25 @@ def add_town(x_pos, z_pos, name=None, discord_uuid=None, mc_uuid=None): return add_location(x_pos, z_pos, name=name, discord_uuid=discord_uuid, mc_uuid=mc_uuid, loc_type=Town) + +@command("POST") +def add_farm(x_pos, z_pos, name=None, discord_uuid=None, mc_uuid=None): + ''' + :request: POST + :param x_pos: MC X Coordinate + :param z_pos: MC Z Coordinate + :param name: Shop Name (If None, Defaults to Player's Shop) + :param discord_uuid: Discord UUID + :param mc_uuid: Minecraft UUID + :return: JSON representation of the new town + :raises: EntryNameNotUniqueError, PlayerNotFound, LocationLookupError + :help: Adds your public farm to the database. + ''' + + return add_location(x_pos, z_pos, name=name, discord_uuid=discord_uuid, mc_uuid=mc_uuid, loc_type=PublicFarm) + + + @command("POST") def add_tunnel(tunnel_direction, tunnel_number, location_name=None, discord_uuid=None, mc_uuid=None): ''' @@ -296,6 +315,36 @@ def add_item(item_name, quantity, diamond_price, shop_name=None, discord_uuid=No return item_listing.json +@command("POST") +def add_resource(resource_name, farm_name=None, discord_uuid=None, mc_uuid=None): + ''' + :request: POST + :param resource_name: name of the resource + ;param farm_name: name of the farm to add the resource to. Can be none. + :param discord_uuid: Discord UUID + :param mc_uuid: Minecraft UUID + :return: Item Listing + :raises: PlayerNotFound, LocationLookupError, EntryNameNotUniqueError, NoLocationsInDatabase + :help: Adds a resource to a farm. + ''' + + player = get_player(discord_uuid, mc_uuid) + + farm = get_location(player, farm_name, PublicFarm).publicfarm + + resource = Resource.objects.create(farm=farm, resource_name=resource_name) + + return resource.json + + +@command("GET") +def find_farm(resource_name): + if len(resource_name) == 0: + raise EmptryString + + return objects_list_to_json(PublicFarm.objects.filter(resource__resource_name__icontains=resource_name).all()) + + @command("GET") def selling(item_name): ''' @@ -323,7 +372,6 @@ def selling_price(item_name): def get_selling(item_name, sort): - items = [] if len(item_name) == 0: raise EmptryString @@ -513,6 +561,33 @@ def delete_item(item, shop_name=None, discord_uuid=None, mc_uuid=None): return shop.json +@command("POST") +def delete_resource(resource_name, farm_name=None, discord_uuid=None, mc_uuid=None): + ''' + :request: POST + :param resource: resource to delete + :param farm_name: Farm with resource, can be None if the user only has one farm + :param discord_uuid: Discord UUID + :param mc_uuid: Minecraft UUID + :return: PublicFarm where the resource was deleted from + :raises: PlayerNotFound, LocationLookupError, EntryNameNotUniqueError, NoLocationsInDatabase, ItemNotFound + :help: Deletes a resource from a farm + ''' + + player = get_player(discord_uuid=discord_uuid, mc_uuid=mc_uuid) + + farm = get_location(player, farm_name, PublicFarm) + + delete_list = Resource.objects.filter(resource_name=resource_name, farm=farm).all() + + if len(delete_list) == 0: + raise ResourceNotFoundError + + delete_list.delete() + + return farm.json + + @command("GET") def me(discord_uuid=None, mc_uuid=None): ''' diff --git a/errors.py b/errors.py index 4d8e244..20f5bf8 100644 --- a/errors.py +++ b/errors.py @@ -105,3 +105,7 @@ class ResidentNotFoundError(DataBaseError): class OwnerNotFoundError(DataBaseError): """No owner matches""" + + +class ResourceNotFoundError(DataBaseError): + """No resource found""" diff --git a/migrations/0008_publicfarm_resource.py b/migrations/0008_publicfarm_resource.py new file mode 100644 index 0000000..6a0bcda --- /dev/null +++ b/migrations/0008_publicfarm_resource.py @@ -0,0 +1,31 @@ +# Generated by Django 2.1.2 on 2019-03-01 20:28 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + dependencies = [ + ('GeoffreyApp', '0007_town'), + ] + + operations = [ + migrations.CreateModel( + name='PublicFarm', + fields=[ + ('location_ptr', + models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, + primary_key=True, serialize=False, to='GeoffreyApp.Location')), + ], + bases=('GeoffreyApp.location',), + ), + migrations.CreateModel( + name='Resource', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('resource_name', models.CharField(max_length=128)), + ('farm', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='resource', + to='GeoffreyApp.PublicFarm')), + ], + ), + ] diff --git a/models.py b/models.py index 3412cf1..e96250c 100644 --- a/models.py +++ b/models.py @@ -112,10 +112,6 @@ class Location(models.Model): return owner_list - @property - def link(self): - return "" - @property def json(self): return {"type": self.__class__.__name__, @@ -137,7 +133,10 @@ class Location(models.Model): @property def link(self): - return self.loc_child_obj.link + child = self.loc_child_obj + + if child != self: + return self.loc_child_obj.link @property def loc_child_obj(self): @@ -147,6 +146,8 @@ class Location(models.Model): return self.base elif hasattr(self, "town"): return self.town + elif hasattr(self, "publicfarm"): + return self.publicfarm else: return self @@ -208,6 +209,23 @@ class Town(Location): return reverse("GeoffreyTownInfo", kwargs={"id": self.id}) +class PublicFarm(Location): + @property + def link(self): + return reverse("GeoffreyPublicFarmInfo", kwargs={"id": self.id}) + + +class Resource(models.Model): + farm = models.ForeignKey(PublicFarm, related_name="resource", on_delete=models.CASCADE) + resource_name = models.CharField(max_length=128) + + @property + def json(self): + return {"name": self.resource_name, + "farm_id": self.farm_id + } + + class ItemListing(models.Model): item_name = models.CharField(max_length=128) ''' diff --git a/templates/GeoffreyApp/itemlisting_table.html b/templates/GeoffreyApp/itemlisting_table.html index 9d161a3..8f6bc6c 100644 --- a/templates/GeoffreyApp/itemlisting_table.html +++ b/templates/GeoffreyApp/itemlisting_table.html @@ -20,7 +20,7 @@
Resource Name | +
---|
{{ resource.resource_name }} | +