Skip to content

Commit

Permalink
Merge branch 'master' into minimize
Browse files Browse the repository at this point in the history
  • Loading branch information
HadronCollider committed Sep 23, 2021
2 parents 9bd31b4 + 6fbf11c commit 59a4118
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 28 deletions.
7 changes: 7 additions & 0 deletions app/bd_helper/bd_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ def get_checks_pdf(checks_id):
return None


def find_pdf_by_file_id(file_id):
try:
return fs.open_download_stream(file_id)
except NoFile:
return None


# Deletes checks with given id, returns presentation
def delete_check(presentation, checks_id):
if checks_id in presentation.checks:
Expand Down
5 changes: 4 additions & 1 deletion app/lti_session_passback/lti/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ def get_criteria_from_launch(data):
detect_additional = custom.get('detect_additional', 'True')
criteria = dict((k, custom[k]) for k in all_checks if k in custom)
eval_criteria = dict((key, eval(value)) for key, value in criteria.items() if key != 'slides_number')
eval_criteria['slides_number'] = {'sld_num': sld_num[criteria.get('slides_number', 'bsc')], 'detect_additional': eval(detect_additional)}
if criteria.get('slides_number') not in ['bsc', 'msc']:
eval_criteria['slides_number'] = {'sld_num': eval(criteria.get('slides_number')), 'detect_additional': eval(detect_additional)}
else:
eval_criteria['slides_number'] = {'sld_num': sld_num[criteria.get('slides_number', 'bsc')], 'detect_additional': eval(detect_additional)}
return eval_criteria

def extract_passback_params(data):
Expand Down
6 changes: 3 additions & 3 deletions app/main/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

