<s> from sqlalchemy import Column <EOL> from sqlalchemy . orm import relationship , backref <EOL> from sqlalchemy . types import Integer , String , Text , DateTime , Boolean <EOL> from . master_import import db <EOL> class Channel ( db . Model ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = db . Column ( Integer , primary_key = True ) <EOL> name = db . Column ( String ( <NUM_LIT:255> ) , index = True ) <EOL> frequency = db . Column ( Integer ) <EOL> channel_id = db . Column ( Integer , index = True ) <EOL> icon_url = db . Column ( String ( <NUM_LIT:255> ) ) <EOL> hidden = db . Column ( Boolean , nullable = False , default = False ) <EOL> def __init__ ( self , name , frequency , channel_id , icon_url = None , hidden = False ) : <EOL> self . name = name <EOL> self . frequency = frequency <EOL> self . channel_id = channel_id <EOL> self . icon_url = icon_url <EOL> self . hidden = hidden <EOL> def add ( self , commit = False ) : <EOL> db . session . add ( self ) <EOL> if commit : <EOL> db . session . commit ( ) <EOL> def save ( self ) : <EOL> db . session . flush ( ) <EOL> db . session . commit ( ) <EOL> @ staticmethod <EOL> def sorted_by_name ( ) : <EOL> return Channel . query . filter ( Channel . hidden == <NUM_LIT:0> ) . order_by ( Channel . name ) . all ( ) <EOL> def clear ( self ) : <EOL> from simple_pvr import Programme <EOL> Programme . query . all ( ) . delete ( ) <EOL> Channel . query . all ( ) . delete ( ) <EOL> db . session . flush <EOL> db . session . commit <EOL> @ staticmethod <EOL> def with_current_programmes ( id ) : <EOL> from datetime import datetime <EOL> channel = Channel . query . get ( id ) <EOL> if not channel : <EOL> return None <EOL> return Channel . _decorated_with_current_programmes ( channel , datetime . now ( ) ) <EOL> @ staticmethod <EOL> def all_with_current_programmes ( ) : <EOL> return map ( Channel . _decorated_with_current_programmes , Channel . query . order_by ( Channel . name ) . all ( ) ) <EOL> @ staticmethod <EOL> def with_name ( name ) : <EOL> result = Channel . query . filter ( Channel . name == name ) . first ( ) <EOL> if not result : <EOL> raise ValueError ( '<STR_LIT>' % ( name ) ) <EOL> return result <EOL> def getId ( self ) : <EOL> return self . id <EOL> @ staticmethod <EOL> def _decorated_with_current_programmes ( channel , now = None ) : <EOL> from datetime import datetime <EOL> from . master_import import Programme <EOL> if now is None : <EOL> now = datetime . now ( ) <EOL> current_programme = Programme . _current_programme_for ( channel , now ) <EOL> number_of_upcoming_programmes = <NUM_LIT:3> if current_programme else <NUM_LIT:4> <EOL> upcoming_programmes = Programme . _upcoming_programmes_for ( channel , number_of_upcoming_programmes , now ) <EOL> return { <EOL> '<STR_LIT>' : channel , <EOL> '<STR_LIT>' : current_programme , <EOL> '<STR_LIT>' : upcoming_programmes <EOL> } <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . name , str ( self . channel_id ) , str ( self . id ) ) <EOL> @ property <EOL> def serialize ( self ) : <EOL> from . master_import import safe_value <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT:id>' : self . id , <EOL> '<STR_LIT:name>' : safe_value ( self . name ) , <EOL> '<STR_LIT>' : self . icon_url , <EOL> '<STR_LIT>' : self . hidden <EOL> } </s>
