This repository has been archived by the owner on Aug 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
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
622d79e
commit ad9e180
Showing
6 changed files
with
50 additions
and
106 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
from django.conf import settings | ||
|
||
PROMOTERSCORE_PERMISSION_VIEW = getattr(settings, 'PROMOTERSCORE_PERMISSION_VIEW', lambda u: u.is_staff) | ||
PROMOTERSCORE_USER_RANGES_DEFAULT = { | ||
'promoters': [9, 10], | ||
'passive': [7, 8], | ||
'detractors': [1, 2, 3, 4, 5, 6], | ||
'skipped': [None] | ||
} | ||
|
||
PROMOTERSCORE_PERMISSION_VIEW = getattr(settings, 'PROMOTERSCORE_PERMISSION_VIEW', lambda u: u.is_staff) | ||
PROMOTERSCORE_USER_RANGES = getattr(settings, 'PROMOTERSCORE_USER_RANGES', PROMOTERSCORE_USER_RANGES_DEFAULT) |
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,19 +1,9 @@ | ||
from django.http import HttpResponse | ||
from .app_settings import PROMOTERSCORE_PERMISSION_VIEW | ||
from .utils import safe_admin_login_prompt | ||
|
||
|
||
def login_required(f): | ||
def wrap(request, *args, **kwargs): | ||
if request.user.is_anonymous(): | ||
return HttpResponse(status=401) | ||
return f(request, *args, **kwargs) | ||
return wrap | ||
|
||
|
||
def admin_required(f): | ||
def wrap(request, *args, **kwargs): | ||
if not PROMOTERSCORE_PERMISSION_VIEW(request.user): | ||
return safe_admin_login_prompt(request) | ||
return f(request, *args, **kwargs) | ||
return wrap | ||
return wrap |
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,50 +1,21 @@ | ||
import datetime | ||
from django.contrib.admin.forms import AdminAuthenticationForm | ||
from django.contrib.auth.views import login | ||
from django.contrib.auth import REDIRECT_FIELD_NAME | ||
from app_settings import PROMOTERSCORE_USER_RANGES | ||
|
||
|
||
monthDict = {1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June', 7: 'July', 8: 'August', | ||
9: 'September', 10: 'October', 11: 'November', 12: 'December'} | ||
|
||
|
||
def safe_admin_login_prompt(request): | ||
defaults = { | ||
'template_name': 'admin/login.html', | ||
'authentication_form': AdminAuthenticationForm, | ||
'extra_context': { | ||
'title': 'Log in', | ||
'app_path': request.get_full_path(), | ||
REDIRECT_FIELD_NAME: request.get_full_path(), | ||
}, | ||
} | ||
return login(request, **defaults) | ||
def count_score(score): | ||
promoters, detractors, passive = score.get('promoters', 0), score.get('detractors', 0), score.get('passive', 0) | ||
total = promoters + detractors + passive | ||
if total > 0: | ||
return round(((promoters - detractors) / float(total)) * 100, 2) | ||
else: | ||
return 'Not enough information' | ||
|
||
|
||
def get_many_previous_months(month, total_months=12): | ||
months = [] | ||
for x in xrange(total_months): | ||
month = get_previous_month(month) | ||
months.append(month) | ||
return months | ||
|
||
def get_previous_month(date): | ||
month = date.month - 1 | ||
if month == 0: | ||
return datetime.date(year=date.year-1, month=12, day=1) | ||
return datetime.date(year=date.year, month=month, day=1) | ||
|
||
def get_next_month(date): | ||
month = date.month + 1 | ||
if month == 13: | ||
return datetime.date(year=date.year+1, month=1, day=1) | ||
return datetime.date(year=date.year, month=month, day=1) | ||
|
||
def count_score(promoters, detractors, passive, skipped=None): | ||
total = promoters + detractors + passive | ||
if total > 0: | ||
promoter_percentage = float(promoters) / float(total) | ||
detractor_percentage = float(detractors) / float(total) | ||
return round((promoter_percentage - detractor_percentage) * 100, 2) | ||
else: | ||
return 'Not enough information' | ||
def get_range_name_by_score(score): | ||
for r_name, range in PROMOTERSCORE_USER_RANGES.iteritems(): | ||
if score in range: | ||
return r_name | ||
raise Exception('Range not found') |
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