def init_db(): import models ModelBase.metadata.create_all(bind=engine) log = create_logger('users-api-initdb') def create_users(): word_url = "http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain" log.info("Getting words for random names...") words = requests.get(word_url).text.splitlines() upper_words = [word for word in words if word[0].isupper()] name_words = [ word.lower() for word in upper_words if not word.isupper() ] li = LoremIpsum() for name in name_words: u = models.Users(name=name, comments=[li.get_sentences(5) for i in range(10)]) db_session.add(u) db_session.commit() del upper_words del name_words if not models.Users.query.all(): log.info("Seeding new database with test data...") create_users() log.info("Successfully seeded database!")
import os import sys import json from factories.log import create_logger log = create_logger('users-api-configuration') class Config(object): INITDB = False DB_URI = os.getenv('DB_URI') CLOUD_FOUNDRY = False VCAP_SERVICES = json.loads(os.getenv('VCAP_SERVICES', '{}')) def __init__(self): self.deployment_type() if self.CLOUD_FOUNDRY: self.get_vcap_db() else: if not self.DB_URI: log.error( "DB_URI is required in non cloud foundry environments") sys.exit(127) def deployment_type(self): index = os.getenv('CF_INSTANCE_INDEX') if index: self.CLOUD_FOUNDRY = True if index == "0": self.INITDB = True
import os import json from uuid import uuid4 from factories.log import create_logger log = create_logger('guids-api') def generate_data(filename): if not os.path.isfile(filename): result = {} n = 100000 log.info('Generating data...') for i in range(n): result[str(uuid4())] = str(uuid4()) if i % (n / 10) == 0: log.info(f'Generating {i * 10} done') with open(filename, 'w') as f: json.dump(result, f)
import os import sys import logging import argparse import boto3 import redisdl from boto3.session import Session parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, parent_dir) from config import get_config from factories.log import create_logger config = get_config(os.getenv('CONFIG_ENV', 'dev')) log = create_logger('product-api-tasks') def backup_redis(outfile): with open(outfile, 'w') as f: redisdl.dump( f, host=config.REDIS_HOST, password=config.REDIS_PASSWORD, ) def copy_to_s3(outfile, s3_key): session = Session(aws_access_key_id=config.S3_ACCESS_KEY_ID, aws_secret_access_key=config.S3_SECRET_KEY_ID) s3 = session.resource('s3')
from flask import abort, jsonify, Blueprint, request from sqlalchemy.sql.expression import func from database import db_session from models import Users from factories.log import create_logger users_blueprint = Blueprint('users', __name__) log = create_logger('users-api') @users_blueprint.route("/random/user") def random_user(): u = Users.query.order_by(func.random()).limit(1).one_or_none() if not u: abort(404, "user not found") return jsonify(u.to_dict()) @users_blueprint.route("/user/comments/<username>") def comments(username): u = Users.query.filter(Users.name == username).one_or_none() if not u: abort(404, "user not found") return jsonify(u.to_dict()) @users_blueprint.route("/users") def users(): return jsonify([x[0] for x in db_session.query(Users.name).all()])