summaryrefslogtreecommitdiff
path: root/website
diff options
context:
space:
mode:
Diffstat (limited to 'website')
-rwxr-xr-xwebsite/ajax.py44
-rw-r--r--website/models.py106
-rw-r--r--website/query.py91
-rw-r--r--website/static/website/js/cloud.js43
-rw-r--r--website/static/website/templates/index.html3
-rw-r--r--website/urls.py3
-rw-r--r--website/views.py335
7 files changed, 566 insertions, 59 deletions
diff --git a/website/ajax.py b/website/ajax.py
index ecd040a..a18fe86 100755
--- a/website/ajax.py
+++ b/website/ajax.py
@@ -16,7 +16,7 @@ from django.views.decorators.csrf import csrf_exempt, csrf_protect
from textwrap import dedent
from R_on_Cloud.config import FROM_EMAIL, TO_EMAIL, CC_EMAIL, BCC_EMAIL
from R_on_Cloud.config import UPLOADS_PATH
-from website.views import catg
+from website.views import catg, dictfetchall
from website.models import *
from website.forms import BugForm, RevisionForm, issues
@@ -115,7 +115,6 @@ def chapters(request):
'chapter': obj[2],
}
- print(obj.name)
response_dict.append(response)
return HttpResponse(simplejson.dumps(response_dict),
content_type='application/json')
@@ -194,21 +193,22 @@ def code(request):
if not example_id:
example_id = int(request.GET.get('example_id'))
file_path = request.session['filepath']
- review = ScilabCloudComment.objects.using('r')\
- .filter(example=example_id).count()
+ #review = ScilabCloudComment.objects.using('r')\
+ # .filter(example=example_id).count()
+ review = 0
with connections['r'].cursor() as cursor:
- cursor.execute(GET_TBC_EXAMPLE_FILE_VIEW_SQL, params=[example_id])
+ cursor.execute(GET_TBC_EXAMPLE_VIEW_SQL, params=[example_id])
exmple = cursor.fetchone()
review_url = "https://scilab.in/cloud_comments/" + str(example_id)
# example_path = UPLOADS_PATH + '/' + file_path
file = utils.get_file(file_path, commit_sha, main_repo=True)
- code = base64.b64decode(file['content'])
+ code = file
response = {
- 'code': code.decode('UTF-8'),
+ 'code': code,
'review': review,
'review_url': review_url,
- 'exmple': exmple[1]
+ 'exmple': exmple[0]
}
# response_dict.append(response)
return HttpResponse(simplejson.dumps(response),
@@ -225,15 +225,13 @@ def contributor(request):
with connections['r'].cursor() as cursor:
cursor.execute(GET_TBC_CONTRIBUTOR_DETAILS_SQL, params=[book_id])
contributor = cursor.fetchone()
-
- for obj in contributor:
- response = {
- "contributor_name": obj[1],
- "proposal_faculty": obj[2],
- "proposal_reviewer": obj[3],
- "proposal_university": obj[4],
- }
- response_dict.append(response)
+ response = {
+ "contributor_name": contributor[1],
+ "proposal_faculty": contributor[2],
+ "proposal_reviewer": contributor[3],
+ "proposal_university": contributor[4],
+ }
+ response_dict.append(response)
return HttpResponse(simplejson.dumps(response),
content_type='application/json')
@@ -271,13 +269,10 @@ def bug_form_submit(request):
book_id = request.GET.get('book_id')
chapter_id = request.GET.get('chapter_id')
ex_id = request.GET.get('ex_id')
- form = BugForm(deserialize_form(form))
- print(form)
if form.is_valid():
comment = form.cleaned_data['description']
error = form.cleaned_data['issue']
email = form.cleaned_data['email']
- print(comment)
comment_data = TextbookCompanionPreference.objects\
.db_manager('r').raw(dedent("""\
SELECT 1 as id, tcp.book as book, tcp.author as author,
@@ -448,9 +443,9 @@ def diff(request):
context = {}
file_path = request.session['filepath']
file = utils.get_file(file_path, diff_commit_sha, main_repo=True)
- code = base64.b64decode(file['content'])
+ code = file
response = {
- 'code2': code.decode('UTF-8'),
+ 'code2': code,
}
return HttpResponse(simplejson.dumps(response),
content_type='application/json')
@@ -462,7 +457,6 @@ def diff(request):
def review_revision(request):
if request.is_ajax():
revision_id = request.GET.get('revision_id')
- print(revision_id)
revision = TextbookCompanionRevision.objects.using(
'r').get(id=revision_id)
file = utils.get_file(revision.example_file.filepath,
@@ -498,8 +492,6 @@ def push_revision(request):
code = code.decode('UTF-8')
revision = TextbookCompanionRevision.objects.using(
'r').get(id=request.session['revision_id'])
-
- print('pushing to repo')
utils.update_file(
revision.example_file.filepath,
revision.commit_message,
@@ -507,8 +499,6 @@ def push_revision(request):
[revision.committer_name, revision.committer_email],
branch='master',
main_repo=True)
-
- print('update push_status')
revision.push_status = True
revision.save()
diff --git a/website/models.py b/website/models.py
index 71a8362..38ace40 100644
--- a/website/models.py
+++ b/website/models.py
@@ -1,3 +1,109 @@
from django.db import models
# Create your models here.
+
+class TextbookCompanionCategoryList(models.Model):
+ id = models.IntegerField(unique=True, primary_key=True)
+ category_name = models.CharField(max_length=100)
+ category_id = models.IntegerField()
+ maincategory = models.CharField(max_length=255)
+
+ class Meta:
+ db_table = 'list_of_category'
+
+class TextbookCompanionSubCategoryList(models.Model):
+ id = models.IntegerField(unique=True, primary_key=True)
+ subcategory_id = models.IntegerField()
+ subcategory = models.CharField(max_length=255)
+ maincategory_id = models.IntegerField()
+
+ class Meta:
+ db_table = 'list_of_subcategory'
+
+class TextbookCompanionProposal(models.Model):
+ id = models.IntegerField(unique=True, primary_key=True)
+ uid = models.IntegerField()
+ approver_uid = models.IntegerField()
+ full_name = models.CharField(max_length=50)
+ mobile = models.CharField(max_length=15)
+ gender = models.CharField(max_length=10)
+ how_project = models.CharField(max_length=50)
+ course = models.CharField(max_length=50)
+ branch = models.CharField(max_length=50)
+ university = models.CharField(max_length=100)
+ faculty = models.CharField(max_length=100)
+ reviewer = models.CharField(max_length=100)
+ completion_date = models.IntegerField()
+ creation_date = models.IntegerField()
+ approval_date = models.IntegerField()
+ proposal_status = models.IntegerField()
+ message = models.TextField()
+ scilab_version = models.CharField(max_length=20)
+ operating_system = models.CharField(max_length=20)
+ teacher_email = models.CharField(max_length=20)
+
+ class Meta:
+ db_table = 'textbook_companion_proposal'
+
+
+class TextbookCompanionPreference(models.Model):
+ id = models.IntegerField(unique=True, primary_key=True)
+ proposal = models.ForeignKey(
+ TextbookCompanionProposal, on_delete=models.CASCADE)
+ pref_number = models.IntegerField()
+ book = models.CharField(max_length=100)
+ author = models.CharField(max_length=100)
+ isbn = models.CharField(max_length=25)
+ publisher = models.CharField(max_length=50)
+ edition = models.CharField(max_length=2)
+ year = models.IntegerField()
+ category = models.IntegerField()
+ approval_status = models.IntegerField()
+ cloud_pref_err_status = models.IntegerField()
+
+ class Meta:
+ db_table = 'textbook_companion_preference'
+
+
+class TextbookCompanionChapter(models.Model):
+ id = models.IntegerField(primary_key=True)
+ preference = models.ForeignKey(
+ TextbookCompanionPreference, on_delete=models.CASCADE)
+ number = models.IntegerField()
+ name = models.CharField(max_length=255)
+ cloud_chapter_err_status = models.CharField(max_length=255)
+
+ class Meta:
+ db_table = 'textbook_companion_chapter'
+
+
+class TextbookCompanionExample(models.Model):
+ id = models.IntegerField(primary_key=True)
+ chapter = models.ForeignKey(
+ TextbookCompanionChapter, on_delete=models.CASCADE)
+ approver_uid = models.IntegerField()
+ number = models.CharField(max_length=10)
+ caption = models.CharField(max_length=255)
+ approval_date = models.IntegerField()
+ approval_status = models.IntegerField()
+ timestamp = models.IntegerField()
+ cloud_err_status = models.IntegerField()
+
+ class Meta:
+ db_table = 'textbook_companion_example'
+
+
+class TextbookCompanionExampleFiles(models.Model):
+ id = models.IntegerField(primary_key=True)
+ example = models.ForeignKey(
+ TextbookCompanionExample, on_delete=models.CASCADE)
+ filename = models.CharField(max_length=255)
+ filepath = models.CharField(max_length=500)
+ filemime = models.CharField(max_length=255)
+ filesize = models.IntegerField()
+ filetype = models.CharField(max_length=1)
+ caption = models.CharField(max_length=100)
+ timestamp = models.IntegerField()
+
+ class Meta:
+ db_table = 'textbook_companion_example_files'
diff --git a/website/query.py b/website/query.py
index 43b172b..06e37a7 100644
--- a/website/query.py
+++ b/website/query.py
@@ -49,10 +49,10 @@ GET_TBC_EXAMPLE_FILE_SQL = """
"""
-GET_TBC_EXAMPLE_FILE_VIEW_SQL = """
- SELECT id, views_count FROM textbook_companion_example_views
- WHERE example_id = %s
- """
+# GET_TBC_EXAMPLE_FILE_VIEW_SQL = """
+# SELECT id, views_count FROM textbook_companion_example_views
+# WHERE example_id = %s
+# """
GET_TBC_CONTRIBUTOR_DETAILS_SQL = """
@@ -65,3 +65,86 @@ GET_TBC_CONTRIBUTOR_DETAILS_SQL = """
INNER JOIN textbook_companion_preference preference
ON proposal.id = preference.proposal_id
WHERE preference.id = %s
+ """
+
+GET_TBC_CHAPTER_ID_SQL = """
+ SELECT chapter_id FROM textbook_companion_example WHERE id = %s
+ """
+
+INSERT_TBC_EXAMPLE_VIEW_SQL = """
+ INSERT INTO textbook_companion_example_views
+ (example_id,chapter_id) VALUES(%s, %s)
+ """
+
+UPDATE_TBC_EXAMPLE_VIEW_SQL = """
+ UPDATE textbook_companion_example_views
+ SET views_count = views_count + 1 WHERE example_id = %s
+ """
+
+GET_TBC_EXAMPLE_VIEW_SQL = """
+ SELECT views_count FROM textbook_companion_example_views
+ WHERE example_id = %s
+ """
+
+GET_TBC_EXAMPLE_R_CLOUD_COMMENT_SQL = """
+ SELECT COUNT(id) FROM r_cloud_comment WHERE example= %s
+ """
+
+GET_TBC_EXAMPLE_CHAPTER_ID_SQL = """
+ SELECT DISTINCT(chapter_id)
+ FROM textbook_companion_example
+ WHERE cloud_err_status=0 AND chapter_id = (
+ SELECT chapter_id FROM textbook_companion_example
+ WHERE id = %s )
+ """
+
+GET_TBC_CHAPTER_PREFERENCE_ID_SQL = """
+ SELECT DISTINCT(preference_id)
+ FROM textbook_companion_chapter
+ WHERE cloud_chapter_err_status = 0
+ AND preference_id = (SELECT preference_id
+ FROM textbook_companion_chapter WHERE id = %s)
+ """
+
+GET_TBC_PREFERENCE_DETAIL_CATEGORY_SQL = """
+ SELECT DISTINCT (loc.category_id),
+ tcbm.sub_category,loc.maincategory, pe.book as book,
+ pe.author as author, pe.publisher as publisher,
+ pe.year as year, pe.id as pe_id, pe.edition, pe.id as pref_id
+ FROM textbook_companion_preference pe
+ INNER JOIN textbook_companion_proposal po
+ ON pe.proposal_id = po.id
+ INNER JOIN textbook_companion_book_main_subcategories tcbm
+ ON pe.id = tcbm.pref_id
+ INNER JOIN list_of_category loc
+ ON tcbm.main_category = loc.category_id
+ WHERE po.proposal_status = 3 AND pe.approval_status = 1
+ AND pe.id = tcbm.pref_id AND pe.cloud_pref_err_status = 0
+ AND pe.id= %s
+ """
+
+GET_TBC_PREFERENCE_FROM_CATEGORY_ID_SQL = """
+ SELECT DISTINCT (loc.category_id),pe.id,
+ tcbm.sub_category,loc.maincategory, pe.book as
+ book,loc.category_id,tcbm.sub_category,
+ pe.author as author, pe.publisher as publisher,
+ pe.year as year, pe.id as pe_id, pe.edition,
+ po.approval_date as approval_date
+ FROM textbook_companion_preference pe INNER JOIN
+ textbook_companion_proposal po ON pe.proposal_id = po.id
+ INNER JOIN textbook_companion_book_main_subcategories
+ tcbm ON pe.id = tcbm.pref_id INNER JOIN list_of_category
+ loc ON tcbm.main_category = loc.category_id
+ WHERE po.proposal_status = 3 AND pe.approval_status = 1
+ AND pe.id = tcbm.pref_id AND pe.cloud_pref_err_status = 0
+ AND tcbm.sub_category = %s
+ """
+
+GET_TBC_CHAPTER_DETAIL_SQL = """
+ SELECT id, name, number, preference_id
+ FROM textbook_companion_chapter
+ WHERE cloud_chapter_err_status = 0 AND
+ preference_id = (SELECT preference_id
+ FROM textbook_companion_chapter WHERE id = %s)
+ ORDER BY number ASC
+ """
diff --git a/website/static/website/js/cloud.js b/website/static/website/js/cloud.js
index 7d7d5dd..e762429 100644
--- a/website/static/website/js/cloud.js
+++ b/website/static/website/js/cloud.js
@@ -472,6 +472,7 @@ $(document.body).ready(function() {
$("#databox-wrapper").hide();
editor.setValue("");
result.setValue("");
+ console.log(example_id);
ajax_loader('#revisions');
$.ajax({
url: 'get_revisions/',
@@ -489,8 +490,8 @@ $(document.body).ready(function() {
data: {
ex_id: ex_id,
},
- success: function(data) {
- $("#example_views_count").text(data);
+ success: function(data1) {
+ $("#example_views_count").text(data1);
}
});
$("#revisions").html(
@@ -498,30 +499,30 @@ $(document.body).ready(function() {
);
var i = 1;
- data.forEach(
- function(
- item) {
- $('#revisions').append(
- '<option value="' + item.commit_sha + '"> ' +
- i + ' - ' + item.commit_message +
+ $.each(data, function(key, value){
+ $.each(value, function(key, value){
+ $('#revisions').append(
+ '<option value="' + value[1] + '"> ' +
+ i + ' - ' + value[0]+
'</option>'
- );
- i++;
+ );
+ i++;
});
+ });
$("#revisions-diff").html(
' <option value="">Select a revision</option>'
);
var i = 1;
- data.forEach(
- function(
- item) {
- $('#revisions-diff').append(
- '<option value="' + item.commit_sha + '"> ' +
- i + ' - ' + item.commit_message +
+ $.each(data, function(key, value){
+ $.each(value, function(key, value){
+ $('#revisions-diff').append(
+ '<option value="' + value[1] + '"> ' +
+ i + ' - ' + value[0]+
'</option>'
- );
- i++;
+ );
+ i++;
});
+ });
$(
'#revisions option:eq(1)'
).prop(
@@ -704,21 +705,21 @@ $(document.body).ready(function() {
/********************************************/
$(document).on("click", "#download-book", function(e) {
window.location =
- "http://r.fossee.in/download/book/" + $(
+ "http://r.fossee.in/textbook-companion/download/book/" + $(
"#books").val();
e.preventDefault();
});
$(document).on("click", "#download-chapter", function(e) {
window.location =
- "http://r.fossee.in/download/chapter/" +
+ "http://r.fossee.in/textbook-companion/download/chapter/" +
$("#chapters").val();
e.preventDefault();
});
$(document).on("click", "#download-example", function(e) {
window.location =
- "http://r.fossee.in/download/example/" +
+ "http://r.fossee.in/textbook-companion/download/example/" +
$("#examples").val();
e.preventDefault();
});
diff --git a/website/static/website/templates/index.html b/website/static/website/templates/index.html
index b147294..4972799 100644
--- a/website/static/website/templates/index.html
+++ b/website/static/website/templates/index.html
@@ -106,9 +106,10 @@
<a href="#" class="extra-link col-sm-2" id="contributor">+Contributor</a>
</div>
<div id="books-wrapper" class="form-row form-group">
- <label for="disabledInput" class="col-sm-2 col-form-label font-weight-bold">Book :</label>
+ <label for="disabledInput" class="col-sm-2 col-form-label font-weight-bold">Book : </label>
<select id="books" class="form-control-sm col-sm-8">
<option value="0">Select Book </option>
+
{% for book in books %} {% if book_id == book.id %}
<option value="{{ book.id }}" selected>{{ forloop.counter }} - {{ book.book }} (by {{ book.author }})</option>
{% else %}
diff --git a/website/urls.py b/website/urls.py
index b7b509c..3f12488 100644
--- a/website/urls.py
+++ b/website/urls.py
@@ -15,5 +15,6 @@ urlpatterns = [
path('get_code/', ajax.code, name='code'),
path('get_diff/', ajax.diff, name='diff'),
path('update_view_count/', views.update_view_count,
- name='update_view_count')
+ name='update_view_count'),
+ path('get_contributor/', ajax.contributor, name='contributor'),
]
diff --git a/website/views.py b/website/views.py
index 8d63b28..4a956ea 100644
--- a/website/views.py
+++ b/website/views.py
@@ -3,12 +3,337 @@ from django.http import HttpResponse
from django.template import loader
import requests
import uuid
+from website.models import *
+from django.db.models import Q
+import json as simplejson
+from . import utils
+from django.db import connections
+from .query import *
+
+def dictfetchall(cursor):
+ "Return all rows from a cursor as a dict"
+ columns = [col[0] for col in cursor.description]
+ return [
+ dict(zip(columns, row))
+ for row in cursor.fetchall()
+ ]
+
+def catg(cat_id, all_cat):
+ if all_cat is False:
+ category = TextbookCompanionCategoryList.objects.using('r')\
+ .get(category_id=cat_id)
+ return category.maincategory
+ else:
+ category = TextbookCompanionCategoryList.objects.using('r')\
+ .filter(~Q(category_id=0)).order_by('maincategory')
+ return category
+
+
+def subcatg(subcat_id, all_subcat):
+ if all_subcat is False:
+ category = TextbookCompanionSubCategoryList.objects.using('r')\
+ .get(id=subcat_id)
+ return category.subcategory
+ else:
+ category = TextbookCompanionSubCategoryList.objects.using('r')\
+ .all().order_by('subcategory')
+ return category
+
+
+def get_subcategories(maincat_id):
+ subcategories = TextbookCompanionSubCategoryList.objects.using('r')\
+ .filter(maincategory_id=maincat_id).order_by('subcategory_id')
+ return subcategories
+
+
+
+def get_books(category_id):
+
+ with connections['r'].cursor() as cursor:
+ cursor.execute(GET_TBC_PREFERENCE_FROM_CATEGORY_ID_SQL,
+ params=[category_id])
+ books = dictfetchall(cursor)
+ return books
+
+def get_chapters(book_id):
+ with connections['r'].cursor() as cursor:
+ cursor.execute(GET_TBC_CHAPTER_SQL,
+ params=[book_id])
+ chapters = dictfetchall(cursor)
+ return chapters
+
+
+def get_examples(chapter_id):
+ with connections['r'].cursor() as cursor:
+ cursor.execute(GET_TBC_EXAMPLE_SQL,
+ params=[chapter_id])
+ examples = dictfetchall(cursor)
+ return examples
+
+
+def get_revisions(example_id):
+ with connections['r'].cursor() as cursor:
+ cursor.execute(GET_TBC_EXAMPLE_FILE_SQL, params=[example_id])
+ example_file = cursor.fetchone()
+ example_file_filepath = example_file[4] + '/' + example_file[5]
+ commits = utils.get_commits(file_path=example_file_filepath)
+ return commits
+
+
+def get_code(file_path, commit_sha):
+ code= utils.get_file(file_path, commit_sha, main_repo=True)
+ return code
+
def index(request):
context = {}
- session_id = uuid.uuid4()
- context['session_id'] = str(session_id)
- request.session['session_id'] = str(session_id)
- template = loader.get_template('index.html')
- return HttpResponse(template.render(context, request))
+ book_id = request.GET.get('book_id')
+ user = request.user
+
+ # if not user.is_anonymous():
+ # social = user.social_auth.get(provider='google-oauth2')
+ # url = 'https://www.googleapis.com/plus/v1/people/me'
+ # params = {'access_token': social.extra_data['access_token']}
+ # # r = requests.get(url, params=params)
+ # # print(r.content)
+
+ # context = {
+ # 'user': user
+ # }
+
+ if not (request.GET.get('eid') or request.GET.get('book_id')):
+ catg_all = catg(None, all_cat=True)
+ subcatg_all = subcatg(None, all_subcat=True)
+ context = {
+ 'catg': catg_all,
+ 'subcatg': subcatg_all,
+ }
+ if 'maincat_id' in request.session:
+ maincat_id = request.session['maincat_id']
+ context['maincat_id'] = int(maincat_id)
+ context['subcatg'] = get_subcategories(maincat_id)
+
+ if 'subcategory_id' in request.session:
+ category_id = request.session['subcategory_id']
+ context['subcategory_id'] = int(category_id)
+ context['books'] = get_books(category_id)
+
+ if 'book_id' in request.session:
+ book_id = request.session['book_id']
+ context['book_id'] = int(book_id)
+ context['chapters'] = get_chapters(book_id)
+
+ if 'chapter_id' in request.session:
+ chapter_id = request.session['chapter_id']
+ context['chapter_id'] = int(chapter_id)
+ context['examples'] = get_examples(chapter_id)
+
+ if 'example_id' in request.session:
+ example_id = request.session['example_id']
+ context['eid'] = int(example_id)
+ context['revisions'] = get_revisions(example_id)
+ with connections['r'].cursor() as cursor:
+ cursor.execute(GET_TBC_EXAMPLE_R_CLOUD_COMMENT_SQL,
+ params=[example_id])
+ review = cursor.fetchone()
+ review_url = "https://r.fossee.in/cloud_comments/" + str(example_id)
+ context['review'] = review[0]
+ context['review_url'] = review_url
+
+ if 'commit_sha' in request.session:
+ commit_sha = request.session['commit_sha']
+ context['commit_sha'] = commit_sha
+
+ if 'code' in request.session:
+ session_code = request.session['code']
+ context['code'] = session_code
+ elif 'filepath' in request.session:
+ session_code = get_code(
+ request.session['filepath'], commit_sha)
+ context['code'] = session_code
+ template = loader.get_template('index.html')
+ return HttpResponse(template.render(context, request))
+ elif book_id:
+ books = TextbookCompanionPreference.objects\
+ .db_manager('r').raw("""
+ SELECT DISTINCT (loc.category_id),pe.id,
+ tcbm.sub_category,loc.maincategory, pe.book as
+ book,loc.category_id,tcbm.sub_category,
+ pe.author as author, pe.publisher as publisher,
+ pe.year as year, pe.id as pe_id, pe.edition,
+ po.approval_date as approval_date
+ FROM textbook_companion_preference pe LEFT JOIN
+ textbook_companion_proposal po ON pe.proposal_id = po.id
+ LEFT JOIN textbook_companion_book_main_subcategories
+ tcbm ON pe.id = tcbm.pref_id LEFT JOIN list_of_category
+ loc ON tcbm.main_category = loc.category_id WHERE
+ po.proposal_status = 3 AND pe.approval_status = 1
+ AND pe.id = tcbm.pref_id AND
+ pe.cloud_pref_err_status = 0 AND
+ pe.id=%s""", [book_id])
+ books = list(books)
+
+ if len(books) == 0:
+ catg_all = catg(None, all_cat=True)
+ context = {
+ 'catg': catg_all,
+ 'err_msg': """This book is not supported by Scilab on Cloud."""
+ """ You are redirected to home page."""
+ }
+
+ template = loader.get_template('index.html')
+ return HttpResponse(template.render(context, request))
+
+ books = get_books(books[0].sub_category)
+ maincat_id = books[0].category_id
+ subcat_id = books[0].sub_category
+ request.session['maincat_id'] = maincat_id
+ request.session['subcategory_id'] = subcat_id
+ request.session['book_id'] = book_id
+ chapters = get_chapters(book_id)
+ subcateg_all = TextbookCompanionSubCategoryList.objects\
+ .using('r').filter(maincategory_id=maincat_id)\
+ .order_by('subcategory_id')
+ categ_all = TextbookCompanionCategoryList.objects.using('r')\
+ .filter(~Q(category_id=0)).order_by('maincategory')
+ context = {
+ 'catg': categ_all,
+ 'subcatg': subcateg_all,
+ 'maincat_id': maincat_id,
+ 'chapters': chapters,
+ 'subcategory_id': books[0].sub_category,
+ 'books': books,
+ 'book_id': int(book_id),
+
+ }
+ template = loader.get_template('index.html')
+ return HttpResponse(template.render(context, request))
+ else:
+ try:
+ eid = int(request.GET['eid'])
+ except ValueError:
+ context = {
+ 'catg': catg_all,
+ 'err_msg': """This example is currently not available on """
+ """scilab on cloud."""
+ }
+ template = loader.get_template('index.html')
+ return HttpResponse(template.render(context, request))
+
+ if eid:
+ try:
+ with connections['r'].cursor() as cursor:
+ cursor.execute(GET_TBC_EXAMPLE_R_CLOUD_COMMENT_SQL,
+ params=[eid])
+ review = cursor.fetchone()
+ review_url = "https://r.fossee.in/cloud_comments/" + str(eid)
+
+ with connections['r'].cursor() as cursor:
+ cursor.execute(GET_TBC_EXAMPLE_CHAPTER_ID_SQL,
+ params=[eid])
+ chapter_id = cursor.fetchone()
+
+ with connections['r'].cursor() as cursor:
+ cursor.execute(GET_TBC_CHAPTER_DETAIL_SQL,
+ params=[chapter_id[0]])
+ chapters = dictfetchall(cursor)
+
+ with connections['r'].cursor() as cursor:
+ cursor.execute(GET_TBC_CHAPTER_PREFERENCE_ID_SQL,
+ params=[chapter_id[0]])
+ preference_id = cursor.fetchone()
+ with connections['r'].cursor() as cursor:
+ cursor.execute(GET_TBC_PREFERENCE_DETAIL_CATEGORY_SQL,
+ params=[preference_id[0]])
+ books_detail = cursor.fetchone()
+ books = get_books(books_detail[1])
+ maincat_id = books_detail[0]
+ subcat_id = books_detail[1]
+
+
+ with connections['r'].cursor() as cursor:
+ cursor.execute(GET_TBC_EXAMPLE_FILE_SQL,
+ params=[eid])
+ example_file = cursor.fetchone()
+ example_file_filepath = example_file[4] + '/' + example_file[5]
+ with connections['r'].cursor() as cursor:
+ cursor.execute(GET_TBC_EXAMPLE_VIEW_SQL,
+ params=[eid])
+ ex_views_count = cursor.fetchone()
+
+ request.session['maincat_id'] = maincat_id
+ request.session['subcategory_id'] = subcat_id
+ request.session['book_id'] = preference_id[0]
+ request.session['chapter_id'] = chapter_id[0]
+ request.session['example_id'] = eid
+ request.session['example_file_id'] = example_file[3]
+ request.session['filepath'] = example_file_filepath
+
+ revisions = get_revisions(eid)
+ code = get_code(example_file_filepath, revisions[0][1])
+ request.session['commit_sha'] = revisions[0][1]
+
+ except IndexError:
+ categ_all = TextbookCompanionCategoryList.objects\
+ .using('r').filter(~Q(category_id=0))\
+ .order_by('maincategory')
+ context = {
+ 'catg': categ_all,
+ 'err_msg': """This example is currently not available on"""
+ """ scilab on cloud."""
+ }
+ template = loader.get_template('index.html')
+ return HttpResponse(template.render(context, request))
+ subcateg_all = TextbookCompanionSubCategoryList.objects\
+ .using('r').filter(maincategory_id=maincat_id)\
+ .order_by('subcategory_id')
+ categ_all = TextbookCompanionCategoryList.objects.using('r')\
+ .filter(~Q(category_id=0)).order_by('maincategory')
+ if len(list([ex_views_count[0]])) == 0:
+ ex_views_count = 0
+ else:
+ ex_views_count = ex_views_count[0]
+ context = {
+ 'catg': categ_all,
+ 'subcatg': subcateg_all,
+ 'maincat_id': maincat_id,
+ 'subcategory_id': subcat_id,
+ 'books': list(books),
+ 'book_id': preference_id[0],
+ 'chapters': chapters,
+ 'chapter_id': chapter_id[0],
+ 'examples': get_examples(chapter_id[0]),
+ 'eid': eid,
+ 'revisions': revisions,
+ 'commit_sha': revisions[0][1],
+ 'code': code,
+ 'ex_views_count': ex_views_count,
+ 'review': review[0],
+ 'review_url': review_url,
+ }
+
+ # if not user.is_anonymous():
+ # context['user'] = user
+
+ template = loader.get_template('index.html')
+ return HttpResponse(template.render(context, request))
+
+def update_view_count(request):
+ ex_id = request.GET.get('ex_id')
+ with connections['r'].cursor() as cursor:
+ cursor.execute(GET_TBC_CHAPTER_ID_SQL, params=[ex_id])
+ Example_chapter_id = cursor.fetchone()
+ with connections['r'].cursor() as cursor:
+ cursor.execute(INSERT_TBC_EXAMPLE_VIEW_SQL,
+ params=[ex_id,Example_chapter_id[0]])
+ with connections['r'].cursor() as cursor:
+ cursor.execute(UPDATE_TBC_EXAMPLE_VIEW_SQL,
+ params=[ex_id])
+ with connections['r'].cursor() as cursor:
+ cursor.execute(GET_TBC_EXAMPLE_VIEW_SQL,
+ params=[ex_id])
+ Example_views_count = cursor.fetchone()
+ data = Example_views_count[0]
+ return HttpResponse(simplejson.dumps(data),
+ content_type='application/json')