from db import Base
from sqlalchemy import Column, Unicode, Table, Integer, ForeignKey
from sqlalchemy.orm import relation, backref
from main.model import Computer

metadata = Base.metadata

class HardwareProfile(Base):
    __tablename__ = 'hardware_profile'

    id = Column(Integer, primary_key=True)
    name = Column(Unicode(256))

    computer_id = Column(Integer, ForeignKey('computer.id'))

    hardware_profile_computer = Table(
            'hardware_profile_computer', metadata,
            Column('profile_id', Integer, ForeignKey('hardware_profile.id')),
            Column('computer_id', Integer, ForeignKey('computer.id')))

    computers = relation('Computer', secondary = hardware_profile_computer,
            backref='hardware')

    def __init__(self, name=''):
        self.name = name

class HardwareElement(Base):
    __tablename__ = 'hardware_element'

    id = Column(Integer, primary_key=True)
    type = Column(Unicode(3))
    description = Column(Unicode(512))

    hardware_profile_hardware_element = Table(
            'hardware_profile_hardware_element',metadata,
            Column('element_id', Integer, ForeignKey('hardware_element.id')),
            Column('profile_id', Integer, ForeignKey('hardware_profile.id')))

    profiles = relation('HardwareProfile',
            secondary = hardware_profile_hardware_element, backref='elements')

    def __cmp__(self, other):
        return self.id == other.id
