Beispiel #1
0
def main(args):
    """Main entry point for script."""
    today = datetime.date.today()
    if args.all:
        with app.app_context():
            purchases = Purchase.query.all()
            sales = sum(p.product.price for p in purchases)
            for purchase in purchases:
                print str(purchase), purchase.sold_at
            print '{} sales in that period'.format(len(purchases))
            print '${} in total sales in that period'.format(sales)
            return

    if args.today:
        threshold = today - datetime.timedelta(days=1)
    elif args.yesterday:
        threshold = today - datetime.timedelta(days=2)
    elif args.week:
        threshold = today - datetime.timedelta(days=7)
    with app.app_context():
        purchases = Purchase.query.filter(Purchase.sold_at>threshold).all()
        sales = sum(p.product.price for p in purchases)
        for purchase in purchases:
            print str(purchase), purchase.sold_at
        print '{} sales in that period'.format(len(purchases))
        print '{} in total sales in that period'.format(sales)
Beispiel #2
0
 def setUp(self):
     """Pre-test activities."""
     app.testing = True
     app.config['STRIPE_SECRET_KEY'] = 'foo'
     app.config['STRIPE_PUBLIC_KEY'] = 'bar'
     app.config['SITE_NAME'] = 'www.foo.com'
     app.config['STRIPE_SECRET_KEY'] = 'foo'
     app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
     app.config['WTF_CSRF_ENABLED'] = False
     app.config['FILE_DIRECTORY'] = os.path.abspath(
         os.path.join(os.path.split(os.path.abspath(__file__))[0], 'files'))
     with app.app_context():
         db.init_app(current_app)
         db.metadata.create_all(db.engine)
         mail.init_app(current_app)
         bcrypt.init_app(current_app)
         self.db = db
         self.app = app.test_client()
         self.purchase_uuid = str(uuid.uuid4())
         product = Product(name='Test Product',
                           file_name='test.txt',
                           price=5.01)
         purchase = Purchase(product=product,
                             email='*****@*****.**',
                             uuid=self.purchase_uuid,
                             sold_at=datetime.datetime(
                                 2014, 1, 1, 12, 12, 12))
         user = User(email='*****@*****.**',
                     password=bcrypt.generate_password_hash('password'))
         db.session.add(product)
         db.session.add(purchase)
         db.session.add(user)
         db.session.commit()
Beispiel #3
0
 def test_get_purchase(self):
     """Can we retrieve the Purchase instance created in setUp?"""
     with app.app_context():
         purchase = Purchase.query.get(self.purchase_uuid)
         assert purchase is not None
         assert purchase.product.price == 5.01
         assert purchase.email == '*****@*****.**'
Beispiel #4
0
 def setUp(self):
     """Pre-test activities."""
     app.testing = True
     app.config['STRIPE_SECRET_KEY'] = 'foo'
     app.config['STRIPE_PUBLIC_KEY'] = 'bar'
     app.config['SITE_NAME'] = 'www.foo.com'
     app.config['STRIPE_SECRET_KEY'] = 'foo'
     app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
     app.config['WTF_CSRF_ENABLED'] = False
     app.config['FILE_DIRECTORY'] = os.path.abspath(os.path.join(os.path.split(os.path.abspath(__file__))[0], 'files'))
     with app.app_context():
         db.init_app(current_app)
         db.metadata.create_all(db.engine)
         mail.init_app(current_app)
         bcrypt.init_app(current_app)
         self.db = db
         self.app = app.test_client()
         self.purchase_uuid = str(uuid.uuid4())
         product = Product(
             name='Test Product',
             file_name='test.txt',
             price=5.01)
         purchase = Purchase(product=product,
                 email='*****@*****.**',
                 uuid=self.purchase_uuid,
                 sold_at=datetime.datetime(2014, 1, 1, 12, 12, 12))
         user = User(email='*****@*****.**',
                 password=bcrypt.generate_password_hash('password'))
         db.session.add(product)
         db.session.add(purchase)
         db.session.add(user)
         db.session.commit()
Beispiel #5
0
 def test_user_authentication(self):
     """Do the authencation methods for the User model work as expected?"""
     with app.app_context():
         user = User.query.get('*****@*****.**')
         response = self.app.get('/reports')
         assert response.status_code == 401
         assert self.login(user.email, 'password').status_code == 200
         response = self.app.get('/reports')
         assert response.status_code == 200
         assert 'drawSalesChart' in response.data
         response = self.app.get('/logout')
         assert response.status_code == 200
         response = self.app.get('/reports')
         assert response.status_code == 401
Beispiel #6
0
 def test_user_authentication(self):
     """Do the authencation methods for the User model work as expected?"""
     with app.app_context():
         user = User.query.get('*****@*****.**')
         response = self.app.get('/reports')
         assert response.status_code == 401
         assert self.login(user.email, 'password').status_code == 200
         response = self.app.get('/reports')
         assert response.status_code == 200
         assert 'drawSalesChart' in response.data
         response = self.app.get('/logout')
         assert response.status_code == 200
         response = self.app.get('/reports')
         assert response.status_code == 401
Beispiel #7
0
def main():
    """Main entry point for script."""
    with app.app_context():
        db.metadata.create_all(db.engine)
        if User.query.all():
            print 'A user already exists! Create another? (y/n):',
            create = raw_input()
            if create == 'n':
                return
        
        print 'Enter email address: ',
        email = raw_input()
        password = getpass()
        assert password == getpass('Password (again):')

        user = User(email=email, password=bcrypt.generate_password_hash(password))
        db.session.add(user)
        db.session.commit()
        print 'User added.'
Beispiel #8
0
 def test_get_user(self):
     """Can we retrieve the User instance created in setUp?"""
     with app.app_context():
         user = User.query.get('*****@*****.**')
         assert bcrypt.check_password_hash(user.password, 'password')
Beispiel #9
0
 def test_get_purchase_string(self):
     """Is the string representation of the Purchase model what we expect?"""
     with app.app_context():
         purchase = Purchase.query.get(self.purchase_uuid)
         assert str(purchase) == 'Test Product bought by [email protected]'
Beispiel #10
0
 def test_get_purchase_date(self):
     """Can we retrieve the date of the Purchase instance created in setUp?"""
     with app.app_context():
         purchase = Purchase.query.get(self.purchase_uuid)
         assert purchase.sell_date() == datetime.datetime(2014, 1, 1).date()
Beispiel #11
0
 def test_product_with_version_as_string(self):
     """Is the string representation of the Product model what we expect?"""
     with app.app_context():
         product = Product.query.get(1)
         product.version = '1.0'
         assert str(product) == 'Test Product (v1.0)'
Beispiel #12
0
 def test_get_product(self):
     """Can we retrieve the Product instance created in setUp?"""
     with app.app_context():
         product = Product.query.get(1)
         assert product is not None
         assert product.name == 'Test Product'
Beispiel #13
0
 def test_get_user(self):
     """Can we retrieve the User instance created in setUp?"""
     with app.app_context():
         user = User.query.get('*****@*****.**')
         assert bcrypt.check_password_hash(user.password, 'password')
Beispiel #14
0
 def test_get_product(self):
     """Can we retrieve the Product instance created in setUp?"""
     with app.app_context():
         product = Product.query.get(1)
         assert product is not None
         assert product.name == 'Test Product'
Beispiel #15
0
 def test_get_purchase_date(self):
     """Can we retrieve the date of the Purchase instance created in setUp?"""
     with app.app_context():
         purchase = Purchase.query.get(self.purchase_uuid)
         assert purchase.sell_date() == datetime.datetime(2014, 1, 1).date()
Beispiel #16
0
 def test_product_with_version_as_string(self):
     """Is the string representation of the Product model what we expect?"""
     with app.app_context():
         product = Product.query.get(1)
         product.version = '1.0'
         assert str(product) == 'Test Product (v1.0)'
Beispiel #17
0
 def test_get_purchase_string(self):
     """Is the string representation of the Purchase model what we expect?"""
     with app.app_context():
         purchase = Purchase.query.get(self.purchase_uuid)
         assert str(purchase) == 'Test Product bought by [email protected]'
Beispiel #18
0
"""Create a free copy of the book for the given version and email address."""
import uuid
import sys

from bull import app
app.config['STRIPE_SECRET_KEY'] = None
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite+pysqlite:///sqlite3.db'
from bull.models import Product, Purchase, db

NAME_MAP = {'pdf2': 1, 'pdf3': 2, 'epub3': 3, 'epub2': 4, 'bundle': 5}

with app.app_context():
    session = db.session()
    book = session.query(Product).get(NAME_MAP[sys.argv[1]])
    purchase = Purchase(
        uuid=str(uuid.uuid4()),
        email=sys.argv[2],
        product_id=book.id
        )
    session.add(purchase)
    session.commit()
    print 'link is https://buy.jeffknupp.com/{}'.format(purchase.uuid)

#with app.app_context():
#    session = db.session()
#    db.metadata.create_all(db.engine)
#    session.add(pdf2)
#    session.add(pdf3)
#    session.add(epub2)
#    session.add(epub3)
#    session.add(bundle)
Beispiel #19
0
__FILENAME__ = app
from bull import app, db
def get_app():
    """Return the application object."""
    return app

if __name__ == '__main__':
    app.config.from_object('config')
    with app.app_context():
        db.metadata.create_all(bind=db.engine)
    get_app().run(debug=True)

########NEW FILE########
__FILENAME__ = bull
"""Bull is a library used to sell digital products on your website. It's meant
to be run on the same domain as your sales page, making analytics tracking
trivially easy.
"""

import logging
import sys
import uuid

from flask import (Blueprint, send_from_directory, abort, request,
                   render_template, current_app, render_template, redirect,
                   url_for, current_app)
from flaskext.bcrypt import Bcrypt
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.login import LoginManager, login_required, login_user, logout_user, current_user
from flask.ext.mail import Mail, Message
import stripe