Jul 13 - Optimize code
This commit is contained in:
+9
-8
@@ -10,6 +10,7 @@ Category sponsor FK wired to categories.sponsor_id (set separately).
|
||||
PromotedKeyword: pinned listing for a search keyword (promoted search).
|
||||
"""
|
||||
from datetime import datetime
|
||||
from app.utils.time import utcnow
|
||||
from app.extensions import db
|
||||
|
||||
|
||||
@@ -37,9 +38,9 @@ class Ad(db.Model):
|
||||
clicks = db.Column(db.Integer, nullable=False, default=0)
|
||||
is_active = db.Column(db.Boolean, nullable=False, default=True)
|
||||
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
|
||||
updated_at = db.Column(db.DateTime, default=datetime.utcnow,
|
||||
onupdate=datetime.utcnow, nullable=False)
|
||||
created_at = db.Column(db.DateTime, default=utcnow, nullable=False)
|
||||
updated_at = db.Column(db.DateTime, default=utcnow,
|
||||
onupdate=utcnow, nullable=False)
|
||||
|
||||
@property
|
||||
def ctr(self):
|
||||
@@ -49,7 +50,7 @@ class Ad(db.Model):
|
||||
|
||||
@property
|
||||
def is_running(self):
|
||||
now = datetime.utcnow()
|
||||
now = utcnow()
|
||||
return self.is_active and self.starts_at <= now <= self.ends_at
|
||||
|
||||
def __repr__(self):
|
||||
@@ -75,14 +76,14 @@ class Sponsor(db.Model):
|
||||
starts_at = db.Column(db.DateTime, nullable=False)
|
||||
ends_at = db.Column(db.DateTime, nullable=False)
|
||||
is_active = db.Column(db.Boolean, nullable=False, default=True)
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
|
||||
created_at = db.Column(db.DateTime, default=utcnow, nullable=False)
|
||||
|
||||
category = db.relationship("Category",
|
||||
backref=db.backref("sponsors", lazy="selectin"))
|
||||
|
||||
@property
|
||||
def is_running(self):
|
||||
now = datetime.utcnow()
|
||||
now = utcnow()
|
||||
return self.is_active and self.starts_at <= now <= self.ends_at
|
||||
|
||||
def __repr__(self):
|
||||
@@ -103,7 +104,7 @@ class PromotedKeyword(db.Model):
|
||||
db.ForeignKey("listings.id"), nullable=False)
|
||||
priority = db.Column(db.Integer, nullable=False, default=0)
|
||||
expires_at = db.Column(db.DateTime, nullable=False, index=True)
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
|
||||
created_at = db.Column(db.DateTime, default=utcnow, nullable=False)
|
||||
|
||||
listing = db.relationship("Listing",
|
||||
backref=db.backref("promoted_keywords",
|
||||
@@ -111,7 +112,7 @@ class PromotedKeyword(db.Model):
|
||||
|
||||
@property
|
||||
def is_active(self):
|
||||
return self.expires_at > datetime.utcnow()
|
||||
return self.expires_at > utcnow()
|
||||
|
||||
def __repr__(self):
|
||||
return f"<PromotedKeyword '{self.keyword}' L{self.listing_id}>"
|
||||
|
||||
Reference in New Issue
Block a user