JourneyPoint / journeypoint / app / models / pin.py
pin.py
Raw
from sqlalchemy import Column, Integer, String, Float, Boolean, DateTime, ForeignKey, Date
from sqlalchemy.orm import relationship, declarative_base
from datetime import datetime
from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy.ext.mutable import MutableList

Base = declarative_base()


# pins table depends on username as a foreign key, so we define it first here for
# it to use as a dependency, otherwise if the pins table is attempted to be
# created first we would have a users table not found error
class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    username = Column(String, unique=True, index=True, nullable=False)
    email = Column(String, unique=True, index=True, nullable=False)
    hashed_password = Column(String, nullable=False)
    dob = Column(Date, nullable=False)
    country_of_origin = Column(String, nullable=False)

    followers = Column(MutableList.as_mutable(ARRAY(String)), default=[])
    following = Column(MutableList.as_mutable(ARRAY(String)), default=[])
    bio = Column(String, nullable=True)
    profile_picture = Column(String, nullable=True)

    pins = relationship("Pin", back_populates="users")


class Pin(Base):
    __tablename__ = "pins"

    id = Column(Integer, primary_key=True, index=True)
    pin_name = Column(String, nullable=False)
    username = Column(String, ForeignKey("users.username"), nullable=False)
    x = Column(Float, nullable=False)  # long
    y = Column(Float, nullable=False)  # lat
    description = Column(String, nullable=True)  # optional
    category = Column(String, nullable=True)  # optional
    public = Column(Boolean, default=False)
    created_at = Column(DateTime, default=datetime.utcnow)

    users = relationship("User", back_populates="pins")  # references users table