Skip to content
Snippets Groups Projects
Commit e1211e0a authored by robban64's avatar robban64
Browse files

fix: return media object instead of media_id

parent 9ec6c20c
No related branches found
No related tags found
No related merge requests found
Pipeline #42682 passed with warnings
Showing
with 42 additions and 38 deletions
...@@ -26,7 +26,7 @@ it('dispatches no actions when failing to get competitions', async () => { ...@@ -26,7 +26,7 @@ it('dispatches no actions when failing to get competitions', async () => {
}) })
it('dispatches correct actions when setting slide', () => { it('dispatches correct actions when setting slide', () => {
const testSlide: Slide = { competition_id: 0, id: 5, order: 5, timer: 20, title: '' } const testSlide: Slide = { competition_id: 0, id: 5, order: 5, timer: 20, title: '', background_image_id: 0 }
const expectedActions = [{ type: Types.SET_PRESENTATION_SLIDE, payload: testSlide }] const expectedActions = [{ type: Types.SET_PRESENTATION_SLIDE, payload: testSlide }]
const store = mockStore({}) const store = mockStore({})
setCurrentSlide(testSlide)(store.dispatch) setCurrentSlide(testSlide)(store.dispatch)
......
...@@ -38,13 +38,14 @@ export interface Slide { ...@@ -38,13 +38,14 @@ export interface Slide {
order: number order: number
timer: number timer: number
title: string title: string
background_image?: Media
} }
export interface Competition extends NameID { export interface Competition extends NameID {
font: string font: string
city_id: number city_id: number
year: number year: number
background_image_id: number background_image?: Media
} }
export interface Team extends NameID { export interface Team extends NameID {
...@@ -69,7 +70,7 @@ export interface QuestionAnswer { ...@@ -69,7 +70,7 @@ export interface QuestionAnswer {
id: number id: number
question_id: number question_id: number
team_id: number team_id: number
data: string answer: string
score: number score: number
} }
...@@ -83,8 +84,7 @@ export interface Component { ...@@ -83,8 +84,7 @@ export interface Component {
} }
export interface ImageComponent extends Component { export interface ImageComponent extends Component {
media_id: number media: Media
filename: string
} }
export interface TextComponent extends Component { export interface TextComponent extends Component {
......
...@@ -7,6 +7,7 @@ export interface RichCompetition { ...@@ -7,6 +7,7 @@ export interface RichCompetition {
city_id: number city_id: number
slides: RichSlide[] slides: RichSlide[]
teams: RichTeam[] teams: RichTeam[]
background_image?: Media
} }
export interface RichSlide { export interface RichSlide {
...@@ -15,9 +16,9 @@ export interface RichSlide { ...@@ -15,9 +16,9 @@ export interface RichSlide {
timer: number timer: number
title: string title: string
competition_id: number competition_id: number
background_image?: Media
components: Component[] components: Component[]
questions: RichQuestion[] questions: RichQuestion[]
medias: Media[]
} }
export interface RichTeam { export interface RichTeam {
......
...@@ -5,7 +5,15 @@ import ImageComponentDisplay from './ImageComponentDisplay' ...@@ -5,7 +5,15 @@ import ImageComponentDisplay from './ImageComponentDisplay'
it('renders competition settings', () => { it('renders competition settings', () => {
render( render(
<ImageComponentDisplay <ImageComponentDisplay
component={{ id: 0, x: 0, y: 0, w: 0, h: 0, data: { media_id: 0, filename: '' }, type_id: 2 }} component={{
id: 0,
x: 0,
y: 0,
w: 0,
h: 0,
media: { id: 0, mediatype_id: 0, user_id: 0, filename: '' },
type_id: 2,
}}
width={0} width={0}
height={0} height={0}
/> />
......
...@@ -10,7 +10,7 @@ type ImageComponentProps = { ...@@ -10,7 +10,7 @@ type ImageComponentProps = {
const ImageComponentDisplay = ({ component, width, height }: ImageComponentProps) => { const ImageComponentDisplay = ({ component, width, height }: ImageComponentProps) => {
return ( return (
<img <img
src={`http://localhost:5000/static/images/${component.filename}`} src={`http://localhost:5000/static/images/${component.media?.filename}`}
height={height} height={height}
width={width} width={width}
draggable={false} draggable={false}
......
...@@ -65,7 +65,7 @@ const Images = ({ activeSlide, competitionId }: ImagesProps) => { ...@@ -65,7 +65,7 @@ const Images = ({ activeSlide, competitionId }: ImagesProps) => {
const handleCloseimageClick = async (image: ImageComponent) => { const handleCloseimageClick = async (image: ImageComponent) => {
// Removes selected image component and deletes its file from the server. // Removes selected image component and deletes its file from the server.
await axios await axios
.delete(`/api/media/images/${image.media_id}`) .delete(`/api/media/images/${image.media?.id}`)
.then(() => { .then(() => {
dispatch(getEditorCompetition(competitionId)) dispatch(getEditorCompetition(competitionId))
}) })
...@@ -98,9 +98,9 @@ const Images = ({ activeSlide, competitionId }: ImagesProps) => { ...@@ -98,9 +98,9 @@ const Images = ({ activeSlide, competitionId }: ImagesProps) => {
images.map((image) => ( images.map((image) => (
<div key={image.id}> <div key={image.id}>
<ListItem divider button> <ListItem divider button>
<ImportedImage src={`http://localhost:5000/static/images/thumbnail_${image.filename}`} /> <ImportedImage src={`http://localhost:5000/static/images/thumbnail_${image.media?.filename}`} />
<Center> <Center>
<ListItemText primary={image.filename} /> <ListItemText primary={image.media?.filename} />
</Center> </Center>
<CloseIcon onClick={() => handleCloseimageClick(image)} /> <CloseIcon onClick={() => handleCloseimageClick(image)} />
</ListItem> </ListItem>
......
...@@ -16,6 +16,7 @@ const initialState: EditorState = { ...@@ -16,6 +16,7 @@ const initialState: EditorState = {
city_id: 1, city_id: 1,
slides: [], slides: [],
teams: [], teams: [],
background_image: undefined,
}, },
activeSlideId: -1, activeSlideId: -1,
loading: true, loading: true,
......
...@@ -19,6 +19,7 @@ const initialState: PresentationState = { ...@@ -19,6 +19,7 @@ const initialState: PresentationState = {
slides: [], slides: [],
year: 0, year: 0,
teams: [], teams: [],
background_image: undefined,
}, },
slide: { slide: {
competition_id: 0, competition_id: 0,
...@@ -26,6 +27,7 @@ const initialState: PresentationState = { ...@@ -26,6 +27,7 @@ const initialState: PresentationState = {
order: 0, order: 0,
timer: 0, timer: 0,
title: '', title: '',
background_image: undefined,
}, },
code: '', code: '',
timer: { timer: {
......
...@@ -10,12 +10,12 @@ schema = QuestionAnswerDTO.schema ...@@ -10,12 +10,12 @@ schema = QuestionAnswerDTO.schema
list_schema = QuestionAnswerDTO.list_schema list_schema = QuestionAnswerDTO.list_schema
question_answer_parser = reqparse.RequestParser() question_answer_parser = reqparse.RequestParser()
question_answer_parser.add_argument("data", type=dict, required=True, location="json") question_answer_parser.add_argument("answer", type=str, required=True, location="json")
question_answer_parser.add_argument("score", type=int, required=True, location="json") question_answer_parser.add_argument("score", type=int, required=True, location="json")
question_answer_parser.add_argument("question_id", type=int, required=True, location="json") question_answer_parser.add_argument("question_id", type=int, required=True, location="json")
question_answer_edit_parser = reqparse.RequestParser() question_answer_edit_parser = reqparse.RequestParser()
question_answer_edit_parser.add_argument("data", type=dict, default=None, location="json") question_answer_edit_parser.add_argument("answer", type=str, default=None, location="json")
question_answer_edit_parser.add_argument("score", type=int, default=None, location="json") question_answer_edit_parser.add_argument("score", type=int, default=None, location="json")
......
...@@ -18,6 +18,9 @@ competition_parser.add_argument("name", type=str, location="json") ...@@ -18,6 +18,9 @@ competition_parser.add_argument("name", type=str, location="json")
competition_parser.add_argument("year", type=int, location="json") competition_parser.add_argument("year", type=int, location="json")
competition_parser.add_argument("city_id", type=int, location="json") competition_parser.add_argument("city_id", type=int, location="json")
competition_edit_parser = competition_parser.copy()
competition_edit_parser.add_argument("background_image_id", default=None, type=int, location="json")
competition_search_parser = search_parser.copy() competition_search_parser = search_parser.copy()
competition_search_parser.add_argument("name", type=str, default=None, location="args") competition_search_parser.add_argument("name", type=str, default=None, location="args")
competition_search_parser.add_argument("year", type=int, default=None, location="args") competition_search_parser.add_argument("year", type=int, default=None, location="args")
...@@ -48,7 +51,7 @@ class Competitions(Resource): ...@@ -48,7 +51,7 @@ class Competitions(Resource):
@check_jwt(editor=True) @check_jwt(editor=True)
def put(self, competition_id): def put(self, competition_id):
args = competition_parser.parse_args(strict=True) args = competition_edit_parser.parse_args(strict=True)
item = dbc.get.one(Competition, competition_id) item = dbc.get.one(Competition, competition_id)
item = dbc.edit.default(item, **args) item = dbc.edit.default(item, **args)
......
...@@ -13,6 +13,7 @@ slide_parser = reqparse.RequestParser() ...@@ -13,6 +13,7 @@ slide_parser = reqparse.RequestParser()
slide_parser.add_argument("order", type=int, default=None, location="json") slide_parser.add_argument("order", type=int, default=None, location="json")
slide_parser.add_argument("title", type=str, default=None, location="json") slide_parser.add_argument("title", type=str, default=None, location="json")
slide_parser.add_argument("timer", type=int, default=None, location="json") slide_parser.add_argument("timer", type=int, default=None, location="json")
slide_parser.add_argument("background_image_id", default=None, type=int, location="json")
@api.route("") @api.route("")
......
...@@ -42,8 +42,7 @@ class SlideSchemaRich(RichSchema): ...@@ -42,8 +42,7 @@ class SlideSchemaRich(RichSchema):
title = ma.auto_field() title = ma.auto_field()
timer = ma.auto_field() timer = ma.auto_field()
competition_id = ma.auto_field() competition_id = ma.auto_field()
background_image_id = ma.auto_field() background_image = fields.Nested(schemas.MediaSchema, many=False)
background_image = ma.Function(lambda x: x.background_image.filename if x.background_image is not None else "")
questions = fields.Nested(QuestionSchemaRich, many=True) questions = fields.Nested(QuestionSchemaRich, many=True)
components = fields.Nested(schemas.ComponentSchema, many=True) components = fields.Nested(schemas.ComponentSchema, many=True)
...@@ -56,8 +55,7 @@ class CompetitionSchemaRich(RichSchema): ...@@ -56,8 +55,7 @@ class CompetitionSchemaRich(RichSchema):
name = ma.auto_field() name = ma.auto_field()
year = ma.auto_field() year = ma.auto_field()
city_id = ma.auto_field() city_id = ma.auto_field()
background_image_id = ma.auto_field() background_image = fields.Nested(schemas.MediaSchema, many=False)
background_image = ma.Function(lambda x: x.background_image.filename if x.background_image is not None else "")
slides = fields.Nested( slides = fields.Nested(
SlideSchemaRich, SlideSchemaRich,
......
...@@ -65,7 +65,7 @@ class QuestionAnswerSchema(BaseSchema): ...@@ -65,7 +65,7 @@ class QuestionAnswerSchema(BaseSchema):
model = models.QuestionAnswer model = models.QuestionAnswer
id = ma.auto_field() id = ma.auto_field()
data = ma.Function(lambda obj: obj.data) answer = ma.auto_field()
score = ma.auto_field() score = ma.auto_field()
question_id = ma.auto_field() question_id = ma.auto_field()
team_id = ma.auto_field() team_id = ma.auto_field()
...@@ -116,8 +116,7 @@ class SlideSchema(BaseSchema): ...@@ -116,8 +116,7 @@ class SlideSchema(BaseSchema):
title = ma.auto_field() title = ma.auto_field()
timer = ma.auto_field() timer = ma.auto_field()
competition_id = ma.auto_field() competition_id = ma.auto_field()
background_image_id = ma.auto_field() background_image = fields.Nested(MediaSchema, many=False)
background_image = ma.Function(lambda x: x.background_image.filename if x.background_image is not None else "")
class TeamSchema(BaseSchema): class TeamSchema(BaseSchema):
...@@ -148,20 +147,13 @@ class CompetitionSchema(BaseSchema): ...@@ -148,20 +147,13 @@ class CompetitionSchema(BaseSchema):
name = ma.auto_field() name = ma.auto_field()
year = ma.auto_field() year = ma.auto_field()
city_id = ma.auto_field() city_id = ma.auto_field()
background_image_id = ma.auto_field() background_image = fields.Nested(MediaSchema, many=False)
background_image = ma.Function(lambda x: x.background_image.filename if x.background_image is not None else "")
class ComponentSchema(BaseSchema): class ComponentSchema(BaseSchema):
class Meta(BaseSchema.Meta): class Meta(BaseSchema.Meta):
model = models.Component model = models.Component
@post_dump
def handle_filename(self, data, *args, **kwargs):
if data["filename"] == "":
del data["filename"]
return data
id = ma.auto_field() id = ma.auto_field()
x = ma.auto_field() x = ma.auto_field()
y = ma.auto_field() y = ma.auto_field()
...@@ -171,5 +163,4 @@ class ComponentSchema(BaseSchema): ...@@ -171,5 +163,4 @@ class ComponentSchema(BaseSchema):
type_id = ma.auto_field() type_id = ma.auto_field()
text = fields.fields.String() text = fields.fields.String()
media_id = fields.fields.Integer() media = fields.Nested(MediaSchema, many=False)
filename = ma.Function(lambda x: x.media.filename if hasattr(x, "media_id") else "")
...@@ -246,5 +246,5 @@ def question_alternative(text, value, question_id): ...@@ -246,5 +246,5 @@ def question_alternative(text, value, question_id):
return db_add(QuestionAlternative(text, value, question_id)) return db_add(QuestionAlternative(text, value, question_id))
def question_answer(data, score, question_id, team_id): def question_answer(answer, score, question_id, team_id):
return db_add(QuestionAnswer(data, score, question_id, team_id)) return db_add(QuestionAnswer(answer, score, question_id, team_id))
...@@ -77,8 +77,7 @@ def slide_to_competition(item_slide_old, item_competition): ...@@ -77,8 +77,7 @@ def slide_to_competition(item_slide_old, item_competition):
item_slide_new.body = item_slide_old.body item_slide_new.body = item_slide_old.body
item_slide_new.timer = item_slide_old.timer item_slide_new.timer = item_slide_old.timer
item_slide_new.settings = item_slide_old.settings item_slide_new.settings = item_slide_old.settings
item_slide_new.background_image_id = item_slide_old.background_image_id
# TODO: Add background image
for item_component in item_slide_old.components: for item_component in item_slide_old.components:
_component(item_component, item_slide_new) _component(item_component, item_slide_new)
...@@ -109,6 +108,7 @@ def competition(item_competition_old): ...@@ -109,6 +108,7 @@ def competition(item_competition_old):
item_competition_old.font, item_competition_old.font,
) )
# TODO: Add background image # TODO: Add background image
item_competition_new.background_image_id = item_competition_old.background_image_id
for item_slide in item_competition_old.slides: for item_slide in item_competition_old.slides:
slide_to_competition(item_slide, item_competition_new) slide_to_competition(item_slide, item_competition_new)
......
from app.core import bcrypt, db from app.core import bcrypt, db
from app.database import Dictionary
from sqlalchemy.ext.hybrid import hybrid_method, hybrid_property from sqlalchemy.ext.hybrid import hybrid_method, hybrid_property
from app.database.types import ID_IMAGE_COMPONENT, ID_TEXT_COMPONENT from app.database.types import ID_IMAGE_COMPONENT, ID_TEXT_COMPONENT
...@@ -175,7 +174,7 @@ class QuestionAlternative(db.Model): ...@@ -175,7 +174,7 @@ class QuestionAlternative(db.Model):
class QuestionAnswer(db.Model): class QuestionAnswer(db.Model):
__table_args__ = (db.UniqueConstraint("question_id", "team_id"),) __table_args__ = (db.UniqueConstraint("question_id", "team_id"),)
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
data = db.Column(Dictionary(), nullable=False) answer = db.Column(db.String(STRING_SIZE), nullable=False)
score = db.Column(db.Integer, nullable=False, default=0) score = db.Column(db.Integer, nullable=False, default=0)
question_id = db.Column(db.Integer, db.ForeignKey("question.id"), nullable=False) question_id = db.Column(db.Integer, db.ForeignKey("question.id"), nullable=False)
......
...@@ -20,7 +20,7 @@ class Config: ...@@ -20,7 +20,7 @@ class Config:
class DevelopmentConfig(Config): class DevelopmentConfig(Config):
DEBUG = False DEBUG = True
SQLALCHEMY_ECHO = False SQLALCHEMY_ECHO = False
# HOST = "localhost" # HOST = "localhost"
# PORT = 5432 # PORT = 5432
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment