feat: sistem de linkuri publice pentru proprietăți
Expune fluxul de rezervare anonimă printr-un link public partajabil
(/book/<slug>), cu calendar public, cod QR și proprietate demo publică.
Backend:
- slug + list_on_landing pe Property; utilitar slugify (diacritice RO,
cuvinte rezervate, ban all-digit) + migrare idempotentă raw-DDL rulată
din entrypoint; SQLite WAL + busy_timeout
- GET /public/properties/{slug_or_id}(+/spaces): rezolvare slug/ID, 404 uniform
- GET /public/spaces/{id}/busy: doar {start_time,end_time} (fără PII),
pending+approved, tz-aware/interval>60z → 422, orfan/privat → 404
- availability: scrub user_name/title (fix scurgere PII)
- POST /public/bookings: overlap pending+approved, cap 5/email/spațiu/zi,
rate-limit per-IP, suprimare notificări+email pe demo, email confirmare guest
- PATCH slug (409 duplicat / 422 format / 403 non-manager); guest_email EmailStr
- email guest la aprobare/respingere cu cod #RB-<id>
- teste noi: test_public.py + test_migrate_slug.py (33 teste)
Frontend:
- rută /book/:slug; PublicBooking rescris cu calendar grid manual
(PublicCalendar + GuestBookingForm), 30min, 08-20, mobil day-view, RO
- card „Link public" în PropertyDetail: copy, edit slug, QR pe URL-ul cu ID
- secțiune proprietăți publice + CTA pe Landing; buton copy în Properties
Gate aprobat (U1-U3, G1 grid manual, G2 list_on_landing); G3-G5 în TODOS.md.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
89
backend/migrate_add_property_slug.py
Normal file
89
backend/migrate_add_property_slug.py
Normal file
@@ -0,0 +1,89 @@
|
||||
"""Migration script to add public-booking-link slug support to properties.
|
||||
|
||||
Idempotent (safe to run multiple times):
|
||||
- Adds `slug` (VARCHAR, nullable) and `list_on_landing` (BOOLEAN NOT NULL
|
||||
DEFAULT 0) columns to `properties` if missing.
|
||||
- Creates a UNIQUE INDEX on `slug` if missing.
|
||||
- Backfills NULL slugs deterministically from each property's name.
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add backend to path
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
from sqlalchemy import inspect, text
|
||||
|
||||
from app.core.slug import generate_unique_slug
|
||||
from app.db.session import SessionLocal, engine
|
||||
from app.models.property import Property
|
||||
|
||||
SLUG_INDEX_NAME = "ix_properties_slug_unique"
|
||||
|
||||
|
||||
def migrate() -> None:
|
||||
"""Run migration to add slug + list_on_landing support to properties."""
|
||||
inspector = inspect(engine)
|
||||
columns = [col["name"] for col in inspector.get_columns("properties")]
|
||||
|
||||
print("Starting property slug migration...")
|
||||
|
||||
with engine.connect() as conn:
|
||||
if "slug" not in columns:
|
||||
print("1. Adding slug column to properties...")
|
||||
conn.execute(text("ALTER TABLE properties ADD COLUMN slug VARCHAR"))
|
||||
conn.commit()
|
||||
print(" Column added.")
|
||||
else:
|
||||
print("1. slug column already exists.")
|
||||
|
||||
if "list_on_landing" not in columns:
|
||||
print("2. Adding list_on_landing column to properties...")
|
||||
conn.execute(
|
||||
text("ALTER TABLE properties ADD COLUMN list_on_landing BOOLEAN DEFAULT 0 NOT NULL")
|
||||
)
|
||||
conn.commit()
|
||||
print(" Column added.")
|
||||
else:
|
||||
print("2. list_on_landing column already exists.")
|
||||
|
||||
# Recompute indexes after potentially adding the slug column.
|
||||
inspector = inspect(engine)
|
||||
index_names = {ix["name"] for ix in inspector.get_indexes("properties")}
|
||||
if SLUG_INDEX_NAME not in index_names:
|
||||
print("3. Creating unique index on slug...")
|
||||
with engine.connect() as conn:
|
||||
conn.execute(
|
||||
text(f"CREATE UNIQUE INDEX {SLUG_INDEX_NAME} ON properties (slug)")
|
||||
)
|
||||
conn.commit()
|
||||
print(" Index created.")
|
||||
else:
|
||||
print("3. Unique index on slug already exists.")
|
||||
|
||||
# Backfill: deterministic unique slug for every property missing one.
|
||||
print("4. Backfilling missing slugs...")
|
||||
db = SessionLocal()
|
||||
try:
|
||||
properties_without_slug = (
|
||||
db.query(Property)
|
||||
.filter(Property.slug.is_(None))
|
||||
.order_by(Property.id)
|
||||
.all()
|
||||
)
|
||||
for prop in properties_without_slug:
|
||||
prop.slug = generate_unique_slug(db, prop.name, exclude_id=prop.id)
|
||||
db.flush()
|
||||
db.commit()
|
||||
print(f" Backfilled {len(properties_without_slug)} propert(y/ies).")
|
||||
except Exception:
|
||||
db.rollback()
|
||||
raise
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
print("Migration completed successfully!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
migrate()
|
||||
Reference in New Issue
Block a user