summaryrefslogtreecommitdiff
path: root/website/views.py
blob: 1b9b9140d4c731067f5724a08cce79cdd0994496 (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
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_in').filter(foss_name='Python', type ='Workshop').order_by('-w_id')
		python_wokshop_page_content = Page.objects.get(permalink='python-workshops-page')

		context['page'] = python_wokshop_page_content
		context['permalink'] = permalink
		context['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
		context['permalink'] = permalink
		context['obj'] = completed_books

	if permalink == '' or permalink == 'home' :
		permalink = 'home'
		page = get_object_or_404(Page, permalink=permalink)
		blocks = get_blocks()
		context['page'] =  page
		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
		context['permalink'] = permalink

	context['navs'] = blocks['navs']
	context['sidebar'] = blocks['sidebar']
	context['footer'] = blocks['footer']
	return render(request, 'website/templates/page.html', context)