Skip to content
Snippets Groups Projects
Commit e798f045 authored by Victor Löfgren's avatar Victor Löfgren
Browse files

Order competitions by year, users and regions by name

parent 8d4f4ab1
No related branches found
No related tags found
No related merge requests found
Pipeline #48205 failed
...@@ -36,7 +36,7 @@ class TypesResponseSchema(BaseSchema): ...@@ -36,7 +36,7 @@ class TypesResponseSchema(BaseSchema):
class Types(MethodView): class Types(MethodView):
@blp.response(http_codes.OK, TypesResponseSchema) @blp.response(http_codes.OK, TypesResponseSchema)
def get(self): def get(self):
""" Gets a list of all types """ """Gets a list of all types"""
return dict( return dict(
media_types=dbc.get.all(MediaType), media_types=dbc.get.all(MediaType),
component_types=dbc.get.all(ComponentType), component_types=dbc.get.all(ComponentType),
...@@ -50,7 +50,7 @@ class RoleList(MethodView): ...@@ -50,7 +50,7 @@ class RoleList(MethodView):
@blp.authorization(allowed_roles=ALL) @blp.authorization(allowed_roles=ALL)
@blp.response(http_codes.OK, RoleSchema(many=True)) @blp.response(http_codes.OK, RoleSchema(many=True))
def get(self): def get(self):
""" Gets a list of all roles. """ """Gets a list of all roles."""
return dbc.get.all(Role) return dbc.get.all(Role)
...@@ -66,16 +66,16 @@ class CitiesList(MethodView): ...@@ -66,16 +66,16 @@ class CitiesList(MethodView):
@blp.authorization(allowed_roles=ALL) @blp.authorization(allowed_roles=ALL)
@blp.response(http_codes.OK, CitySchema(many=True)) @blp.response(http_codes.OK, CitySchema(many=True))
def get(self): def get(self):
""" Gets a list of all cities. """ """Gets a list of all cities."""
return dbc.get.all(City) return dbc.get.all(City, order_columns=(City.name,))
@blp.authorization(allowed_roles=["Admin"]) @blp.authorization(allowed_roles=["Admin"])
@blp.arguments(CitySchema) @blp.arguments(CitySchema)
@blp.response(http_codes.OK, CitySchema(many=True)) @blp.response(http_codes.OK, CitySchema(many=True))
def post(self, args): def post(self, args):
""" Posts the specified city. """ """Posts the specified city."""
dbc.add.city(**args) dbc.add.city(**args)
return dbc.get.all(City) return dbc.get.all(City, order_columns=(City.name,))
@blp.route("/cities/<city_id>") @blp.route("/cities/<city_id>")
...@@ -88,9 +88,9 @@ class Cities(MethodView): ...@@ -88,9 +88,9 @@ class Cities(MethodView):
http_codes.CONFLICT, ErrorSchema, description="The city can't be updated with the provided values" http_codes.CONFLICT, ErrorSchema, description="The city can't be updated with the provided values"
) )
def put(self, args, city_id): def put(self, args, city_id):
""" Edits the specified city with the provided arguments. """ """Edits the specified city with the provided arguments."""
dbc.edit.default(dbc.get.one(City, city_id), **args) dbc.edit.default(dbc.get.one(City, city_id), **args)
return dbc.get.all(City) return dbc.get.all(City, order_columns=(City.name,))
@blp.authorization(allowed_roles=["Admin"]) @blp.authorization(allowed_roles=["Admin"])
@blp.response(http_codes.OK, CitySchema(many=True)) @blp.response(http_codes.OK, CitySchema(many=True))
...@@ -99,9 +99,9 @@ class Cities(MethodView): ...@@ -99,9 +99,9 @@ class Cities(MethodView):
http_codes.CONFLICT, ErrorSchema, description="The city can't be updated with the provided values" http_codes.CONFLICT, ErrorSchema, description="The city can't be updated with the provided values"
) )
def delete(self, city_id): def delete(self, city_id):
""" Deletes the specified city. """ """Deletes the specified city."""
dbc.delete.default(dbc.get.one(City, city_id)) dbc.delete.default(dbc.get.one(City, city_id))
return dbc.get.all(City) return dbc.get.all(City, order_columns=(City.name,))
class StatisticsResponseSchema(BaseSchema): class StatisticsResponseSchema(BaseSchema):
...@@ -115,5 +115,5 @@ class Statistics(MethodView): ...@@ -115,5 +115,5 @@ class Statistics(MethodView):
@blp.authorization(allowed_roles=ALL) @blp.authorization(allowed_roles=ALL)
@blp.response(http_codes.OK, StatisticsResponseSchema) @blp.response(http_codes.OK, StatisticsResponseSchema)
def get(self): def get(self):
""" Gets statistics. """ """Gets statistics."""
return {"users": User.query.count(), "competitions": Competition.query.count(), "regions": City.query.count()} return {"users": User.query.count(), "competitions": Competition.query.count(), "regions": City.query.count()}
...@@ -49,7 +49,7 @@ class ExtendedQuery(BaseQuery): ...@@ -49,7 +49,7 @@ class ExtendedQuery(BaseQuery):
return item return item
def paginate_api(self, pagination_parameters, order_column=None, order=1): def paginate_api(self, pagination_parameters, order_columns=None):
""" """
When looking for lists of items this is used to only return a few of When looking for lists of items this is used to only return a few of
them to allow for pagination. them to allow for pagination.
...@@ -57,8 +57,8 @@ class ExtendedQuery(BaseQuery): ...@@ -57,8 +57,8 @@ class ExtendedQuery(BaseQuery):
:type page: int :type page: int
:param page_size: Amount of rows that will be retrieved from the query :param page_size: Amount of rows that will be retrieved from the query
:type page_size: int :type page_size: int
:param order_column: Field of a DbModel in which the query shall order by :param order_columns: Field of a DbModel in which the query shall order by
:type order_column: sqlalchemy.sql.schema.Column :type tuple: Tuple containting sqlalchemy.sql.schema.Column
:param order: If equals 1 then order by ascending otherwise order by descending :param order: If equals 1 then order by ascending otherwise order by descending
:type order: int :type order: int
:return: A page/list of items with offset page*page_size and the total count of all rows ignoring page and page_size :return: A page/list of items with offset page*page_size and the total count of all rows ignoring page and page_size
...@@ -67,8 +67,8 @@ class ExtendedQuery(BaseQuery): ...@@ -67,8 +67,8 @@ class ExtendedQuery(BaseQuery):
pagination_parameters = pagination_parameters or PaginationParameters(page=1, page_size=10) pagination_parameters = pagination_parameters or PaginationParameters(page=1, page_size=10)
if order_column: if order_columns:
self = self.order_by(order_column if order == 1 else order_column.desc()) self = self.order_by(*order_columns)
pagination = self.paginate(page=pagination_parameters.page, per_page=pagination_parameters.page_size) pagination = self.paginate(page=pagination_parameters.page, per_page=pagination_parameters.page_size)
pagination_parameters.item_count = pagination.total pagination_parameters.item_count = pagination.total
......
...@@ -24,21 +24,26 @@ from sqlalchemy.orm import joinedload, subqueryload ...@@ -24,21 +24,26 @@ from sqlalchemy.orm import joinedload, subqueryload
from sqlalchemy.orm.util import with_polymorphic from sqlalchemy.orm.util import with_polymorphic
def all(db_type): def all(db_type, order_columns=None):
""" Gets a list of all lazy db-items in the provided table. """ """Gets a list of all lazy db-items in the provided table."""
return db_type.query.all() query = db_type.query
if order_columns:
query = query.order_by(*order_columns)
return query.all()
def one(db_type, id, required=True): def one(db_type, id, required=True):
""" Get lazy db-item in the table that has the same id. """ """Get lazy db-item in the table that has the same id."""
return db_type.query.filter(db_type.id == id).first_api(required=required) return db_type.query.filter(db_type.id == id).first_api(required=required)
### Codes ### ### Codes ###
def code_by_code(code): def code_by_code(code):
""" Gets the code object associated with the provided code. """ """Gets the code object associated with the provided code."""
return Code.query.filter(Code.code == code.upper()).first_api(True, "A presentation with that code does not exist") return Code.query.filter(Code.code == code.upper()).first_api(True, "A presentation with that code does not exist")
...@@ -56,13 +61,13 @@ def code_list(competition_id): ...@@ -56,13 +61,13 @@ def code_list(competition_id):
### Users ### ### Users ###
def user_exists(email): def user_exists(email):
""" Checks if an user has that email. """ """Checks if an user has that email."""
return dbc.utils.count(User, {"email": email}) > 0 return dbc.utils.count(User, {"email": email}) > 0
def user_by_email(email): def user_by_email(email):
""" Gets the user object associated with the provided email. """ """Gets the user object associated with the provided email."""
return User.query.filter(User.email == email).first_api() return User.query.filter(User.email == email).first_api()
...@@ -91,7 +96,7 @@ def slide_list(competition_id): ...@@ -91,7 +96,7 @@ def slide_list(competition_id):
### Teams ### ### Teams ###
def team(competition_id, team_id): def team(competition_id, team_id):
""" Gets the team object associated with the competition and team. """ """Gets the team object associated with the competition and team."""
join_competition = Competition.id == Team.competition_id join_competition = Competition.id == Team.competition_id
filters = (Competition.id == competition_id) & (Team.id == team_id) filters = (Competition.id == competition_id) & (Team.id == team_id)
...@@ -297,7 +302,7 @@ def component_list(competition_id, slide_id): ...@@ -297,7 +302,7 @@ def component_list(competition_id, slide_id):
### Competitions ### ### Competitions ###
def competition(competition_id, required=True): def competition(competition_id, required=True):
""" Get Competition and all it's sub-entities. """ """Get Competition and all it's sub-entities."""
join_component = joinedload(Competition.slides).subqueryload(Slide.components) join_component = joinedload(Competition.slides).subqueryload(Slide.components)
join_alternatives = joinedload(Competition.slides).joinedload(Slide.questions).joinedload(Question.alternatives) join_alternatives = joinedload(Competition.slides).joinedload(Slide.questions).joinedload(Question.alternatives)
......
...@@ -6,7 +6,7 @@ from app.database.models import Competition, Media, Question, Slide, User ...@@ -6,7 +6,7 @@ from app.database.models import Competition, Media, Question, Slide, User
def image(pagination_parameters=None, filename=None, order=1, order_by=None): def image(pagination_parameters=None, filename=None, order=1, order_by=None):
""" Finds and returns an image from the file name. """ """Finds and returns an image from the file name."""
query = Media.query.filter(Media.type_id == 1) query = Media.query.filter(Media.type_id == 1)
if filename: if filename:
...@@ -22,7 +22,7 @@ def user( ...@@ -22,7 +22,7 @@ def user(
city_id=None, city_id=None,
role_id=None, role_id=None,
): ):
""" Finds and returns any number of users from the provided parameters. """ """Finds and returns any number of users from the provided parameters."""
query = User.query query = User.query
if name: if name:
...@@ -34,7 +34,7 @@ def user( ...@@ -34,7 +34,7 @@ def user(
if role_id: if role_id:
query = query.filter(User.role_id == role_id) query = query.filter(User.role_id == role_id)
return query.paginate_api(pagination_parameters) return query.paginate_api(pagination_parameters, order_columns=(User.name,))
def competition( def competition(
...@@ -43,7 +43,7 @@ def competition( ...@@ -43,7 +43,7 @@ def competition(
year=None, year=None,
city_id=None, city_id=None,
): ):
""" Finds and returns a competition from the provided parameters. """ """Finds and returns a competition from the provided parameters."""
query = Competition.query query = Competition.query
if name: if name:
...@@ -53,7 +53,7 @@ def competition( ...@@ -53,7 +53,7 @@ def competition(
if city_id: if city_id:
query = query.filter(Competition.city_id == city_id) query = query.filter(Competition.city_id == city_id)
return query.paginate_api(pagination_parameters) return query.paginate_api(pagination_parameters, order_columns=(Competition.year.desc(), Competition.name))
def slide( def slide(
...@@ -62,9 +62,8 @@ def slide( ...@@ -62,9 +62,8 @@ def slide(
title=None, title=None,
body=None, body=None,
competition_id=None, competition_id=None,
order_by=None,
): ):
""" Finds and returns a slide from the provided parameters. """ """Finds and returns a slide from the provided parameters."""
query = Slide.query query = Slide.query
if slide_order: if slide_order:
...@@ -90,7 +89,7 @@ def questions( ...@@ -90,7 +89,7 @@ def questions(
order=1, order=1,
order_by=None, order_by=None,
): ):
""" Finds and returns a question from the provided parameters. """ """Finds and returns a question from the provided parameters."""
query = Question.query query = Question.query
if name: if name:
......
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