class UserUpdateEmailRequest(db.Model): MINUTES_EXPIRY = 30 id = db.Column(db.Integer, primary_key=True) token = db.Column(db.String(255), unique=True, nullable=False) email = db.Column(db.String(255)) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) user = db.relationship('User', backref=db.backref('update_email_requests', lazy='dynamic')) expiry = db.Column(db.DateTime()) def __init__(self, user, email): self.token = secrets.token_urlsafe(8) self.user = user self.email = email self.expiry = datetime.datetime.now() + datetime.timedelta(self.MINUTES_EXPIRY) @classmethod def from_email(cls, session, email): return session.query(cls).filter(cls.email == email).first() @classmethod def from_token(cls, session, token): return session.query(cls).filter(cls.token == token).first() def __str__(self): return self.email
class User(db.Model): id = db.Column(db.Integer, primary_key=True) mac_address = db.Column(db.String(500), unique=True, nullable=True) username = db.Column(db.String(500), unique=True, nullable=True) password = db.Column(db.String(1000), nullable=True) is_admin = db.Column(db.Boolean, default=False) def __init__(self, username, password, mac_address, is_admin=False): #self.mac_address = str(uuid4()) self.mac_address = mac_address self.username = username self.password = bcrypt.generate_password_hash(password) self.is_admin = is_admin def check_password(self, password): return bcrypt.check_password_hash(self.password, password) def as_dict(self): return { 'id': self.id, 'username': self.username, 'password': self.password, 'mac_address': self.mac_address, 'is_admin': self.is_admin }
class AMDevice(db.Model): __tablename__ = 'amdevice' id = db.Column(db.Integer(), primary_key=True) wallet_id = db.Column(db.Integer, db.ForeignKey('amwallet.id'), nullable=False) wallet = db.relationship('AMWallet', backref=db.backref('devices', lazy='joined')) date = db.Column(db.DateTime()) app_version = db.Column(db.String()) os = db.Column(db.String()) os_version = db.Column(db.String()) manufacturer = db.Column(db.String()) brand = db.Column(db.String()) device_id = db.Column(db.String()) def __init__(self, wallet, app_version, os, os_version, manufacturer, brand, device_id): self.wallet = wallet self.date = datetime.datetime.now() self.app_version = app_version self.os = os self.os_version = os_version self.manufacturer = manufacturer self.brand = brand self.device_id = device_id def __repr__(self): return "<AMDevice %r %r>" % (self.brand, self.device_id)
class WavesTxSig(db.Model): id = db.Column(db.Integer, primary_key=True) waves_tx_id = db.Column(db.Integer, db.ForeignKey('waves_tx.id'), nullable=False) waves_tx = db.relationship('WavesTx', backref=db.backref('signatures', lazy='dynamic')) signer_index = db.Column(db.Integer, nullable=False) value = db.Column(db.String, unique=False) def __init__(self, waves_tx, signer_index, value): self.waves_tx = waves_tx self.signer_index = signer_index self.value = value
class Role(db.Model, RoleMixin): id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(80), unique=True) description = db.Column(db.String(255)) @classmethod def from_name(cls, session, name): return session.query(cls).filter(cls.name == name).first() def __str__(self): return self.name
class BronzeData(db.Model): __tablename__ = 'bronze_data' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False, unique=True) kyc_validated = db.Column(db.Boolean()) def __init__(self, user, kyc_validated): self.user_id = user.id self.kyc_validated = kyc_validated
class Setting(db.Model): __tablename__ = 'settings' id = db.Column(db.Integer, primary_key=True) key = db.Column(db.String, nullable=False, unique=True) value = db.Column(db.String, unique=False) def __init__(self, key, value): self.key = key self.value = value def __repr__(self): return '<Setting %r %r>' % (self.key, self.value)
class Block(db.Model): __tablename__ = 'blocks' id = db.Column(db.Integer, primary_key=True) date = db.Column(db.Float, nullable=False, unique=False) num = db.Column(db.Integer, nullable=False) hash = db.Column(db.String, nullable=False, unique=True) reorged = db.Column(db.Boolean, nullable=False, default=False) transactions = db.relationship('Transaction') def __init__(self, block_date, block_num, block_hash): self.date = block_date self.num = block_num self.hash = block_hash self.reorged = False def set_reorged(self, session): for tx in self.transactions: session.delete(tx) self.reorged = True session.add(self) @classmethod def last_block(cls, session): return session.query(cls).filter(cls.reorged == False).order_by( cls.id.desc()).first() @classmethod def from_number(cls, session, num): return session.query(cls).filter((cls.num == num) & (cls.reorged == False)).first() @classmethod def from_hash(cls, session, hash): return session.query(cls).filter(cls.hash == hash).first() @classmethod def tx_block_num(cls, session, tx_block_id): if tx_block_id: block = session.query(cls).filter(cls.id == tx_block_id).first() if block: return block.num return -1 @classmethod def tx_confirmations(cls, session, current_block_num, tx_block_id): block_num = cls.tx_block_num(session, tx_block_id) if block_num != -1: return current_block_num - block_num return 0 def __repr__(self): return '<Block %r %r>' % (self.num, self.hash)
class Role(db.Model): __tablename__ = 'roles' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) default = db.Column(db.Boolean, default=False, index=True) permissions = db.Column(db.Integer) users = db.relationship('User', backref='role', lazy='dynamic') def __init__(self, **kwargs): super(Role, self).__init__(**kwargs) if self.permissions is None: self.permissions = 0 @staticmethod def insert_roles(): roles = { 'User': [Permission.FOLLOW, Permission.COMMENT, Permission.WRITE], 'Moderator': [Permission.FOLLOW, Permission.COMMENT, Permission.WRITE, Permission.MODERATE], 'Administrator': [Permission.FOLLOW, Permission.COMMENT, Permission.WRITE, Permission.MODERATE, Permission.ADMIN], } default_role = 'User' for r in roles: role = Role.query.filter_by(name=r).first() if role is None: role = Role(name=r) role.reset_permissions() for permission in roles[r]: role.add_permission(permission) role.default = (role.name == default_role) db.session.add(role) db.session.commit() def add_permission(self, permission): if not self.has_permission(permission): self.permissions += permission def remove_permission(self, permission): if self.has_permission(permission): self.permissions -= permission def reset_permissions(self): self.permissions = 0 def has_permission(self, permission): return self.permissions & permission == permission def __repr__(self): return '<Role %r>' % self.name
class ClaimCode(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) user = db.relationship('User', backref=db.backref('claimcodes', lazy='dynamic')) date = db.Column(db.DateTime()) token = db.Column(db.String(255), unique=True, nullable=False) secret = db.Column(db.String(255)) amount = db.Column(db.Integer) address = db.Column(db.String(255)) status = db.Column(db.String(255)) def __init__(self, user, token, amount): self.user = user self.date = datetime.datetime.now() self.token = token self.secret = None self.amount = amount self.address = None self.status = "created" @classmethod def count(cls, session): return session.query(cls).count() @classmethod def from_token(cls, session, token): return session.query(cls).filter(cls.token == token).first() def __repr__(self): return "<ClaimCode %r>" % (self.token) def to_json(self): schema = ClaimCodeSchema() return schema.dump(self).data
class Role(db.Model, RoleMixin): ROLE_ADMIN = 'admin' ROLE_PROPOSER = 'proposer' ROLE_AUTHORIZER = 'authorizer' ROLE_REFERRAL_CLAIMER = 'referral_claimer' id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(80), unique=True) description = db.Column(db.String(255)) @classmethod def from_name(cls, session, name): return session.query(cls).filter(cls.name == name).first() def __str__(self): return f'{self.name}'
class Category(db.Model): CATEGORY_MARKETING = 'marketing' CATEGORY_MISC = 'misc' CATEGORY_TESTING = 'testing' CATEGORY_REFERRAL = 'referral' id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(80), unique=True) description = db.Column(db.String(255)) @classmethod def from_name(cls, session, name): return session.query(cls).filter(cls.name == name).first() def __str__(self): return f'{self.name}'
class Comment(db.Model): __tablename__ = "comments" id = db.Column(db.Integer, primary_key=True) body = db.Column(db.Text) body_html = db.Column(db.Text) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) disabled = db.Column(db.Boolean) author_id = db.Column(db.Integer, db.ForeignKey('users.id')) post_id = db.Column(db.Integer, db.ForeignKey('posts.id')) @staticmethod def on_changed_body(target, value, old_value, initiator): allowed_tags = ['a', 'abbr', 'acronym', 'b', 'code', 'em', 'i', 'strong'] target.body_html = bleach.linkify( bleach.clean(markdown(value, output_format='html'), tags=allowed_tags, strip=True)) def to_json(self): json_comment = { 'url': url_for('api.get_comment', comment_id=self.id), 'post_url': url_for('api.get_post', post_id=self.post_id), 'body': self.body, 'body_html': self.body_html, 'timestamp': self.timestamp, 'author_url': url_for('api.get_user', user_id=self.author_id), } return json_comment @staticmethod def from_json(json_comment): body = json_comment.get('body') if body is None or body == '': raise ValidationError('comment does not have a body') return Comment(body=body)
class ApiKeyRequest(db.Model): MINUTES_EXPIRY = 30 id = db.Column(db.Integer, primary_key=True) token = db.Column(db.String(255), unique=True, nullable=False) secret = db.Column(db.String(255), unique=True, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) user = db.relationship('User', backref=db.backref('api_key_requests', lazy='dynamic')) device_name = db.Column(db.String(255)) expiry = db.Column(db.DateTime()) created_api_key_id = db.Column(db.Integer, db.ForeignKey('api_key.id')) created_api_key = db.relationship('ApiKey') def __init__(self, user, device_name): self.token = secrets.token_urlsafe(8) self.secret = secrets.token_urlsafe(16) self.user = user self.device_name = device_name self.expiry = datetime.datetime.now() + datetime.timedelta(self.MINUTES_EXPIRY) @classmethod def from_token(cls, session, token): return session.query(cls).filter(cls.token == token).first() def __str__(self): return self.token
class ApiKey(db.Model): id = db.Column(db.Integer, primary_key=True) token = db.Column(db.String(255), unique=True, nullable=False) secret = db.Column(db.String(255), nullable=False) nonce = db.Column(db.BigInteger, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) user = db.relationship('User', backref=db.backref('api_keys', lazy='dynamic')) device_name = db.Column(db.String(255)) expiry = db.Column(db.DateTime()) permissions = db.relationship('Permission', secondary=permissions_api_keys, backref=db.backref('api_keys', lazy='dynamic')) def __init__(self, user, device_name): self.user_id = user.id self.token = secrets.token_urlsafe(8) self.secret = secrets.token_urlsafe(16) self.nonce = 0 self.device_name = device_name self.expiry = datetime.datetime.now() + datetime.timedelta(30) def has_permission(self, permission_name): perm = Permission.from_name(db.session, permission_name) if perm: return perm in self.permissions # pylint: disable=unsupported-membership-test return False @classmethod def from_token(cls, session, token): return session.query(cls).filter(cls.token == token).first()
class Permission(db.Model): PERMISSION_RECIEVE = 'receive' PERMISSION_BALANCE = 'balance' PERMISSION_HISTORY = 'history' PERMISSION_TRANSFER = 'transfer' PERMISSION_ISSUE = 'issue' PERMS_ALL = [PERMISSION_BALANCE, PERMISSION_HISTORY, PERMISSION_ISSUE, PERMISSION_RECIEVE, PERMISSION_TRANSFER] id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(80), unique=True) description = db.Column(db.String(255)) @classmethod def from_name(cls, session, name): return session.query(cls).filter(cls.name == name).first() def __str__(self): return f'{self.name}'
class Topic(db.Model): __tablename__ = 'topics' id = db.Column(db.Integer, primary_key=True) topic = db.Column(db.String, nullable=False, unique=True) def __init__(self, topic): self.topic = topic @classmethod def topic_list(cls, session): return [row.topic for row in session.query(cls.topic)] @classmethod def from_name(cls, session, name): return session.query(cls).filter(cls.topic == name).first() def __repr__(self): return '<Topic %r %r>' % self.topic
class WavesTx(db.Model): id = db.Column(db.Integer, primary_key=True) date = db.Column(db.Integer, nullable=False) txid = db.Column(db.String, nullable=False, unique=True) type = db.Column(db.String, nullable=False) state = db.Column(db.String, nullable=False) amount = db.Column(db.Integer, nullable=False) json_data_signed = db.Column(db.Boolean, nullable=False) json_data = db.Column(db.String, nullable=False) def __init__(self, txid, type_, state, amount, json_data_signed, json_data): self.date = time.time() self.type = type_ self.state = state self.txid = txid self.amount = amount self.json_data_signed = json_data_signed self.json_data = json_data @classmethod def from_txid(cls, session, txid): return session.query(cls).filter(cls.txid == txid).first() @classmethod def expire_transactions(cls, session, above_age, from_state, to_state): now = time.time() txs = session.query(cls).filter(cls.date < now - above_age, cls.state == from_state).all() for tx in txs: tx.state = to_state tx.json_data = "" session.add(tx) return len(txs) @classmethod def count(cls, session): return session.query(cls).count() def __repr__(self): return '<WavesTx %r>' % (self.txid) def to_json(self): tx_schema = WavesTxSchema() return tx_schema.dump(self) def tx_with_sigs(self): tx = json.loads(self.json_data) if self.json_data_signed: return tx proofs = tx["proofs"] for sig in self.signatures: while sig.signer_index >= len(proofs): proofs.append('todo') proofs[sig.signer_index] = sig.value return tx
class Transaction(db.Model): __tablename__ = 'transactions' id = db.Column(db.Integer, primary_key=True) txid = db.Column(db.String, nullable=False, unique=True) sender = db.Column(db.String, nullable=False) recipient = db.Column(db.String, nullable=False) amount = db.Column(db.Integer, nullable=False) attachment = db.Column(db.String, nullable=True) invoice_id = db.Column(db.String, nullable=True) block_id = db.Column(db.Integer, db.ForeignKey('blocks.id')) block = db.relationship('Block') def __init__(self, txid, sender, recipient, amount, attachment, invoice_id, block_id): self.txid = txid self.sender = sender self.recipient = recipient self.amount = amount self.attachment = attachment self.invoice_id = invoice_id self.block_id = block_id @classmethod def from_txid(cls, session, txid): return session.query(cls).filter(cls.txid == txid).first() @classmethod def from_invoice_id(cls, session, invoice_id, start_date, end_date, offset, limit): query = session.query(cls) if invoice_id: query = query.filter(cls.invoice_id == invoice_id) if start_date != 0 or end_date != 0: query = query.join(Block) if start_date != 0: query = query.filter(Block.date >= start_date) if end_date != 0: query = query.filter(Block.date <= end_date) query = query.offset(offset).limit(limit) return query.all() @classmethod def count(cls, session): return session.query(cls).count() def __repr__(self): return '<Transaction %r>' % (self.txid) def to_json(self): self.block_num = self.block.num self.block_date = self.block.date tx_schema = TransactionSchema() return tx_schema.dump(self).data
class Proposal(db.Model): STATE_CREATED = "created" STATE_AUTHORIZED = "authorized" STATE_DECLINED = "declined" STATE_EXPIRED = "expired" HOURS_EXPIRY = 72 id = db.Column(db.Integer, primary_key=True) date = db.Column(db.DateTime(), nullable=False) proposer_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) proposer = db.relationship('User', foreign_keys=[proposer_id], backref=db.backref('proposals', lazy='dynamic')) reason = db.Column(db.String()) authorizer_id = db.Column(db.Integer, db.ForeignKey('user.id')) authorizer = db.relationship('User', foreign_keys=[authorizer_id], backref=db.backref('proposals_authorized', lazy='dynamic')) date_authorized = db.Column(db.DateTime()) date_expiry = db.Column(db.DateTime()) status = db.Column(db.String(255)) categories = db.relationship('Category', secondary=categories_proposals, backref=db.backref('proposals', lazy='dynamic')) def __init__(self, proposer, reason): self.generate_defaults() self.proposer = proposer self.reason = reason def generate_defaults(self): self.date = datetime.datetime.now() self.proposer = current_user self.authorizer = None self.date_authorized = None self.date_expiry = None self.status = self.STATE_CREATED @classmethod def count(cls, session): return session.query(cls).count() @classmethod def in_status(cls, session, status): return session.query(cls).filter(cls.status == status).all() def __repr__(self): return "<Proposal %r>" % (self.id)
class Payment(db.Model): STATE_CREATED = "created" STATE_SENT_CLAIM_LINK = "sent_claim_link" STATE_EXPIRED = "expired" STATE_SENT_FUNDS = "sent_funds" id = db.Column(db.Integer, primary_key=True) proposal_id = db.Column(db.Integer, db.ForeignKey('proposal.id'), nullable=False) proposal = db.relationship('Proposal', backref=db.backref('payments', lazy='dynamic')) token = db.Column(db.String(255), unique=True, nullable=False) mobile = db.Column(db.String(255)) email = db.Column(db.String(255)) wallet_address = db.Column(db.String(255)) message = db.Column(db.String()) amount = db.Column(db.Integer) status = db.Column(db.String(255)) txid = db.Column(db.String(255)) def __init__(self, proposal, mobile, email, wallet_address, message, amount): self.proposal = proposal self.token = generate_key(8) self.mobile = mobile self.email = email self.wallet_address = wallet_address self.message = message self.amount = amount self.status = self.STATE_CREATED self.txid = None @classmethod def count(cls, session): return session.query(cls).count() @classmethod def from_token(cls, session, token): return session.query(cls).filter(cls.token == token).first() def __repr__(self): return "<Payment %r>" % (self.token)
class DashboardHistory(db.Model): __tablename__ = "dashboard_history" id = db.Column(db.Integer, primary_key=True) date = db.Column(db.Integer, nullable=False) incomming_tx_count = db.Column(db.Integer, nullable=False) created_tx_count = db.Column(db.Integer, nullable=False) zap_balance = db.Column(db.Integer, nullable=False) master_waves_balance = db.Column(db.Integer, nullable=False) def __init__(self, incomming_tx_count, created_tx_count, zap_balance, master_waves_balance): self.date = time.time() self.incomming_tx_count = incomming_tx_count self.created_tx_count = created_tx_count self.zap_balance = zap_balance self.master_waves_balance = master_waves_balance @classmethod def last_entry(cls, session): return session.query(cls).order_by(cls.id.desc()).first() @classmethod def last_week(cls, session): now = time.time() week = 60 * 60 * 24 * 7 return session.query(cls).filter(cls.date > now - week).all()
class PayDbTransaction(db.Model): ACTION_ISSUE = "issue" ACTION_TRANSFER = "transfer" ACTION_DESTROY = "destroy" id = db.Column(db.Integer, primary_key=True) token = db.Column(db.String(255), unique=True, nullable=False) date = db.Column(db.DateTime()) action = db.Column(db.String(255), nullable=False) sender_token = db.Column(db.String(255), db.ForeignKey('user.token'), nullable=False) sender = db.relationship('User', foreign_keys=[sender_token], backref=db.backref('sent', lazy='dynamic')) recipient_token = db.Column(db.String(255), db.ForeignKey('user.token'), nullable=True) recipient = db.relationship('User', foreign_keys=[recipient_token], backref=db.backref('recieved', lazy='dynamic')) amount = db.Column(db.Integer()) attachment = db.Column(db.String(255)) def __init__(self, action, sender, recipient, amount, attachment): self.token = secrets.token_urlsafe(8) self.date = datetime.datetime.now() self.action = action self.sender = sender self.recipient = recipient self.amount = amount self.attachment = attachment @property def timestamp(self): if not self.date: return 0 return int(datetime.datetime.timestamp(self.date)) @classmethod def from_token(cls, session, token): return session.query(cls).filter(cls.token == token).first() @classmethod def related_to_user(cls, session, user, offset, limit): # pylint: disable=no-member return session.query(cls).filter(or_(cls.sender_token == user.token, cls.recipient_token == user.token)).order_by(cls.id.desc()).offset(offset).limit(limit) @classmethod def all(cls, session): return session.query(cls).all() def __str__(self): return self.token def to_json(self): tx_schema = PayDbTransactionSchema() return tx_schema.dump(self)
class User(db.Model): id = db.Column(db.Integer, primary_key=True) public_id = db.Column(db.String(500), unique=True, nullable=True) email = db.Column(db.String(100), unique=True, nullable=True) password = db.Column(db.String(1000), nullable=True) is_admin = db.Column(db.Boolean, default=False) def __init__(self, email, password, is_admin=False): self.public_id = str(uuid4()) self.email = email self.password = bcrypt.generate_password_hash(password) self.is_admin = is_admin def check_password(self, password): return bcrypt.check_password_hash(self.password, password) def as_dict(self): return { "id": self.public_id, "mail": self.email, "is_admin": self.is_admin }
class TxNotification(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) user = db.relationship('User', backref=db.backref('txnotifications', lazy='dynamic')) date = db.Column(db.DateTime()) txid = db.Column(db.String(255), unique=True) def __init__(self, user, txid): self.user = user self.date = datetime.datetime.now() self.txid = txid @classmethod def exists(cls, session, txid): return session.query(cls).filter(cls.txid == txid).first() @classmethod def count(cls, session): return session.query(cls).count() def __repr__(self): return "<TxNotification %r>" % (self.txid)
class Bank(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) user = db.relationship('User', backref=db.backref('banks', lazy='dynamic')) token = db.Column(db.String(255), unique=True, nullable=False) account_number = db.Column(db.String(255), nullable=False) account_name = db.Column(db.String(255), nullable=False) account_holder_address = db.Column(db.String(255), nullable=False) bank_name = db.Column(db.String(255), nullable=False) default_account = db.Column(db.Boolean, nullable=False) def __init__(self, token, account_number, account_name, account_holder_address, bank_name, default_account): self.account_number = account_number self.account_name = account_name self.account_holder_address = acount_holder_address self.bank_name = bank_name self.default_account = default_account self.generate_defaults() def generate_defaults(self): self.user = current_user self.token = generate_key(4) def ensure_default_account_exclusive(self, session): if self.default_account: session.query(Bank).filter(Bank.user_id == self.user_id, Bank.id != self.id).update( dict(default_account=False)) @classmethod def count(cls, session): return session.query(cls).count() @classmethod def from_token(cls, session, token): return session.query(cls).filter(cls.token == token).first() @classmethod def from_user(cls, session, user): return session.query(cls).filter(cls.user_id == user.id).all() def __repr__(self): return self.account_number def to_json(self): schema = BankSchema() return schema.dump(self).data
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(255), unique=True) password = db.Column(db.String(255)) active = db.Column(db.Boolean()) confirmed_at = db.Column(db.DateTime()) roles = db.relationship('Role', secondary=roles_users, backref=db.backref('users', lazy='dynamic')) bronze_data = db.relationship('BronzeData', cascade='delete', backref=db.backref('user'), uselist=False) def __init__(self, **kwargs): super().__init__(**kwargs) @classmethod def from_email(cls, session, email): return session.query(cls).filter(cls.email == email).first() def __str__(self): return '%s' % self.email
class Utility(db.Model): id = db.Column(db.Integer, primary_key=True) date = db.Column(db.DateTime(), nullable=False) name = db.Column(db.String(255), nullable=False) description = db.Column(db.Text()) bank_description = db.Column(db.Text(), nullable=False) def __init__(self, name): self.generate_defaults() def generate_defaults(self): self.date = datetime.datetime.now() @classmethod def count(cls, session): return session.query(cls).count() @classmethod def all(cls, session): return session.query(cls).all() @classmethod def all_alphabetical(cls, session): return session.query(cls).order_by(cls.name).all() @classmethod def from_id(cls, session, utility_id): return session.query(cls).filter(cls.id == utility_id).first() @classmethod def jsonify_bank_descriptions(cls, utilities): for utility in utilities: utility.bank_description_json = json.loads( utility.bank_description) def __repr__(self): return "<Utility %r>" % (self.name)
class PushNotificationLocation(db.Model): id = db.Column(db.Integer, primary_key=True) fcm_registration_token = db.Column(db.String, nullable=False) latitude = db.Column(db.Float, nullable=False) longitude = db.Column(db.Float, nullable=False) date = db.Column(db.DateTime(), nullable=False) def __init__(self, registration_token, latitude, longitude): self.fcm_registration_token = registration_token self.update(latitude, longitude) def update(self, latitude, longitude): self.latitude = latitude self.longitude = longitude self.date = datetime.datetime.now() @classmethod def from_token(cls, session, token): return session.query(cls).filter(cls.fcm_registration_token == token).first() @classmethod def tokens_at_location(cls, session, latitude, max_lat_delta, longitude, max_long_delta, max_age_minutes): since = datetime.datetime.now() - datetime.timedelta(minutes=max_age_minutes) return session.query(cls).filter(and_(cls.date >= since, and_(and_(cls.latitude <= latitude + max_lat_delta, cls.latitude >= latitude - max_lat_delta), and_(cls.longitude <= longitude + max_long_delta, cls.longitude >= longitude - max_long_delta)))).all()
class Gender(db.Model): __tablename__ = 'genders' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(16), unique=True) users = db.relationship('User', backref='gender', lazy='dynamic') @staticmethod def insert_genders(): genders = { "Male", "Female", "Transgender(MTF)", "Transgender(FTM)", "Rather not say" } for g in genders: gender = Gender.query.filter_by(name=g).first() if gender is None: gender = Gender(name=g) db.session.add(gender) db.session.commit() def __repr__(self): return '<Gender %r>' % self.name