예제 #1
0
class Suppliers(ResourceMixin, db.Model):

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

    # Genral Info
    name = db.Column(db.String(50), nullable=False)
    company = db.Column(db.String(50), index=True)
    display_name = db.Column(db.String(50), nullable=False)
    supplier_type = db.Column(db.String(50))

    # Contant Info
    phone = db.Column(db.String(15), server_default='')
    email = db.Column(db.String(255), server_default='')
    website = db.Column(db.String(255), server_default='')

    # Billing Address
    address = db.Column(db.String(300), server_default='')
    pincode = db.Column(db.String(25), server_default='')
    city = db.Column(db.String(255), index=True, server_default='')
    state = db.Column(db.String(255), server_default='')
    country = db.Column(db.String(255), server_default='')

    # Tax Info
    gst = db.Column(db.String(15), server_default='')
    pan = db.Column(db.String(10), server_default='')

    # Meta Info
    is_active = db.Column('is_active',
                          db.Boolean(),
                          nullable=False,
                          server_default='1')
예제 #2
0
class Company(ResourceMixin, db.Model):

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

    name = db.Column(db.String(24), unique=True, index=True)

    # Contant Info
    phone = db.Column(db.String(15), server_default='')
    email = db.Column(db.String(255), server_default='')
    website = db.Column(db.String(255), server_default='')

    # Address
    address = db.Column(db.String(300), server_default='')
    pincode = db.Column(db.String(25), server_default='')
    city = db.Column(db.String(255), server_default='')
    state = db.Column(db.String(255), server_default='')
    country = db.Column(db.String(255), server_default='')

    # Tax Info
    gst = db.Column(db.String(15), server_default='')
    pan = db.Column(db.String(10), server_default='')

    # Meta Info
    is_active = db.Column('is_active',
                          db.Boolean(),
                          nullable=False,
                          server_default='1')

    # RelationShips
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
예제 #3
0
class Customer(ResourceMixin, db.Model):

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

    # Genral Info
    first_name = db.Column(db.String(80), nullable=False)
    last_name = db.Column(db.String(65))

    display_name = db.Column(db.String(145), nullable=False)
    company = db.Column(db.String(125))
    customer_type = db.Column(db.String(45))

    # Contant Info
    phone = db.Column(db.String(15), server_default='')
    email = db.Column(db.String(45), server_default='')
    website = db.Column(db.String(255), server_default='')

    # Billing Address
    address = db.Column(db.String(200), server_default='')
    pincode = db.Column(db.String(25), server_default='')
    city = db.Column(db.String(45), server_default='')
    state = db.Column(db.String(45), server_default='')
    country = db.Column(db.String(45), server_default='')

    # Shiping Address
    s_address = db.Column(db.String(200), server_default='')
    s_pincode = db.Column(db.String(25), server_default='')
    s_city = db.Column(db.String(45), server_default='')
    s_state = db.Column(db.String(45), server_default='')
    s_country = db.Column(db.String(45), server_default='')

    # Tax Info
    gst = db.Column(db.String(15), server_default='')
    pan = db.Column(db.String(10), server_default='')

    # Meta Info
    is_active = db.Column('is_active',
                          db.Boolean(),
                          nullable=False,
                          server_default='1')

    # Relationships
    machines = db.relationship('Machine', backref='owner', lazy=True)
    contacts = db.relationship('Contact', backref='in_reach', lazy=True)
