Created
April 3, 2014 02:03
-
-
Save comynli/9947036 to your computer and use it in GitHub Desktop.
hybrid_property
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Host(db.Model): | |
__tablename__ = 'hosts' | |
host_id = db.Column(db.Integer, primary_key=True, autoincrement=True) | |
_ip = db.Column("ip", db.Integer, nullable=False) | |
port = db.Column(db.Integer, nullable=False) | |
user = db.Column(db.String(64), nullable=False) | |
_password = db.Column("password", db.Text, nullable=False) | |
@hybrid_property | |
def ip(self): | |
return socket.inet_ntoa(struct.pack("!I", int(self._ip))) | |
@ip.setter | |
def ip(self, ip): | |
self._ip = struct.unpack("!I", socket.inet_aton(ip))[0] | |
class ip_comparator(Comparator): | |
def operate(self, op, *other, **kwargs): | |
return op(self.__clause_element__(), struct.unpack("!I", socket.inet_aton(other[0]))[0], **kwargs) | |
@ip.comparator | |
def ip(cls): | |
return cls.ip_comparator(cls._ip) | |
#ip = db.synonym("_ip", descriptor=ip) | |
def _get_password(self): | |
return cipher.decrypt(self._password) | |
def _set_password(self, password): | |
self._password = cipher.encrypt(password) | |
password = db.synonym("_password", descriptor=property(_get_password, _set_password)) | |
__table_args__ = ( | |
db.UniqueConstraint("ip", "port", name="host_uniq"), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment