# Path: bundestagger/account/auth.py
# def logged_in(func):
#     def loggedin_func(*args, **kwargs):
#         request = args[0]
#         if "bundesuser" in request.session:
#             request.bundesuser = request.session["bundesuser"]
#             return func(*args, **kwargs)
#         raise PermissionDenied
#     return loggedin_func
# 
# Path: bundestagger/helper/utils.py
# 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 is_post(func):
#     return is_method("POST", func)
# 
# def is_get(func):
#     return is_method("GET", func)


import re
from django.shortcuts import render_to_response, get_object_or_404, redirect
from django.http import HttpResponse, Http404, HttpResponseBadRequest, HttpResponseForbidden, HttpResponseNotAllowed
from django.template import RequestContext
from django.utils.itercompat import groupby
from django.core.paginator import Paginator, EmptyPage, InvalidPage
from django.conf import settings
from django.db.models import Q
from improvetext.models import Improvement
from models import ParliamentSession, Speech, SpeechPart, Event, Politician
from forms import EditSpeechPartForm
from bundestagger.account.auth import logged_in
from bundestagger.helper.utils import invalidate_cache, is_post, is_get
    new_events = {}
    for k,v in events:
        new_events[k] = list(v)
    return render_to_response("bundestag/show.html", RequestContext(request, {"session" : parliament_session, 
        "speech_parts": speech_parts,
        "speeches": new_speeches, 
        "events": new_events,
        "paginator": paginator,
        "page_obj":page_obj,
#        "sessiontops":sessiontops
        "all_speeches": all_speeches
        }))

@is_post
@logged_in
def edit_speechpart(request, speechpart_id):
    try:
        speech_part = SpeechPart.objects.select_related("speech").get(id=int(speechpart_id))
    except SpeechPart.DoesNotExist:
        raise Http404
    edit_form = EditSpeechPartForm(request.POST)
    if edit_form.is_valid():
        if request.bundesuser.is_moderator:
            speech_part.set_text(edit_form.cleaned_data["text"])
            messages.add_message(request, messages.INFO, u"Änderung gespeichert")
        else:
            Improvement.objects.suggest(obj=speech_part, user=request.bundesuser, field="text", change=edit_form.cleaned_data["text"])
            messages.add_message(request, messages.INFO, u"Änderung wurde vorgeschlagen, ist aber noch nicht sichtbar!")
    return redirect(speech_part.speech.get_absolute_url())


# Path: bundestagger/account/auth.py
# def logged_in(func):
#     def loggedin_func(*args, **kwargs):
#         request = args[0]
#         if "bundesuser" in request.session:
#             request.bundesuser = request.session["bundesuser"]
#             return func(*args, **kwargs)
#         raise PermissionDenied
#     return loggedin_func
# 
# Path: bundestagger/helper/utils.py
# 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 is_post(func):
#     return is_method("POST", func)
# 
# def is_get(func):
#     return is_method("GET", func)


# Path: bundestagger/bundestag/views.py
import re
from django.shortcuts import render_to_response, get_object_or_404, redirect
from django.http import HttpResponse, Http404, HttpResponseBadRequest, HttpResponseForbidden, HttpResponseNotAllowed
from django.template import RequestContext
from django.utils.itercompat import groupby
from django.core.paginator import Paginator, EmptyPage, InvalidPage
from django.conf import settings
from django.db.models import Q
from improvetext.models import Improvement
from models import ParliamentSession, Speech, SpeechPart, Event, Politician
from forms import EditSpeechPartForm
from bundestagger.account.auth import logged_in
from bundestagger.helper.utils import invalidate_cache, is_post, is_get

    new_events = {}
    for k,v in events:
        new_events[k] = list(v)
    return render_to_response("bundestag/show.html", RequestContext(request, {"session" : parliament_session, 
        "speech_parts": speech_parts,
        "speeches": new_speeches, 
        "events": new_events,
        "paginator": paginator,
        "page_obj":page_obj,
#        "sessiontops":sessiontops
        "all_speeches": all_speeches
        }))

@is_post
@logged_in
def edit_speechpart(request, speechpart_id):
    try:
        speech_part = SpeechPart.objects.select_related("speech").get(id=int(speechpart_id))
    except SpeechPart.DoesNotExist:
        raise Http404
    edit_form = EditSpeechPartForm(request.POST)
    if edit_form.is_valid():
        if request.bundesuser.is_moderator:
            speech_part.set_text(edit_form.cleaned_data["text"])
            messages.add_message(request, messages.INFO, u"Änderung gespeichert")
        else:
            Improvement.objects.suggest(obj=speech_part, user=request.bundesuser, field="text", change=edit_form.cleaned_data["text"])
            messages.add_message(request, messages.INFO, u"Änderung wurde vorgeschlagen, ist aber noch nicht sichtbar!")
    return redirect(speech_part.speech.get_absolute_url())


@is_post