예제 #4
0
class Users(UserMixin, ResourceMixin, db.Model):

    ROLE = OrderedDict([('member', 'Member'), ('admin', 'Admin')])

    id = db.Column(db.Integer, primary_key=True)
    # Relationships.
    # credit_card = db.relationship(CreditCard, uselist=False, backref='users',
    #                               passive_deletes=True)
    # subscription = db.relationship(Subscription, uselist=False,
    #                                backref='users', passive_deletes=True)
    # invoices = db.relationship(Invoice, backref='users', passive_deletes=True)

    # Authentication.
    role = db.Column(db.Enum(*ROLE, name='role_types', native_enum=False),
                     index=True,
                     nullable=False,
                     server_default='member')
    active = db.Column('is_active',
                       db.Boolean(),
                       nullable=False,
                       server_default='1')
    username = db.Column(db.String(24), unique=True, index=True)
    phone = db.Column(db.String(13))
    email = db.Column(db.String(255),
                      unique=True,
                      nullable=False,
                      server_default='')
    password = db.Column(db.String(128), nullable=False, server_default='')

    # Billing.
    name = db.Column(db.String(128), index=True)
    organization = db.Column(db.String(128))
    payment_id = db.Column(db.String(128), index=True)
    cancelled_subscription_on = db.Column(AwareDateTime())
    previous_plan = db.Column(db.String(128))

    # Activity tracking.
    sign_in_count = db.Column(db.Integer, nullable=False, default=0)
    current_sign_in_on = db.Column(AwareDateTime())
    current_sign_in_ip = db.Column(db.String(45))
    last_sign_in_on = db.Column(AwareDateTime())
    last_sign_in_ip = db.Column(db.String(45))

    # Additional settings.
    locale = db.Column(db.String(5), nullable=False, server_default='en')

    # RelationShips
    companies = db.relationship('Company', backref='owner', lazy=True)

    def __init__(self, **kwargs):
        # Call Flask-SQLAlchemy's constructor.
        super(Users, self).__init__(**kwargs)
        self.password = Users.encrypt_password(kwargs.get('password', ''))

    @classmethod
    def find_by_identity(cls, identity):
        return Users.query.filter((Users.email == identity)
                                  | (Users.username == identity)).first()

    @classmethod
    def encrypt_password(cls, plaintext_password):
        if plaintext_password:
            return generate_password_hash(plaintext_password)

        return None

    @classmethod
    def deserialize_token(cls, token):
        private_key = TimedJSONWebSignatureSerializer(
            current_app.config['SECRET_KEY'])
        try:
            decoded_payload = private_key.loads(token)

            return Users.find_by_identity(decoded_payload.get('user_email'))
        except Exception:
            return None

    # @classmethod
    # def initialize_password_reset(cls, identity):
    #     u = Users.find_by_identity(identity)
    #     reset_token = u.serialize_token()

    #     # This prevents circular imports.
    #     # from manager.apps.user.tasks import (deliver_password_reset_email)
    #     # deliver_password_reset_email.delay(u.id, reset_token)

    #     return u

    @classmethod
    def search(cls, query):
        if not query:
            return ''

        search_query = '%{0}%'.format(query)
        search_chain = (Users.email.ilike(search_query),
                        Users.username.ilike(search_query))

        return or_(*search_chain)

    @classmethod
    def is_last_admin(cls, user, new_role, new_active):
        is_changing_roles = user.role == 'admin' and new_role != 'admin'
        is_changing_active = user.active is True and new_active is None

        if is_changing_roles or is_changing_active:
            admin_count = Users.query.filter(Users.role == 'admin').count()
            active_count = Users.query.filter(Users.is_active is True).count()

            if admin_count == 1 or active_count == 1:
                return True

        return False

    # @classmethod
    # def bulk_delete(cls, ids):
    #     delete_count = 0

    #     for id in ids:
    #         user = Users.query.get(id)

    #         if user is None:
    #             continue

    #         if user.payment_id is None:
    #             user.delete()
    #         else:
    #             # subscription = Subscription()
    #             # cancelled = subscription.cancel(user=user)

    #             # If successful, delete it locally.
    #             # if cancelled:
    #             #     user.delete()

    #         delete_count += 1

    #     return delete_count

    def is_active(self):
        return self.active

    def get_auth_token(self):
        private_key = current_app.config['SECRET_KEY']

        serializer = URLSafeTimedSerializer(private_key)
        data = [str(self.id), md5(self.password.encode('utf-8')).hexdigest()]

        return serializer.dumps(data)

    def authenticated(self, with_password=True, password=''):
        if with_password:
            return check_password_hash(self.password, password)

        return True

    def serialize_token(self, expiration=3600):
        private_key = current_app.config['SECRET_KEY']

        serializer = TimedJSONWebSignatureSerializer(private_key, expiration)
        return serializer.dumps({'user_email': self.email}).decode('utf-8')

    def update_activity_tracking(self, ip_address):
        self.sign_in_count += 1

        self.last_sign_in_on = self.current_sign_in_on
        self.last_sign_in_ip = self.current_sign_in_ip

        self.current_sign_in_on = datetime.datetime.now(pytz.utc)
        self.current_sign_in_ip = ip_address

        return self.save()
