Initial import of codebase

This commit is contained in:
2013-05-05 18:24:00 +01:00
commit 51337beeb0
42 changed files with 1121 additions and 0 deletions

1
app/timer/__init__.py Normal file
View File

@@ -0,0 +1 @@
__version__ = '0.1'

8
app/timer/admin.py Normal file
View File

@@ -0,0 +1,8 @@
from django.contrib import admin
from timer.models import Timer
class TimerAdmin(admin.ModelAdmin):
readonly_fields = ['location']
admin.site.register(Timer, admin.ModelAdmin)

View File

View File

@@ -0,0 +1,23 @@
from django.core.management.base import BaseCommand, CommandError
from django.db import transaction
from timer.models import Alliance, Corporation
from eveapi import EVEAPIConnection
class Command(BaseCommand):
args = ''
help = 'Updates alliances from the EVE API'
def handle(self, *args, **options):
api = EVEAPIConnection()
alliance_list = api.eve.AllianceList().alliances
print 'Updating %d alliances... ' % len(alliance_list)
with transaction.commit_on_success():
for alliance in alliance_list:
allobj, created = Alliance.objects.get_or_create(pk=alliance.allianceID)
allobj.name = alliance.name
allobj.save()
corp_ids = [x.corporationID for x in alliance.memberCorporations]
Corporation.objects.exclude(pk__in=corp_ids).update(alliance=None)
Corporation.objects.filter(pk__in=corp_ids).update(alliance=allobj)
print 'Done'

View File

@@ -0,0 +1,44 @@
from __future__ import division
import sqlite3
from django.core.management.base import BaseCommand, CommandError
from django.db import transaction
from timer.models import Region, Constellation, System, Planet, Moon
class Command(BaseCommand):
args = '<map csv>'
help = 'Imports the EVE Map from a sqlite dump of the SDE table mapDenormalized'
def handle(self, *args, **options):
# Connect to the sqlite3 DB
db = sqlite3.connect(args[0])
cur = db.cursor()
objs = []
# eveUnits
print "Importing mapDenormalize..."
cur.execute("""SELECT itemID, typeID, groupID, solarSystemID, constellationID, regionID, orbitID, x, y, z, itemName FROM mapDenormalize WHERE typeID in (3, 4, 5, 14) OR groupID = 7""")
for row in cur.fetchall():
id, type, group, solarid, constellationid, regionid, orbitid, x, y, z, name = row
if int(type) == 3:
objs.append(Region(pk=id, name=name, x=0, y=0, z=0))
elif int(type) == 4:
objs.append(Constellation(pk=id, name=name, region_id=regionid, x=0, y=0, z=0))
elif int(type) == 5:
objs.append(System(pk=id, name=name, constellation_id=constellationid, x=0, y=0, z=0))
elif int(group) == 7:
objs.append(Planet(pk=id, name=name, system_id=solarid, x=x, y=y, z=z))
elif int(type) == 14:
objs.append(Moon(pk=id, name=name, planet_id=orbitid, x=x, y=y, z=z))
print "Done"
# Dump to DB
print 'Processing %d objects for commiting...' % len(objs)
with transaction.commit_on_success():
for i, x in enumerate(objs, start=1):
if i % 1000 == 0: print "%d/%d (%d%%)" % (i, len(objs), round(i/len(objs) * 100))
x.save()
print 'Commited'

View File

@@ -0,0 +1,24 @@
from django.core.management.base import BaseCommand
from django.db import transaction
from timer.models import Station, System
from eveapi import EVEAPIConnection
class Command(BaseCommand):
args = ''
help = 'Updates conquerable stations from the EVE API'
def handle(self, *args, **options):
api = EVEAPIConnection()
station_list = api.eve.ConquerableStationList().outposts
print 'Updating %d stations... ' % len(station_list)
with transaction.commit_on_success():
for station in station_list:
try:
obj = Station.objects.get(pk=station.stationID)
except Station.DoesNotExist:
obj = Station(pk=station.stationID)
obj.system, created = System.objects.get_or_create(pk=station.solarSystemID)
obj.name = station.stationName
obj.save()
print 'Done'

View File

@@ -0,0 +1,4 @@
from .locations import *
from .owners import *
from .timer import *
from .utils import *

View File

