Преглед на файлове

finalising changes for new approach to practices

practices
Simon Bowie преди 2 дни
родител
ревизия
fdf4daa3e8
променени са 2 файла, в които са добавени 28 реда и са изтрити 0 реда
  1. +7
    -0
      web/app/practice.py
  2. +21
    -0
      web/app/resources.py

+ 7
- 0
web/app/practice.py Целия файл

@@ -48,6 +48,13 @@ def get_practices():
# finalise the query and add pagination
practices = practices_query.order_by(Resource.name).paginate(page=page, per_page=25)

# fill the description field with the first paragraph of the associated practice Markdown file
for practice in practices:
if not practice.description:
practice_markdown = get_practice_markdown(practice.name, 'markdown')
description = extract_first_paragraph(practice_markdown)
practice.description = description

# POST-FILTERING PROCESSING
# if view is 'expanded' then append relationships
if view != 'list':

+ 21
- 0
web/app/resources.py Целия файл

@@ -53,6 +53,27 @@ def write_practice_markdown(practice_name, markdown):
with open(f'content/practices/{practice_name}.md', 'w+') as f:
f.write(markdown)

# function to extract only the first paragraph of practice Markdown
def extract_first_paragraph(markdown):
# Split the text into lines
lines = markdown.split("\n")
# Initialize a flag to track when we find the first paragraph
paragraph = []
for line in lines:
# Ignore headings (lines starting with #)
if line.startswith("#"):
continue
# If the line is not empty, it's part of a paragraph
if line.strip():
paragraph.append(line.strip())
elif paragraph: # Stop once we have collected a paragraph and hit an empty line
break

return " ".join(paragraph)

# function to retrieve data about a curated list of resources
def get_curated_resources(resource_ids):
resources = Resource.query.filter(Resource.id.in_(resource_ids)).order_by(func.random()).all()

Loading…
Отказ
Запис