summaryrefslogtreecommitdiff
path: root/website/views.py
blob: a49a00799fb11166685af44cae4b6283a6f18744 (plain)
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
from django.http import HttpResponse
from django.shortcuts import render, render_to_response, get_object_or_404
from website.models import FOSSEEStats, TBCPYTHONBook

from website.models import Nav, Page, Block

def block_sort(obj):
    linkboxes = obj.linkbox_set.all()
    textboxes = obj.textbox_set.all()

    items = []
    for item in linkboxes:
        items.append(item)

    for item in textboxes:
        items.append(item)

    def get_pos(x): return x.position
    items = sorted(items, key=get_pos)
    return items

def get_blocks():
    sidebar = Block.objects.get(block_name = "sidebar")
    footer = Block.objects.get(block_name = "footer")
    blocks = {
        'navs': Nav.objects.order_by('position'),
        'sidebar': block_sort(sidebar),
        'footer': block_sort(footer)
    }
    return blocks

def dispatcher(request, permalink=''):
    context = {}

    if permalink == 'python-workshops':
        blocks = get_blocks()
        rows = FOSSEEStats.objects.using('fossee_new').filter(foss_name='Python', type ='Workshop').order_by('-w_id')
        print("----------", rows)
        python_wokshop_page_content = Page.objects.get(permalink='python-workshops-page')
        context = {
            'page':python_wokshop_page_content,
            'navs': blocks['navs'],
            'sidebar': blocks['sidebar'],
            'footer': blocks['footer'],
            'permalink': permalink,
            'obj': rows
        }

    if permalink == 'textbook-companions-for-academics':
        blocks = get_blocks()
        textbook_companions_for_academics = Page.objects.get(permalink='textbook-companions-for-academics-page')
        completed_books = TBCPYTHONBook.objects.using('tbcpython').values('id', 'title', 'author').filter(approved=True).order_by('id')

        context = {
            'page':textbook_companions_for_academics,
            'navs': blocks['navs'],
            'sidebar': blocks['sidebar'],
            'footer': blocks['footer'],
            'permalink': permalink,
            'obj': completed_books
        }

    if permalink == '' or permalink == 'home' :
        permalink = 'home'
        page = get_object_or_404(Page, permalink=permalink)
        blocks = get_blocks()
        context = {
            'page': page,
            'navs': blocks['navs'],
            'sidebar': blocks['sidebar'],
            'footer': blocks['footer'],
            'permalink': permalink
        }
        context['permalink'] = permalink

    if permalink != 'home' and permalink != 'python-workshops' and permalink != 'textbook-companions-for-academics':
        page = get_object_or_404(Page, permalink=permalink)
        blocks = get_blocks()
        context = {
            'page': page,
            'navs': blocks['navs'],
            'sidebar': blocks['sidebar'],
            'footer': blocks['footer'],
            'permalink': permalink
        }
    return render(request, "website/page.html", context)