# 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
    
    tags = models.TextField(blank=True)
    
    objects = ParliamentSessionManager()
    
    def __unicode__(self):
        return u"%d. Sitzung  (%s) des %d. Deutschen Bundestags" % (self.number, self.date.strftime("%d.%m.%Y"), self.parliament.number)
        
    def get_absolute_url(self):
        return reverse("bundestagger-bundestag-show_session", args=(self.parliament.number, self.number))
        
    @property
    def tag_list(self):
        return [tag.strip() for tag in self.tags.split(",") if len(tag.strip())>0]
        
    def update_tags(self,new_tags):
        tag_set = set([t.strip() for t in self.tags.split(",") if len(t.strip())>0])
        tag_set.update([t.strip() for t in new_tags.split(",") if len(t.strip())>0])
        self.tags = ",".join(tag_set)
        self.clear_cache()
        self.save()
    
    @property
    def document_url(self):
        return "http://dip21.bundestag.de/dip21/btp/%(leg)d/%(leg)d%(id)s.pdf" % {"leg":self.parliament.number, "id": padd_zeros(self.number)}
    
    def clear_cache(self):
        invalidate_cache(reverse("bundestagger-bundestag-list_sessions"))
        last_speech = self.speech_set.order_by("-ordernr")[0]
        url = last_speech.get_base_url()

# 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

    
    tags = models.TextField(blank=True)
    
    objects = ParliamentSessionManager()
    
    def __unicode__(self):
        return u"%d. Sitzung  (%s) des %d. Deutschen Bundestags" % (self.number, self.date.strftime("%d.%m.%Y"), self.parliament.number)
        
    def get_absolute_url(self):
        return reverse("bundestagger-bundestag-show_session", args=(self.parliament.number, self.number))
        
    @property
    def tag_list(self):
        return [tag.strip() for tag in self.tags.split(",") if len(tag.strip())>0]
        
    def update_tags(self,new_tags):
        tag_set = set([t.strip() for t in self.tags.split(",") if len(t.strip())>0])
        tag_set.update([t.strip() for t in new_tags.split(",") if len(t.strip())>0])
        self.tags = ",".join(tag_set)
        self.clear_cache()
        self.save()
    
    @property
    def document_url(self):
        return "http://dip21.bundestag.de/dip21/btp/%(leg)d/%(leg)d%(id)s.pdf" % {"leg":self.parliament.number, "id": padd_zeros(self.number)}
    
    def clear_cache(self):
        invalidate_cache(reverse("bundestagger-bundestag-list_sessions"))
        last_speech = self.speech_set.order_by("-ordernr")[0]
        url = last_speech.get_base_url()

        invalidate_cache_all_pages(url, last_speech.ordernr, settings.SPEECHES_PER_PAGE)