# 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
    
    @property
    def webtv_url(self):
        if self.webtv is not None:
            return u"http://webtv.bundestag.de/iptv/player/macros/_v_f_514_de/od_player.html?singleton=true&content=%d" % (self.webtv,)
        return u""
    
    @property
    def webtv_search(self):
        url = u"http://webtv.bundestag.de/iptv/player/macros/bttv/list.html?pageOffset=0&pageLength=40&sort=2&lastName=%(lastname)s&firstName=%(firstname)s&fraction=&meetingNumber=%(session)s&period=%(period)s&startDay=&endDay=&topic=&submit=Suchen"
        return url % {  "firstname": self.speaker.first_name, 
                        "lastname": self.speaker.last_name,
                        "session": self.session.number,
                        "period": self.session.parliament.number
                        }
    
    def get_base_url(self):
        return reverse("bundestagger-bundestag-show_session", args=(self.session.parliament.number, self.session.number))
        
    def get_absolute_url(self):
        page = get_page(self.ordernr, settings.SPEECHES_PER_PAGE)
        if page == 1:
            page = ""
        else:
            page = "?page=%d" % page
        fragment = "#speech-%d" % self.id
        return self.get_base_url() + page + fragment

    def clear_cache(self):
        url = self.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

    
    @property
    def webtv_url(self):
        if self.webtv is not None:
            return u"http://webtv.bundestag.de/iptv/player/macros/_v_f_514_de/od_player.html?singleton=true&content=%d" % (self.webtv,)
        return u""
    
    @property
    def webtv_search(self):
        url = u"http://webtv.bundestag.de/iptv/player/macros/bttv/list.html?pageOffset=0&pageLength=40&sort=2&lastName=%(lastname)s&firstName=%(firstname)s&fraction=&meetingNumber=%(session)s&period=%(period)s&startDay=&endDay=&topic=&submit=Suchen"
        return url % {  "firstname": self.speaker.first_name, 
                        "lastname": self.speaker.last_name,
                        "session": self.session.number,
                        "period": self.session.parliament.number
                        }
    
    def get_base_url(self):
        return reverse("bundestagger-bundestag-show_session", args=(self.session.parliament.number, self.session.number))
        
    def get_absolute_url(self):
        page = get_page(self.ordernr, settings.SPEECHES_PER_PAGE)
        if page == 1:
            page = ""
        else:
            page = "?page=%d" % page
        fragment = "#speech-%d" % self.id
        return self.get_base_url() + page + fragment

    def clear_cache(self):
        url = self.get_base_url()

        invalidate_cache(url, self.ordernr, settings.SPEECHES_PER_PAGE)