Verified Commit 4b99380c authored by Peter Stanko's avatar Peter Stanko
Browse files

Changes should be updated

parent 11504fcd
Pipeline #32284 passed with stage
in 7 minutes and 29 seconds
"""
Models module where all of the models are specified
"""
import copy
import logging
import uuid
from pathlib import Path
......@@ -819,8 +819,8 @@ class Submission(db.Model, EntityBase):
uuid.uuid4()), primary_key=True)
scheduled_for = db.Column(db.TIMESTAMP(timezone=True))
parameters = db.Column(JSONEncodedDict(), nullable=True)
note = db.Column(JSONEncodedDict(), nullable=True)
state = db.Column(db.Enum(SubmissionState, name='SubmissionState'), nullable=False)
note = db.Column(JSONEncodedDict())
source_hash = db.Column(db.String(64))
user_id = db.Column(db.String(36), db.ForeignKey('user.id', ondelete='cascade'),
nullable=False, index=True)
......@@ -869,12 +869,11 @@ class Submission(db.Model, EntityBase):
if self.state not in allow:
return False
self.state = new_state
self.make_change(dict(state=new_state, message=message))
self.make_change(dict(state=new_state.name, message=message))
return True
def make_change(self, change):
if self.note is None:
self.note = {}
self.note = copy.deepcopy(self.note or {})
if 'changes' not in self.note:
self.note['changes'] = []
self.note['changes'].append(change)
......
......@@ -3,11 +3,19 @@ from enum import Enum
import sqlalchemy
import yaml
from sqlalchemy import TypeDecorator
from sqlalchemy import TypeDecorator, String
from sqlalchemy.sql import operators
class JSONEncodedDict(TypeDecorator):
"Represents an immutable structure as a json-encoded string."
"""Represents an immutable structure as a json-encoded string."""
def process_literal_param(self, value, dialect):
return self.process_bind_param(value, dialect)
@property
def python_type(self):
return dict
impl = sqlalchemy.Text
......@@ -21,9 +29,15 @@ class JSONEncodedDict(TypeDecorator):
value = json.loads(value)
return value
def coerce_compared_value(self, op, value):
if op in (operators.like_op, operators.notlike_op):
return String()
else:
return self
class YAMLEncodedDict(TypeDecorator):
"Represents an immutable structure as a yaml-encoded string."
"""Represents an immutable structure as a yaml-encoded string."""
impl = sqlalchemy.Text
......
......@@ -112,6 +112,7 @@ class GeneralService:
log.trace(f"[WRITE] Entity {entity.__class__.__name__}: {entity}")
self.db_session.add(entity)
self.db_session.commit()
return entity
def delete_entity(self, entity):
"""Deletes an entity from the database.
......
......@@ -217,6 +217,7 @@ class SubmissionsService(GeneralService):
def set_state(self, state: Union[str, SubmissionState], message: str = None):
if isinstance(state, str):
state = SubmissionState[state]
if self.submission.change_state(state, message=message):
self.write_entity(self.submission)
log.info(f"[UPDATE] Submission state {self.submission.log_name} "
......
import pytest
@pytest.fixture
def admin_user(app, portal_services):
return portal_services.find.user('admin')
@pytest.fixture
def student_user(app, portal_services):
return portal_services.find.user('student1')
@pytest.fixture
def teacher_user(app, portal_services):
return portal_services.find.user('teacher1')
@pytest.fixture
def course(app, portal_services):
return portal_services.find.course('testcourse1')
@pytest.fixture
def project(app, portal_services, course):
return portal_services.find.project(course, 'hw01')
......@@ -7,21 +7,6 @@ from portal.service.services_collection import ServicesCollection
log = logger.get_logger(__name__)
@pytest.fixture
def admin_user(app, portal_services):
return portal_services.find.user('admin')
@pytest.fixture
def student_user(app, portal_services):
return portal_services.find.user('student1')
@pytest.fixture
def course(app, portal_services):
return portal_services.find.course('testcourse1')
@pytest.fixture()
def perm():
return PermissionsService()
......
import pytest
from portal.database import Submission, SubmissionState
@pytest.fixture
def submission(project, student_user, portal_services) -> Submission:
params = {
"project_params": {'tags': 'docker'},
"file_params": {
"source": {
"type": "git",
"url": "https://gitlab.fi.muni.cz/xkompis/test-hello-world.git",
"branch": "master",
"checkout": "master"
}
}
}
return portal_services.submissions.create(user=student_user, project=project,
submission_params=params)
def test_submission_has_been_created(submission: Submission, project, student_user):
assert submission.id is not None
assert submission.project == project
assert submission.user == student_user
def test_submission_set_state(submission: Submission, portal_services):
message = "Archiving submission"
portal_services.submissions(submission).set_state(SubmissionState.ARCHIVED, message=message)
updated = portal_services.find.submission(submission.id)
assert updated.id == submission.id
assert updated.note is not None
assert updated.changes is not None
assert isinstance(updated.changes, list)
assert updated.changes[0]['state'] == 'ARCHIVED'
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