Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def save(self, commit=True):
"""Save the record."""
db.session.add(self)
if commit:
db.session.commit()
return self
return commit and self.save() or self
def save(self, commit=True):
"""Save the record."""
db.session.add(self)
if commit:
db.session.commit()
return self
def delete(self, commit=True):
"""Remove the record from the database."""
db.session.delete(self)
return commit and db.session.commit()
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)
"""Package stats tables."""
from pypistats.database import Column
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 "
def register_extensions(app):
"""Register Flask extensions."""
db.init_app(app)
github.init_app(app)
migrate.init_app(app, db)
def before_request():
"""Execute before requests."""
g.user = None
if "user_id" in session:
g.user = User.query.get(session["user_id"])
if "db" not in g:
g.db = db
"""Database classes and models."""
from pypistats.extensions import db
Column = db.Column
basestring = (str, bytes)
class CRUDMixin(object):
"""Mixin that adds convenience methods for CRUD operations."""
@classmethod
def create(cls, **kwargs):
"""Create a new record and save it the database."""
instance = cls(**kwargs)
return instance.save()
def update(self, commit=True, **kwargs):
"""Update specific fields of a record."""
for attr, value in kwargs.items():
setattr(self, attr, value)