diff --git a/evesde/eveapi/__init__.py b/evesde/eveapi/__init__.py new file mode 100644 index 0000000..4fe931d --- /dev/null +++ b/evesde/eveapi/__init__.py @@ -0,0 +1,43 @@ +from datetime import timedelta +from django.utils.timezone import now +from django.conf import settings +from eveapi import EVEAPIConnection + +from evesde.models.cache import EVEAPICache + + +class EVEAPICache(object): + + def hash(self, data): + from hashlib import sha1 + return sha1('-'.join(data)).hexdigest() + + def store(self, host, path, params, doc, obj): + key = self.hash((host, path, str(params.items()))) + cached = now() + timedelta(seconds=obj.cachedUntil - obj.currentTime) + + try: + obj = EVEAPICache.objects.get(key=key) + except EVEAPICache.DoesNotExist: + EVEAPICache.objects.create(key=key, cache_until=cached, document=doc) + else: + obj.cache_until = cached + obj.document = doc + obj.save() + + def retrieve(self, host, path, params): + key = self.hash((host, path, str(params.items()))) + try: + obj = EVEAPICache.objects.get(key=key) + except EVEAPICache.DoesNotExist: + pass + else: + if obj.cache_until >= now(): + return obj.document + return None + + +def get_api_connection(): + cache_handler = getattr(settings, 'EVE_SDE_CACHE_HANDLER', 'evesde.eveapi.EVEAPICache') + + return EVEAPIConnection(cacheHandler=cache_obj) \ No newline at end of file diff --git a/evesde/eveapi/eve.py b/evesde/eveapi/eve.py new file mode 100644 index 0000000..4fcc825 --- /dev/null +++ b/evesde/eveapi/eve.py @@ -0,0 +1,25 @@ +from django.db import transaction + +from evesde.models.locations import Station +from evesde.eveapi import get_api_connection + + +def import_conquerable_stations(): + """Import all conquerable stations and outposts from the EVE API""" + api = get_api_connection() + stations = Station.objects.all() + + objs = [] + for station in api.eve.ConquerableStationList().outposts: + print "Importing %s" % station.stationName + try: + obj = stations.get(pk=station.stationID) + except Station.DoesNotExist: + obj = Station(pk=station.stationID) + obj.name = station.stationName + obj.system_id = station.solarSystemID + objs.append(obj) + + with transaction.atomic(): + for obj in objs: + obj.save() \ No newline at end of file diff --git a/evesde/management/commands/import_api_stations.py b/evesde/management/commands/import_api_stations.py index a91d9c7..1bc229d 100644 --- a/evesde/management/commands/import_api_stations.py +++ b/evesde/management/commands/import_api_stations.py @@ -1,27 +1,8 @@ from django.core.management.base import BaseCommand -from django.db import transaction -from evesde.models.locations import Station -from eveapi import EVEAPIConnection - +from evesde.eveapi.eve import import_conquerable_stations class Command(BaseCommand): help = 'Imports outposts from the EVE API.' def handle(self, *args, **options): - api = EVEAPIConnection() - stations = Station.objects.all() - - objs = [] - for station in api.eve.ConquerableStationList().outposts: - print "Importing %s" % station.stationName - try: - obj = stations.get(pk=station.stationID) - except Station.DoesNotExist: - obj = Station(pk=station.stationID) - obj.name = station.stationName - obj.system_id = station.solarSystemID - objs.append(obj) - - with transaction.atomic(): - for obj in objs: - obj.save() + import_conquerable_stations() \ No newline at end of file diff --git a/evesde/models/__init__.py b/evesde/models/__init__.py index 91d7e68..ee6db4f 100644 --- a/evesde/models/__init__.py +++ b/evesde/models/__init__.py @@ -1,3 +1,4 @@ from locations import Location, Region, Constellation, System, SystemJump, Planet, Moon, Station from objects import InSpaceObject -from types import TypeCategory, TypeGroup, Type, UnitType, AttributeType, TypeAttribute \ No newline at end of file +from types import TypeCategory, TypeGroup, Type, UnitType, AttributeType, TypeAttribute +from cache import EVEAPICache \ No newline at end of file diff --git a/evesde/models/cache.py b/evesde/models/cache.py new file mode 100644 index 0000000..b293c5d --- /dev/null +++ b/evesde/models/cache.py @@ -0,0 +1,14 @@ +from django.db import models + + +class EVEAPICache(models.Model): + + key = models.CharField('Cache Key', blank=False, max_length=40) + cache_until = models.DateTimeField('Cached Until', blank=False) + document = models.TextField('Document') + + class Meta: + app_label = 'evesde' + + def __unicode__(self): + return '%(key)s - %(cache_until)s' % self.__dict__ \ No newline at end of file diff --git a/evesde/models/utils.py b/evesde/models/utils.py index b2a7fdb..8e15a0b 100644 --- a/evesde/models/utils.py +++ b/evesde/models/utils.py @@ -6,8 +6,8 @@ class InheritanceQuerySet(QuerySet): def select_subclasses(self, *subclasses): if not subclasses: subclasses = [o for o in dir(self.model) - if isinstance(getattr(self.model, o), SingleRelatedObjectDescriptor)\ - and issubclass(getattr(self.model,o).related.model, self.model)] + if isinstance(getattr(self.model, o), SingleRelatedObjectDescriptor) + and issubclass(getattr(self.model, o).related.model, self.model)] new_qs = self.select_related(*subclasses) new_qs.subclasses = subclasses return new_qs