from django.db import models
from django.db.models import Q
from .debug import CollisionException

class MutexModelOptions(object):
    def __init__(self, opts):
        if opts:
            for key, value in opts.__dict__.iteritems():
                setattr(self, key, value)


class MutexModelBase(models.base.ModelBase):
    def __new__(cls, name, bases, attrs):
        new = super(MutexModelBase, cls).__new__(cls, name, bases, attrs)
        meta = attrs.pop('MutexMeta', None)
        setattr(new, '_mutex_meta', MutexModelOptions(meta))
        return new

### Custom queryset, that ignores some updates
class MutexQuerySet(models.query.QuerySet):
    def update(self, **kwargs):
        if 'start_time' in kwargs.keys() or 'end_time' in kwargs.keys():
            raise NotImplementedError("Mutex event start and end time can't be updated via queryset update method. Use object save method instead!")
        return super(MutexQuerySet, self).update(**kwargs)

class MutexManager(models.Manager):
    def get_query_set(self):
        return MutexQuerySet(self.model, using=self._db)

    def bulk_create(self, objs, batch_size=None):
        raise NotImplementedError("Mutex event can't be bulk created!")

    #def update(self, **kwargs):
    #    return self.get_query_set().update(**kwargs, obj)

    def overlapping_events(self, start, end, obj=None):
        filters = (
            Q(start_time__range = [start, end]) | 
            Q(end_time__range = [start, end]) | 
            Q(start_time__lte = start, end_time__gte = end),
        )
        filters2 = {}
        exclude = getattr(self.model._mutex_meta, 'exclude', Q())
        if obj:
            for field in getattr(self.model._mutex_meta, 'collision_fields', []):
                if getattr(obj, field, None) is not None:
                    filters2[field] = getattr(obj, field)
            ## when we modify the event, then exclude that from the queryset
            if obj.pk is not None:
                if len(exclude):
                    exclude = Q(pk=obj.pk) | exclude
                else:
                    exclude = Q(pk=obj.pk)
        return super(MutexManager, self).get_query_set()\
                                        .filter(*filters, **filters2)\
                                        .exclude(*(exclude, ))\
                                        .values_list('id')

class MutexEvent(models.Model):
    __metaclass__ = MutexModelBase

    start_time = models.DateTimeField(blank=False, null=False, db_index=True)
    end_time = models.DateTimeField(blank=False, null=False, db_index=True)
    objects = MutexManager()

    class Meta:
        abstract = True

    def save(self, *args, **kwargs):
        self.full_clean()
        overlaps = self._default_manager.overlapping_events(start=self.start_time, end=self.end_time, obj=self)
        if overlaps.count():
            raise CollisionException(start=self.start_time, end=self.end_time, queryset=overlaps)
        return super(MutexEvent, self).save(*args, **kwargs)
@@ -1,20 +1,53 @@
+from django.conf import settings%0A
 from django.db impor
@@ -55,16 +55,16 @@
  models%0A
-
 from dja
@@ -1384,32 +1384,109 @@
 end, obj=None):%0A
+        if getattr(settings, 'MUTEX_INTERVALL_TYPE', 'close') == 'open':%0A    
         filters 
@@ -1493,32 +1493,36 @@
 = (%0A            
+    
 Q(start_time__ra
@@ -1523,82 +1523,95 @@
 me__
-range = %5Bstart, end%5D) %7C %0A            Q(end_time__range = %5Bstart, end%5D) %7C %0A
+lt=end), Q(end_time__gt=start)%0A            )%0A        else:%0A            filters = (%0A    
     
@@ -1639,18 +1639,17 @@
 _lte
- = start, 
+=end), Q(
 end_
@@ -1661,17 +1661,21 @@
 _gte
- = end),%0A
+=start) %0A    
     
@@ -3093,16 +3093,16 @@
 erlaps)%0A
-
         
@@ -3137,24 +3137,25 @@
 f).save(*args, **kwargs)
+%0A

Add intervall type settings