Example #1
0
class InvoiceLink(db.Model):
    __tablename__ = 'invoice_link'

    #column definitions
    id = db.Column(db.Integer(11), primary_key=True, nullable=False)
    user_id = db.Column(db.Integer(11),
                        db.ForeignKey('user.id'),
                        nullable=False)
    invoice_id = db.Column(db.Integer(11),
                           db.ForeignKey('invoice.id'),
                           nullable=False)
    link = db.Column(db.Unicode(25), default=u'', nullable=False)
    link_hash = db.Column(db.Unicode(50), nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.now)

    def generate_link_code(self):
        code = random_id(8)
        while True:
            obj = InvoiceLink.query.filter_by(link=code).first()
            if not obj:
                break
            else:
                code = random_id(8)

        self.link = code
        return self.link

    def get_url(self):
        url = 'http://%s.%s/portal/invoice/%s' % (
            self.user.username, current_app.config['SERVER_NAME'], self.link)
        return url
Example #2
0
class Subscription(db.Model):
    __tablename__ = 'subscription'
    id = db.Column(db.Integer(11), primary_key=True, nullable=False)
    user_id = db.Column(db.Integer(11),
                        db.ForeignKey('user.id'),
                        nullable=False)
    plan_id = db.Column(db.Integer(11),
                        db.ForeignKey('plan.id'),
                        nullable=False)
    active = db.Column(db.Boolean, nullable=False, default=False)
    start_date = db.Column(db.DateTime, nullable=True)
    end_date = db.Column(db.DateTime, nullable=True)
    stripe_data = db.Column(db.UnicodeText(4294967295),
                            nullable=False,
                            default=u'{}')

    updated_at = db.Column(db.DateTime, nullable=False, default=datetime.now)
    created_at = db.Column(db.DateTime,
                           nullable=False,
                           default=datetime.now,
                           onupdate=datetime.now)

    # relations
    transactions = db.relation('Transaction',
                               backref=db.backref('subscription',
                                                  lazy='joined',
                                                  uselist=False))
Example #3
0
class StripePayment(db.Model):
    __tablename__ = 'stripe_payment'

    id = db.Column(db.Integer(11), primary_key=True, nullable=False)
    user_id = db.Column(db.Integer(11),
                        db.ForeignKey('user.id'),
                        nullable=False)
    payment_id = db.Column(db.Integer(11),
                           db.ForeignKey('payment.id'),
                           nullable=True)
    invoice_id = db.Column(db.Integer(11),
                           db.ForeignKey('invoice.id'),
                           nullable=False)
    amount = db.Column(db.Numeric(8, 2), default=0)
    token = db.Column(db.Unicode(100), nullable=False)
    state = db.Column(db.Unicode(20), default=u'initialized')
    error_message = db.Column(db.Unicode(255), nullable=True)
    charge_id = db.Column(db.Unicode(100), nullable=True)
    charge = db.Column(db.UnicodeText(4294967295),
                       nullable=False,
                       default=u'{}')
    created_at = db.Column(db.DateTime(), nullable=False, default=datetime.now)

    @classmethod
    def get_or_create_for_user(cls, user_id):
        account = cls.query.filter_by(user_id=user_id).first()
        if not account:
            model = cls()
            model.user_id = user_id
            db.session.add(model)
            db.session.commit()
        return account

    def create_payment_object(self):
        """Create related payment object"""
        payment = Payment()
        payment.invoice_id = self.invoice_id
        payment.date = datetime.now()
        payment.currency_code = self.invoice.currency_code
        payment.amount = self.amount
        payment.method = 'stripe'
        payment.description = 'Credit card payment'

        db.session.add(payment)
        db.session.commit()

        return payment
Example #4
0
class StripeAccount(db.Model):
    __tablename__ = 'stripe_account'

    id = db.Column(db.Integer(11), primary_key=True, nullable=False)
    user_id = db.Column(db.Integer(11), db.ForeignKey('user.id'))
    secret_key = db.Column(db.Unicode(255))
    public_key = db.Column(db.Unicode(255))
    enabled = db.Column(db.Boolean, default=False)

    updated_at = db.Column(db.DateTime(), nullable=False, default=datetime.now)
    created_at = db.Column(db.DateTime(), nullable=False, default=datetime.now)

    @classmethod
    def get_or_create_for_user(cls, user_id):
        account = cls.query.filter_by(user_id=user_id).first()
        if not account:
            model = cls()
            model.user_id = user_id
            db.session.add(model)
            db.session.commit()
        return account
Example #5
0
class Transaction(db.Model):
    __tablename__ = 'transaction'

    id = db.Column(db.Integer(11), primary_key=True, nullable=False)
    user_id = db.Column(db.Integer(11),
                        db.ForeignKey('user.id'),
                        nullable=False)
    subscription_id = db.Column(db.Integer(11),
                                db.ForeignKey('subscription.id'),
                                nullable=False)
    success = db.Column(db.Boolean, default=False)
    amount = db.Column(db.Numeric(8, 2), default=0)
    charge_id = db.Column(db.Unicode(100), nullable=True)
    charge = db.Column(db.UnicodeText(4294967295),
                       nullable=True,
                       default=u'{}')

    updated_at = db.Column(db.DateTime, nullable=False, default=datetime.now)
    created_at = db.Column(db.DateTime,
                           nullable=False,
                           default=datetime.now,
                           onupdate=datetime.now)