예제 #5
0
파일: users.py 프로젝트: Ajay1290/agust-crm
class User(UserMixin, ResourceMixin, db.Model):

    ROLE = OrderedDict([('member', 'Member'), ('admin', 'Admin')])

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

    # Authentication.
    role = db.Column(db.Enum(*ROLE, name='role_types', native_enum=False),
                     index=True,
                     nullable=False,
                     server_default='member')
    active = db.Column('is_active',
                       db.Boolean(),
                       nullable=False,
                       server_default='1')
    username = db.Column(db.String(24), unique=True, index=True)
    phone = db.Column(db.String(13))
    email = db.Column(db.String(255),
                      unique=True,
                      nullable=False,
                      server_default='')
    password = db.Column(db.String(128), nullable=False, server_default='')
    phone_confirmed = db.Column(db.Boolean(),
                                nullable=False,
                                server_default='0')
    email_confirmed = db.Column(db.Boolean(),
                                nullable=False,
                                server_default='0')

    # Billing.
    name = db.Column(db.String(128), index=True)
    organization = db.Column(db.String(128))
    payment_id = db.Column(db.String(128), index=True)
    cancelled_subscription_on = db.Column(AwareDateTime())
    previous_plan = db.Column(db.String(128))

    # Billing Relationships.
    # credit_card = db.relationship(CreditCard, uselist=False, backref='users', passive_deletes=True)
    # subscription = db.relationship(Subscription, uselist=False, backref='users', passive_deletes=True)
    # invoices = db.relationship(Invoice, backref='users', passive_deletes=True)

    # Activity tracking.
    sign_in_count = db.Column(db.Integer, nullable=False, default=0)
    current_sign_in_on = db.Column(AwareDateTime())
    current_sign_in_ip = db.Column(db.String(45))
    last_sign_in_on = db.Column(AwareDateTime())
    last_sign_in_ip = db.Column(db.String(45))

    # Additional settings.
    locale = db.Column(db.String(5), nullable=False, server_default='en')

    # RelationShips
    companies = db.relationship('Company', backref='owner', lazy=True)

    def __init__(self, **kwargs):
        # Call Flask-SQLAlchemy's constructor.
        super(User, self).__init__(**kwargs)
        self.password = User.encrypt_password(kwargs.get('password', ''))

    @classmethod
    def find_by_identity(cls, identity):
        return User.query.filter((User.email == identity)
                                 | (User.username == identity)).first()

    @classmethod
    def encrypt_password(cls, plaintext_password):
        if plaintext_password:
            return generate_password_hash(plaintext_password)

        return None

    def authenticated(self, with_password=True, password=''):
        if with_password:
            return check_password_hash(self.password, password)

        return True

    def is_active(self, act=False):
        if act:
            self.active = True
            return self.active
        return self.active

    def get_auth_token(self):
        private_key = current_app.config['SECRET_KEY']
        serializer = URLSafeTimedSerializer(private_key)
        data = [str(self.id), md5(self.password.encode('utf-8')).hexdigest()]

        return serializer.dumps(data)

    def serialize_token(self, expiration=3600):
        private_key = current_app.config['SECRET_KEY']
        serializer = TimedJSONWebSignatureSerializer(private_key, expiration)

        return serializer.dumps({'user_email': self.email}).decode('utf-8')

    @classmethod
    def confirm_email_token(cls, username, email, password, expiration=3600):
        private_key = current_app.config['SECRET_KEY']
        serializer = TimedJSONWebSignatureSerializer(private_key, expiration)

        token = serializer.dumps({
            'user_username': username,
            'user_email': email,
            'user_password': password
        }).decode('utf-8')

        return token

    @classmethod
    def deserialize_token(cls, token):
        private_key = TimedJSONWebSignatureSerializer(
            current_app.config['SECRET_KEY'])
        try:
            decoded_payload = private_key.loads(token)
            data = {
                'user_username': decoded_payload.get('user_username'),
                'user_email': decoded_payload.get('user_email'),
                'user_password': decoded_payload.get('user_password')
            }
            return data
        except Exception:
            return None

    def update_activity_tracking(self, ip_address):
        self.sign_in_count += 1
        self.active = True

        self.last_sign_in_on = self.current_sign_in_on
        self.last_sign_in_ip = self.current_sign_in_ip

        self.current_sign_in_on = datetime.datetime.now(pytz.utc)
        self.current_sign_in_ip = ip_address

        return self.save()
