|
-
-
-
-
-
-
-
-
-
- from flask import Blueprint, render_template, request, flash, redirect, url_for
- from .models import Resource
- from werkzeug.exceptions import abort
- from . import db
- from .relationships import *
- from isbntools.app import *
- import requests
- import re
- import json
- from sqlalchemy.sql import func
- import markdown
-
-
- def get_resource(resource_id):
- resource = Resource.query.filter_by(id=resource_id).first()
- if resource is None:
- abort(404)
- return resource
-
-
- def get_full_resource(resource_id):
- resource = get_resource(resource_id)
- resource = append_relationships(resource)
- if resource.type == 'book':
-
- resource.description = markdown.markdown(resource.description)
-
- book_data = get_book_data(resource.isbn)
- if book_data:
- resource.__dict__.update(book_data)
-
- if resource.repositoryUrl and "github" in resource.repositoryUrl:
-
- date = get_commit_date(resource.repositoryUrl)
- resource.__dict__['commitDate'] = date
- return resource
-
-
- def get_practice_markdown(practice_name, option='html'):
- file_name = practice_name.replace(" ", "_")
- with open(f'content/practices/{file_name}.md', 'r') as f:
- practice_text = f.read()
- if option == 'html':
- practice_text = markdown.markdown(practice_text)
- return practice_text
-
-
- def write_practice_markdown(practice_name, markdown):
- practice_name = practice_name.replace(" ", "_")
- with open(f'content/practices/{practice_name}.md', 'w+') as f:
- f.write(markdown)
-
-
- def extract_first_paragraph(markdown):
-
- lines = markdown.split("\n")
-
-
- paragraph = []
-
- for line in lines:
-
- if line.startswith("#"):
- continue
-
-
- if line.strip():
- paragraph.append(line.strip())
- elif paragraph:
- break
-
- return " ".join(paragraph)
-
-
- def get_curated_resources(resource_ids):
- resources = Resource.query.filter(Resource.id.in_(resource_ids)).filter_by(published=True).order_by(func.random()).all()
-
- append_relationships_multiple(resources)
- return resources
-
-
- def delete_resource(resource_id):
- deletion = Resource.query.get(resource_id)
- db.session.delete(deletion)
- db.session.commit()
- flash('Successfully deleted!')
-
-
- def get_filter_values(field, type):
-
- field_filter = Resource.query.filter_by(type=type).filter_by(published=True).with_entities(getattr(Resource, field))
-
- field_filter = [i for i, in field_filter]
-
- field_filter = [y for x in field_filter for y in x.split(' / ')]
-
- field_filter = list(dict.fromkeys(field_filter))
-
- field_filter = filter(None, field_filter)
-
- field_filter = sorted(field_filter)
- return field_filter
-
-
- def get_book_data(isbn):
- try:
- book = meta(isbn)
- description = {'desc': desc(isbn)}
- book.update(description)
-
- return book
- except:
- pass
-
-
- def get_book_cover(book):
-
- openl_url = 'https://covers.openlibrary.org/b/isbn/' + book['ISBN-13'] + '-L.jpg?default=false'
- request = requests.get(openl_url)
- if request.status_code != 200:
- book.update(cover(isbn))
- else:
- book_cover = {'thumbnail': openl_url}
- book.update(book_cover)
- return book
-
-
- def get_last_date():
- resource = Resource.query.order_by(Resource.created.desc()).filter_by(published=True).first()
- return resource.created.isoformat()
-
-
- def get_commit_date(repositoryUrl):
-
- api_url = repositoryUrl.replace("https://github.com", "https://api.github.com/repos")
-
- r = requests.get(api_url)
- r = json.loads(r.content)
- branch = r['default_branch']
-
- api_url = api_url + "/branches/" + branch
- r = requests.get(api_url)
- r = json.loads(r.content)
- date = r['commit']['commit']['author']['date'][0:10]
- return date
|