-
Notifications
You must be signed in to change notification settings - Fork 66
/
views.py
101 lines (75 loc) · 2.59 KB
/
views.py
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
90
91
92
93
94
95
96
97
98
99
100
101
from django.http import HttpResponseRedirect, HttpResponseBadRequest
from django.contrib.auth.decorators import login_required
from annoying.decorators import ajax_request, render_to
from django.shortcuts import render_to_response
from django.template import RequestContext
from snipts.utils import get_lexers_list
from django.db.models import Count
from taggit.models import Tag
import stripe
from local_settings import STRIPE_API_KEY
@ajax_request
def lexers(request):
lexers = get_lexers_list()
objects = []
for l in lexers:
try:
filters = l[2]
except IndexError:
filters = []
try:
mimetypes = l[3]
except IndexError:
mimetypes = []
objects.append({
'name': l[0],
'lexers': l[1],
'filters': filters,
'mimetypes': mimetypes
})
return {'objects': objects}
@login_required
@render_to('pro-signup.html')
def pro_signup(request):
if request.user.profile.is_pro:
return HttpResponseRedirect('/' + request.user.username + '/')
return {}
@login_required
@render_to('pro-signup-complete.html')
def pro_signup_complete(request):
if request.method == 'POST':
token = request.POST['token']
stripe.api_key = STRIPE_API_KEY
plan = 'snipt-pro-monthly'
customer = stripe.Customer.create(
card = token,
plan = plan,
email = request.user.email
)
profile = request.user.profile
profile.is_pro = True
profile.stripe_id = customer.id
profile.save()
return {}
else:
return HttpResponseBadRequest()
def sitemap(request):
tags = Tag.objects.filter(snipt__public=True)
tags = tags.annotate(count=Count('taggit_taggeditem_items__id'))
tags = tags.order_by('-count')[:1000]
return render_to_response('sitemap.xml',
{'tags': tags},
context_instance=RequestContext(request),
mimetype='application/xml')
@render_to('tags.html')
def tags(request):
all_tags = Tag.objects.filter(snipt__public=True).order_by('name')
all_tags = all_tags.annotate(count=Count('taggit_taggeditem_items__id'))
popular_tags = Tag.objects.filter(snipt__public=True)
popular_tags = popular_tags.annotate(count=Count('taggit_taggeditem_items__id'))
popular_tags = popular_tags.order_by('-count')[:20]
popular_tags = sorted(popular_tags, key=lambda tag: tag.name)
return {
'all_tags': all_tags,
'tags': popular_tags
}