예제 #1
0
def add_track_to_db(data):
    """ Socket used to add a track in the database. """
    if (in_subnet(request.remote_addr)):
        # Use underground DB or main DB
        if data['is_main_logger'] == 'true':
            Track = MainTrack
            Group = MainGroup
        elif data['is_main_logger'] == 'false':
            Track = UndergroundTrack
            Group = UndergroundGroup
        else:
            print('ERROR: in request_initial_tracks socket ' +
                  data['is_main_logger'],
                  file=sys.stderr)
            return

        try:
            group = Group.query.filter_by(name=data['new_group']).first()

            track = Track(artist=data['new_artist'],
                          title=data['new_title'],
                          group=group,
                          created_at=datetime.strptime(data['new_time'],
                                                       '%m/%d/%y %I:%M %p'))

            db.session.add(track)
            db.session.commit()

            update_clients(-1, data)
        except ValueError:
            # Invalid datetime format.
            emit('invalid_add_datetime')
예제 #2
0
def rivendell_update():
    """ Rivendell posts to this route with an XML document containing a
        track. This function then parses that XML and saves it to the
        main logger database.
    """
    if in_subnet(request.remote_addr):
        # Get XML root
        root = ET.fromstring(request.data)
        data = {}

        # Turn XML into dictionary
        for child in root:
            data[child.tag] = child.text

        # Create the new track and save it to the DB
        track = MainTrack(artist=data['artist'],
                          title=data['title'],
                          group=MainGroup.query.get(int(data['group'])),
                          created_at=datetime.now())
        db.session.add(track)
        db.session.commit()

        # Update clients
        socketio.emit('add_tracks', {
            'tracks': tracks_to_json(track),
            'is_main_logger': 'true'
        },
                      json=True)

        return 'Added track to main logger database.'
    else:
        return 'Access Denied'
예제 #3
0
def remove_track(data):
    """ Socket used to remove a track from the database. """
    if (in_subnet(request.remote_addr)):
        track_id = data['track_id']
        is_main_logger = data['is_main_logger']

        if is_main_logger == 'true':
            track = MainTrack.query.get(track_id)
        elif is_main_logger == 'false':
            track = UndergroundTrack.query.get(track_id)
        else:
            print('ERROR: in request_initial_tracks socket ' +
                  str(is_main_logger),
                  file=sys.stderr)
            return

        db.session.delete(track)
        db.session.commit()
        emit('removeTrack', {
            'track_id': track_id,
            'is_main_logger': is_main_logger
        },
             broadcast=True)
예제 #4
0
def udpupdate():
    """ Rivendell posts to this route with an XML document containing
        a track. This function then parses that XML and 
        saves it to the database 
    """
    if in_subnet(request.remote_addr):
        root = ET.fromstring(request.data)
        data = {}

        for child in root:
            data[child.tag] = child.text

        track = UndergroundTrack(artist=data['artist'],
                                 title=data['title'],
                                 group=UndergroundGroup.query.get(
                                     int(data['group'])),
                                 created_at=datetime.now())
        db.session.add(track)
        db.session.commit()

        return 'Added track to logger database'
    else:
        return 'Access Denied'
예제 #5
0
def commit_update(data):
    """ Socket used to update a track in the database. """
    if in_subnet(request.remote_addr):
        # Choose underground DB or main DB
        if data['is_main_logger'] == 'true':
            Track = MainTrack
            Group = MainGroup
        elif data['is_main_logger'] == 'false':
            Track = UndergroundTrack
            Group = UndergroundGroup
        else:
            print('ERROR: in request_initial_tracks socket ' +
                  data['is_main_logger'],
                  file=sys.stderr)
            return

        try:
            track = Track.query.get(data['track_id'])
            group = Group.query.filter_by(name=data['new_group']).first()

            if group is None:  # Invalid group name, show error and exit
                emit('invalid_update_group_name', data['track_id'])
                return None

            track.artist = data['new_artist']
            track.title = data['new_title']
            track.created_at = datetime.strptime(data['new_time'],
                                                 '%m/%d/%y %I:%M %p')
            track.group = group

            db.session.commit()

            emit('successful_update', track.id)
            update_clients(track.id, data)
        except ValueError:
            # Invalid datetime format.
            emit('invalid_update_datetime', data['track_id'])
예제 #6
0
def is_in_subnet():
    """ Route client uses to check if it is in the WITR Subnet """
    return str(in_subnet(request.remote_addr))
예제 #7
0
def underground():
    """ Renders the underground home/root page. """
    return render_template("index.html",
                           in_subnet=in_subnet(request.remote_addr),
                           stream_type="UDG")
예제 #8
0
def page():
    """ Renders the home/root url page. """
    return render_template("index.html",
                           in_subnet=in_subnet(request.remote_addr),
                           stream_type="FM")