mirror of
https://github.com/nikdoof/limetime.git
synced 2025-12-16 03:22:23 +00:00
Initial import of codebase
This commit is contained in:
4
app/timer/models/__init__.py
Normal file
4
app/timer/models/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from .locations import *
|
||||
from .owners import *
|
||||
from .timer import *
|
||||
from .utils import *
|
||||
88
app/timer/models/locations.py
Normal file
88
app/timer/models/locations.py
Normal 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'
|
||||
24
app/timer/models/owners.py
Normal file
24
app/timer/models/owners.py
Normal 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
35
app/timer/models/timer.py
Normal 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
29
app/timer/models/utils.py
Normal 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
|
||||
Reference in New Issue
Block a user