Files
crawler/models.py
2025-06-08 13:17:33 -04:00

51 lines
2.3 KiB
Python

# models.py
from sqlalchemy import Column, Integer, String, Float, Date, Boolean, BigInteger, ForeignKey, DateTime # Added DateTime
from sqlalchemy.orm import relationship
from datetime import date, datetime # Import datetime as well
# Import Base from our database.py
from database import Base
# --- OilPrice Model ---
class OilPrice(Base):
__tablename__ = "oil_prices"
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
state = Column(String(100), index=True)
zone = Column(Integer, index=True)
name = Column(String(255), index=True)
price = Column(Float, nullable=True)
date = Column(String(20)) # This is the 'Date Posted' from the website
# --- UPDATED scrapetimestamp ---
# To store both date and time of scraping
scrapetimestamp = Column(DateTime, default=datetime.utcnow, index=True)
# 'datetime.utcnow' will automatically provide the current UTC date and time
# when a new record is created and this field is not explicitly set.
company_id = Column(Integer, ForeignKey("company.id"), nullable=True)
def __repr__(self):
return (f"<OilPrice(id={self.id}, state='{self.state}', zone='{self.zone}', "
f"name='{self.name}', price={self.price}, date='{self.date}', "
f"scraped_at='{self.scrapetimestamp}')>") # Added scraped_at to repr
# --- Company Model (remains the same) ---
class Company(Base):
__tablename__ = "company"
# ... (fields as before) ...
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
active = Column(Boolean, nullable=False, default=True)
created = Column(Date, nullable=False, default=date.today) # This 'created' is for the company record
name = Column(String(255), nullable=False, index=True, unique=True)
address = Column(String(500), nullable=True)
town = Column(String(100), nullable=True)
state = Column(String(50), nullable=True)
phone = Column(String(20), nullable=True)
owner_name = Column(String(255), nullable=True)
owner_phone_number = Column(String(20), nullable=True)
email = Column(String(255), nullable=True, unique=True)
user_id = Column(Integer, ForeignKey("users.id"), nullable=True, index=True)
def __repr__(self):
return f"<Company(id={self.id}, name='{self.name}', active={self.active})>"