60 lines
1.7 KiB
Python
60 lines
1.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
from __future__ import division, print_function, absolute_import
|
|
|
|
from django.db import models
|
|
from django.utils.encoding import python_2_unicode_compatible
|
|
|
|
from item.models import ItemSort
|
|
|
|
from . import managers
|
|
|
|
|
|
def parse_hash(value):
|
|
return int(value, 16) - 9223372036854775808
|
|
|
|
def format_hash(value):
|
|
return hex(value + 9223372036854775808)[2:-1].upper()
|
|
|
|
@python_2_unicode_compatible
|
|
class Sequence(models.Model):
|
|
class Meta:
|
|
unique_together = ("sort", "start", "end", "mode")
|
|
|
|
MODE = {
|
|
'shape': 0,
|
|
'color': 1
|
|
}
|
|
mode = models.IntegerField(choices=sorted(zip(MODE.values(), list(MODE)), key=lambda k: k[0]), default=0)
|
|
sort = models.ForeignKey(ItemSort, null=True, related_name='sequences')
|
|
|
|
hash = models.BigIntegerField(db_index=True, default=-9223372036854775808)
|
|
start = models.FloatField(default=-1)
|
|
end = models.FloatField(default=-1)
|
|
duration = models.FloatField(default=0)
|
|
|
|
objects = managers.SequenceManager()
|
|
|
|
def save(self, *args, **kwargs):
|
|
self.duration = self.end - self.start
|
|
super(Sequence, self).save(*args, **kwargs)
|
|
|
|
@property
|
|
def public_id(self):
|
|
return u"%s/%0.03f-%0.03f" % (self.sort.item.public_id, float(self.start), float(self.end))
|
|
|
|
def __str__(self):
|
|
return self.public_id
|
|
|
|
def json(self, keys=None, user=None):
|
|
j = {
|
|
'id': self.public_id,
|
|
'hash': format_hash(self.hash),
|
|
'in': float('%0.03f' % self.start),
|
|
'out': float('%0.03f' % self.end),
|
|
}
|
|
if keys:
|
|
for key in keys:
|
|
if key not in j:
|
|
j[key] = self.sort.item.json.get(key)
|
|
return j
|