2012-06-06 19:49:32 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
from __future__ import division, with_statement
|
|
|
|
|
2013-02-21 13:28:36 +00:00
|
|
|
from django.db import models
|
2012-06-06 19:49:32 +00:00
|
|
|
|
|
|
|
import managers
|
2013-03-06 08:40:52 +00:00
|
|
|
from item.models import ItemSort
|
2012-06-06 19:49:32 +00:00
|
|
|
|
|
|
|
|
2013-03-06 08:40:52 +00:00
|
|
|
def parse_hash(value):
|
|
|
|
return int(value, 16) - 9223372036854775808
|
|
|
|
|
|
|
|
def format_hash(value):
|
|
|
|
return hex(value + 9223372036854775808)[2:-1].upper()
|
|
|
|
|
2012-06-06 19:49:32 +00:00
|
|
|
class Sequence(models.Model):
|
2012-06-15 20:32:15 +00:00
|
|
|
class Meta:
|
2013-03-06 08:40:52 +00:00
|
|
|
unique_together = ("sort", "start", "end", "mode")
|
2012-06-15 20:32:15 +00:00
|
|
|
|
2013-03-06 08:40:52 +00:00
|
|
|
MODE = {
|
|
|
|
'shape': 0,
|
|
|
|
'color': 1
|
|
|
|
}
|
|
|
|
mode = models.IntegerField(choices=sorted(zip(MODE.values(), MODE.keys()), key=lambda k: k[0]), default=0)
|
2012-06-06 19:49:32 +00:00
|
|
|
sort = models.ForeignKey(ItemSort, null=True, related_name='sequences')
|
|
|
|
|
2013-03-06 08:40:52 +00:00
|
|
|
hash = models.BigIntegerField(db_index=True, default=-9223372036854775808)
|
2013-03-06 15:57:46 +00:00
|
|
|
start = models.FloatField(default=-1)
|
2012-06-06 19:49:32 +00:00
|
|
|
end = models.FloatField(default=-1)
|
2012-06-16 21:44:05 +00:00
|
|
|
duration = models.FloatField(default=0)
|
2012-06-06 19:49:32 +00:00
|
|
|
|
|
|
|
objects = managers.SequenceManager()
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
2012-06-16 21:44:05 +00:00
|
|
|
self.duration = self.end - self.start
|
2012-06-06 19:49:32 +00:00
|
|
|
super(Sequence, self).save(*args, **kwargs)
|
|
|
|
|
2012-06-18 14:36:04 +00:00
|
|
|
@property
|
|
|
|
def public_id(self):
|
2013-03-06 08:40:52 +00:00
|
|
|
return u"%s/%0.03f-%0.03f" % (self.sort.item.itemId, float(self.start), float(self.end))
|
2012-06-18 14:36:04 +00:00
|
|
|
|
2012-06-06 19:49:32 +00:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.public_id
|
|
|
|
|
|
|
|
def json(self, keys=None, user=None):
|
|
|
|
j = {
|
|
|
|
'id': self.public_id,
|
2013-03-06 08:40:52 +00:00
|
|
|
'hash': format_hash(self.hash),
|
2012-06-16 09:00:45 +00:00
|
|
|
'in': float('%0.03f' % self.start),
|
|
|
|
'out': float('%0.03f' % self.end),
|
2012-06-06 19:49:32 +00:00
|
|
|
}
|
|
|
|
if keys:
|
|
|
|
for key in keys:
|
|
|
|
if key not in j:
|
2013-03-06 08:40:52 +00:00
|
|
|
j[key] = self.sort.item.json.get(key)
|
2012-06-06 19:49:32 +00:00
|
|
|
return j
|