# Path: bundestagger/bundestagging/views.py
# def add_tags(request, content_type=None, object_id=None, ajax=False):
#     if request.method=="POST":
#         content_type = get_object_or_404(ContentType, id = int(content_type))
#         tagged_object = content_type.get_object_for_this_type(id = int(object_id))
#         if not hasattr(tagged_object,"tags"):
#             return HttpResponseBadRequest()
#         li = tagged_object.tags.split(",")
#         s = set([l.strip() for l in li if len(l.strip())>0])
#         new_tags = parse_tag_input(request.POST["tags"])
#         s.update(new_tags)
#         tagged_object.tags = ",".join(s)
#         tagged_object.save()
#         tagged_object.clear_cache()
#         if request.is_ajax():
#             t = loader.get_template("tagging/_tag_list.html")
#             return HttpResponse(t.render(RequestContext(request, {"tags": new_tags})))
#             
#         else:
#             next = "/"
#             if "next" in request.POST:
#                 next = request.POST["next"]
#             else:
#                 next = tagged_object.get_absolute_url()
#             return HttpResponseRedirect(next)
# 
# def autocomplete(request):
#     q = request.GET.get("q","")
#     try:
#         limit = int(request.GET.get("limit",10))
#     except TypeError:
#         limit = 10
#     return HttpResponse("\n".join(map(lambda x:x.name,Tag.objects.filter(name__istartswith=q)[:limit])),mimetype="text/plain")
# 
# Path: bundestagger/bundestag/models.py
# class Speech(models.Model):
#     session = models.ForeignKey(ParliamentSession)
#     ordernr = models.IntegerField()
#     speaker = models.ForeignKey(Politician)
#     start = models.DateTimeField(blank=True, default=None, null=True)
#     end = models.DateTimeField(blank=True, default=None, null=True)
#     webtv = models.IntegerField(blank=True, null=True)
#     
#     tags = TagField(blank=True)
#     
#     def save(self, *args, **kwargs):
#         super(Speech,self).save(*args, **kwargs)
#         self.session.update_tags(self.tags)
#         
#     
#     def __unicode__(self):
#         return u"Rede von %s in der %s" % (self.speaker, self.session)
#         
#     def text(self):
#         return u"\n".join(map(lambda x:x.text, self.speechpart_set.order_by("ordernr")))
#         
#     @property
#     def tag_list(self):
#         return [tag.strip() for tag in self.tags.split(",") if len(tag.strip())>0]
#     
#     @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)


from django.conf.urls.defaults import *
from bundestagger.bundestagging.views import add_tags, autocomplete
from bundestagger.bundestag.models import Speech

urlpatterns = patterns('',
    url(r'^add/(?P<content_type>\d+)/(?P<object_id>\d+)/$', add_tags, name='tagging_add-tags'),
    url(r'^autocomplete/$', autocomplete, name='tagging_autocomplete'),
)

urlpatterns += patterns('',

# Path: bundestagger/bundestagging/views.py
# def add_tags(request, content_type=None, object_id=None, ajax=False):
#     if request.method=="POST":
#         content_type = get_object_or_404(ContentType, id = int(content_type))
#         tagged_object = content_type.get_object_for_this_type(id = int(object_id))
#         if not hasattr(tagged_object,"tags"):
#             return HttpResponseBadRequest()
#         li = tagged_object.tags.split(",")
#         s = set([l.strip() for l in li if len(l.strip())>0])
#         new_tags = parse_tag_input(request.POST["tags"])
#         s.update(new_tags)
#         tagged_object.tags = ",".join(s)
#         tagged_object.save()
#         tagged_object.clear_cache()
#         if request.is_ajax():
#             t = loader.get_template("tagging/_tag_list.html")
#             return HttpResponse(t.render(RequestContext(request, {"tags": new_tags})))
#             
#         else:
#             next = "/"
#             if "next" in request.POST:
#                 next = request.POST["next"]
#             else:
#                 next = tagged_object.get_absolute_url()
#             return HttpResponseRedirect(next)
# 
# def autocomplete(request):
#     q = request.GET.get("q","")
#     try:
#         limit = int(request.GET.get("limit",10))
#     except TypeError:
#         limit = 10
#     return HttpResponse("\n".join(map(lambda x:x.name,Tag.objects.filter(name__istartswith=q)[:limit])),mimetype="text/plain")
# 
# Path: bundestagger/bundestag/models.py
# class Speech(models.Model):
#     session = models.ForeignKey(ParliamentSession)
#     ordernr = models.IntegerField()
#     speaker = models.ForeignKey(Politician)
#     start = models.DateTimeField(blank=True, default=None, null=True)
#     end = models.DateTimeField(blank=True, default=None, null=True)
#     webtv = models.IntegerField(blank=True, null=True)
#     
#     tags = TagField(blank=True)
#     
#     def save(self, *args, **kwargs):
#         super(Speech,self).save(*args, **kwargs)
#         self.session.update_tags(self.tags)
#         
#     
#     def __unicode__(self):
#         return u"Rede von %s in der %s" % (self.speaker, self.session)
#         
#     def text(self):
#         return u"\n".join(map(lambda x:x.text, self.speechpart_set.order_by("ordernr")))
#         
#     @property
#     def tag_list(self):
#         return [tag.strip() for tag in self.tags.split(",") if len(tag.strip())>0]
#     
#     @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)


# Path: bundestagger/bundestagging/urls.py
from django.conf.urls.defaults import *
from bundestagger.bundestagging.views import add_tags, autocomplete
from bundestagger.bundestag.models import Speech


urlpatterns = patterns('',
    url(r'^add/(?P<content_type>\d+)/(?P<object_id>\d+)/$', add_tags, name='tagging_add-tags'),
    url(r'^autocomplete/$', autocomplete, name='tagging_autocomplete'),
)

urlpatterns += patterns('',

    ('^(?P<tag>[^/]+)$', 'tagging.views.tagged_object_list', {'queryset_or_model': Speech, 'template_name': "bundestagging/tag.html"}, 'tagging-tag_url'),