# Path: bundestagger/helper/utils.py
# def padd_zeros(num, padd_base=3):
#     num = str(num)
#     for k in range(padd_base-len(num)):
#         num = "0"+num
#     return num
# 
# def invalidate_cache(path, *args):
#     page = None
#     if len(args)==2:
#         index, max_per_page = args
#         page = get_page(index, max_per_page)
#     if page is not None and page != 1:
#         request = FakeRequest(path, {"page": page})
#     else:
#         request = FakeRequest(path)
#     cache.delete(get_cache_key(request))
# 
# def invalidate_cache_all_pages(path, highest, max_per_page):
#     last = get_page(highest,max_per_page)
#     request = FakeRequest(path)
#     cache.delete(get_cache_key(request))
#     for i in range(last-1)+2:
#         request = FakeRequest(path, {"page": last})
#         cache.delete(get_cache_key(request))
# 
# def get_page(index, max_per_page):
#     return int(math.ceil(float(index) / max_per_page))


import datetime
import re
import django.dispatch
from difflib import SequenceMatcher
from django.db import models
from django.db.models import Q
from django.conf import settings
from django.core.urlresolvers import reverse
from django.conf import settings
from django.template.defaultfilters import slugify
from tagging.fields import TagField
from bundestagger.helper.utils import padd_zeros, invalidate_cache, invalidate_cache_all_pages, get_page
    def clear_cache(self):
        url = self.get_base_url()
        invalidate_cache(url, self.ordernr, settings.SPEECHES_PER_PAGE)
        
class SpeechPartManager(models.Manager):
    def list(self):
        return self.order_by("ordernr").select_related()
    
class SpeechPart(models.Model):
    speech = models.ForeignKey(Speech)
    ordernr = models.IntegerField()
    text = models.TextField(blank=True)
    
    objects = SpeechPartManager()
    
    changed_signal = django.dispatch.Signal(providing_args=["old_text", "new_text"])
    
    annotatable = True
    
    def __unicode__(self):
        return u"Teil %d der %s" % (self.ordernr, self.speech)
    
    def append_text(self, text):
        text = text.strip()
        if self.text == u"":
            self.text = text
        else:
            if self.text.endswith("-"):
                self.text = self.text[:-1] + text
            else:

# Path: bundestagger/helper/utils.py
# def padd_zeros(num, padd_base=3):
#     num = str(num)
#     for k in range(padd_base-len(num)):
#         num = "0"+num
#     return num
# 
# def invalidate_cache(path, *args):
#     page = None
#     if len(args)==2:
#         index, max_per_page = args
#         page = get_page(index, max_per_page)
#     if page is not None and page != 1:
#         request = FakeRequest(path, {"page": page})
#     else:
#         request = FakeRequest(path)
#     cache.delete(get_cache_key(request))
# 
# def invalidate_cache_all_pages(path, highest, max_per_page):
#     last = get_page(highest,max_per_page)
#     request = FakeRequest(path)
#     cache.delete(get_cache_key(request))
#     for i in range(last-1)+2:
#         request = FakeRequest(path, {"page": last})
#         cache.delete(get_cache_key(request))
# 
# def get_page(index, max_per_page):
#     return int(math.ceil(float(index) / max_per_page))


# Path: bundestagger/bundestag/models.py
import datetime
import re
import django.dispatch
from difflib import SequenceMatcher
from django.db import models
from django.db.models import Q
from django.conf import settings
from django.core.urlresolvers import reverse
from django.conf import settings
from django.template.defaultfilters import slugify
from tagging.fields import TagField
from bundestagger.helper.utils import padd_zeros, invalidate_cache, invalidate_cache_all_pages, get_page

    def clear_cache(self):
        url = self.get_base_url()
        invalidate_cache(url, self.ordernr, settings.SPEECHES_PER_PAGE)
        
class SpeechPartManager(models.Manager):
    def list(self):
        return self.order_by("ordernr").select_related()
    
class SpeechPart(models.Model):
    speech = models.ForeignKey(Speech)
    ordernr = models.IntegerField()
    text = models.TextField(blank=True)
    
    objects = SpeechPartManager()
    
    changed_signal = django.dispatch.Signal(providing_args=["old_text", "new_text"])
    
    annotatable = True
    
    def __unicode__(self):
        return u"Teil %d der %s" % (self.ordernr, self.speech)
    
    def append_text(self, text):
        text = text.strip()
        if self.text == u"":
            self.text = text
        else:
            if self.text.endswith("-"):
                self.text = self.text[:-1] + text
            else:

                self.text = self.text+ " " + text