Commit d9e84339 authored by Anton Akhmerov's avatar Anton Akhmerov
Browse files

Merge branch '514-restrict-widget-size' into 'master'

Restrict widget size on problem creation

Closes #514

See merge request zesje/zesje!307
parents a512b2d2 889d54aa
......@@ -121,6 +121,7 @@ class ExamEditor extends React.Component {
this.props.createNewWidget(widgetData)
}).catch(err => {
console.log(err)
err.json().then(e => Notification.error(e.message))
})
}
}
......@@ -141,18 +142,19 @@ class ExamEditor extends React.Component {
/**
* Calculate selection box dimensions
*
* TODO: Clamp values to parent
*/
calculateSelectionBox = (selectionStartPoint, selectionEndPoint) => {
if (!this.state.mouseDown || selectionEndPoint === null || selectionStartPoint === null) {
return null
}
const left = Math.min(selectionStartPoint.x, selectionEndPoint.x)
const top = Math.min(selectionStartPoint.y, selectionEndPoint.y)
const width = Math.abs(selectionStartPoint.x - selectionEndPoint.x)
const height = Math.abs(selectionStartPoint.y - selectionEndPoint.y)
// add a small padding to all the sides
const padding = 1
const left = Math.max(Math.min(selectionStartPoint.x, selectionEndPoint.x), padding)
const top = Math.max(Math.min(selectionStartPoint.y, selectionEndPoint.y), padding)
const width = Math.min(Math.abs(left - Math.max(selectionStartPoint.x, selectionEndPoint.x)), 595 - padding - left)
const height = Math.min(Math.abs(top - Math.max(selectionStartPoint.y, selectionEndPoint.y)), 841 - padding - top)
const result = {
left: left,
top: top,
......
import pytest
from datetime import datetime
from zesje.database import db, Exam, Problem, FeedbackOption, ProblemWidget, Student, Submission, Solution, Grader
@pytest.fixture
def add_test_data(app):
exam = Exam(id=1, name='exam 1', finalized=True)
db.session.add(exam)
problem = Problem(id=1, name='Problem 1', exam_id=1)
db.session.add(problem)
problem_widget = ProblemWidget(id=1, name='problem widget', problem_id=1, page=2,
width=100, height=150, x=40, y=200, type='problem_widget')
db.session.add(problem_widget)
db.session.commit()
feedback_option = FeedbackOption(id=1, problem_id=1, text='text', description='desc', score=1)
db.session.add(feedback_option)
db.session.commit()
yield app, exam, problem
@pytest.mark.parametrize('id, status', [
(1, 200),
(42, 404)
], ids=['Exists', 'Not exists'])
def test_get_problem(test_client, add_test_data, id, status):
result = test_client.get(f'/api/problems/{id}')
assert result.status_code == status
@pytest.mark.parametrize('position, status', [
((-1, 4, 400, 200), 409),
((10, -5, 400, 200), 409),
((10, 10, 800, 200), 409),
((10, 700, 400, 500), 409)
], ids=['Exceeds left', 'Exceeds top', 'Exceeds right', 'Exceeds bottom'])
def test_add_problem(test_client, add_test_data, position, status):
req_body = {
'exam_id': 1,
'x': position[0],
'y': position[1],
'width': position[2],
'height': position[3],
'page': 1,
'name': 'Problem'
}
result = test_client.post('/api/problems', data=req_body)
assert result.status_code == status
@pytest.mark.parametrize('id, status', [
(1, 200),
(42, 404),
], ids=['Exists', 'Not exists'])
def test_rename_problem(test_client, add_test_data, id, status):
new_name = 'New'
result = test_client.put(f'/api/problems/{id}', data={'name': new_name})
assert result.status_code == status
if status == 200:
assert Problem.query.get(id).name == new_name
@pytest.mark.parametrize('id, status', [
(1, 200),
(42, 404),
], ids=['Exists', 'Not exists'])
def test_delete_problem(test_client, add_test_data, id, status):
result = test_client.delete(f'/api/problems/{id}')
assert result.status_code == status
assert Problem.query.get(id) is None
def test_delete_problem_graded(test_client, add_test_data):
app, exam, problem = add_test_data
student = Student(first_name='', last_name='')
db.session.add(student)
grader = Grader(name='Zesje')
db.session.add(grader)
db.session.commit()
sub = Submission(student=student, exam=exam)
db.session.add(sub)
db.session.commit()
sol = Solution(problem=problem, submission=sub, graded_by=grader, graded_at=datetime.now())
db.session.add(sol)
db.session.commit()
result = test_client.delete(f'/api/problems/{problem.id}')
assert result.status_code == 403
assert Problem.query.get(problem.id) is not None
......@@ -97,6 +97,11 @@ class Problems(Resource):
msg = f"Exam with id {exam_id} doesn't exist"
return dict(status=400, message=msg), 400
page_size = current_app.config['PAGE_FORMATS'][current_app.config['PAGE_FORMAT']]
if not (0 <= args['x'] < args['width'] + args['x'] < page_size[0]
and 0 <= args['y'] < args['height'] + args['y'] < page_size[1]):
return dict(status=409, message='Problem size exceeds the page size.'), 409
widget = ProblemWidget(
x=args['x'],
y=args['y'],
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment