-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
89 lines (67 loc) · 3.35 KB
/
views.py
File metadata and controls
89 lines (67 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from __future__ import absolute_import, unicode_literals
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.utils.translation import gettext as _
try:
from wagtail.admin import messages
from wagtail.core.models import Page
except ImportError: # fallback for Wagtail <2.0
from wagtail.wagtailadmin import messages
from wagtail.wagtailcore.models import Page
from .models import Experiment, get_backend
from .utils import get_user_id, impersonate_other_page, percentage
def record_completion(request, slug):
experiment = get_object_or_404(Experiment, slug=slug)
user_id = get_user_id(request)
experiment.record_completion_for_user(user_id, request)
return HttpResponse("OK")
def experiment_report(request, experiment_id):
# TODO: Decide if we need a custom permission to access reports
backend = get_backend()
experiment = get_object_or_404(Experiment, pk=experiment_id)
variations = experiment.get_variations()
report = backend.get_report(experiment)
report_by_variation = {}
for variation in variations:
for variation_report in report['variations']:
if variation_report['variation_pk'] == variation.pk:
if 'history' in variation_report:
for history_entry in variation_report['history']:
history_entry['conversion_rate'] = percentage(
history_entry['completion_count'],
history_entry['participant_count'],
)
variation_report['total_conversion_rate'] = percentage(
variation_report['total_completion_count'],
variation_report['total_participant_count'],
)
report_by_variation[variation] = variation_report
break
return render(request, 'experiments/report.html', {
'experiment': experiment,
'report_by_variation': report_by_variation,
'winning_variation': experiment.winning_variation if experiment.status == 'completed' else None,
})
def select_winner(request, experiment_id, variation_id):
if not request.user.has_perm('experiments.change_experiment'):
raise PermissionDenied
experiment = get_object_or_404(Experiment, pk=experiment_id)
variation = get_object_or_404(Page, pk=variation_id)
if request.method == 'POST':
experiment.select_winner(variation)
messages.success(
request,
_("Page '{0}' has been selected as the winning variation.").format(variation.title),
)
return redirect('experiments:report', experiment.pk)
def preview_for_report(request, experiment_id, page_id):
experiment = get_object_or_404(Experiment, pk=experiment_id)
page = get_object_or_404(Page, id=page_id).specific
if not page.permissions_for_user(request.user).can_publish():
raise PermissionDenied
# hack the title and page-tree-related fields to match the control page
impersonate_other_page(page, experiment.control_page)
# pass in the real user request rather than page.dummy_request(), so that request.user
# and request.revision_id will be picked up by the wagtail user bar
return page.serve_preview(request, page.default_preview_mode)