Skip to content
Snippets Groups Projects
models.py 4.3 KiB
Newer Older
  • Learn to ignore specific revisions
  • 
    #
    # Aura Engine API (https://gitlab.servus.at/aura/engine-api)
    #
    # Copyright (C) 2020 - The Aura Engine Team.
    #
    # This program is free software: you can redistribute it and/or modify
    # it under the terms of the GNU Affero General Public License as published by
    # the Free Software Foundation, either version 3 of the License, or
    # (at your option) any later version.
    #
    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    # GNU Affero General Public License for more details.
    #
    # You should have received a copy of the GNU Affero General Public License
    # along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    
    import datetime
    
    from sqlalchemy import create_engine, Column, DateTime, String, Integer, Boolean
    
    from src.server import db, ma
    
    
    
    
    class PlayLog(db.Model):
        """
        Table holding play-log entries.
        """
        __tablename__ = 'playlog'
    
        # Primary Key
        track_start             = Column(DateTime, primary_key=True)
    
        # Columns
        track_artist            = Column(String(256))
        track_album             = Column(String(256))
        track_title             = Column(String(256))
        track_duration          = Column(String(256))
        track_type              = Column(Integer)
        schedule_id             = Column(Integer)
        schedule_start          = Column(DateTime)
        schedule_end            = Column(DateTime)
        schedule_repetition     = Column(Boolean)
        schedule_playlist_id    = Column(Integer)
        schedule_fallback_type  = Column(Integer)
        show_id                 = Column(Integer)
        schedule_repetition     = Column(Boolean)
        schedule_playlist_id    = Column(Integer)
        schedule_fallback_type  = Column(Integer)
        show_id                 = Column(Integer)
        show_name               = Column(String(256))
        show_funding_category   = Column(String(256))
        show_name               = Column(String(256))
        show_type               = Column(String(256))
        show_category           = Column(String(256))
        show_topic              = Column(String(256))
    
    
    
        def __init__(self):
            """
            Initializes a trackservice entry
            """
            pass
    
    
        @staticmethod
        def select_current():
            """
            Selects the currently playing track.
            """
            db.session.commit()
            now = datetime.datetime.now()
            track = db.session.query(PlayLog).\
                filter(PlayLog.track_start <= str(now)).\
                order_by(PlayLog.track_start.desc()).first()
            return track
    
    
        @staticmethod
        def select_last_hours(n):
            """
            Selects the tracks playing in the past (`n`) hours.
            """
            db.session.commit()
            last_hours = datetime.datetime.today() - datetime.timedelta(hours=n)
            tracks = db.session.query(PlayLog).filter(PlayLog.track_start >= str(last_hours)).all()
            return tracks
    
    
        @staticmethod
        def select_by_day(day):
            """
            Select the track-service items for a day.
            """
            db.session.commit()
            day_plus_one = day + datetime.timedelta(days=1)
            tracks = db.session.query(PlayLog).\
                filter(PlayLog.track_start >= str(day), PlayLog.track_start < str(day_plus_one)).\
                order_by(PlayLog.track_start.desc()).all()
            return tracks
        
    
        @staticmethod
        def select_by_range(from_day, to_day):
            """
            Selects the track-service items for a day range.
            """
            db.session.commit()
            tracks = db.session.query(PlayLog).filter(PlayLog.track_start >= str(from_day),\
                PlayLog.track_start < str(to_day)).all()
            return tracks
    
    
        def __str__(self):
            return "Track [track_start: %s, track_title: %s]" % (str(self.track_start), str(self.track_title))
    
    
    class PlayLogSchema(ma.SQLAlchemyAutoSchema):
        """
        Schema for playlog entries.
        """
        class Meta:
            model = PlayLog
            sqla_session = db.session
    
    
    class TrackServiceSchema(ma.SQLAlchemySchema):
        """
        Schema for trackservice entries.
        """
        class Meta:
            model = PlayLog
            sqla_session = db.session
            fields = (
                "track_start",
                "track_artist",
                "track_album",
                "track_title"
                )
    
    
    
    # Create Tables
    db.create_all()