class URL(db.Model): short_url = db.Column(db.String(50), primary_key=True) redirect_url = db.Column(db.String(150), nullable=False) password = db.Column(db.String(60), nullable=True) def __repr__(self): return f"URL('{short_url} - {redirect_url}')"
class ShortURL(db.Model, AutoInitModelMixin): long_url = db.Column(db.String(255), unique=True) slug = db.Column(db.String(30), unique=True) created = db.Column(db.DateTime, default=datetime.now) clicks = db.relationship("Click") def __repr__(self): return "<ShortURL: '%s'>" % self.long_url
class URL(db.Model): id = db.Column(db.Integer, primary_key=True) url = db.Column(db.String(2048), nullable=False) shortened = db.Column(db.String(6), unique=True, nullable=False) estimated_date = db.Column(db.DateTime(), nullable=False) def __repr__(self): return f'{self.url} shortened to {self.shortened} till {self.estimated_date}'
class User(db.Model): id = db.Column(db.Integer, primary_key=True) login = db.Column(db.String(30), unique=True) password = db.Column(db.String(30)) def __init__(self, login, password): self.login = login self.password = password
class Hash(db.Model): id = db.Column(db.Integer, primary_key=True) creation_date = db.Column(db.DateTime) url_hash = db.Column(db.String(8), unique=True) full_url = db.Column(db.String(1000)) redirects = db.Column(db.Integer) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) user = db.relationship('User', backref=db.backref('hashes', lazy='dynamic')) def __init__(self, url_hash, full_url, redirects=0, creation_date=None): if creation_date == None: self.creation_date = datetime.utcnow() self.url_hash = url_hash self.full_url = full_url self.redirects = redirects
class Click(db.Model, AutoInitModelMixin): url_id = db.Column(db.Integer, db.ForeignKey('shorturl.id')) user_agent = db.Column(db.String(255), default='Unknown') time = db.Column(db.DateTime, default=datetime.now) @cached_property def url(self): return ShortURL.query.filter_by(id=self.url_id).one() def __repr__(self): return "<Click on '%s'>" % self.url.long_url
class Link(db.Model): id = db.Column(db.Integer, primary_key=True) original_url = db.Column(db.String(512)) short_url = db.Column(db.String(32), unique=True) visits = db.Column(db.Integer, default=0) date_created = db.Column(db.DateTime, default=datetime.datetime.now()) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.short_url = self.generate_short_link() def generate_short_link(self): characters = string.digits + string.ascii_letters short_url = ''.join(choices(characters, k=5)) link = self.query.filter_by(short_url=short_url).first() if link: return self.generate_short_link() return short_url
class URL(db.Model): """URL Model description""" id = db.Column(db.Integer, primary_key=True) original_url = db.Column(db.String(512)) shortened_url = db.Column(db.String(5), unique=True) created_at = db.Column(db.DateTime, default=datetime.today()) expire_in = db.Column(db.Integer, default=90) def __init__(self, original_url, expire_in, shortened_url=None, created_at=None): self.original_url = original_url self.created_at = datetime.today() if expire_in is None: self.expire_in = 90 else: self.expire_in = expire_in self.shortened_url = self.generate_short_url() def generate_short_url(self): """Generates shortend url part""" # URL-allowed characters characters = string.digits + string.ascii_letters + "$-_.+!*'()," # Random string of URL-allowed characters of length 5 # Quantity of available variations of random strings would be # 2706784157 (77^5) shortened_url = "".join(choices(characters, k=5)) # Returns True if such random string already exists exists = self.query.filter_by(shortened_url=shortened_url).first() if exists: pass return shortened_url
class Url(db.Model): id = db.Column(db.Integer, primary_key=True) url = db.Column(db.String(length=300)) short_url = db.Column(db.String(length=300))