revamp-file-discover #33
|
@ -228,72 +228,28 @@ class BlogPost(Searchable):
|
|||
|
||||
files = [f for f in os.listdir(data_dir) if (
|
||||
data_dir.joinpath(f)).is_file()]
|
||||
logger.debug(f"discovered files: {files}")
|
||||
|
||||
# finding lang and title
|
||||
regex = r"^(en|de)-(.*)\.md"
|
||||
# TODO: only find toml files
|
||||
# find the meta file
|
||||
regex = r"^(.*)\.toml"
|
||||
|
||||
# filepath, language codes, slug
|
||||
files = [[f, cls.DEFAULT_LANGS, ""] for f in files]
|
||||
# filepath, slug
|
||||
files = [[f, ""] for f in files]
|
||||
for file in files:
|
||||
# parse file name
|
||||
try:
|
||||
matches = re.match(regex, file[0])
|
||||
if matches is None:
|
||||
logger.warning(
|
||||
f"Data file '{file[0]}' does not fit to the filename\
|
||||
regex")
|
||||
files.remove(file)
|
||||
else:
|
||||
current_lang = matches.group(1)
|
||||
file[1][current_lang] = True
|
||||
file[2] = matches.group(2)
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
files.remove(file)
|
||||
file[1] = matches.group(1)
|
||||
|
||||
# PERF:
|
||||
# Optimize for single loop, should be doable and better design
|
||||
|
||||
# collapse diffrent versions
|
||||
for file in files:
|
||||
try:
|
||||
if [_f[2] for _f in files].count(file[2]) >= 2:
|
||||
logger.debug(f"multiple versions of '{file[2]}'")
|
||||
versions = [_f for _f in files if _f[2] == file[2]]
|
||||
lang: dict[str, bool] = file[1]
|
||||
for version in versions:
|
||||
for key in version[1]:
|
||||
lang[key] |= version[1][key]
|
||||
else:
|
||||
# only a single version of this file
|
||||
continue
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"Could not combine BlogPosts for '{file[0]}': {e}")
|
||||
|
||||
# TODO: not needed when relying on toml files
|
||||
try:
|
||||
# deduplicate
|
||||
_files = []
|
||||
for f in [[_f[1], _f[2]]
|
||||
for _f in files]: # dont care about fname
|
||||
if f not in _files:
|
||||
_files.append(f)
|
||||
files = _files
|
||||
logger.debug(f"to save: {files}")
|
||||
except Exception as e:
|
||||
logger.error(f"Could not dedup BlogPosts: {e}")
|
||||
|
||||
for file in files:
|
||||
try:
|
||||
obj = BlogPost(langs=file[0], slug=file[1])
|
||||
obj = BlogPost(slug=file[1])
|
||||
obj.sync_file()
|
||||
obj.regenerate()
|
||||
obj.save()
|
||||
except Exception as e:
|
||||
logger.error(f"Could not create BlogPost for '{file[1]}': {e}")
|
||||
files.remove(file)
|
||||
|
||||
class Meta:
|
||||
verbose_name = _("blog post")
|
||||
|
|
|
@ -59,7 +59,7 @@ class Searchable(models.Model):
|
|||
obj.regenerate()
|
||||
|
||||
def __str__(self):
|
||||
return f"{{<{self.__class__.__name__}>\"{self.slug}\"}}"
|
||||
return f"{{<{self.__class__.__name__}>\"{self.title_en}\"}}"
|
||||
|
||||
def regenerate(self):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue