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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.template import loader
import requests
import uuid
from R_on_Cloud.config import (API_URL_UPLOAD, API_URL_RESET, AUTH_KEY,
API_URL_SERVER)
from website.models import *
from django.db.models import Q
import json as simplejson
from . import utils
from django.db import connections
from collections import defaultdict
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():
with connections['r'].cursor() as cursor:
cursor.execute(GET_ALLMAINCATEGORY_SQL)
category = dictfetchall(cursor)
return category
def get_subcategories(maincat_id):
with connections['r'].cursor() as cursor:
cursor.execute(GET_SUBCATEGORY_SQL,
params=[maincat_id])
subcategories = dictfetchall(cursor)
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 = {}
user_id = uuid.uuid4()
context['api_url_upload'] = API_URL_UPLOAD
context['reset_req_url'] = API_URL_RESET
context['api_url'] = API_URL_SERVER
book_id = request.GET.get('book_id')
user = request.user
if not 'user_id' in request.session:
request.session['user_id'] = str(user_id)
if not (request.GET.get('eid') or request.GET.get('book_id')):
catg_all = catg()
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
context = {
'catg': catg_all,
'api_url_upload': API_URL_UPLOAD,
'user_id': request.session['user_id'],
'key': AUTH_KEY,
'api_url': API_URL_SERVER,
}
template = loader.get_template('index.html')
return HttpResponse(template.render(context, request))
elif book_id:
with connections['r'].cursor() as cursor:
cursor.execute(GET_BOOK_CATEGORY_FROM_ID,
params=[book_id])
books = cursor.fetchone()
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."""
}
context['api_url_upload'] = API_URL_UPLOAD
context['reset_req_url'] = API_URL_RESET
context['api_url'] = API_URL_SERVER
template = loader.get_template('index.html')
return HttpResponse(template.render(context, request))
req_books = get_books(books[2])
maincat_id = books[0]
subcat_id = books[2]
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[2],
'books': req_books,
'book_id': int(book_id),
}
context['api_url_upload'] = API_URL_UPLOAD
context['reset_req_url'] = API_URL_RESET
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."""
}
context['api_url_upload'] = API_URL_UPLOAD
context['reset_req_url'] = API_URL_RESET
context['api_url'] = API_URL_SERVER
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:
rows_count = cursor.execute(GET_TBC_PREFERENCE_DETAIL_CATEGORY_SQL,
params=[preference_id[0]])
if rows_count > 0:
books_detail = cursor.fetchone()
books = get_books(books_detail[1])
maincat_id = books_detail[0]
subcat_id = books_detail[1]
else:
catg_all = catg()
context = {
'catg': catg_all,
'err_msg': """This book is not supported by R on Cloud."""
""" You are redirected to home page."""
}
context['api_url_upload'] = API_URL_UPLOAD
context['reset_req_url'] = API_URL_RESET
context['api_url'] = API_URL_SERVER
template = loader.get_template('index.html')
return HttpResponse(template.render(context, request))
with connections['r'].cursor() as cursor:
cursor.execute(GET_TBC_EXAMPLE_FILE_SQL,
params=[eid])
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."""
}
context['api_url_upload'] = API_URL_UPLOAD
context['api_url'] = API_URL_SERVER
template = loader.get_template('index.html')
return HttpResponse(template.render(context, request))
subcateg_all = get_subcategories(maincat_id)
categ_all = catg()
if ex_views_count != None:
if len(list([ex_views_count[0]])) != 0:
ex_views_count = ex_views_count[0]
else:
ex_views_count = 0
else:
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
context['api_url_upload'] = API_URL_UPLOAD
context['reset_req_url'] = API_URL_RESET
context['api_url'] = API_URL_SERVER
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')
def reset(request):
try:
for key, value in list(request.session.items()):
if key != 'user_id':
del request.session[key]
response = {"data": "ok"}
return HttpResponse(simplejson.dumps(response),
content_type='application/json')
except KeyError:
pass
response = {"data": "ok"}
return HttpResponse(simplejson.dumps(response),
content_type='application/json')
def search_book(request):
result = {}
response_dict = []
if request.is_ajax():
exact_search_string = request.GET.get('search_string')
search_string = "%" + exact_search_string + "%"
with connections['r'].cursor() as cursor:
cursor.execute(GET_SEARCH_BOOK_SQL, [search_string, search_string,
str(exact_search_string),
str(exact_search_string)])
result = dictfetchall(cursor)
return HttpResponse(simplejson.dumps(result),
content_type='application/json')
def popular(request):
result = {}
response_dict = []
if request.is_ajax():
search_string = request.GET.get('search_string')
search_string = "%" + search_string + "%"
with connections['r'].cursor() as cursor:
cursor.execute(GET_SEARCH_POPULAR_BOOK_SQL)
result = dictfetchall(cursor)
return HttpResponse(simplejson.dumps(result),
content_type='application/json')
def recent(request):
result = {}
response_dict = []
if request.is_ajax():
exact_search_string = request.GET.get('search_string')
search_string = "%" + exact_search_string + "%"
with connections['r'].cursor() as cursor:
cursor.execute(GET_SEARCH_RECENT_BOOK_SQL)
result = dictfetchall(cursor)
return HttpResponse(simplejson.dumps(result),
content_type='application/json')
def update_pref_hits(pref_id):
updatecount = TextbookCompanionPreferenceHits.objects.using('r')\
.filter(pref_id=pref_id)\
.update(hitcount=F('hitcount') + 1)
if not updatecount:
insertcount = TextbookCompanionPreferenceHits.objects.using('r')\
.get_or_create(pref_id=pref_id, hitcount=1)
return
|