from django.db import models

# Create your models here.
class Schema(models.Model):
    
    name = models.CharField(max_length=200)
    description = models.TextField()


class Code(models.Model):
    
    name = models.CharField(max_length=200)
    description = models.TextField()
    active_instances = models.PositiveIntegerField(default=0)
    schema = models.ForeignKey(Schema, related_name="codes")
    code_type = models.IntegerField(default=0)

    def __unicode__(self):
        if self.description:
            return "%s/%s (%d): %s" % (self.schema_id, self.name, self.id, self.description)
        else:
            return "%s/%s (%d)" % (self.schema_id, self.name, self.id)
        

class DataSet(models.Model):
    
    name = models.CharField(max_length=100)
    created = models.DateTimeField()


class Session(models.Model):
    
    set = models.ForeignKey(DataSet)
    started = models.DateTimeField()
    ended = models.DateTimeField()

    def __unicode__(self):
        return "%d (%s - %s)" % (self.id, str(self.started), str(self.ended))
        
class Participant(models.Model):
    
    name = models.CharField(max_length=100)
    description = models.TextField()

    def __unicode__(self):
        return self.name

class Message(models.Model):
    
    session = models.ForeignKey(Session)
    idx = models.IntegerField()
    time = models.DateTimeField()
    type = models.IntegerField()
    participant = models.ForeignKey(Participant, related_name='messages')
    message = models.TextField()

    codes = models.ManyToManyField(Code, through='CodeInstance')

    @classmethod
    def get_between(cls, start, end):
        """
        Get messages that are inclusively between the two messages, or two dates.

        Takes into account the exact ordering of messages,
        meaning that you won't get messages at the same time but after the last message, for example.
        """

        if isinstance(start, Message):
            after_first = ~models.Q(session=start.session) | models.Q(idx__gte=start.idx)
            after_first = models.Q(time__gte=start.time) & after_first
        else:
            after_first = models.Q(time__gte=start)

        if isinstance(end, Message):
            before_last = ~models.Q(session=end.session) | models.Q(idx__lte=end.idx)
            before_last = models.Q(time__lte=end.time) & before_last
        else:
            before_last = models.Q(time__lte=end)

        return cls.objects.filter(after_first, before_last)

    @property
    def text(self):
        return self.message

    @property
    def user_name(self):
        return self.participant.name

    @property
    def created_at(self):
        return self.time

class User(models.Model):
    
    name = models.CharField(max_length=100)
    full_name = models.CharField(max_length=250)
    email = models.CharField(max_length=250)

    def __unicode__(self):
        return self.name
        
class AbstractCodeInstance(models.Model):
    class Meta:
        abstract = True

    code = models.ForeignKey(Code)
    message = models.ForeignKey(Message)
    added = models.DateTimeField()


class CodeInstance(AbstractCodeInstance):
    
    user = models.ForeignKey(User)
    task_id = models.PositiveIntegerField()
    intensity = models.FloatField()
    flag = models.IntegerField()