@@ -0,0 +1,88 @@
from django.db import models
from .utils import InheritanceQuerySet
from .owners import Corporation
class LocationManager(models.Manager):
def all_subclassed(self):
return InheritanceQuerySet(model=self.model).select_subclasses()
class Location(models.Model):
id = models.BigIntegerField('Location ID', primary_key=True)
name = models.CharField('Location Name', max_length=200)
x = models.BigIntegerField('X Location', null=True)
y = models.BigIntegerField('Y Location', null=True)
z = models.BigIntegerField('Z Location', null=True)
def __unicode__(self):
return "%(name)s (%(id)d)" % self.__dict__
class Meta:
app_label = 'timer'
class Region(Location):
@property
def systems(self):
return System.objects.filter(constellation__in=self.constellations.all())
@property
def planets(self):
return Planet.objects.filter(system__constellation__in=self.constellations.all())
@property
def moons(self):
return Moon.objects.filter(planet__system__constellation__in=self.constellations.all())
class Meta:
app_label = 'timer'
class Constellation(Location):
region = models.ForeignKey(Region, related_name='constellations')
@property
def planets(self):
return Planet.objects.filter(system__in=self.systems.all())
@property
def moons(self):
return Moon.objects.filter(planet__system__in=self.systems.all())
class Meta:
app_label = 'timer'
class System(Location):
constellation = models.ForeignKey(Constellation, related_name='systems')
owner = models.ForeignKey(Corporation, related_name='systems', null=True)
@property
def moons(self):
return Moon.objects.filter(planet__in=self.planets.all())
class Meta:
app_label = 'timer'
class Planet(Location):
system = models.ForeignKey(System, related_name='planets')
class Meta:
app_label = 'timer'
class Moon(Location):
planet = models.ForeignKey(Planet, related_name='moons')
class Meta:
app_label = 'timer'
class Station(Location):
system = models.ForeignKey(System, related_name='stations')
class Meta:
app_label = 'timer'

View File

@@ -0,0 +1,24 @@
from django.db import models
class Owner(models.Model):
id = models.BigIntegerField('Owner ID', primary_key=True)
name = models.CharField('Owner Name', max_length=200)
class Meta:
app_label = 'timer'
def __unicode__(self):
return self.name
class Alliance(Owner):
class Meta:
app_label = 'timer'
class Corporation(Owner):
"""Represents a EVE Corporation """
alliance = models.ForeignKey(Alliance, related_name='corporations', null=True)
class Meta:
app_label = 'timer'

35
app/timer/models/timer.py Normal file
View File

@@ -0,0 +1,35 @@
from django.db import models
from django.utils.timezone import now
class Timer(models.Model):
STATE_ACTIVE = 1
STATE_EXPIRED = 2
STATE_CHOICES = (
(STATE_ACTIVE, 'Active'),
(STATE_EXPIRED, 'Expired'),
)
TYPE_SHEILD_REENFORCEMENT = 1
TYPE_ARMOR_REENFORCEMENT = 2
TYPE_CHOICES = (
(TYPE_SHEILD_REENFORCEMENT, 'Sheild Reenforcement'),
(TYPE_ARMOR_REENFORCEMENT, 'Armor Reenforcement'),
)
location = models.ForeignKey('timer.Location', related_name='timers')
expiration = models.DateTimeField('Timer Expiration')
reenforcement_type = models.PositiveIntegerField('Timer Type', choices=TYPE_CHOICES)
@property
def state(self):
if self.expiration <= now():
return self.STATE_EXPIRED
return self.STATE_ACTIVE
class Meta:
app_label = 'timer'
ordering = ['-expiration']

29
app/timer/models/utils.py Normal file
View File

@@ -0,0 +1,29 @@
from django.db.models.fields.related import SingleRelatedObjectDescriptor
from django.db.models.query import QuerySet
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)]
new_qs = self.select_related(*subclasses)
new_qs.subclasses = subclasses
return new_qs
def _clone(self, klass=None, setup=False, **kwargs):
try:
kwargs.update({'subclasses': self.subclasses})
except AttributeError:
pass
return super(InheritanceQuerySet, self)._clone(klass, setup, **kwargs)
def iterator(self):
iter = super(InheritanceQuerySet, self).iterator()
if getattr(self, 'subclasses', False):
for obj in iter:
obj = [getattr(obj, s) for s in self.subclasses if getattr(obj, s)] or [obj]
yield obj[0]
else:
for obj in iter:
yield obj

View File

@@ -0,0 +1,3 @@
{% for timer in timer_list %}
{{ timer.location.name }} - {{ timer.expiration_datetime }}
{% endfor %}

16
app/timer/tests.py Normal file
View File

@@ -0,0 +1,16 @@
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.assertEqual(1 + 1, 2)

6
app/timer/urls.py Normal file
View File

@@ -0,0 +1,6 @@
from django.conf.urls import patterns, url
from timer.views import TimerListView
urlpatterns = patterns('',
url(r'^$', TimerListView.as_view(), name='timer-list'),
)

14
app/timer/views.py Normal file
View File

@@ -0,0 +1,14 @@
from django.views.generic import ListView
from django.utils.timezone import now
from timer.models import Timer
class TimerListView(ListView):
model = Timer
def get_queryset(self):
qs = super(TimerListView, self).get_queryset()
if 'active' in self.kwargs:
qs = qs.filter(expiry_datetime__gt=now())
return qs