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 Models module where all of the models are specified
""" """
import copy
import logging import logging
import uuid import uuid
from pathlib import Path from pathlib import Path
...@@ -819,8 +819,8 @@ class Submission(db.Model, EntityBase): ...@@ -819,8 +819,8 @@ class Submission(db.Model, EntityBase):
uuid.uuid4()), primary_key=True) uuid.uuid4()), primary_key=True)
scheduled_for = db.Column(db.TIMESTAMP(timezone=True)) scheduled_for = db.Column(db.TIMESTAMP(timezone=True))
parameters = db.Column(JSONEncodedDict(), nullable=True) parameters = db.Column(JSONEncodedDict(), nullable=True)
note = db.Column(JSONEncodedDict(), nullable=True)
state = db.Column(db.Enum(SubmissionState, name='SubmissionState'), nullable=False) state = db.Column(db.Enum(SubmissionState, name='SubmissionState'), nullable=False)
note = db.Column(JSONEncodedDict())
source_hash = db.Column(db.String(64)) source_hash = db.Column(db.String(64))
user_id = db.Column(db.String(36), db.ForeignKey('user.id', ondelete='cascade'), user_id = db.Column(db.String(36), db.ForeignKey('user.id', ondelete='cascade'),
nullable=False, index=True) nullable=False, index=True)
...@@ -869,12 +869,11 @@ class Submission(db.Model, EntityBase): ...@@ -869,12 +869,11 @@ class Submission(db.Model, EntityBase):
if self.state not in allow: if self.state not in allow:
return False return False
self.state = new_state 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 return True
def make_change(self, change): def make_change(self, change):
if self.note is None: self.note = copy.deepcopy(self.note or {})
self.note = {}
if 'changes' not in self.note: if 'changes' not in self.note:
self.note['changes'] = [] self.note['changes'] = []
self.note['changes'].append(change) self.note['changes'].append(change)
......
...@@ -3,11 +3,19 @@ from enum import Enum ...@@ -3,11 +3,19 @@ from enum import Enum
import sqlalchemy import sqlalchemy
import yaml import yaml
from sqlalchemy import TypeDecorator from sqlalchemy import TypeDecorator, String
from sqlalchemy.sql import operators
class JSONEncodedDict(TypeDecorator): 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 impl = sqlalchemy.Text
...@@ -21,9 +29,15 @@ class JSONEncodedDict(TypeDecorator): ...@@ -21,9 +29,15 @@ class JSONEncodedDict(TypeDecorator):
value = json.loads(value) value = json.loads(value)
return 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): class YAMLEncodedDict(TypeDecorator):
"Represents an immutable structure as a yaml-encoded string." """Represents an immutable structure as a yaml-encoded string."""
impl = sqlalchemy.Text impl = sqlalchemy.Text
......
...@@ -112,6 +112,7 @@ class GeneralService: ...@@ -112,6 +112,7 @@ class GeneralService:
log.trace(f"[WRITE] Entity {entity.__class__.__name__}: {entity}") log.trace(f"[WRITE] Entity {entity.__class__.__name__}: {entity}")
self.db_session.add(entity) self.db_session.add(entity)
self.db_session.commit() self.db_session.commit()
return entity
def delete_entity(self, entity): def delete_entity(self, entity):
"""Deletes an entity from the database. """Deletes an entity from the database.
......
...@@ -217,6 +217,7 @@ class SubmissionsService(GeneralService): ...@@ -217,6 +217,7 @@ class SubmissionsService(GeneralService):
def set_state(self, state: Union[str, SubmissionState], message: str = None): def set_state(self, state: Union[str, SubmissionState], message: str = None):
if isinstance(state, str): if isinstance(state, str):
state = SubmissionState[state] state = SubmissionState[state]
if self.submission.change_state(state, message=message): if self.submission.change_state(state, message=message):
self.write_entity(self.submission) self.write_entity(self.submission)
log.info(f"[UPDATE] Submission state {self.submission.log_name} " 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 ...@@ -7,21 +7,6 @@ from portal.service.services_collection import ServicesCollection
log = logger.get_logger(__name__) 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() @pytest.fixture()
def perm(): def perm():
return PermissionsService() 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'
Supports Markdown
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