def check(presentation, checks, presentation_name):
check_names = checks.get_checks().keys()
check_classes = [SldNumCheck(presentation, checks.slides_number), SldEnumCheck(presentation), TitleFormatCheck(presentation), \
FindDefSld(presentation, key_slide.goals_and_tasks), FindDefSld(presentation, key_slide.approbation), \
SearchKeyWord(presentation, key_slide.relevance), FindDefSld(presentation, key_slide.conclusion), \
check_classes = [SldNumCheck(presentation, checks.slides_number), SldEnumCheck(presentation, checks.conv_pdf_fs_id), TitleFormatCheck(presentation, checks.conv_pdf_fs_id), \
FindDefSld(presentation, key_slide.goals_and_tasks, checks.conv_pdf_fs_id), FindDefSld(presentation, key_slide.approbation, checks.conv_pdf_fs_id), \
SearchKeyWord(presentation, key_slide.relevance, checks.conv_pdf_fs_id), FindDefSld(presentation, key_slide.conclusion, checks.conv_pdf_fs_id), \
FindTasks(presentation, key_slide.goals_and_tasks, checks.slide_every_task), \
SldSimilarity(presentation, key_slide.goals_and_tasks, key_slide.conclusion, checks.conclusion_actual),
FurtherDev(presentation, key_slide.goals_and_tasks, key_slide.conclusion)]
Expand Down
7 changes: 7 additions & 0 deletions app/main/checks/base_check.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from flask import url_for

def answer(mod, value, *args):
return {
'pass': bool(mod),
Expand All @@ -11,3 +13,8 @@ def __init__(self, presentation):

def check(self):
raise NotImplementedError()

def format_page_link(self, error):
base_pdf_link = url_for('get_pdf', _id=self.pdf_id)
page = lambda err: f'{base_pdf_link}#page={str(err)}'
return [f'<a href="{page(e)}"target="_blank" rel="noopener">{str(e)}<a>' for e in error]
4 changes: 3 additions & 1 deletion app/main/checks/find_def_sld.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from app.main.checks.base_check import BaseCheck, answer

class FindDefSld(BaseCheck):
def __init__(self, presentation, type_of_slide):
def __init__(self, presentation, type_of_slide, pdf_id):
super().__init__(presentation)
self.type_of_slide = type_of_slide
self.pdf_id = pdf_id

def check(self):
found_slides, found_idxs = [], []
Expand All @@ -14,4 +15,5 @@ def check(self):
if len(found_slides) == 0:
return answer(False, None, 'Слайд не найден')
else:
found_idxs = self.format_page_link(found_idxs)
return answer(True, found_idxs, 'Найден под номером: {}'.format(', '.join(map(str, found_idxs))))
6 changes: 4 additions & 2 deletions app/main/checks/search_keyword.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from app.main.checks.base_check import BaseCheck, answer

class SearchKeyWord(BaseCheck):
def __init__(self, presentation, key_slide):
def __init__(self, presentation, key_slide, pdf_id):
super().__init__(presentation)
self.key_slide = key_slide
self.pdf_id = pdf_id

def check(self):
for i, text in enumerate(self.presentation.get_text_from_slides(), 1):
if self.key_slide.lower() in str(text).lower():
return answer(True, i, 'Найден под номером: {}'.format(i))
found = self.format_page_link([i])
return answer(True, i, 'Найден под номером: {}'.format(', '.join(map(str, found))))
return answer(False, None, 'Слайд не найден')
4 changes: 3 additions & 1 deletion app/main/checks/sld_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@


class SldEnumCheck(BaseCheck):
def __init__(self, presentation):
def __init__(self, presentation, pdf_id):
super().__init__(presentation)
self.pdf_id = pdf_id

def check(self):
error = []
Expand All @@ -16,5 +17,6 @@ def check(self):
if not error:
return answer(True, error, "Пройдена!")
else:
error = self.format_page_link(error)
return answer(False, error, 'Не пройдена, проблемные слайды: {}'.format(', '.join(map(str, error))), \
'Убедитесь в корректности формата номеров слайдов')
7 changes: 4 additions & 3 deletions app/main/checks/title_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
import itertools

class TitleFormatCheck(BaseCheck):
def __init__(self, presentation):
def __init__(self, presentation, pdf_id):
super().__init__(presentation)
self.empty_headers = []
self.len_exceeded = []
self.pdf_id = pdf_id

def exceeded_verdict(self):
return 'Превышение длины: {}'.format(', '.join(map(str, self.len_exceeded))), \
return 'Превышение длины: {}'.format(', '.join(map(str, self.format_page_link(self.len_exceeded)))), \
'Убедитесь в корректности заголовка и текста слайда'

def empty_verdict(self):
return 'Заголовки не найдены: {}.'.format(', '.join(map(str, self.empty_headers))), \
return 'Заголовки не найдены: {}.'.format(', '.join(map(str, self.format_page_link(self.empty_headers)))), \
'Убедитесь, что слайд озаглавлен соответстующим элементом'

def get_failing_headers(self):
Expand Down
2 changes: 1 addition & 1 deletion app/servants/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def upload(request, upload_folder):
presentation = get_presentation(presentation_id)

checks = create_check(current_user)
check(parse(filename), checks, presentation_name)
checks.conv_pdf_fs_id = converted_id
check(parse(filename), checks, presentation_name)
presentation, checks_id = add_check(presentation, checks, filename)

if delete and exists(filename):
Expand Down
2 changes: 1 addition & 1 deletion app/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def checks(_id):
@login_required
def get_pdf(_id):
try:
file = bd_helper.get_checks_pdf(ObjectId(_id))
file = bd_helper.find_pdf_by_file_id(ObjectId(_id))
except bson.errors.InvalidId:
logger.error('_id exception in fetching pdf occured:', exc_info=True)
return render_template("./404.html")
Expand Down
1 change: 1 addition & 0 deletions assets/scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'bootstrap-datepicker';
import 'bootstrap-datepicker/dist/css/bootstrap-datepicker.min.css'

import * as CryptoJS from "crypto-js";
import * as pdfjsLib from 'pdfjs-dist';

import '../styles/main.css';

Expand Down
86 changes: 86 additions & 0 deletions assets/scripts/results.js
Original file line number Diff line number Diff line change
@@ -1 +1,87 @@
import '../styles/results.css';
import * as pdfjsLib from 'pdfjs-dist';
import pdfjsWorker from "pdfjs-dist/build/pdf.worker.entry";

let pdfDoc,
pageNum,
pageIsRendering,
pageNumIsPending,
scale,
canvas,
ctx,
currentPage;

pdfjsLib.GlobalWorkerOptions.workerSrc = pdfjsWorker;

const renderPage = num => {
pageIsRendering = true;

pdfDoc.getPage(num).then(page => {
const viewport = page.getViewport({ scale });
canvas.height = viewport.height;
canvas.width = viewport.width;

const renderCtx = {
canvasContext: ctx,
viewport
};

page.render(renderCtx).promise.then(() => {
pageIsRendering = false;

if (pageNumIsPending !== null) {
renderPage(pageNumIsPending);
pageNumIsPending = null;
}
});

$('#page-num')[0].textContent = num;
});
};

const queueRenderPage = num => {
if (pageIsRendering) {
pageNumIsPending = num;
} else {
renderPage(num);
}
};

const showPrevPage = () => {
if (pageNum <= 1) {
return;
}
pageNum--;
queueRenderPage(pageNum);
};

const showNextPage = () => {
if (pageNum >= pdfDoc.numPages) {
return;
}
pageNum++;
queueRenderPage(pageNum);
};

if ($("#pdf").length !== 0){
var href = $("#pdf").attr('href');
pdfDoc = null;
pageNum = 1;
pageIsRendering = false,
pageNumIsPending = null;
scale = 1.1;
canvas = $("#the-canvas")[0];
ctx = canvas.getContext("2d");

pdfjsLib
.getDocument(href)
.promise.then(pdfDoc_ => {
pdfDoc = pdfDoc_;

$('#page-count')[0].textContent = pdfDoc.numPages;
renderPage(pageNum);
});

$('#prev-page').click(showPrevPage);
$('#next-page').click(showNextPage);
}
43 changes: 36 additions & 7 deletions db_versioning/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ class Version:
@classmethod
def update_database(cls, collections, prev_version):
"""
<collections> must contains (objects from pymongo)
<collections> must contains (objects from pymongo)
- users
- presentations
- checks
"""
raise NotImplementedError()

@classmethod
def to_dict(cls):
return dict(
Expand All @@ -27,7 +27,7 @@ def get_version(version_name):
for version in VERSIONS:
if version.version == version_name:
return version
return None
return None


class Version10(Version):
Expand Down Expand Up @@ -69,7 +69,7 @@ class Version20(Version):
@classmethod
def update_database(cls, collections, prev_version):
if prev_version in (Version10.VERSION_NAME, Version11.VERSION_NAME):
# process all checks of pres and set filename + user
# process all checks of pres and set filename + user
for presentation in collections['presentations'].find({}):
filename = presentation['name']
user_doc = collections['users'].find_one({'presentations': presentation['_id'] })
Expand All @@ -79,7 +79,7 @@ def update_database(cls, collections, prev_version):
{"_id": check_id},
{ '$set': { 'filename': filename, 'user': user } }
)

# if we have checks without presentation == after prev loop it doesn't include filename/user field
# set default user='moevm', filename='_.pptx'
collections['checks'].update(
Expand Down Expand Up @@ -126,14 +126,43 @@ def update_database(cls, collections, prev_version):
else:
raise Exception(f'Неподдерживаемый переход с версии {prev_version}')

class Version21(Version):
VERSION_NAME = '2.1'
CHANGES = '0/1 -> T/F; criteria.slides_number: [] -> {}'

@classmethod
def update_database(cls, collections, prev_version):
if prev_version in (Version10.VERSION_NAME, Version11.VERSION_NAME, Version20.VERSION_NAME):

#mv from 0/-1 -> T/F
for check in collections['checks'].find({}):
check_dt = Checks(check).get_checks().items()
upd_check = {k: False if v == -1 else v for k, v in check_dt}
collections['checks'].update(
{'_id': check['_id']},
{'$set': upd_check}
)

for user in collections['users'].find():
criteria_dt = Checks(user['criteria']).get_checks()
upd_criteria = {k: False if v == -1 else True for k, v in criteria_dt.items()}
upd_criteria['slides_number'] = {"sld_num": criteria_dt['slides_number'], "detect_additional": True} if upd_criteria['slides_number'] else False
collections['users'].update(
{'_id': user['_id']},
{'$set': {'criteria': upd_criteria}}
)

else:
raise Exception(f'Неподдерживаемый переход с версии {prev_version}')

VERSIONS = {
'1.0': Version10,
'1.1': Version11,
'2.0': Version20,
'2.1': Version21,
}
LAST_VERSION = '2.0'
LAST_VERSION = '2.1'


for _, ver in VERSIONS.items():
print(ver.to_dict())
print(ver.to_dict())
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
"crypto-js": "^3.1.2",
"bootstrap-datepicker": "^1.9.0",
"bootstrap-table": "^1.18.3",
"bootstrap-icons": "1.5.0"
"bootstrap-icons": "1.5.0",
"pdfjs-dist": "^2.2.228"

}
}
Loading

0 comments on commit 59a4118

Please sign in to comment.