예제 #6
0
class Users(UserMixin, ResourceMixin, db.Model):
    
    id = db.Column(db.Integer, primary_key=True)

    username = db.Column(db.String(24), unique=True, index=True)
    phone = db.Column(db.String(13))
    email = db.Column(db.String(255), unique=True, nullable=False, server_default='')
    password = db.Column(db.String(128), nullable=False, server_default='')
    
    phone_confirmed = db.Column(db.Boolean(), nullable=False, server_default='0')
    email_confirmed = db.Column(db.Boolean(), nullable=False, server_default='0')
    
    active = db.Column('is_active', db.Boolean(), nullable=False, server_default='1')
    otp = db.Column(db.String(10), nullable=True)

    # Activity tracking.
    sign_in_count = db.Column(db.Integer, nullable=False, default=0)
    current_sign_in_on = db.Column(AwareDateTime())
    current_sign_in_ip = db.Column(db.String(45))
    last_sign_in_on = db.Column(AwareDateTime())
    last_sign_in_ip = db.Column(db.String(45))

    def __init__(self, **kwargs):
        super(Users, self).__init__(**kwargs)
        self.password = Users.encrypt_password(kwargs.get('password', ''))

    @classmethod
    def find_by_identity(cls, identity):
        return Users.query.filter((Users.email == identity) | (Users.username == identity)).first()
    
    @classmethod
    def encrypt_password(cls, plaintext_password):
        if plaintext_password:
            return generate_password_hash(plaintext_password)

        return None
    
    def authenticated(self, with_password=True, password=''):        
        if with_password:
            return check_password_hash(self.password, password)

        return True
    
    def is_active(self, act=False):
        if act:
            self.active = True
            return self.active
        return self.active

    def update_activity_tracking(self, ip_address):
        self.sign_in_count += 1
        self.active = True

        self.last_sign_in_on = self.current_sign_in_on
        self.last_sign_in_ip = self.current_sign_in_ip

        self.current_sign_in_on = datetime.datetime.now(pytz.utc)
        self.current_sign_in_ip = ip_address

        return self.save()

    def get_auth_token(self):        
        private_key = current_app.config['SECRET_KEY']
        serializer = URLSafeTimedSerializer(private_key)
        data = [str(self.id), md5(self.password.encode('utf-8')).hexdigest()]

        return serializer.dumps(data)

    def serialize_token(self, expiration=3600):        
        private_key = current_app.config['SECRET_KEY']
        serializer = TimedJSONWebSignatureSerializer(private_key, expiration)

        return serializer.dumps({'user_email': self.email}).decode('utf-8')

    @classmethod
    def confirm_email_token(cls, username, email, password, expiration=3600):
        private_key = current_app.config['SECRET_KEY']
        serializer = TimedJSONWebSignatureSerializer(private_key, expiration)

        token = serializer.dumps({'user_username': username,'user_email': email,'user_password': password}).decode('utf-8')

        return token

    @classmethod
    def deserialize_token(cls, token):        
        private_key = TimedJSONWebSignatureSerializer(current_app.config['SECRET_KEY'])
        try:
            decoded_payload = private_key.loads(token)
            data = {'user_username':decoded_payload.get('user_username'),
                    'user_email':decoded_payload.get('user_email'),
                    'user_password':decoded_payload.get('user_password')}
            return data
        except Exception:
            return None


    def genrate_otp(self, digit=4):
        f = int('1'+'0'*(digit - 1))
        l = int('9'*digit)
        otp = random.randint(f,l)

        return otp
    


    def verify_otp(self, otp):
        if self.otp:
            if self.otp == otp:
                return True
        
        return False