Example #6
0
class GoCardlessAccount(db.Model):
    __tablename__ = 'gocardless_account'

    id = db.Column(db.Integer(11), primary_key=True, nullable=False)
    user_id = db.Column(db.Integer(11), db.ForeignKey('user.id'))
    app_identifier = db.Column(db.Unicode(255))
    app_secret = db.Column(db.Unicode(255))
    merchant_access_token = db.Column(db.Unicode(255))
    merchant_id = db.Column(db.Unicode(255))
    enabled = db.Column(db.Boolean, default=False)

    updated_at = db.Column(db.DateTime(), nullable=False, default=datetime.now)
    created_at = db.Column(db.DateTime(), nullable=False, default=datetime.now)

    @classmethod
    def get_or_create_for_user(cls, user_id):
        account = cls.query.filter_by(user_id=user_id).first()
        if not account:
            model = cls()
            model.user_id = user_id
            db.session.add(model)
            db.session.commit()
        return account
Example #7
0
class GoCardlessPayment(db.Model):
    __tablename__ = 'gocardless_payment'

    id = db.Column(db.Integer(11), primary_key=True, nullable=False)
    invoice_id = db.Column(db.Integer(11),
                           db.ForeignKey('invoice.id'),
                           nullable=False)
    user_id = db.Column(db.Integer(11),
                        db.ForeignKey('user.id'),
                        nullable=False)
    payment_id = db.Column(db.Integer(11),
                           db.ForeignKey('payment.id'),
                           nullable=True)
    amount = db.Column(db.Numeric(8, 2), default=0)
    reference = db.Column(db.Unicode(100), nullable=False)
    state = db.Column(db.Unicode(20), default=u'initialized')
    resource_id = db.Column(db.Unicode(100), nullable=True)
    resource_uri = db.Column(db.Unicode(255), nullable=True)
    error_message = db.Column(db.Unicode(255), nullable=True)

    created_at = db.Column(db.DateTime(), nullable=False, default=datetime.now)

    def create_payment_object(self):
        """Create related payment object"""
        payment = Payment()
        payment.invoice_id = self.invoice_id
        payment.date = datetime.now()
        payment.currency_code = self.invoice.currency_code
        payment.amount = self.amount
        payment.method = 'gocardless'
        payment.description = 'Direct debit payment'

        db.session.add(payment)
        db.session.commit()

        return payment
Example #8
0
class Plan(db.Model):
    __tablename__ = 'plan'

    id = db.Column(db.Integer(11), primary_key=True, nullable=False)
    name = db.Column(db.Unicode(20))
    gateway_uid = db.Column(db.Unicode(255))
    description = db.Column(db.Unicode(255))
    amount = db.Column(db.Numeric(8, 2), default=0)
    billing_interval = db.Column(db.Enum('daily', 'weekly', 'monthly',
                                         'yearly'),
                                 default='monthly')

    created_at = db.Column(db.DateTime, nullable=False, default=datetime.now)

    # relationships
    subscriptions = db.relation('Subscription',
                                backref=db.backref('plan',
                                                   lazy='joined',
                                                   uselist=False))

    @property
    def interval_name(self):
        if self.billing_interval == 'monthly':
            return 'month'
Example #9
0
class File(db.Model):
    __tablename__ = 'file'

    id = db.Column(db.Integer, primary_key=True)

    filename = db.Column(db.String(255))
    filesize = db.Column(db.Integer(20))
    extension = db.Column(db.String(25))
    mimetype = db.Column(db.String(255))
    location = db.Column(db.String(255))

    width = db.Column(db.Integer(11))
    height = db.Column(db.Integer(11))
    hash = db.Column(db.String(40))

    # times
    created_at = db.Column(db.DateTime, default=get_current_time())
    updated_at = db.Column(db.DateTime, default=get_current_time())

    def is_image(self):
        extensions = ['jpg', 'jpeg', 'png', 'bmp', 'gif']
        if self.extension in extensions:
            return True
        return False

    def get_web_url(self, w=80, h=80, method='normal'):
        return self.location.replace(current_app.config['UPLOAD_DIR'] + '/',
                                     '')

    @classmethod
    def save_uploaded_file(cls, fs, filename=None):
        # make a new folder for the image
        now = datetime.now()
        _uuid = uuid.uuid4().hex
        path = '%s/%s/%s/%s/%s/%s' % (now.year, now.month, now.day, _uuid[:3],
                                      _uuid[3:6], _uuid[6:])
        base_path = os.path.join(current_app.config['UPLOAD_DIR'], path)
        os.makedirs(base_path)

        name = secure_filename(fs.filename) if filename is None else filename
        save_path = os.path.join(base_path, name)
        fs.save(save_path)

        # get hash of file contents
        f1 = open(save_path, 'rb')
        sha1sum = hashlib.sha1(f1.read()).hexdigest()
        f1.close()

        obj = File()
        obj.filename = fs.filename
        obj.filesize = os.path.getsize(save_path)
        obj.mimetype = fs.mimetype
        obj.extension = fs.filename.rsplit('.', 1)[1]
        obj.location = save_path
        obj.hash = sha1sum
        return obj

    @classmethod
    def create_from_file(cls, oldpath, filename=None):
        now = datetime.now()
        _uuid = uuid.uuid4().hex
        path = '%s/%s/%s/%s/%s/%s' % (now.year, now.month, now.day, _uuid[:3],
                                      _uuid[3:6], _uuid[6:])
        base_path = os.path.join(current_app.config['UPLOAD_DIR'], path)
        os.makedirs(base_path)

        save_path = os.path.join(base_path, filename)
        shutil.copy(oldpath, save_path)

        # get hash of file contents
        f1 = open(save_path, 'rb')
        sha1sum = hashlib.sha1(f1.read()).hexdigest()
        f1.close()

        obj = File()
        obj.filename = filename
        obj.filesize = os.path.getsize(save_path)
        obj.mimetype = mimetypes.guess_type(oldpath)[0]
        obj.extension = filename.rsplit('.', 1)[1]
        obj.location = save_path
        obj.hash = sha1sum
        return obj