# Path: matl_online/database.py
# class CRUDMixin(object):
# class Model(CRUDMixin, db.Model):
#     def create(cls, **kwargs):
#     def update(self, commit=True, **kwargs):
#     def save(self, commit=True):
#     def delete(self, commit=True):
# 
# Path: matl_online/public/models.py
# class DocumentationLink(Model):
#     """Model for storing hyperlinks to MATLAB's documentation."""
# 
#     __tablenamme__ = 'doclinks'
# 
#     id = Column(db.Integer, primary_key=True)
#     name = Column(db.String, unique=True, nullable=False)
#     link = Column(db.String, nullable=False)
# 
#     @classmethod
#     def refresh(cls):
#         """Fetch updated documentation from the Mathworks."""
#         # Flip the order of the links so that the first URL listed is the
#         # highest priority and will take precedence
#         for url in current_app.config['MATLAB_DOC_LINKS'][::-1]:
#             resp = requests.get(url)
#             soup = BeautifulSoup(resp.text, 'html.parser')
# 
#             terms = soup.findAll('td', {'class': 'term'})
#             links = [term.find('a') for term in terms]
# 
#             for link in links:
# 
#                 function = link.text.rstrip()
# 
#                 doc = cls.query.filter_by(name=function).first()
#                 doc_url = urljoin(url, link['href'])
# 
#                 # Create an entry if one doesn't already exist
#                 if doc is None:
#                     doc = cls(name=function)
# 
#                 doc.link = doc_url
#                 doc.save()
# 
#         # Make sure to remove i and j entries
#         toremove = cls.query.filter(or_(cls.name == 'i', cls.name == 'j')).all()
#         for item in toremove:
#             item.delete()
# 
#         return cls.query.all()
# 
# class Release(Model):
#     """Model for storing metadata associated with MATL releases."""
# 
#     __tablename__ = 'releases'
# 
#     id = Column(db.Integer, primary_key=True)
#     tag = Column(db.String, unique=True, nullable=False)
#     date = Column(db.DateTime, unique=True, nullable=False)
# 
#     def __repr__(self):
#         """Create a custom string representation."""
#         return '<Release %r>' % self.tag
# 
#     @hybrid_property
#     def version(self):
#         """Convert release number to tuple for comparisons."""
#         return tuple(int(x) for x in self.tag.split('.'))
# 
#     @classmethod
#     def latest(cls):
#         """Get the latest release from Github."""
#         releases = cls.query.all()
#         if len(releases) == 0:
#             return None
# 
#         releases.sort(key=lambda x: x.version)
#         return releases[-1]


from datetime import datetime
from factory import Sequence, LazyFunction, LazyAttribute
from factory.alchemy import SQLAlchemyModelFactory
from matl_online.database import db
from matl_online.public.models import DocumentationLink, Release
    """Base factory for SQLAlchemy Model generation."""

    class Meta:
        """Meta information for the factory."""

        abstract = True
        sqlalchemy_session = db.session


class ReleaseFactory(BaseFactory):
    """Factory for creating Release objects on demand."""

    tag = Sequence(lambda n: '{0}.0.0'.format(n))
    date = LazyFunction(datetime.now)

    class Meta:
        """Meta information for the factory."""

        model = Release


class DocumentationLinkFactory(BaseFactory):
    """Factory for creating DocumentationLink objects on demand."""

    # The default link will simply append .html to the end of the name
    link = LazyAttribute(lambda o: o.name + '.html')

    class Meta:
        """Meta information for the factory."""


# Path: matl_online/database.py
# class CRUDMixin(object):
# class Model(CRUDMixin, db.Model):
#     def create(cls, **kwargs):
#     def update(self, commit=True, **kwargs):
#     def save(self, commit=True):
#     def delete(self, commit=True):
# 
# Path: matl_online/public/models.py
# class DocumentationLink(Model):
#     """Model for storing hyperlinks to MATLAB's documentation."""
# 
#     __tablenamme__ = 'doclinks'
# 
#     id = Column(db.Integer, primary_key=True)
#     name = Column(db.String, unique=True, nullable=False)
#     link = Column(db.String, nullable=False)
# 
#     @classmethod
#     def refresh(cls):
#         """Fetch updated documentation from the Mathworks."""
#         # Flip the order of the links so that the first URL listed is the
#         # highest priority and will take precedence
#         for url in current_app.config['MATLAB_DOC_LINKS'][::-1]:
#             resp = requests.get(url)
#             soup = BeautifulSoup(resp.text, 'html.parser')
# 
#             terms = soup.findAll('td', {'class': 'term'})
#             links = [term.find('a') for term in terms]
# 
#             for link in links:
# 
#                 function = link.text.rstrip()
# 
#                 doc = cls.query.filter_by(name=function).first()
#                 doc_url = urljoin(url, link['href'])
# 
#                 # Create an entry if one doesn't already exist
#                 if doc is None:
#                     doc = cls(name=function)
# 
#                 doc.link = doc_url
#                 doc.save()
# 
#         # Make sure to remove i and j entries
#         toremove = cls.query.filter(or_(cls.name == 'i', cls.name == 'j')).all()
#         for item in toremove:
#             item.delete()
# 
#         return cls.query.all()
# 
# class Release(Model):
#     """Model for storing metadata associated with MATL releases."""
# 
#     __tablename__ = 'releases'
# 
#     id = Column(db.Integer, primary_key=True)
#     tag = Column(db.String, unique=True, nullable=False)
#     date = Column(db.DateTime, unique=True, nullable=False)
# 
#     def __repr__(self):
#         """Create a custom string representation."""
#         return '<Release %r>' % self.tag
# 
#     @hybrid_property
#     def version(self):
#         """Convert release number to tuple for comparisons."""
#         return tuple(int(x) for x in self.tag.split('.'))
# 
#     @classmethod
#     def latest(cls):
#         """Get the latest release from Github."""
#         releases = cls.query.all()
#         if len(releases) == 0:
#             return None
# 
#         releases.sort(key=lambda x: x.version)
#         return releases[-1]


# Path: tests/factories.py
from datetime import datetime
from factory import Sequence, LazyFunction, LazyAttribute
from factory.alchemy import SQLAlchemyModelFactory
from matl_online.database import db
from matl_online.public.models import DocumentationLink, Release

    """Base factory for SQLAlchemy Model generation."""

    class Meta:
        """Meta information for the factory."""

        abstract = True
        sqlalchemy_session = db.session


class ReleaseFactory(BaseFactory):
    """Factory for creating Release objects on demand."""

    tag = Sequence(lambda n: '{0}.0.0'.format(n))
    date = LazyFunction(datetime.now)

    class Meta:
        """Meta information for the factory."""

        model = Release


class DocumentationLinkFactory(BaseFactory):
    """Factory for creating DocumentationLink objects on demand."""

    # The default link will simply append .html to the end of the name
    link = LazyAttribute(lambda o: o.name + '.html')

    class Meta:
        """Meta information for the factory."""


        model = DocumentationLink