From: Radek Czajka Date: Fri, 8 Feb 2019 11:46:25 +0000 (+0100) Subject: Bring Piwik for API back. X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/832bfbcc54cd38942d6df89e850d4268386d6241?hp=25cb0dced028c91d9a73d7faedae4d3e73162902 Bring Piwik for API back. --- diff --git a/src/api/urls.py b/src/api/urls.py index 8f9914113..16fdfb749 100644 --- a/src/api/urls.py +++ b/src/api/urls.py @@ -6,7 +6,7 @@ from django.conf.urls import url, include from django.views.decorators.csrf import csrf_exempt from django.views.generic import TemplateView import catalogue.views -from api import handlers +from stats.utils import piwik_track_view from api.piston_patch import oauth_user_auth from . import views @@ -23,11 +23,18 @@ urlpatterns = [ url(r'tag/(?P\d*?)/info\.html$', catalogue.views.tag_info), # reading data - url(r'^reading/(?P[a-z0-9-]+)/$', views.BookUserDataView.as_view(), name='api_reading'), - url(r'^reading/(?P[a-z0-9-]+)/(?P[a-z]+)/$', views.BookUserDataView.as_view(), name='api_reading'), - url(r'^username/$', views.UserView.as_view(), name='api_username'), + url(r'^reading/(?P[a-z0-9-]+)/$', + piwik_track_view(views.BookUserDataView.as_view()), + name='api_reading'), + url(r'^reading/(?P[a-z0-9-]+)/(?P[a-z]+)/$', + piwik_track_view(views.BookUserDataView.as_view()), + name='api_reading'), + url(r'^username/$', + piwik_track_view(views.UserView.as_view()), + name='api_username'), - url(r'^blog/$', views.BlogView.as_view()), + url(r'^blog/$', + piwik_track_view(views.BlogView.as_view())), url(r'^pictures/$', include('picture.api.urls')), url(r'^', include('social.api.urls')), diff --git a/src/catalogue/api/urls.py b/src/catalogue/api/urls.py index 07729aea8..cf73c2cff 100644 --- a/src/catalogue/api/urls.py +++ b/src/catalogue/api/urls.py @@ -3,6 +3,7 @@ # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # from django.conf.urls import include, url +from stats.utils import piwik_track_view from . import views @@ -12,45 +13,67 @@ paginate_re = r'(?:after/(?P[a-z0-9-]+)/)?(?:count/(?P[0-9]+)/)?$' urlpatterns = [ # books by collections - url(r'^collections/$', views.CollectionList.as_view(), name="api_collections"), + url(r'^collections/$', + piwik_track_view(views.CollectionList.as_view()), + name="api_collections"), url(r'^collections/(?P[^/]+)/$', - views.CollectionDetail.as_view(), name="collection-detail"), + piwik_track_view(views.CollectionDetail.as_view()), + name="collection-detail"), url(tags_re + r'books/' + paginate_re, - views.BookList.as_view(), name='catalogue_api_book_list'), + piwik_track_view(views.BookList.as_view()), + name='catalogue_api_book_list'), url(tags_re + r'parent_books/' + paginate_re, - views.BookList.as_view(), {"top_level": True}, name='catalogue_api_parent_book_list'), + piwik_track_view(views.BookList.as_view()), + {"top_level": True}, name='catalogue_api_parent_book_list'), url(tags_re + r'audiobooks/' + paginate_re, - views.BookList.as_view(), {"audiobooks": True}, name='catalogue_api_audiobook_list'), + piwik_track_view(views.BookList.as_view()), + {"audiobooks": True}, name='catalogue_api_audiobook_list'), url(tags_re + r'daisy/' + paginate_re, - views.BookList.as_view(), {"daisy": True}, name='catalogue_api_daisy_list'), + piwik_track_view(views.BookList.as_view()), + {"daisy": True}, name='catalogue_api_daisy_list'), url(r'^recommended/' + paginate_re, - views.BookList.as_view(), + piwik_track_view(views.BookList.as_view()), {"recommended": True}, name='catalogue_api_recommended_list'), url(r'^newest/$', - views.BookList.as_view(), - {"newest": True, "top_level": True, "count": 20}, name='catalogue_api_newest_list'), + piwik_track_view(views.BookList.as_view()), + {"newest": True, "top_level": True, "count": 20}, + name='catalogue_api_newest_list'), - url(r'^books/(?P[^/]+)/$', views.BookDetail.as_view(), name='catalogue_api_book'), + url(r'^books/(?P[^/]+)/$', + piwik_track_view(views.BookDetail.as_view()), + name='catalogue_api_book'), url(tags_re + r'ebooks/' + paginate_re, - views.EbookList.as_view(), + piwik_track_view(views.EbookList.as_view()), name='catalogue_api_ebook_list'), url(tags_re + r'parent_ebooks/' + paginate_re, - views.EbookList.as_view(), + piwik_track_view(views.EbookList.as_view()), {"top_level": True}, name='catalogue_api_parent_ebook_list'), - url(r'^filter-books/$', views.FilterBookList.as_view(), name='catalogue_api_filter_books'), + url(r'^filter-books/$', + piwik_track_view(views.FilterBookList.as_view()), + name='catalogue_api_filter_books'), - url(r'^epub/(?P[a-z0-9-]+)/$', views.EpubView.as_view(), name='catalogue_api_epub'), + url(r'^epub/(?P[a-z0-9-]+)/$', + piwik_track_view(views.EpubView.as_view()), + name='catalogue_api_epub'), - url(r'^preview/$', views.Preview.as_view(), name='catalogue_api_preview'), + url(r'^preview/$', + piwik_track_view(views.Preview.as_view()), + name='catalogue_api_preview'), - url(r'^(?P(?:(?:[a-z0-9-]+/){2}){1,6})fragments/$', views.FragmentList.as_view()), + url(r'^(?P(?:(?:[a-z0-9-]+/){2}){1,6})fragments/$', + piwik_track_view(views.FragmentList.as_view())), url(r'^books/(?P[a-z0-9-]+)/fragments/(?P[a-z0-9-]+)/$', - views.FragmentView.as_view(), name="catalogue_api_fragment"), + piwik_track_view(views.FragmentView.as_view()), + name="catalogue_api_fragment"), - url(r'^(?P[a-z]+)s/$', views.TagCategoryView.as_view(), name='catalogue_api_tag_list'), - url(r'^(?P[a-z]+)s/(?P[a-z0-9-]+)/$', views.TagView.as_view(), name="catalogue_api_tag"), + url(r'^(?P[a-z]+)s/$', + piwik_track_view(views.TagCategoryView.as_view()), + name='catalogue_api_tag_list'), + url(r'^(?P[a-z]+)s/(?P[a-z0-9-]+)/$', + piwik_track_view(views.TagView.as_view()), + name="catalogue_api_tag"), ] diff --git a/src/social/api/urls.py b/src/social/api/urls.py index 8ded909b4..5c633d805 100644 --- a/src/social/api/urls.py +++ b/src/social/api/urls.py @@ -3,10 +3,15 @@ # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # from django.conf.urls import include, url +from stats.utils import piwik_track_view from . import views urlpatterns = [ - url(r'^like/(?P[a-z0-9-]+)/$', views.LikeView.as_view(), name='social_api_like'), - url(r'^shelf/(?P[a-z]+)/$', views.ShelfView.as_view(), name='social_api_shelf'), + url(r'^like/(?P[a-z0-9-]+)/$', + piwik_track_view(views.LikeView.as_view()), + name='social_api_like'), + url(r'^shelf/(?P[a-z]+)/$', + piwik_track_view(views.ShelfView.as_view()), + name='social_api_shelf'), ] diff --git a/src/stats/utils.py b/src/stats/utils.py index fc473a6f0..68a01f568 100644 --- a/src/stats/utils.py +++ b/src/stats/utils.py @@ -62,3 +62,16 @@ def piwik_track(klass_or_method): return klass else: return wrap + + +def piwik_track_view(view): + if not getattr(settings, 'PIWIK_SITE_ID', 0): + return view + + def wrap(request, *args, **kwargs): + if getattr(request, 'piwik_track', True): + track_request.delay(piwik_url(request)) + return view(self, request, *args, **kwargs) + + update_wrapper(wrap, view) + return wrap