Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 73 lines (61 sloc) 2.388 kb
8afdbd8 @samuelclay Adding bookmarklet modal. Also adding the api app, which will handle inc...
authored
1 from django.utils.hashcompat import sha_constructor
48ba40e @samuelclay Initial
authored
2 from django.contrib.auth.models import User
ff13e9c @samuelclay Beginnings of minor cache work. So far, only on user accounts.
authored
3 from django.core.cache import cache
e46f6ce @samuelclay Refactoring a bit and marking a subscription as read when it's out of st...
authored
4 from django.utils.hashcompat import md5_constructor
5 from django.utils.http import urlquote
31407a9 @samuelclay Handling anonymous users with more grace. They will now be told that the...
authored
6 from django.http import HttpResponseForbidden
8afdbd8 @samuelclay Adding bookmarklet modal. Also adding the api app, which will handle inc...
authored
7 from django.conf import settings
ff13e9c @samuelclay Beginnings of minor cache work. So far, only on user accounts.
authored
8
31407a9 @samuelclay Handling anonymous users with more grace. They will now be told that the...
authored
9 def ajax_login_required(function=None):
10 def _dec(view_func):
11 def _view(request, *args, **kwargs):
12 if request.user.is_anonymous():
13 return HttpResponseForbidden()
14 else:
15 return view_func(request, *args, **kwargs)
16
17 _view.__name__ = view_func.__name__
18 _view.__dict__ = view_func.__dict__
19 _view.__doc__ = view_func.__doc__
20
21 return _view
22
23 if function is None:
24 return _dec
25 else:
26 return _dec(function)
27
ae7f79b @samuelclay Adding moderation queue to recommended feeds (finally!)
authored
28 def admin_only(function=None):
29 def _dec(view_func):
30 def _view(request, *args, **kwargs):
31 if not request.user.is_staff:
32 return HttpResponseForbidden()
33 else:
34 return view_func(request, *args, **kwargs)
35
36 _view.__name__ = view_func.__name__
37 _view.__dict__ = view_func.__dict__
38 _view.__doc__ = view_func.__doc__
39
40 return _view
41
42 if function is None:
43 return _dec
44 else:
45 return _dec(function)
46
48ba40e @samuelclay Initial
authored
47 def get_user(request):
2e437fc @samuelclay Adding a subscribed note in recommended feeds.
authored
48 if not hasattr(request, 'user'):
49 user = request
48ba40e @samuelclay Initial
authored
50 else:
2e437fc @samuelclay Adding a subscribed note in recommended feeds.
authored
51 user = request.user
52
53 if user.is_anonymous():
ff50d85 @samuelclay Updating a bunch of defaults to make NewsBlur installable on anybody's m...
authored
54 user = cache.get('user:%s' % settings.HOMEPAGE_USERNAME, None)
ff13e9c @samuelclay Beginnings of minor cache work. So far, only on user accounts.
authored
55 if not user:
ff50d85 @samuelclay Updating a bunch of defaults to make NewsBlur installable on anybody's m...
authored
56 try:
57 user = User.objects.get(username=settings.HOMEPAGE_USERNAME)
58 cache.set('user:%s' % user, user)
59 except User.DoesNotExist:
60 user = User.objects.create(username=settings.HOMEPAGE_USERNAME)
61 user.set_password('')
62 user.save()
e46f6ce @samuelclay Refactoring a bit and marking a subscription as read when it's out of st...
authored
63 return user
64
65 def invalidate_template_cache(fragment_name, *variables):
66 args = md5_constructor(u':'.join([urlquote(var) for var in variables]))
67 cache_key = 'template.cache.%s.%s' % (fragment_name, args.hexdigest())
8afdbd8 @samuelclay Adding bookmarklet modal. Also adding the api app, which will handle inc...
authored
68 cache.delete(cache_key)
69
70 def generate_secret_token(phrase, size=12):
71 """Generate a (SHA1) security hash from the provided info."""
72 info = (phrase, settings.SECRET_KEY)
73 return sha_constructor("".join(info)).hexdigest()[:size]
Something went wrong with that request. Please try again.