Serotonin Storm

source>core>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
from django.utils import simplejson
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from basic.blog.models import Post, Category
from django.conf import settings
from django import http
from django.template import loader, Context
from django_proxy.models import Proxy
from django.views.generic import list_detail


def post_result_item(post):
    '''Generates the item result object for django-springsteen integration.'''
    return {
        'title': post.title,
        'url': settings.SITE_URL + post.get_absolute_url(),
        'text': post.body,
        }
        
def springsteen_results(request):
    '''
    Creates the django-springsteen compliant JSON results for only for findjango 
    integration.
    
    Results:
        Published Post objects.
    '''

    results = [ post_result_item(item) for item in Post.objects.published() ]
    response_dict = { 'total_results': Post.objects.published().count(), 'results': results, }
    return HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')
    
    
def server_error(request, template_name='500.html'):
    '''Handles displaying 500 server error page along with application MEDIA.'''
    
    t = loader.get_template(template_name)
    return http.HttpResponseServerError(t.render(Context({
        "MEDIA_URL": settings.MEDIA_URL,
        "STATIC_URL": settings.STATIC_URL,
    })))


def springsteen_firehose(request):
    '''Generates django-springsteen compliant JSON results of proxy models for findjango integration.'''
    
    def result_item(proxy):
        '''Generates the item result object.'''
        
        if proxy.content_type.name == 'bookmark':
            url = proxy.content_object.get_absolute_url()
        else:
            url = settings.SITE_URL + proxy.content_object.get_absolute_url()
        
        return {
            'title': proxy.title,
            'url': url,
            'text': proxy.description,
            }
    
    results = [ result_item(item) for item in Proxy.objects.published().order_by('-pub_date') ]
    response_dict = { 'total_results': Proxy.objects.published().count(), 'results': results, }
    return HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')
    
def springsteen_category(request, slug):
    '''
    Creates the django-springsteen compliant JSON results for only for findjango 
    integration.
    
    Results:
        Published Post objects by category.
    '''
    
    category = get_object_or_404(Category, slug__iexact=slug)

    results = [ post_result_item(item) for item in category.post_set.published() ]
    response_dict = { 'total_results': category.post_set.published().count(), 'results': results, }
    return HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')

    
def home_list(request, page=0, template_name='proxy/proxy_list.html', **kwargs):
    '''
    Homepage.
    
    Template: ``proxy/proxy_list.html``
    Context:
        object_list
            Aggregated list of Proxy instances (post, quote, bookmark).
        
    '''
    return list_detail.object_list(
        request,
        queryset = Proxy.objects.published().order_by('-pub_date'),
        paginate_by = getattr(settings,'BLOG_PAGESIZE',20),
        page = page,
        template_name = template_name,
        **kwargs
    )