stuff is working again
This commit is contained in:
@ -82,6 +82,7 @@ class Plant(db.Model):
|
||||
|
||||
plant_type = db.Column(db.String(50), nullable=False)
|
||||
notes = db.Column(db.Text, nullable=True)
|
||||
short_id = db.Column(db.String(8), unique=True, nullable=True, index=True)
|
||||
|
||||
vendor_name = db.Column(db.String(255), nullable=True)
|
||||
price = db.Column(db.Numeric(10, 2), nullable=True)
|
||||
@ -151,3 +152,16 @@ class Plant(db.Model):
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Plant {self.uuid} ({self.plant_type})>"
|
||||
|
||||
@classmethod
|
||||
def generate_short_id(cls, length: int = 6) -> str:
|
||||
"""
|
||||
Produce a random [a–z0–9] string of the given length
|
||||
and ensure it doesn’t collide with any existing plant.short_id.
|
||||
"""
|
||||
alphabet = string.ascii_lowercase + string.digits
|
||||
while True:
|
||||
candidate = ''.join(random.choices(alphabet, k=length))
|
||||
# Check uniqueness
|
||||
if not cls.query.filter_by(short_id=candidate).first():
|
||||
return candidate
|
||||
|
Reference in New Issue
Block a user