2013-03-20 15:05:52 -07:00
|
|
|
import hashlib
|
2014-02-07 12:32:44 -08:00
|
|
|
from simplejson.decoder import JSONDecodeError
|
|
|
|
from utils import json_functions as json
|
2009-06-16 03:08:55 +00:00
|
|
|
from django.contrib.auth.models import User
|
2009-07-25 15:24:27 +00:00
|
|
|
from django.core.cache import cache
|
2010-07-06 16:37:49 -04:00
|
|
|
from django.utils.http import urlquote
|
2010-07-24 15:54:25 -04:00
|
|
|
from django.http import HttpResponseForbidden
|
2014-02-07 12:32:44 -08:00
|
|
|
from django.http import HttpResponse
|
2011-01-20 09:57:23 -05:00
|
|
|
from django.conf import settings
|
2009-07-25 15:24:27 +00:00
|
|
|
|
2010-07-24 15:54:25 -04:00
|
|
|
def ajax_login_required(function=None):
|
|
|
|
def _dec(view_func):
|
|
|
|
def _view(request, *args, **kwargs):
|
2020-06-11 15:13:12 -04:00
|
|
|
if request.user.is_anonymous:
|
2010-07-24 15:54:25 -04:00
|
|
|
return HttpResponseForbidden()
|
|
|
|
else:
|
|
|
|
return view_func(request, *args, **kwargs)
|
|
|
|
|
|
|
|
_view.__name__ = view_func.__name__
|
|
|
|
_view.__dict__ = view_func.__dict__
|
|
|
|
_view.__doc__ = view_func.__doc__
|
|
|
|
|
|
|
|
return _view
|
|
|
|
|
|
|
|
if function is None:
|
|
|
|
return _dec
|
|
|
|
else:
|
|
|
|
return _dec(function)
|
|
|
|
|
2014-02-07 12:32:44 -08:00
|
|
|
def oauth_login_required(function=None):
|
|
|
|
def _dec(view_func):
|
|
|
|
def _view(request, *args, **kwargs):
|
2020-06-11 15:13:12 -04:00
|
|
|
if request.user.is_anonymous:
|
2014-02-07 12:32:44 -08:00
|
|
|
return HttpResponse(content=json.encode({
|
|
|
|
"message": "You must have a valid OAuth token.",
|
|
|
|
}), status=401)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
setattr(request, 'body_json', json.decode(request.body))
|
|
|
|
except JSONDecodeError:
|
|
|
|
return HttpResponse(content=json.encode({
|
|
|
|
"message": "Your JSON body is malformed.",
|
|
|
|
}), status=400)
|
|
|
|
return view_func(request, *args, **kwargs)
|
|
|
|
|
|
|
|
_view.__name__ = view_func.__name__
|
|
|
|
_view.__dict__ = view_func.__dict__
|
|
|
|
_view.__doc__ = view_func.__doc__
|
|
|
|
|
|
|
|
return _view
|
|
|
|
|
|
|
|
if function is None:
|
|
|
|
return _dec
|
|
|
|
else:
|
|
|
|
return _dec(function)
|
|
|
|
|
2011-07-11 18:22:28 -07:00
|
|
|
def admin_only(function=None):
|
|
|
|
def _dec(view_func):
|
|
|
|
def _view(request, *args, **kwargs):
|
|
|
|
if not request.user.is_staff:
|
|
|
|
return HttpResponseForbidden()
|
|
|
|
else:
|
|
|
|
return view_func(request, *args, **kwargs)
|
|
|
|
|
|
|
|
_view.__name__ = view_func.__name__
|
|
|
|
_view.__dict__ = view_func.__dict__
|
|
|
|
_view.__doc__ = view_func.__doc__
|
|
|
|
|
|
|
|
return _view
|
|
|
|
|
|
|
|
if function is None:
|
|
|
|
return _dec
|
|
|
|
else:
|
|
|
|
return _dec(function)
|
|
|
|
|
2009-06-16 03:08:55 +00:00
|
|
|
def get_user(request):
|
2011-03-02 12:05:58 -05:00
|
|
|
if not hasattr(request, 'user'):
|
|
|
|
user = request
|
2009-06-16 03:08:55 +00:00
|
|
|
else:
|
2011-03-02 12:05:58 -05:00
|
|
|
user = request.user
|
|
|
|
|
2020-06-11 15:13:12 -04:00
|
|
|
if user.is_anonymous and hasattr(request, 'POST'):
|
2019-12-16 16:59:04 -05:00
|
|
|
# Check secret_token parameter
|
|
|
|
secret_token = request.POST.get('secret_token', None) or request.GET.get('secret_token', None)
|
|
|
|
if secret_token:
|
|
|
|
try:
|
|
|
|
user = User.objects.get(profile__secret_token=secret_token)
|
|
|
|
request.user = user
|
|
|
|
except User.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
2020-06-11 15:13:12 -04:00
|
|
|
if user.is_anonymous:
|
2020-11-13 21:43:52 -05:00
|
|
|
try:
|
|
|
|
user = User.objects.get(username=settings.HOMEPAGE_USERNAME)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
user = User.objects.create(username=settings.HOMEPAGE_USERNAME)
|
|
|
|
user.set_password('')
|
|
|
|
user.save()
|
2010-07-06 16:37:49 -04:00
|
|
|
return user
|
|
|
|
|
|
|
|
def invalidate_template_cache(fragment_name, *variables):
|
2020-06-20 00:27:01 -04:00
|
|
|
args = hashlib.md5((':'.join([urlquote(var) for var in variables]).encode('utf-8')))
|
2010-07-06 16:37:49 -04:00
|
|
|
cache_key = 'template.cache.%s.%s' % (fragment_name, args.hexdigest())
|
2011-01-20 09:57:23 -05:00
|
|
|
cache.delete(cache_key)
|
|
|
|
|
|
|
|
def generate_secret_token(phrase, size=12):
|
|
|
|
"""Generate a (SHA1) security hash from the provided info."""
|
2020-06-19 02:27:48 -04:00
|
|
|
info = f"{phrase} {settings.SECRET_KEY}".encode('utf-8')
|
2020-06-20 01:11:59 -04:00
|
|
|
return hashlib.sha256(info).hexdigest()[:size]
|
2012-09-06 17:16:01 -07:00
|
|
|
|
|
|
|
def extract_user_agent(request):
|
2013-06-15 09:09:03 -07:00
|
|
|
user_agent = request.environ.get('HTTP_USER_AGENT', '').lower()
|
2012-09-06 17:16:01 -07:00
|
|
|
platform = '------'
|
2013-06-15 09:09:03 -07:00
|
|
|
if 'ipad app' in user_agent:
|
2012-09-06 17:16:01 -07:00
|
|
|
platform = 'iPad'
|
2013-06-15 09:09:03 -07:00
|
|
|
elif 'iphone app' in user_agent:
|
2012-09-06 17:16:01 -07:00
|
|
|
platform = 'iPhone'
|
2013-06-15 09:09:03 -07:00
|
|
|
elif 'blar' in user_agent:
|
2012-09-06 17:16:01 -07:00
|
|
|
platform = 'Blar'
|
2015-03-11 10:47:57 -07:00
|
|
|
elif 'Android app' in user_agent:
|
2012-09-06 17:16:01 -07:00
|
|
|
platform = 'Androd'
|
2015-03-11 10:55:17 -07:00
|
|
|
elif 'android' in user_agent:
|
|
|
|
platform = 'androd'
|
2013-06-15 09:09:03 -07:00
|
|
|
elif 'pluggio' in user_agent:
|
2013-02-05 16:10:23 -08:00
|
|
|
platform = 'Plugio'
|
2013-06-15 09:09:03 -07:00
|
|
|
elif 'msie' in user_agent:
|
2012-09-06 17:16:01 -07:00
|
|
|
platform = 'IE'
|
2013-06-15 09:09:03 -07:00
|
|
|
if 'msie 9' in user_agent:
|
2012-09-06 17:16:01 -07:00
|
|
|
platform += '9'
|
2013-06-15 09:09:03 -07:00
|
|
|
elif 'msie 10' in user_agent:
|
2012-09-06 17:16:01 -07:00
|
|
|
platform += '10'
|
2013-06-15 09:09:03 -07:00
|
|
|
elif 'msie 8' in user_agent:
|
2012-09-06 17:16:01 -07:00
|
|
|
platform += '8'
|
2015-03-11 10:55:17 -07:00
|
|
|
elif 'trident/7' in user_agent:
|
|
|
|
platform = 'IE11'
|
2013-06-15 09:09:03 -07:00
|
|
|
elif 'chrome' in user_agent:
|
2012-09-06 17:16:01 -07:00
|
|
|
platform = 'Chrome'
|
2013-06-15 09:09:03 -07:00
|
|
|
elif 'safari' in user_agent:
|
2012-09-06 17:16:01 -07:00
|
|
|
platform = 'Safari'
|
2013-06-15 09:09:03 -07:00
|
|
|
elif 'meego' in user_agent:
|
2012-09-06 17:16:01 -07:00
|
|
|
platform = 'MeeGo'
|
2013-06-15 09:09:03 -07:00
|
|
|
elif 'firefox' in user_agent:
|
2012-09-06 17:16:01 -07:00
|
|
|
platform = 'FF'
|
2013-06-15 09:09:03 -07:00
|
|
|
elif 'opera' in user_agent:
|
2012-09-06 17:16:01 -07:00
|
|
|
platform = 'Opera'
|
2013-06-15 09:09:03 -07:00
|
|
|
elif 'wp7' in user_agent:
|
2012-09-06 17:16:01 -07:00
|
|
|
platform = 'WP7'
|
2013-06-15 09:09:03 -07:00
|
|
|
elif 'wp8' in user_agent:
|
2012-09-06 17:16:01 -07:00
|
|
|
platform = 'WP8'
|
2013-06-15 09:09:03 -07:00
|
|
|
elif 'tafiti' in user_agent:
|
2013-02-13 16:27:20 -08:00
|
|
|
platform = 'Tafiti'
|
2013-06-15 09:09:03 -07:00
|
|
|
elif 'readkit' in user_agent:
|
2013-05-16 11:11:46 -07:00
|
|
|
platform = 'ReadKt'
|
2015-03-11 10:55:17 -07:00
|
|
|
elif 'reeder' in user_agent:
|
|
|
|
platform = 'Reeder'
|
2013-06-24 23:27:47 -07:00
|
|
|
elif 'metroblur' in user_agent:
|
|
|
|
platform = 'Metrob'
|
|
|
|
elif 'feedme' in user_agent:
|
|
|
|
platform = 'FeedMe'
|
2015-03-11 10:55:17 -07:00
|
|
|
elif 'theoldreader' in user_agent:
|
|
|
|
platform = 'OldRdr'
|
|
|
|
elif 'fever' in user_agent:
|
|
|
|
platform = 'Fever'
|
|
|
|
elif 'superfeedr' in user_agent:
|
|
|
|
platform = 'Suprfd'
|
2013-06-24 23:27:47 -07:00
|
|
|
elif 'feed reader-window' in user_agent:
|
|
|
|
platform = 'FeedRe'
|
2013-06-24 23:41:31 -07:00
|
|
|
elif 'feed reader-background' in user_agent:
|
|
|
|
platform = 'FeReBg'
|
2012-09-06 17:16:01 -07:00
|
|
|
|
2013-02-04 21:27:08 -08:00
|
|
|
return platform
|