admin / Bid-Sentinel
publicBid Scrape and Tracking Application with AI Capability
Bid-Sentinel / bid-sentinel-v2 / backend / app / models.py
12212 B · main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | """SQLAlchemy ORM models.""" import enum from datetime import datetime from sqlalchemy import ( Boolean, DateTime, Enum, Float, ForeignKey, Integer, String, Text, UniqueConstraint, func, ) from sqlalchemy.orm import Mapped, mapped_column from app.database import Base class UserRole(str, enum.Enum): admin = "admin" analyst = "analyst" # read-only + Intelligence access standard = "standard" class BidStatus(str, enum.Enum): pending = "Pending" bid = "Bid" no_bid = "No Bid" class BidOutcome(str, enum.Enum): """Result of a bid, set after submission. Drives win-rate metrics. Stored as a plain string column (not a DB enum) so the column can be added to existing databases with a simple idempotent ALTER (see seed.create_tables). """ awaiting = "Awaiting" won = "Won" lost = "Lost" no_bid = "No Bid" # auto-set when Bid Status is "No Bid" disregarded = "Disregarded" # no-bid: hide from active pipeline class User(Base): __tablename__ = "users" id: Mapped[int] = mapped_column(primary_key=True) email: Mapped[str] = mapped_column(String(255), unique=True, index=True, nullable=False) full_name: Mapped[str | None] = mapped_column(String(255), nullable=True) hashed_password: Mapped[str] = mapped_column(String(255), nullable=False) role: Mapped[UserRole] = mapped_column( Enum(UserRole, name="user_role"), default=UserRole.standard, nullable=False ) is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), nullable=False ) class Tender(Base): __tablename__ = "tenders" id: Mapped[int] = mapped_column(primary_key=True) # Deduplication keys. reference is the portal's notice id; url is the canonical link. reference: Mapped[str | None] = mapped_column(String(255), unique=True, index=True, nullable=True) url: Mapped[str] = mapped_column(String(1024), unique=True, index=True, nullable=False) title: Mapped[str] = mapped_column(String(1024), nullable=False) description: Mapped[str | None] = mapped_column(Text, nullable=True) published_date: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) closing_date: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) # Numeric value where parseable; raw string preserved for ranges / "POA". value_amount: Mapped[float | None] = mapped_column(Float, nullable=True) value_text: Mapped[str | None] = mapped_column(String(255), nullable=True) currency: Mapped[str] = mapped_column(String(8), default="GBP", nullable=False) duration: Mapped[str | None] = mapped_column(String(255), nullable=True) buyer: Mapped[str | None] = mapped_column(String(512), nullable=True) source: Mapped[str | None] = mapped_column(String(64), nullable=True) matched_keyword: Mapped[str | None] = mapped_column(String(128), nullable=True) bid_status: Mapped[BidStatus] = mapped_column( Enum(BidStatus, name="bid_status"), default=BidStatus.pending, nullable=False ) # Plain string (see BidOutcome docstring); values are "Awaiting"/"Won"/"Lost". outcome: Mapped[str] = mapped_column( String(16), default="Awaiting", server_default="Awaiting", nullable=False ) # Deterministic analysis (no AI) — see app/analysis.py. required_certs: Mapped[str] = mapped_column( String(1024), default="", server_default="", nullable=False ) fit_score: Mapped[int | None] = mapped_column(Integer, nullable=True) fit_matched: Mapped[str] = mapped_column( String(1024), default="", server_default="", nullable=False ) # Top technical requirements scraped from the description (newline-joined). tech_requirements: Mapped[str] = mapped_column( Text, default="", server_default="", nullable=False ) # AI-generated summary (populated only when the AI bolt-on is enabled). ai_summary: Mapped[str] = mapped_column( Text, default="", server_default="", nullable=False ) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), nullable=False ) updated_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False ) class Portal(Base): """A bid portal Bid Sentinel can scan. Seeded from the portal registry.""" __tablename__ = "portals" id: Mapped[int] = mapped_column(primary_key=True) key: Mapped[str] = mapped_column(String(64), unique=True, index=True, nullable=False) name: Mapped[str] = mapped_column(String(255), nullable=False) url: Mapped[str] = mapped_column(String(1024), nullable=False) scope: Mapped[str | None] = mapped_column(Text, nullable=True) # live = a working scraper exists (vs. registered/scaffolded). live: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False) # enabled = user wants this portal scanned (default on, can be unticked). enabled: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False) # custom = added by an admin via the UI (vs. a built-in registry portal). custom: Mapped[bool] = mapped_column(Boolean, default=False, server_default="false", nullable=False) class CustomKeyword(Base): """User-supplied keyword, searched in addition to the built-in cyber list.""" __tablename__ = "custom_keywords" id: Mapped[int] = mapped_column(primary_key=True) keyword: Mapped[str] = mapped_column(String(128), unique=True, nullable=False) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), nullable=False ) class CapabilityTerm(Base): """A service/capability term (the company profile) used for fit scoring.""" __tablename__ = "capability_terms" id: Mapped[int] = mapped_column(primary_key=True) term: Mapped[str] = mapped_column(String(128), unique=True, nullable=False) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), nullable=False ) class SuppressedOpportunity(Base): """A specific opportunity the user bulk-deleted. Its url/reference are blocked so the scraper never inserts it again (see scraper/runner._persist).""" __tablename__ = "suppressed_opportunities" id: Mapped[int] = mapped_column(primary_key=True) url: Mapped[str | None] = mapped_column(String(1024), index=True, nullable=True) reference: Mapped[str | None] = mapped_column(String(255), index=True, nullable=True) title: Mapped[str | None] = mapped_column(String(1024), nullable=True) # Links a suppression to the delete batch that created it, so an Undo can lift it. batch_id: Mapped[str | None] = mapped_column(String(64), index=True, nullable=True) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), nullable=False ) class DeletedOpportunity(Base): """Snapshot of a deleted opportunity, kept briefly so a deletion can be undone. Grouped by ``batch_id`` (one per delete action).""" __tablename__ = "deleted_opportunities" id: Mapped[int] = mapped_column(primary_key=True) batch_id: Mapped[str] = mapped_column(String(64), index=True, nullable=False) snapshot: Mapped[str] = mapped_column(Text, nullable=False) # JSON of the tender fields suppressed: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), index=True, nullable=False ) class TuneOutTerm(Base): """A 'tune-out' (negative) term. Opportunities matching one are suppressed from scrapes and the dashboard — UNLESS they are cyber-security related, which are always kept (see app/scraper/keywords.matches_tuneout).""" __tablename__ = "tuneout_terms" id: Mapped[int] = mapped_column(primary_key=True) term: Mapped[str] = mapped_column(String(255), unique=True, nullable=False) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), nullable=False ) class CpvCode(Base): """A CPV (Common Procurement Vocabulary) code + description used as scrape criteria, in addition to the keyword filters. A notice classified with (or mentioning) one of these codes/descriptions is treated as relevant.""" __tablename__ = "cpv_codes" id: Mapped[int] = mapped_column(primary_key=True) # Stored normalised (digits only, check-digit suffix stripped) for lenient matching. code: Mapped[str] = mapped_column(String(16), unique=True, nullable=False) description: Mapped[str] = mapped_column( String(255), default="", server_default="", nullable=False ) # Toggled off = kept in the list but not applied to the scrape criteria. enabled: Mapped[bool] = mapped_column( Boolean, default=True, server_default="true", nullable=False ) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), nullable=False ) class Accreditation(Base): """A certification/accreditation the company holds (powers cert-gap analysis).""" __tablename__ = "accreditations" id: Mapped[int] = mapped_column(primary_key=True) name: Mapped[str] = mapped_column(String(128), unique=True, nullable=False) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), nullable=False ) class ScheduleSlot(Base): """One recurring scrape time: a specific day-of-week + time (local TZ). Scheduling by "multiple days and times" is modelled as multiple rows, e.g. Mon/Wed/Fri at 08:00 and 17:00 = 6 slots. """ __tablename__ = "schedule_slots" __table_args__ = ( UniqueConstraint("day_of_week", "hour", "minute", name="uq_schedule_slot"), ) id: Mapped[int] = mapped_column(primary_key=True) day_of_week: Mapped[int] = mapped_column(Integer, nullable=False) # 0=Mon .. 6=Sun hour: Mapped[int] = mapped_column(Integer, nullable=False) # 0..23 minute: Mapped[int] = mapped_column(Integer, default=0, nullable=False) # 0..59 enabled: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), nullable=False ) class Comment(Base): """A user comment/note attached to an opportunity.""" __tablename__ = "comments" id: Mapped[int] = mapped_column(primary_key=True) tender_id: Mapped[int] = mapped_column( ForeignKey("tenders.id", ondelete="CASCADE"), index=True, nullable=False ) author_email: Mapped[str] = mapped_column(String(255), nullable=False) body: Mapped[str] = mapped_column(Text, nullable=False) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), nullable=False ) class AppSetting(Base): """Simple key/value application settings (e.g. the AI on/off toggle).""" __tablename__ = "app_settings" key: Mapped[str] = mapped_column(String(64), primary_key=True) value: Mapped[str] = mapped_column(String(512), nullable=False) class AuditLog(Base): """Append-only record of application activity, viewable by admins.""" __tablename__ = "audit_logs" id: Mapped[int] = mapped_column(primary_key=True) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), index=True, nullable=False ) actor: Mapped[str] = mapped_column(String(255), default="anonymous", nullable=False) action: Mapped[str] = mapped_column(String(255), nullable=False) detail: Mapped[str] = mapped_column(String(512), default="", nullable=False) status_code: Mapped[int | None] = mapped_column(Integer, nullable=True) ip: Mapped[str | None] = mapped_column(String(64), nullable=True) |