Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Create a Flask WSGI app and configure it using values from the module.
app = Flask(__name__)
app.config.from_object(__name__)
# FlaskDB is a wrapper for a peewee database that sets up pre/post-request
# hooks for managing database connections.
flask_db = FlaskDB(app)
# The `database` is the actual peewee database, as opposed to flask_db which is
# the wrapper.
database = flask_db.database
# Configure micawber with the default OEmbed providers (YouTube, Flickr, etc).
# We'll use a simple in-memory cache so that multiple requests for the same
# video don't require multiple network requests.
oembed_providers = bootstrap_basic(OEmbedCache())
class Entry(flask_db.Model):
title = CharField()
slug = CharField(unique=True)
content = TextField()
published = BooleanField(index=True)
timestamp = DateTimeField(default=datetime.datetime.now, index=True)
@property
def html_content(self):
"""
Generate HTML representation of the markdown-formatted blog entry,
and also convert any media URLs into rich media objects such as video
players or images.
"""
from datetime import datetime
from json import loads
import peewee
from peewee_async import Manager
from markdown import markdown
from micawber import bootstrap_basic, parse_html
from micawber.cache import Cache as OEmbedCache
# Configure micawber with the default OEmbed providers (YouTube, Flickr, etc).
# We'll use a simple in-memory cache so that multiple requests for the same
# video don't require multiple network requests.
from slugify import slugify
oembed_providers = bootstrap_basic(OEmbedCache())
def make_models(db, db_name, loop):
class Entry(peewee.Model):
title = peewee.CharField(default='')
slug = peewee.CharField(unique=True)
content = peewee.TextField(default='')
is_published = peewee.BooleanField(default=False, index=True)
created = peewee.DateTimeField(default=datetime.now, index=True)
raw_meta = peewee.TextField(default='')
@staticmethod
def slugify(text):
return slugify(text.lower())
@property
self._cache = self.load()
def load(self):
if os.path.exists(self.filename):
with closing(open(self.filename)) as fh:
contents = fh.read()
return pickle.loads(contents)
return {}
def save(self):
with closing(open(self.filename, 'w')) as fh:
fh.write(pickle.dumps(self._cache))
if Redis:
class RedisCache(Cache):
def __init__(self, namespace='micawber', **conn):
self.namespace = namespace
self.key_fn = lambda self, k: '%s.%s' % (self.namespace, k)
self.conn = Redis(**conn)
def get(self, k):
cached = self.conn.get(self.key_fn(k))
if cached:
return pickle.loads(cached)
def set(self, k, v):
self.conn.set(self.key_fn(k), pickle.dumps(v))
except ImportError:
Redis = None
class Cache(object):
def __init__(self):
self._cache = {}
def get(self, k):
return self._cache.get(k)
def set(self, k, v):
self._cache[k] = v
class PickleCache(Cache):
def __init__(self, filename='cache.db'):
self.filename = filename
self._cache = self.load()
def load(self):
if os.path.exists(self.filename):
with closing(open(self.filename)) as fh:
contents = fh.read()
return pickle.loads(contents)
return {}
def save(self):
with closing(open(self.filename, 'w')) as fh:
fh.write(pickle.dumps(self._cache))