web: add motd parameter to /api/squads/now/by-tag/extended/{tag} endpoint

This commit is contained in:
norohind 2021-12-16 01:05:55 +03:00
parent f0997e9f3b
commit e574375e23
Signed by: norohind
GPG Key ID: 01C3BECC26FB59E1
3 changed files with 58 additions and 5 deletions

View File

@ -1,6 +1,8 @@
import sqlite3
from . import sqlite_sql_requests
import json
from typing import Union
from datetime import datetime
class SqliteModel:
@ -42,10 +44,11 @@ class SqliteModel:
return squads
def list_squads_by_tag_with_tags(self, tag: str, pretty_keys=False) -> list:
def list_squads_by_tag_with_tags(self, tag: str, pretty_keys=False, motd=False) -> list:
"""
Take tag and return all squads with tag matches with user_tags
:param motd: if we should return motd with information
:param pretty_keys:
:param tag:
:return:
@ -54,14 +57,48 @@ class SqliteModel:
if pretty_keys:
sql_req = sqlite_sql_requests.squads_by_tag_extended_pretty_keys
user_tags_key = 'User tags'
motd_key = 'Motd'
motd_date_key = 'Motd Date'
motd_author_key = 'Motd Author'
else:
sql_req = sqlite_sql_requests.squads_by_tag_extended_raw_keys
user_tags_key = 'user_tags'
motd_key = 'motd'
motd_date_key = 'motd_date'
motd_author_key = 'motd_author'
squads = self.db.execute(sql_req, {'tag': tag.upper()}).fetchall()
for squad in squads:
squad[user_tags_key] = json.loads(squad[user_tags_key])
if motd:
motd_dict: dict = self.motd_by_squad_id(squad['squad_id'])
if motd_dict is None:
# if no motd, then all motd related values will be None
motd_dict = dict()
squad[motd_date_key] = None
else:
squad[motd_date_key] = datetime.utcfromtimestamp(int(motd_dict.get('date'))).strftime('%Y-%m-%d '
'%H:%M:%S')
squad[motd_key] = motd_dict.get('motd')
squad[motd_author_key] = motd_dict.get('author')
return squads
def motd_by_squad_id(self, squad_id: int) -> Union[dict, None]:
"""
Take squad_id and returns dict with last motd: motd, date, author keys. It also can return None if motd isn't
set for squad
:param squad_id:
:return:
"""
sql_req = self.db.execute(sqlite_sql_requests.select_latest_motd_by_id, {'squad_id': squad_id})
return sql_req.fetchone()

View File

@ -64,4 +64,16 @@ squads_by_tag_extended_raw_keys = """select
squad_id
from squads_view
where tag = :tag;
"""
"""
select_latest_motd_by_id = """select
motd,
date,
author
from news
where
squad_id = :squad_id and
type_of_news = 'public_statements' and
category = 'Squadrons_History_Category_PublicStatement'
order by date desc
limit 1;"""

View File

@ -24,8 +24,9 @@ class SquadsInfoByTag:
def on_get(self, req: falcon.request.Request, resp: falcon.response.Response, tag: str, details_type: str) -> None:
"""
Params to request:
resolve_tags: bool
pretty_keys: bool
resolve_tags: bool - if we will resolve tags or put it just as tags ids
pretty_keys: bool - if we will return list of dicts with human friendly keys or raw column names from DB
motd: bool - if we will also return motd of squad, works only with `extended`
:param details_type: short or extended, extended includes tags
:param req:
@ -37,11 +38,14 @@ class SquadsInfoByTag:
resp.content_type = falcon.MEDIA_JSON
details_type = details_type.lower()
motd: bool = req.params.get('motd', 'false').lower() == 'true'
if details_type == 'short':
model_method = model.list_squads_by_tag
elif details_type == 'extended':
model_method = model.list_squads_by_tag_with_tags
def model_method(*args, **kwargs):
return model.list_squads_by_tag_with_tags(*args, **kwargs, motd=motd)
else:
raise falcon.HTTPBadRequest(description=f'details_type must be one of short, extended')