def create_channel(session, station_id, client_id=None, name="TestChannel"): channel = Channel() channel.station_id = station_id channel.name = name channel.ecc_id = 159 channel.frequency = "00917" channel.pi = "C00F" channel.type_id = "fm" if client_id: channel.fk_client = client_id session.add(channel) session.commit() time.sleep(config.SPI_GENERATION_INTERVAL * 3) return channel
def channels_edit(request, id): """Edit a channel.""" object = None errors = [] if id != '-': object = Channel.query.join(Station).filter( Channel.id == int(id), Station.orga == int( request.args.get('ebuio_orgapk') or request.form.get('ebuio_orgapk'))).first() if request.method == 'POST': if not object: object = Channel() object.station_id = int(request.form.get('station')) # Get values for x in [ 'name', 'type_id', 'ecc_id', 'pi', 'frequency', 'eid', 'sid', 'scids', 'appty_uatype', 'pa', 'tx', 'cc', 'fqdn', 'serviceIdentifier' ]: val = request.form.get(x) if val == '': val = None # Secial case : CC if x == 'cc' and val is not None: cc_obj = Ecc.query.filter_by(id=val).first() val = cc_obj.pi + cc_obj.ecc setattr(object, x, val) # Check errors if object.name == '' or object.name is None: errors.append("Please set a name") # Set to '' useless values, and check if values needed are present list_props = None for (type_id, _, type_props) in Channel.TYPE_ID_CHOICES: if type_id == object.type_id: list_props = type_props if list_props is None: errors.append('Type not found oO') if list_props: for x in [ 'ecc_id', 'pi', 'frequency', 'eid', 'sid', 'scids', 'appty_uatype', 'pa', 'tx', 'cc', 'fqdn', 'serviceIdentifier' ]: if x in list_props: # Want it ? Keep it ! if x != 'appty_uatype' and x != 'pa': # Exception if getattr(object, x) is None or getattr(object, x) == '': errors.append(x + " cannot be empty") else: setattr(object, x, None) # Check each prop if object.pi is not None: if not re.match(r"^[a-fA-F0-9]{4}$", object.pi): errors.append("pi must be 4 characters in hexadecimal") if object.frequency is not None: if not re.match(r"^[0-9]{5}$", object.frequency) and object.frequency != '*': errors.append("frequency must be 5 digits or *") if object.eid is not None: if not re.match(r"^[a-fA-F0-9]{4}$", object.eid): errors.append("eid must be 4 characters in hexadecimal") if object.sid is not None: if not re.match(r"^[a-fA-F0-9]{4}([a-fA-F0-9]{4})?$", object.sid): errors.append("sid must be 4 or 8 characters in hexadecimal") if object.scids is not None: if not re.match(r"^[a-fA-F0-9]([a-fA-F0-9]{2})?$", object.scids): errors.append("scids must be 1 or 3 characters in hexadecimal") if object.appty_uatype is not None: if not re.match(r"^[a-fA-F0-9]{2}\-[a-fA-F0-9]{3}$", object.appty_uatype): errors.append( "appty_uatype must be 2 char hexadecimal, hyphen, 3 char hexadecimal" ) if object.pa is not None: if object.pa < 1 or object.pa > 1023: errors.append("pa must be between 1 and 1023") if object.tx is not None: if not re.match(r"^[a-fA-F0-9]{5}$", object.tx): errors.append("tx must be 5 characters in hexadecimal") if object.cc is not None: if not re.match(r"^[a-fA-F0-9]{3}$", object.cc): errors.append("cc must be 3 characters in hexadecimal") if object.fqdn is not None: if not re.match(r"^[a-zA-Z\d-]{,63}(\.[a-zA-Z\d-]{,63}).$", object.fqdn): errors.append("fqdn must be a domain name") if object.serviceIdentifier is not None: if not re.match(r"^[a-f0-9]{,16}$", object.serviceIdentifier): errors.append( "serviceIdentifier must be up to 16 characters in hexadecimal, lowercase" ) # Check station sta = Station.query.filter_by(id=object.station_id).first() if not sta or sta.orga != int(request.form.get('ebuio_orgapk')): errors.append("Please set a station") # If no errors, save if not errors: if not object.id: db.session.add(object) db.session.commit() return PlugItRedirect('channels/?saved=yes') if object: object = object.json stations = [] for station in Station.query.filter_by(orga=int( request.args.get('ebuio_orgapk') or request.form.get('ebuio_orgapk'))).all(): stations.append(station.json) return { 'object': object, 'errors': errors, 'stations': stations, 'types_id': Channel.TYPE_ID_CHOICES }
def channels_edit(request, id): """Edit a channel.""" station = None channel = None errors = [] station_id = request.args.get('station_id') if station_id: station = Station.query.filter( Station.id == station_id, Station.orga == int( request.args.get('ebuio_orgapk') or request.form.get('ebuio_orgapk'))).first() if id != '-': channel = Channel.query.join(Station).filter( Channel.id == int(id), Station.orga == int( request.args.get('ebuio_orgapk') or request.form.get('ebuio_orgapk'))).first() if request.method == 'POST': if not channel: channel = Channel() channel.station_id = int(request.form.get('station')) # Get values for x in [ 'name', 'type_id', 'ecc_id', 'pi', 'frequency', 'eid', 'sid', 'scids', 'appty_uatype', 'pa', 'tx', 'cc', 'fqdn', 'stream_url', 'mime_type', 'bitrate', 'serviceIdentifier', 'fk_client' ]: val = request.form.get(x) if val == '': val = None setattr(channel, x, val) # Check errors if channel.name == '' or channel.name is None: errors.append("Please set a name") # Set to '' useless values, and check if values needed are present list_props = None for (type_id, _, type_props) in Channel.TYPE_ID_CHOICES: if type_id == channel.type_id: list_props = type_props if list_props is None: errors.append('Type not found!') if list_props: for x in [ 'ecc_id', 'pi', 'frequency', 'eid', 'sid', 'scids', 'appty_uatype', 'pa', 'tx', 'cc', 'fqdn', 'mime_type', 'stream_url', 'bitrate', 'serviceIdentifier' ]: if x in list_props: # Want it ? Keep it ! if x != 'appty_uatype' and x != 'pa': # Exception if getattr(channel, x) is None or getattr(channel, x) == '': errors.append(x + " cannot be empty") else: setattr(channel, x, None) # Check each prop if channel.pi is not None: if not re.match(r"^[a-fA-F0-9]{4}$", channel.pi): errors.append("pi must be 4 characters in hexadecimal") if channel.frequency is not None: if not re.match(r"^[0-9]{5}$", channel.frequency) and channel.frequency != '*': errors.append("frequency must be 5 digits or *") if channel.eid is not None: if not re.match(r"^[a-fA-F0-9]{4}$", channel.eid): errors.append("eid must be 4 characters in hexadecimal") if channel.sid is not None: if not re.match(r"^[a-fA-F0-9]{4}([a-fA-F0-9]{4})?$", channel.sid): errors.append("sid must be 4 or 8 characters in hexadecimal") if channel.scids is not None: if not re.match(r"^[a-fA-F0-9]([a-fA-F0-9]{2})?$", channel.scids): errors.append("scids must be 1 or 3 characters in hexadecimal") if channel.appty_uatype is not None: if not re.match(r"^[a-fA-F0-9]{2}\-[a-fA-F0-9]{3}$", channel.appty_uatype): errors.append( "appty_uatype must be 2 char hexadecimal, hyphen, 3 char hexadecimal" ) if channel.pa is not None: if channel.pa < 1 or channel.pa > 1023: errors.append("pa must be between 1 and 1023") if channel.tx is not None: if not re.match(r"^[a-fA-F0-9]{5}$", channel.tx): errors.append("tx must be 5 characters in hexadecimal") if channel.cc is not None: if not re.match(r"^[a-fA-F0-9]{3}$", channel.cc): errors.append("cc must be 3 characters in hexadecimal") if channel.fqdn is not None: channel.fqdn = channel.fqdn.rstrip('.') if not re.match( r"(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+[a-zA-Z]{2,63}\.?$)", channel.fqdn): errors.append("fqdn must be a domain name") if channel.serviceIdentifier is not None: if not re.match(r"^[a-z0-9]{,16}$", channel.serviceIdentifier): errors.append( "serviceIdentifier must be up to 16 letters or number, lowercase" ) if channel.mime_type is not None: if not re.match(r"^([!-\.0-~]{1,}\/[!-\.0-~]{1,})+$", channel.mime_type): errors.append("mime_type must be of format string/string ") if channel.bitrate is not None: if not re.match(r"^[0-9]+$", channel.bitrate): errors.append("bitrate must be digits") # Check station sta = Station.query.filter_by(id=channel.station_id).first() if not sta or sta.orga != int(request.form.get('ebuio_orgapk')): errors.append("Please set a station") # If no errors, save if not errors: if not channel.id: db.session.add(channel) db.session.commit() # Update the service entries for the channel channel.updateservicefollowingentry() if station: return PlugItRedirect( ('stations/%s/channels?saved=yes') % station.id) else: return PlugItRedirect('channels/?saved=yes') default_country = None plugitapi = PlugItAPI(config.API_URL) orga_id = request.args.get('ebuio_orgapk') or request.form.get( 'ebuio_orgapk') orga = plugitapi.get_orga(orga_id) if orga.codops: sp = ServiceProvider.query.filter_by(codops=orga.codops).order_by( ServiceProvider.codops).first() if sp: cc_obj = CountryCode.query.filter_by( iso=sp.location_country).first() if cc_obj: default_country = cc_obj.id if channel: channel = channel.json stations = [] for s in Station.query.filter_by(orga=int( request.args.get('ebuio_orgapk') or request.form.get('ebuio_orgapk')), parent=None).all(): stations.append(s.json) if station: station = station.json clients = [gen_default_client()] + Clients.query.filter_by( orga=orga_id).order_by(Clients.id).all() return { 'object': channel, 'errors': errors, 'stations': stations, 'station': station, 'types_id': Channel.TYPE_ID_CHOICES, 'default_country': default_country, 'clients': map(lambda c: c.json, clients) }
def string_to_channel(linedata, station): object = Channel() object.station_id = station # Split line data = linedata.split(',') if len(data) < 2: return None # Define Type object.name = data[0] object.type_id = data[1] # FM # name fm ecc pid frequency if object.type_id == 'fm': if len(data) < 5: return None # ECC cc_obj = Ecc.query.filter_by(iso=data[2].upper()).first() if not cc_obj: ecc_pi = data[2][:1].upper() ecc_ecc = data[2][1:].upper() cc_obj = Ecc.query.filter_by(pi=ecc_pi, ecc=ecc_ecc).first() if cc_obj: object.ecc_id = cc_obj.id # PI object.pi = data[3] object.frequency = data[4] # DAB # name dab ecc eid sid scids UAType PA elif object.type_id == 'dab': if len(data) < 6: return None # ECC cc_obj = Ecc.query.filter_by(iso=data[2].upper()).first() if not cc_obj: ecc_pi = data[2][:1].upper() ecc_ecc = data[2][1:].upper() cc_obj = Ecc.query.filter_by(pi=ecc_pi, ecc=ecc_ecc).first() if cc_obj: object.ecc_id = cc_obj.id # eid object.eid = data[3] object.sid = data[4] object.scids = data[5] # Optional Elements if len(data) >= 7: object.appty_uatype = data[6] if len(data) >= 8: object.pa = data[7] # DRM # name drm sid elif object.type_id == 'drm': if len(data) < 3: return None # sid object.sid = data[2] # AMSS elif object.type_id == 'amss': if len(data) < 3: return None # sid object.sid = data[2] # HD Radio # name hd cc tx elif object.type_id == 'hd': if len(data) < 4: return None # cc object.cc = data[2] object.tx = data[3] # IP # name ip fqdn service id elif object.type_id == 'ip': if len(data) < 4: return None # cc object.fqdn = data[2] object.serviceIdentifier = data[3] else: return None return object