Skip to content

Commit

Permalink
Fixing feed creation
Browse files Browse the repository at this point in the history
  • Loading branch information
jaesivsm committed Jun 20, 2024
1 parent 7922761 commit 2608d3b
Showing 1 changed file with 49 additions and 30 deletions.
79 changes: 49 additions & 30 deletions jarr/api/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@
from urllib.parse import SplitResult, urlsplit, urlunsplit

from flask_restx import fields

from jarr.bootstrap import conf

logger = logging.getLogger(__name__)

MODEL_PARSER_MAPPING = {bool: fields.Boolean, float: fields.Float,
str: fields.String, int: fields.Integer}
MODEL_PARSER_MAPPING = {
bool: fields.Boolean,
float: fields.Float,
str: fields.String,
int: fields.Integer,
}

clustering_options = {
"cluster_enabled": "will allow article in your feeds and categories to"
" to be clusterized",
"cluster_tfidf_enabled": "will allow article in your feeds and categor"
"ies to be clusterized through document compa"
"rison",
"cluster_same_category": "will allow article in your feeds and categor"
"ies to be clusterized while beloning to the "
"same category",
"cluster_same_feed": "will allow article in your feeds and categories "
"to be clusterized while beloning to the same "
"feed",
"cluster_wake_up": "will unread cluster when article from that feed "
"are added to it",
"cluster_enabled": "will allow article in your feeds and categories to"
" to be clusterized",
"cluster_tfidf_enabled": "will allow article in your feeds and categor"
"ies to be clusterized through document comparison",
"cluster_same_category": "will allow article in your feeds and categor"
"ies to be clusterized while beloning to the same category",
"cluster_same_feed": "will allow article in your feeds and categories "
"to be clusterized while beloning to the same feed",
"cluster_wake_up": "will unread cluster when article from that feed "
"are added to it",
}


Expand All @@ -36,8 +36,14 @@ def set_clustering_options(level, model, parser, nullable=True):
elif level == "feed":
suffix = " (article's category and user clustering settings allows it)"
for option, description in clustering_options.items():
set_model_n_parser(model, parser, option, bool, nullable=nullable,
description=description + suffix)
set_model_n_parser(
model,
parser,
option,
bool,
nullable=nullable,
description=description + suffix,
)


class EnumField(fields.String):
Expand All @@ -46,30 +52,43 @@ def __init__(self, enum, **kwargs):
super().__init__(enum=[e.value for e in enum], **kwargs)

def format(self, value):
return super().format(getattr(value, 'value', value))
return super().format(getattr(value, "value", value))


def set_model_n_parser(model, parser, name, type_, **kwargs):
if isinstance(type_, Enum.__class__):
model[name] = EnumField(type_, **kwargs)
kwargs['choices'] = list(type_)
enum = type_
model[name] = EnumField(enum, **kwargs)
kwargs["choices"] = list(enum)

def caster(*args):
return enum(args[0])

else:
model[name] = MODEL_PARSER_MAPPING[type_](**kwargs)
desc = kwargs.pop('description', None)
parser.add_argument(name, type=type_, store_missing=False,
help=desc, **kwargs)
caster = type_
desc = kwargs.pop("description", None)
parser.add_argument(
name, type=caster, store_missing=False, help=desc, **kwargs
)


def parse_meaningful_params(parser):
nullable_keys = {arg.name for arg in parser.args if arg.nullable}
return {key: value for key, value in parser.parse_args().items()
if value is not None or key in nullable_keys}
return {
key: value
for key, value in parser.parse_args().items()
if value is not None or key in nullable_keys
}


def get_ui_url(path_extention):
split = urlsplit(conf.app.url)
split = SplitResult(scheme=split.scheme,
netloc=split.netloc,
path=split.path + path_extention,
query=split.query, fragment=split.fragment)
split = SplitResult(
scheme=split.scheme,
netloc=split.netloc,
path=split.path + path_extention,
query=split.query,
fragment=split.fragment,
)
return urlunsplit(split)

0 comments on commit 2608d3b

Please sign in to comment.