[((6, 11, 6, 43), 'django.db.models.CharField', 'models.CharField', (), '', False, 'from django.db import models\n'), ((7, 18, 7, 36), 'django.db.models.TextField', 'models.TextField', ({}, {}), '()', False, 'from django.db import models\n'), ((12, 11, 12, 43), 'django.db.models.CharField', 'models.CharField', (), '', False, 'from django.db import models\n'), ((13, 18, 13, 36), 'django.db.models.TextField', 'models.TextField', ({}, {}), '()', False, 'from django.db import models\n'), ((14, 23, 14, 61), 'django.db.models.PositiveIntegerField', 'models.PositiveIntegerField', (), '', False, 'from django.db import models\n'), ((15, 13, 15, 60), 'django.db.models.ForeignKey', 'models.ForeignKey', (), '', False, 'from django.db import models\n'), ((16, 16, 16, 46), 'django.db.models.IntegerField', 'models.IntegerField', (), '', False, 'from django.db import models\n'), ((27, 11, 27, 43), 'django.db.models.CharField', 'models.CharField', (), '', False, 'from django.db import models\n'), ((28, 14, 28, 36), 'django.db.models.DateTimeField', 'models.DateTimeField', ({}, {}), '()', False, 'from django.db import models\n'), ((33, 10, 33, 36), 'django.db.models.ForeignKey', 'models.ForeignKey', ({(33, 28, 33, 35): 'DataSet'}, {}), '(DataSet)', False, 'from django.db import models\n'), ((34, 14, 34, 36), 'django.db.models.DateTimeField', 'models.DateTimeField', ({}, {}), '()', False, 'from django.db import models\n'), ((35, 12, 35, 34), 'django.db.models.DateTimeField', 'models.DateTimeField', ({}, {}), '()', False, 'from django.db import models\n'), ((42, 11, 42, 43), 'django.db.models.CharField', 'models.CharField', (), '', False, 'from django.db import models\n'), ((43, 18, 43, 36), 'django.db.models.TextField', 'models.TextField', ({}, {}), '()', False, 'from django.db import models\n'), ((50, 14, 50, 40), 'django.db.models.ForeignKey', 'models.ForeignKey', ({(50, 32, 50, 39): 'Session'}, {}), '(Session)', False, 'from django.db import models\n'), ((51, 10, 51, 31), 'django.db.models.IntegerField', 'models.IntegerField', ({}, {}), '()', False, 'from django.db import models\n'), ((52, 11, 52, 33), 'django.db.models.DateTimeField', 'models.DateTimeField', ({}, {}), '()', False, 'from django.db import models\n'), ((53, 11, 53, 32), 'django.db.models.IntegerField', 'models.IntegerField', ({}, {}), '()', False, 'from django.db import models\n'), ((54, 18, 54, 73), 'django.db.models.ForeignKey', 'models.ForeignKey', (), '', False, 'from django.db import models\n'), ((55, 14, 55, 32), 'django.db.models.TextField', 'models.TextField', ({}, {}), '()', False, 'from django.db import models\n'), ((57, 12, 57, 64), 'django.db.models.ManyToManyField', 'models.ManyToManyField', (), '', False, 'from django.db import models\n'), ((96, 11, 96, 43), 'django.db.models.CharField', 'models.CharField', (), '', False, 'from django.db import models\n'), ((97, 16, 97, 48), 'django.db.models.CharField', 'models.CharField', (), '', False, 'from django.db import models\n'), ((98, 12, 98, 44), 'django.db.models.CharField', 'models.CharField', (), '', False, 'from django.db import models\n'), ((107, 11, 107, 34), 'django.db.models.ForeignKey', 'models.ForeignKey', ({(107, 29, 107, 33): 'Code'}, {}), '(Code)', False, 'from django.db import models\n'), ((108, 14, 108, 40), 'django.db.models.ForeignKey', 'models.ForeignKey', ({(108, 32, 108, 39): 'Message'}, {}), '(Message)', False, 'from django.db import models\n'), ((109, 12, 109, 34), 'django.db.models.DateTimeField', 'models.DateTimeField', ({}, {}), '()', False, 'from django.db import models\n'), ((114, 11, 114, 34), 'django.db.models.ForeignKey', 'models.ForeignKey', ({(114, 29, 114, 33): 'User'}, {}), '(User)', False, 'from django.db import models\n'), ((115, 14, 115, 43), 'django.db.models.PositiveIntegerField', 'models.PositiveIntegerField', ({}, {}), '()', False, 'from django.db import models\n'), ((116, 16, 116, 35), 'django.db.models.FloatField', 'models.FloatField', ({}, {}), '()', False, 'from django.db import models\n'), ((117, 11, 117, 32), 'django.db.models.IntegerField', 'models.IntegerField', ({}, {}), '()', False, 'from django.db import models\n'), ((72, 26, 72, 51), 'django.db.models.Q', 'models.Q', (), '', False, 'from django.db import models\n'), ((78, 26, 78, 49), 'django.db.models.Q', 'models.Q', (), '', False, 'from django.db import models\n'), ((69, 61, 69, 89), 'django.db.models.Q', 'models.Q', (), '', False, 'from django.db import models\n'), ((70, 26, 70, 56), 'django.db.models.Q', 'models.Q', (), '', False, 'from django.db import models\n'), ((75, 59, 75, 85), 'django.db.models.Q', 'models.Q', (), '', False, 'from django.db import models\n'), ((76, 26, 76, 54), 'django.db.models.Q', 'models.Q', (), '', False, 'from django.db import models\n'), ((69, 27, 69, 58), 'django.db.models.Q', 'models.Q', (), '', False, 'from django.db import models\n'), ((75, 27, 75, 56), 'django.db.models.Q', 'models.Q', (), '', False, 'from django.db import models\n')]