-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d40b319
commit 1cba07f
Showing
7 changed files
with
242 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,26 @@ | ||
"""mixpanel_django_graphos URL Configuration | ||
The `urlpatterns` list routes URLs to views. For more information please see: | ||
https://docs.djangoproject.com/en/1.10/topics/http/urls/ | ||
Examples: | ||
Function views | ||
1. Add an import: from my_app import views | ||
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') | ||
Class-based views | ||
1. Add an import: from other_app.views import Home | ||
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') | ||
Including another URLconf | ||
1. Import the include() function: from django.conf.urls import url, include | ||
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) | ||
""" | ||
from django.conf.urls import url | ||
from django.contrib import admin | ||
|
||
from mixpanel_django_graphos.views import ReportActivityView | ||
|
||
|
||
admin.site.index_template = 'admin/index.html' | ||
admin.autodiscover() | ||
|
||
|
||
def get_admin_urls(urls): | ||
""" | ||
Extend admin to include additional urls | ||
""" | ||
def get_urls(): | ||
my_urls = [url(r'^activity-report/$', admin.site.admin_view( | ||
ReportActivityView.as_view()), name='activity-report')] | ||
return my_urls + urls | ||
return get_urls | ||
|
||
admin_urls = get_admin_urls(admin.site.get_urls()) | ||
admin.site.get_urls = admin_urls | ||
|
||
urlpatterns = [ | ||
url(r'^admin/', admin.site.urls), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import datetime | ||
|
||
from django.http import HttpResponse | ||
from django.shortcuts import get_object_or_404 | ||
from django.views.decorators.csrf import csrf_exempt | ||
from django.conf import settings | ||
from django.contrib.auth.models import User | ||
from dateutil.parser import parse | ||
|
||
from mixpanel_django_graphos.mixpanel import Mixpanel | ||
|
||
|
||
@csrf_exempt | ||
def get_user_report(request): | ||
""" | ||
:param request: | ||
:return: Event data from mixpanel | ||
""" | ||
user_id = request.GET.get('user') | ||
|
||
# Connect to Mixpanel | ||
if settings.MIXPANEL_API_KEY and settings.MIXPANEL_SECRET_KEY: | ||
api = Mixpanel(api_secret=settings.MIXPANEL_SECRET_KEY) | ||
else: | ||
return HttpResponse('Mixpanel is not setup', status=500) | ||
|
||
params = {} | ||
params['event'] = request.GET.get('event') | ||
if request.GET.get('limit'): | ||
params['limit'] = request.GET.get('limit') | ||
|
||
# Generate query to extract events | ||
to_date = datetime.datetime.now() | ||
from_date = datetime.date(year=to_date.year, month=to_date.month, day=1) | ||
|
||
if request.GET.get('startdate') and request.GET.get('enddate'): | ||
# If a date range is provided in query parameters, use it. | ||
start_date = parse(request.GET.get('startdate')).strftime('%Y-%m-%d') | ||
end_date = parse(request.GET.get('enddate')) | ||
|
||
if end_date > datetime.datetime.now(): | ||
end_date = datetime.datetime.now() | ||
end_date = end_date.strftime('%Y-%m-%d') | ||
|
||
params.update({ | ||
'from_date': start_date, | ||
'to_date': end_date | ||
}) | ||
else: | ||
# Filter events generated in the current month | ||
params.update({ | ||
'from_date': from_date.strftime('%Y-%m-%d'), | ||
'to_date': to_date.strftime('%Y-%m-%d'), | ||
}) | ||
|
||
# Filter by user if it's defined | ||
if user_id != 'all_users': | ||
user = get_object_or_404(User, id=user_id) | ||
params['where'] = 'properties["user_id"]=={}'.format(user.pk) | ||
|
||
# Do not filter by event if 'all' param was passed | ||
if request.GET.get('all'): | ||
del params['event'] | ||
|
||
methods = ['segmentation'] | ||
|
||
# Retrieve from MixPanel | ||
mp_data = api.request(methods, params) | ||
data_records = { | ||
'mp_activity_data': mp_data | ||
} | ||
|
||
if request.GET.get('top'): | ||
params['on'] = request.GET.get('top') | ||
top_mp_data = api.request(methods, params) | ||
data_records['top_mp_data'] = top_mp_data | ||
|
||
return data_records |
Oops, something went wrong.