Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
class Model(CRUDMixin, db.Model):
"""Base model class that includes CRUD convenience methods."""
__abstract__ = True
class SurrogatePK(object):
"""A mixin that adds a surrogate integer "primary key" column.
Adds a surrogate integer "primary key" column named ``id`` to any
declarative-mapped class.
"""
__table_args__ = {"extend_existing": True}
id = Column(db.Integer, primary_key=True)
@classmethod
def get_by_id(cls, record_id):
"""Get record by ID."""
if any(
(isinstance(record_id, basestring) and record_id.isdigit(),
isinstance(record_id, (int, float))),
):
return cls.query.get(int(record_id))
return None
from pypistats.database import Column
from pypistats.database import Model
from pypistats.database import SurrogatePK
from pypistats.extensions import db
MAX_FAVORITES = 20
class User(UserMixin, SurrogatePK, Model):
"""A user of the app."""
__tablename__ = 'users'
uid = Column(db.Integer(), unique=True)
username = Column(db.String(39), nullable=False)
avatar_url = Column(db.String(256))
token = Column(db.String(256))
created_at = \
Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
active = Column(db.Boolean(), default=False)
is_admin = Column(db.Boolean(), default=False)
favorites = Column(ARRAY(db.String(128), dimensions=1))
def __init__(self, token, **kwargs):
"""Create instance."""
db.Model.__init__(self, token=token, **kwargs)
def __repr__(self):
"""Represent instance as a unique string."""
return f""
f"{str(self.date)} - {str(self.package)} - {str(self.category)}"
)
class PythonMajorDownloadCount(Model):
"""Download counts by python major version."""
__tablename__ = "python_major"
date = Column(db.Date, primary_key=True, nullable=False)
package = Column(
db.String(128), primary_key=True, nullable=False, index=True
)
# python_major version, 2 or 3 (or null)
category = Column(db.String(4), primary_key=True, nullable=True)
downloads = Column(db.Integer(), nullable=False)
def __repr__(self):
return "
from pypistats.database import Model
from pypistats.extensions import db
class OverallDownloadCount(Model):
"""Overall download counts."""
__tablename__ = "overall"
date = Column(db.Date, primary_key=True, nullable=False)
package = Column(
db.String(128), primary_key=True, nullable=False, index=True
)
# with_mirrors or without_mirrors
category = Column(db.String(16), primary_key=True, nullable=False)
downloads = Column(db.Integer(), nullable=False)
def __repr__(self):
return "