
from django.db.models import Model, ForeignKey, PositiveIntegerField
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.generic import \
    GenericRelation, GenericForeignKey

from norc.core.models.queue import Queue

class QueueGroup(Queue):
    """A group of Norc queues."""
    
    class Meta:
        app_label = "core"
        db_table = "norc_queuegroup"
    
    @property
    def queues(self):
        return [i.queue for i in self.items.all()]
    
    def peek(self):
        """Retrieves the next item but does not remove it from the queue.
        
        Returns None if the queue is empty.
        
        """
        for q in self.queues:
            try:
                i = q.peek()
                if i != None:
                    return i
            except:
                pass
        return None
    
    def pop(self):
        """Retrieves the next item and removes it from the queue."""
        for q in self.queues:
            try:
                i = q.pop()
                if i != None:
                    return i
            except:
                pass
        return None
    
    def push(self, item):
        raise NotImplementedError("Cannot push to a queue group.")
    
    def count(self):
        return sum([q.count() for q in self.queues])
    

class QueueGroupItem(Model):
    """Maps queues to QueueGroups."""
    
    class Meta:
        app_label = "core"
        db_table = "norc_queuegroupitem"
        ordering = ["priority"]
        unique_together = [["queue_type", "queue_id"],
            ["queue_type", "queue_id", "priority"]]
    
    group = ForeignKey(QueueGroup, related_name="items")
    
    queue_type = ForeignKey(ContentType)
    queue_id = PositiveIntegerField()
    queue = GenericForeignKey("queue_type", "queue_id")
    
    priority = PositiveIntegerField()
    
    def __unicode__(self):
        return u'[QueueGroupItem G:%s Q:%s P:%s]' % (self.group, self.queue, self.priority)
    
    __repr__ = __unicode__
    

@@ -1548,50 +1548,18 @@
 r = 
-%5B%5B%22queue_type%22, %22queue_id%22%5D,%0A            %5B
+(%22group%22, 
 %22que
@@ -1582,22 +1582,9 @@
 _id%22
-, %22priority%22%5D%5D
+)
 %0A   

Change the QueueGroupItem uniqueness properties.