def register_user(cls, email, password): user = Database.find_one('users', {'email': email}) if user is not None: raise UserRegisterError("User already exists!") else: cls(email, Utils.hash_password(password)).save_to_db() return True
def get_by_url(cls, url): i = 0 while i < len(url): store_json = Database.find_one('stores', {'url_prefix': {'$regex': '^{}'.format(url[:i+1])}}) if store_json: return cls(**store_json) i+=1 pass
def find_outdated_alerts(cls, timeout=ALERT_TIMEOUT): update_limit = datetime.utcnow() - timedelta(minutes=timeout) return [ cls(**alert) for alert in Database.find( 'alerts', {'last_checked': { '$lte': update_limit }}) ]
def is_login_valid(cls, email, password): user = Database.find_one('users', {'email': email}) if user is None: raise UserNotExistsError("User does not exist!") else: if not Utils.check_hashed_password(password, user['password']): raise IncorrectPasswordError("Password not match!") return True
from src.commons.database import Database from src.models.alerts.alert import Alert Database.initialize() outdated_alerts = Alert.find_outdated_alerts() print(outdated_alerts) for alert in outdated_alerts: alert.load_item_price() alert.send_email_if_price_reached()
def save_to_db(self): Database.update('alerts', {'_id': self._id}, self.json())
def get_by_id(cls, _id): return cls(**Database.find_one('items', {'_id': _id}))
def save_to_db(self): Database.insert('items', self.json())
def init_db(): Database.initialize()
def get_by_url_prefix(cls, prefix: str): return cls(**Database.find_one('stores', {'url_prefix': {'$regex': '^{}'.format(prefix)}}))
def get_by_name(cls, name): return cls(**Database.find_one('stores', {'name': name}))
def save_to_db(self): Database.insert('stores', self.json())