예제 #7
0
class CreditCard(ResourceMixin, db.Model):
    IS_EXPIRING_THRESHOLD_MONTHS = 2

    __tablename__ = 'credit_cards'
    id = db.Column(db.Integer, primary_key=True)

    # Relationships.
    user_id = db.Column(db.Integer,
                        db.ForeignKey('user.id',
                                      onupdate='CASCADE',
                                      ondelete='CASCADE'),
                        index=True,
                        nullable=False)

    # Card details.
    brand = db.Column(db.String(32))
    last4 = db.Column(db.Integer)
    exp_date = db.Column(db.Date, index=True)
    is_expiring = db.Column(db.Boolean(), nullable=False, server_default='0')

    def __init__(self, **kwargs):
        # Call Flask-SQLAlchemy's constructor.
        super(CreditCard, self).__init__(**kwargs)

    @classmethod
    def is_expiring_soon(cls, compare_date=None, exp_date=None):
        """Determine whether or not this credit card is expiring soon.

        :param compare_date: Date to compare at
        :type compare_date: date
        :param exp_date: Expiration date
        :type exp_date: date
        :return: bool
        """
        return exp_date <= timedelta_months(
            CreditCard.IS_EXPIRING_THRESHOLD_MONTHS, compare_date=compare_date)

    @classmethod
    def mark_old_credit_cards(cls, compare_date=None):
        """
        Mark credit cards that are going to expire soon or have expired.

        :param compare_date: Date to compare at
        :type compare_date: date
        :return: Result of updating the records
        """
        today_with_delta = timedelta_months(
            CreditCard.IS_EXPIRING_THRESHOLD_MONTHS, compare_date)

        CreditCard.query.filter(
            CreditCard.exp_date <= today_with_delta).update(
                {CreditCard.is_expiring: True})

        return db.session.commit()

    @classmethod
    def extract_card_params(cls, customer):
        """
        Extract the credit card info from a payment customer object.

        :param customer: Payment customer
        :type customer: Payment customer
        :return: dict
        """
        card_data = customer.sources.data[0]
        exp_date = datetime.date(card_data.exp_year, card_data.exp_month, 1)

        card = {
            'brand': card_data.brand,
            'last4': card_data.last4,
            'exp_date': exp_date,
            'is_expiring': CreditCard.is_expiring_soon(exp_date=exp_date)
        }

        return card
