Advertisement
Guest User

Untitled

a guest
Jul 31st, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.33 KB | None | 0 0
  1. # encoding: utf-8
  2. """
  3. User database models
  4. --------------------
  5. """
  6. import enum
  7.  
  8. from sqlalchemy_utils import types as column_types, Timestamp
  9.  
  10. from app.extensions import db
  11. from app.modules.subscriptions.models import Customer
  12.  
  13.  
  14. def _get_is_static_role_property(role_name, static_role):
  15.     """
  16.    A helper function that aims to provide a property getter and setter
  17.    for static roles.
  18.  
  19.    Args:
  20.        role_name (str)
  21.        static_role (int) - a bit mask for a specific role
  22.  
  23.    Returns:
  24.        property_method (property) - preconfigured getter and setter property
  25.        for accessing role.
  26.    """
  27.     @property
  28.     def _is_static_role_property(self):
  29.         return self.has_static_role(static_role)
  30.  
  31.     @_is_static_role_property.setter
  32.     def _is_static_role_property(self, value):
  33.         if value:
  34.             self.set_static_role(static_role)
  35.         else:
  36.             self.unset_static_role(static_role)
  37.  
  38.     _is_static_role_property.fget.__name__ = role_name
  39.     return _is_static_role_property
  40.  
  41.  
  42. class User(db.Model, Timestamp):
  43.     """
  44.    User database model.
  45.    """
  46.  
  47.     __tablename__ = 'user'
  48.  
  49.     id = db.Column(db.Integer, primary_key=True) # pylint: disable=invalid-name
  50.     username = db.Column(db.String(length=80), unique=True, nullable=False)
  51.     password = db.Column(
  52.         column_types.PasswordType(
  53.             max_length=128,
  54.             schemes=('bcrypt', )
  55.         ),
  56.         nullable=False
  57.     )
  58.     email = db.Column(db.String(length=120), unique=True, nullable=False)
  59.  
  60.     first_name = db.Column(db.String(length=30), default='', nullable=False)
  61.     middle_name = db.Column(db.String(length=30), default='', nullable=False)
  62.     last_name = db.Column(db.String(length=30), default='', nullable=False)
  63.  
  64.     stripe_customer = db.relationship(
  65.         "Customer", backref=db.backref("user", cascade='delete, delete-orphan', lazy="joined", single_parent=True))
  66.  
  67.     class StaticRoles(enum.Enum):
  68.         # pylint: disable=missing-docstring,unsubscriptable-object
  69.         INTERNAL = (0x8000, "Internal")
  70.         ADMIN = (0x4000, "Admin")
  71.         REGULAR_USER = (0x2000, "Regular User")
  72.         ACTIVE = (0x1000, "Active Account")
  73.  
  74.         @property
  75.         def mask(self):
  76.             return self.value[0]
  77.  
  78.         @property
  79.         def title(self):
  80.             return self.value[1]
  81.  
  82.     static_roles = db.Column(db.Integer, default=0, nullable=False)
  83.  
  84.     is_internal = _get_is_static_role_property('is_internal', StaticRoles.INTERNAL)
  85.     is_admin = _get_is_static_role_property('is_admin', StaticRoles.ADMIN)
  86.     is_regular_user = _get_is_static_role_property('is_regular_user', StaticRoles.REGULAR_USER)
  87.     is_active = _get_is_static_role_property('is_active', StaticRoles.ACTIVE)
  88.  
  89.     def __repr__(self):
  90.         return (
  91.             "<{class_name}("
  92.             "id={self.id}, "
  93.             "username=\"{self.username}\", "
  94.             "email=\"{self.email}\", "
  95.             "is_internal={self.is_internal}, "
  96.             "is_admin={self.is_admin}, "
  97.             "is_regular_user={self.is_regular_user}, "
  98.             "is_active={self.is_active}, "
  99.             ")>".format(
  100.                 class_name=self.__class__.__name__,
  101.                 self=self
  102.             )
  103.         )
  104.  
  105.     def has_static_role(self, role):
  106.         return (self.static_roles & role.mask) != 0
  107.  
  108.     def set_static_role(self, role):
  109.         if self.has_static_role(role):
  110.             return
  111.         self.static_roles |= role.mask
  112.  
  113.     def unset_static_role(self, role):
  114.         if not self.has_static_role(role):
  115.             return
  116.         self.static_roles ^= role.mask
  117.  
  118.     def check_owner(self, user):
  119.         return self == user
  120.  
  121.     @property
  122.     def is_authenticated(self):
  123.         return True
  124.  
  125.     @property
  126.     def is_anonymous(self):
  127.         return False
  128.  
  129.     @classmethod
  130.     def find_with_password(cls, username, password):
  131.         """
  132.        Args:
  133.            username (str)
  134.            password (str) - plain-text password
  135.  
  136.        Returns:
  137.            user (User) - if there is a user with a specified username and
  138.            password, None otherwise.
  139.        """
  140.         user = cls.query.filter_by(username=username).first()
  141.         if not user:
  142.             return None
  143.         if user.password == password:
  144.             return user
  145.         return None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement