Module shortner.models
Models module deals with models representing entities in the application
Expand source code
"""Models module deals with models representing entities in the application"""
import json
from random import seed, choice
from uuid import uuid4
from django.db import models
from shortner.constants import CHARS, STUB_LENGTH
class Link(models.Model):
"""Link model refers to a conversion of a long URL to a shortened one"""
# 2083 is the max length of URL supported by Microsoft Edge
long_url = models.CharField(max_length=2083)
special_code = models.UUIDField(
primary_key=True, default=uuid4, editable=False)
stub = models.CharField(max_length=STUB_LENGTH, unique=True)
def save(self, *args, **kwargs):
seed(str(self.special_code))
self.stub = "".join(choice(CHARS) for _ in range(STUB_LENGTH))
super().save(*args, **kwargs)
def to_json(self):
"""to_json converts link to JSON string"""
return json.dumps(
{
"long_url": self.long_url,
"special_code": str(self.special_code),
"stub": self.stub,
}
)
Classes
class Link (*args, **kwargs)
-
Link model refers to a conversion of a long URL to a shortened one
Expand source code
class Link(models.Model): """Link model refers to a conversion of a long URL to a shortened one""" # 2083 is the max length of URL supported by Microsoft Edge long_url = models.CharField(max_length=2083) special_code = models.UUIDField( primary_key=True, default=uuid4, editable=False) stub = models.CharField(max_length=STUB_LENGTH, unique=True) def save(self, *args, **kwargs): seed(str(self.special_code)) self.stub = "".join(choice(CHARS) for _ in range(STUB_LENGTH)) super().save(*args, **kwargs) def to_json(self): """to_json converts link to JSON string""" return json.dumps( { "long_url": self.long_url, "special_code": str(self.special_code), "stub": self.stub, } )
Ancestors
- django.db.models.base.Model
Class variables
var DoesNotExist
-
The requested object does not exist
var MultipleObjectsReturned
-
The query returned multiple objects when only one was expected.
var objects
Instance variables
var long_url
-
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
Expand source code
def __get__(self, instance, cls=None): """ Retrieve and caches the value from the datastore on the first lookup. Return the cached value. """ if instance is None: return self data = instance.__dict__ field_name = self.field.attname if field_name not in data: # Let's see if the field is part of the parent chain. If so we # might be able to reuse the already loaded value. Refs #18343. val = self._check_parent_chain(instance) if val is None: instance.refresh_from_db(fields=[field_name]) else: data[field_name] = val return data[field_name]
var special_code
-
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
Expand source code
def __get__(self, instance, cls=None): """ Retrieve and caches the value from the datastore on the first lookup. Return the cached value. """ if instance is None: return self data = instance.__dict__ field_name = self.field.attname if field_name not in data: # Let's see if the field is part of the parent chain. If so we # might be able to reuse the already loaded value. Refs #18343. val = self._check_parent_chain(instance) if val is None: instance.refresh_from_db(fields=[field_name]) else: data[field_name] = val return data[field_name]
var stub
-
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
Expand source code
def __get__(self, instance, cls=None): """ Retrieve and caches the value from the datastore on the first lookup. Return the cached value. """ if instance is None: return self data = instance.__dict__ field_name = self.field.attname if field_name not in data: # Let's see if the field is part of the parent chain. If so we # might be able to reuse the already loaded value. Refs #18343. val = self._check_parent_chain(instance) if val is None: instance.refresh_from_db(fields=[field_name]) else: data[field_name] = val return data[field_name]
Methods
def save(self, *args, **kwargs)
-
Save the current instance. Override this in a subclass if you want to control the saving process.
The 'force_insert' and 'force_update' parameters can be used to insist that the "save" must be an SQL insert or update (or equivalent for non-SQL backends), respectively. Normally, they should not be set.
Expand source code
def save(self, *args, **kwargs): seed(str(self.special_code)) self.stub = "".join(choice(CHARS) for _ in range(STUB_LENGTH)) super().save(*args, **kwargs)
def to_json(self)
-
to_json converts link to JSON string
Expand source code
def to_json(self): """to_json converts link to JSON string""" return json.dumps( { "long_url": self.long_url, "special_code": str(self.special_code), "stub": self.stub, } )