예제 #8
0
파일: Users.py 프로젝트: Ajay1290/booksite
class Users(db.Model,ResourceMixin, UserMixin):    
    
    ROLE = OrderedDict([
        ('member', 'Member'),
        ('admin', 'Admin')
    ])    

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

    # Authentication.
    role = db.Column(db.Enum(*ROLE, name='role_types', native_enum=False), index=True, nullable=False, server_default='member')
    active = db.Column('is_active', db.Boolean(), nullable=False, server_default='1')

    # Meta Info
    username = db.Column(db.String(24), unique=True, index=True)
    phone = db.Column(db.String(13))
    email = db.Column(db.String(255), unique=True, nullable=False, server_default='')
    password = db.Column(db.String(128), nullable=False, server_default='')
    
    pg_views = db.Column(db.Integer, default=0)

    # Activity tracking.
    sign_in_count = db.Column(db.Integer, nullable=False, default=0)
    current_sign_in_on = db.Column(AwareDateTime())
    current_sign_in_ip = db.Column(db.String(45))
    last_sign_in_on = db.Column(AwareDateTime())
    last_sign_in_ip = db.Column(db.String(45))

    # Additional settings.
    locale = db.Column(db.String(5), nullable=False, server_default='en')    

    # RelationShips
    image = db.relationship('ProfileImage', backref='user', lazy=True)    
    books = db.relationship('Books', backref='user', lazy=True)
    comments = db.relationship('Comments', backref='user', lazy='dynamic')
    downloads = db.relationship('Downloads', backref='user', lazy=True)

    def __init__(self, **kwargs):
        # Call Flask-SQLAlchemy's constructor.
        super(Users, self).__init__(**kwargs)
        self.password = Users.encrypt_password(kwargs.get('password', ''))        

    @classmethod
    def find_by_identity(cls, identity):        
        return Users.query.filter(
          (Users.email == identity) | (Users.username == identity)).first()

    @classmethod
    def encrypt_password(cls, plaintext_password):        
        if plaintext_password:
            return generate_password_hash(plaintext_password)

        return None

    @classmethod
    def deserialize_token(cls, token):        
        private_key = TimedJSONWebSignatureSerializer(
            current_app.config['SECRET_KEY'])
        try:
            decoded_payload = private_key.loads(token)

            return Users.find_by_identity(decoded_payload.get('user_email'))
        except Exception:
            return None


    @classmethod
    def search(cls, query):
        if not query:
            return ''

        search_query = '%{0}%'.format(query)
        search_chain = (Users.email.ilike(search_query),
                        Users.username.ilike(search_query))

        return or_(*search_chain)

    @classmethod
    def initialize_password_reset(cls, identity):
        """
        Generate a token to reset the password for a specific user.

        :param identity: User e-mail address or username
        :type identity: str
        :return: User instance
        """
        u = Users.find_by_identity(identity)
        reset_token = u.serialize_token()

        # This prevents circular imports.
        from manager.apps.users.tasks import deliver_password_reset_email
        deliver_password_reset_email.delay(u.id, reset_token)

        return u

    @classmethod
    def is_last_admin(cls, user, new_role, new_active):
        is_changing_roles = user.role == 'admin' and new_role != 'admin'
        is_changing_active = user.active is True and new_active is None

        if is_changing_roles or is_changing_active:
            admin_count = Users.query.filter(Users.role == 'admin').count()
            active_count = Users.query.filter(Users.is_active is True).count()

            if admin_count == 1 or active_count == 1:
                return True

        return False

    def is_active(self, act):
        if act:
            self.active = act
            return self.active
        return self.active

    def get_auth_token(self):        
        private_key = current_app.config['SECRET_KEY']

        serializer = URLSafeTimedSerializer(private_key)
        data = [str(self.id), md5(self.password.encode('utf-8')).hexdigest()]

        return serializer.dumps(data)

    def authenticated(self, with_password=True, password=''):        
        if with_password:
            return check_password_hash(self.password, password)

        return True

    def serialize_token(self, expiration=3600):        
        private_key = current_app.config['SECRET_KEY']

        serializer = TimedJSONWebSignatureSerializer(private_key, expiration)
        return serializer.dumps({'user_email': self.email}).decode('utf-8')

    def update_activity_tracking(self, ip_address):        
        self.sign_in_count += 1

        self.last_sign_in_on = self.current_sign_in_on
        self.last_sign_in_ip = self.current_sign_in_ip

        self.current_sign_in_on = tzware_datetime()
        self.current_sign_in_ip = ip_address

        return self.save()
예제 #9
0
class Coupon(ResourceMixin, db.Model):
    DURATION = OrderedDict([
        ('forever', 'Forever'),
        ('once', 'Once'),
        ('repeating', 'Repeating')
    ])

    __tablename__ = 'coupons'
    id = db.Column(db.Integer, primary_key=True)

    # Coupon details.
    code = db.Column(db.String(128), index=True, unique=True)
    duration = db.Column(db.Enum(*DURATION, name='duration_types'), index=True, nullable=False, server_default='forever')
    amount_off = db.Column(db.Integer())
    percent_off = db.Column(db.Integer())
    currency = db.Column(db.String(8))
    duration_in_months = db.Column(db.Integer())
    max_redemptions = db.Column(db.Integer(), index=True)
    redeem_by = db.Column(AwareDateTime(), index=True)
    times_redeemed = db.Column(db.Integer(), index=True, nullable=False, default=0)
    valid = db.Column(db.Boolean(), nullable=False, server_default='1')

    def __init__(self, **kwargs):
        if self.code:
            self.code = self.code.upper()
        else:
            self.code = Coupon.random_coupon_code()

        # Call Flask-SQLAlchemy's constructor.
        super(Coupon, self).__init__(**kwargs)

    @hybrid_property
    def redeemable(self):
        """
        Return coupons that are still redeemable. Coupons will become invalid
        once they run out on save. We want to explicitly do a date check to
        avoid having to hit Stripe's API to get back potentially valid codes.

        :return: SQLAlchemy query object
        """
        is_redeemable = or_(self.redeem_by.is_(None), self.redeem_by >= datetime.datetime.now(pytz.utc))

        return and_(self.valid, is_redeemable)

    @classmethod
    def search(cls, query):
        """
        Search a resource by 1 or more fields.

        :param query: Search query
        :type query: str
        :return: SQLAlchemy filter
        """
        if not query:
            return ''

        search_query = '%{0}%'.format(query)

        return or_(Coupon.code.ilike(search_query))

    @classmethod
    def random_coupon_code(cls):
        """
        Create a human readable random coupon code.

        :return: str
        """
        charset = string.digits + string.ascii_uppercase
        charset = charset.replace('B', '').replace('I', '')
        charset = charset.replace('O', '').replace('S', '')
        charset = charset.replace('0', '').replace('1', '')

        random_chars = ''.join(choice(charset) for _ in range(14))

        coupon_code = '{0}-{1}-{2}'.format(random_chars[0:4], random_chars[5:9], random_chars[10:14])

        return coupon_code

    @classmethod
    def expire_old_coupons(cls, compare_datetime=None):
        """
        Invalidate coupons that are past their redeem date.

        :param compare_datetime: Time to compare at
        :type compare_datetime: date
        :return: The result of updating the records
        """
        if compare_datetime is None:
            compare_datetime = datetime.datetime.now(pytz.utc)

        Coupon.query.filter(Coupon.redeem_by <= compare_datetime) \
            .update({Coupon.valid: not Coupon.valid})

        return db.session.commit()

    @classmethod
    def create(cls, params):
        """
        Return whether or not the coupon was created successfully.

        :return: bool
        """
        payment_params = params

        payment_params['code'] = payment_params['code'].upper()

        if payment_params.get('amount_off'):
            payment_params['amount_off'] = dollars_to_cents(payment_params['amount_off'])
        from manager.apps.billing.gateways.stripecom import Coupon as PaymentCoupon
        PaymentCoupon.create(**payment_params)

        if 'id' in payment_params:
            payment_params['code'] = payment_params['id']
            del payment_params['id']

        if 'redeem_by' in payment_params:
            if payment_params.get('redeem_by') is not None:
                params['redeem_by'] = payment_params.get('redeem_by').replace(
                    tzinfo=pytz.UTC)

        coupon = Coupon(**payment_params)

        db.session.add(coupon)
        db.session.commit()

        return True

    @classmethod
    def bulk_delete(cls, ids):
        """
        Override the general bulk_delete method because we need to delete them
        one at a time while also deleting them on Stripe.

        :param ids: List of ids to be deleted
        :type ids: list
        :return: int
        """
        delete_count = 0

        for id in ids:
            coupon = Coupon.query.get(id)

            if coupon is None:
                continue
            from manager.apps.billing.gateways.stripecom import Coupon as PaymentCoupon
            # Delete on Stripe.
            stripe_response = PaymentCoupon.delete(coupon.code)

            # If successful, delete it locally.
            if stripe_response.get('deleted'):
                coupon.delete()
                delete_count += 1

        return delete_count

    @classmethod
    def find_by_code(cls, code):
        """
        Find a coupon by its code.

        :param code: Coupon code to find
        :type code: str
        :return: Coupon instance
        """
        formatted_code = code.upper()
        coupon = Coupon.query.filter(Coupon.redeemable,
                                     Coupon.code == formatted_code).first()

        return coupon

    def redeem(self):
        """
        Update the redeem stats for this coupon.

        :return: Result of saving the record
        """
        self.times_redeemed += 1

        if self.max_redemptions:
            if self.times_redeemed >= self.max_redemptions:
                self.valid = False

        return db.session.commit()

    def to_json(self):
        """
        Return JSON fields to represent a coupon.

        :return: dict
        """
        params = {
            'duration': self.duration,
            'duration_in_months': self.duration_in_months,
        }

        if self.amount_off:
            params['amount_off'] = cents_to_dollars(self.amount_off)

        if self.percent_off:
            params['percent_off'] = self.percent_off,

        return params
