snipt/views.py

115 lines
3.4 KiB
Python
Raw Normal View History

2012-07-08 11:20:44 -07:00
from django.http import HttpResponseRedirect, HttpResponseBadRequest
2012-07-03 15:22:48 -07:00
from annoying.decorators import ajax_request, render_to
2012-07-08 11:20:44 -07:00
from django.template.defaultfilters import striptags
2012-06-20 08:11:46 -07:00
from django.shortcuts import render_to_response
from django.template import RequestContext
2012-05-15 17:10:50 -07:00
from snipts.utils import get_lexers_list
2012-06-20 08:22:58 -07:00
from django.db.models import Count
2012-07-03 21:12:12 -07:00
from amazon.api import AmazonAPI
2012-07-08 11:20:44 -07:00
from django.conf import settings
2012-06-20 08:22:58 -07:00
from taggit.models import Tag
2012-07-08 11:20:44 -07:00
import os, urllib
2012-07-03 20:21:40 -07:00
2012-07-03 21:12:12 -07:00
@ajax_request
2012-07-03 20:21:40 -07:00
def amazon_search(request):
2012-07-03 21:12:12 -07:00
products = []
2012-07-03 20:21:40 -07:00
if request.GET.get('q'):
2012-07-03 21:12:12 -07:00
amazon = AmazonAPI('AKIAJJRRQPTSPKB7GYOA', 'DIYz2g5vPjcWE4/YI7wEuUVAskwJxs2llFvGyI1a', 'snipt-20')
2012-07-08 12:06:09 -07:00
products = amazon.search_n(5, Keywords=request.GET.get('q'), SearchIndex='Books')
2012-07-03 21:12:12 -07:00
result = []
for product in products:
2012-07-08 11:54:11 -07:00
if product.small_image_url:
small_image_url = product.small_image_url.replace('http://ecx.images-amazon.com/images/I/', '')
else:
small_image_url = None
2012-07-03 21:12:12 -07:00
result.append({
2012-07-08 11:54:11 -07:00
'image': small_image_url,
2012-07-03 21:12:12 -07:00
'price': product.list_price,
'review': striptags(product.editorial_review),
'reviews': product.reviews,
'title': product.title,
'url': product.offer_url,
})
return {
'result': result
}
2012-05-15 17:10:50 -07:00
2012-07-08 11:20:44 -07:00
def amazon_image(request):
if 'i' in request.GET:
img_filename = request.GET.get('i')
img_src = 'http://ecx.images-amazon.com/images/I/{}'.format(img_filename)
img_loc = os.path.join(settings.STATIC_ROOT, 'images', 'amazon', img_filename)
2012-07-08 12:05:20 -07:00
if settings.DEBUG:
root = '/static/images/amazon/'
else:
root = 'https://snipt.net/static/images/amazon/'
2012-07-08 11:20:44 -07:00
try:
open(img_loc)
2012-07-08 12:05:20 -07:00
return HttpResponseRedirect(root + img_filename)
2012-07-08 11:20:44 -07:00
except IOError:
urllib.urlretrieve(img_src, img_loc)
2012-07-08 12:05:20 -07:00
return HttpResponseRedirect(root + img_filename)
2012-07-08 11:20:44 -07:00
else:
return HttpResponseBadRequest()
2012-05-15 17:10:50 -07:00
@ajax_request
def lexers(request):
2012-05-16 07:53:22 -07:00
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}
2012-07-03 15:22:48 -07:00
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
}