예제 #10
0
class Employee(UserMixin, ResourceMixin, db.Model):
    ROLE = OrderedDict([('engineer', 'Engineer'), ('manager', 'Manager'),
                        ('admin', 'Admin')])

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

    # Authentication.
    role = db.Column(db.Enum(*ROLE, name='role_types', native_enum=False),
                     nullable=False,
                     server_default='engineer')
    active = db.Column('is_active',
                       db.Boolean(),
                       nullable=False,
                       server_default='1')
    name = db.Column(db.String(24), unique=True)
    phone = db.Column(db.String(13))
    email = db.Column(db.String(255),
                      unique=True,
                      nullable=False,
                      server_default='')
    password = db.Column(db.String(128), nullable=False, server_default='')

    # Activity tracking.
    sign_in_count = db.Column(db.Integer, nullable=False, default=0)
    current_sign_in_on = db.Column(AwareDateTime())
    current_sign_in_ip = db.Column(db.String(45))
    last_sign_in_on = db.Column(AwareDateTime())
    last_sign_in_ip = db.Column(db.String(45))

    def __init__(self, **kwargs):
        super(Employee, self).__init__(**kwargs)
        self.password = Employee.encrypt_password(kwargs.get('password', ''))

    @classmethod
    def find_by_identity(cls, identity):
        return User.query.filter((User.email == identity)
                                 | (User.username == identity)).first()

    @classmethod
    def encrypt_password(cls, plaintext_password):
        if plaintext_password:
            return generate_password_hash(plaintext_password)

        return None

    def authenticated(self, with_password=True, password=''):
        if with_password:
            return check_password_hash(self.password, password)

        return True

    def is_active(self, act=False):
        if act:
            self.active = True
            return self.active
        return self.active

    def update_activity_tracking(self, ip_address):
        self.sign_in_count += 1
        self.active = True

        self.last_sign_in_on = self.current_sign_in_on
        self.last_sign_in_ip = self.current_sign_in_ip

        self.current_sign_in_on = datetime.datetime.now(pytz.utc)
        self.current_sign_in_ip = ip_address

        return self.save()