Ejemplo n.º 1
0
def create_test_track(id=None,
                      duration=None,
                      tags=(),
                      attachments=(),
                      lyrics=None,
                      source_filename=None):
    def _get_tag(tag):
        return get_tag(tag, create_if_missing=True)

    def _get_attachment(attachment):
        if hasattr(attachment, 'location') and hasattr(attachment, 'type'):
            return create_attachment(attachment)
        else:
            return DBSession.query(Attachment).filter(
                Attachment.location.like('%%{0}%%'.format(attachment))).one()

    track = Track()
    track.id = id if id else random_string(10)
    track.duration = duration if duration else random.randint(60, 360)
    for tag in tags:
        track.tags.append(_get_tag(tag))
    for attachment in attachments:
        track.attachments.append(_get_attachment(attachment))
    track.lyrics = lyrics or ''
    track.source_filename = source_filename

    DBSession.add(track)

    return track
Ejemplo n.º 2
0
def track_import_post(request):
    existing_track_ids = _existing_tracks_dict().keys()

    for track_dict in _get_json_request(request):
        if track_dict['id'] in existing_track_ids:
            log.warning(
                'Exists: {source_filename} - {id}'.format(**track_dict))
            continue

        log.info('Import: {source_filename} - {id}'.format(**track_dict))
        track = Track()
        track.id = track_dict['id']
        track.source_filename = track_dict['source_filename']
        track.duration = track_dict['duration']
        track.lyrics = track_dict['lyrics']

        # Attachments
        for attachment_dict in track_dict['attachments']:
            assert attachment_dict['type'] in ATTACHMENT_TYPES
            attachment = Attachment()
            attachment.type = attachment_dict['type']
            attachment.location = attachment_dict['location']
            track.attachments.append(attachment)

        # Tags
        for tag_string in track_dict['tags']:
            tag = get_tag(tag_string, create_if_missing=True)
            if tag:
                track.tags.append(tag)
            elif tag_string:
                log.warning('null tag %s', tag_string)
        for duplicate_tag in (tag for tag in track.tags
                              if track.tags.count(tag) > 1):
            log.warning('Unneeded duplicate tag found %s in %s', duplicate_tag,
                        track.source_filename)
            track.tags.remove(duplicate_tag)

        DBSession.add(track)
        commit()

    request.registry.settings['karakara.tracks.version'] += 1
    return action_ok()
Ejemplo n.º 3
0
def track_import_post(request):
    existing_track_ids = _existing_tracks_dict().keys()

    for track_dict in _get_json_request(request):
        if track_dict['id'] in existing_track_ids:
            log.warning('Exists: {source_filename} - {id}'.format(**track_dict))
            continue

        log.info('Import: {source_filename} - {id}'.format(**track_dict))
        track = Track()
        track.id = track_dict['id']
        track.source_filename = track_dict['source_filename']
        track.duration = track_dict['duration']
        track.lyrics = track_dict['lyrics']

        # Attachments
        for attachment_dict in track_dict['attachments']:
            assert attachment_dict['type'] in ATTACHMENT_TYPES
            attachment = Attachment()
            attachment.type = attachment_dict['type']
            attachment.location = attachment_dict['location']
            track.attachments.append(attachment)

        # Tags
        for tag_string in track_dict['tags']:
            tag = get_tag(tag_string, create_if_missing=True)
            if tag:
                track.tags.append(tag)
            elif tag_string:
                log.warning('null tag %s', tag_string)
        for duplicate_tag in (tag for tag in track.tags if track.tags.count(tag) > 1):
            log.warning('Unneeded duplicate tag found %s in %s', duplicate_tag, track.source_filename)
            track.tags.remove(duplicate_tag)

        DBSession.add(track)
        commit()

    request.registry.settings['karakara.tracks.version'] += 1
    return action_ok()
Ejemplo n.º 4
0
def create_test_track(id=None, duration=None, tags=(), attachments=(), lyrics=None, source_filename=None):

    def _get_tag(tag):
        return get_tag(tag, create_if_missing=True)

    def _get_attachment(attachment):
        if hasattr(attachment, 'location') and hasattr(attachment, 'type'):
            return create_attachment(attachment)
        else:
            return DBSession.query(Attachment).filter(Attachment.location.like('%%{0}%%'.format(attachment))).one()

    track = Track()
    track.id = id if id else random_string(10)
    track.duration = duration if duration else random.randint(60, 360)
    for tag in tags:
        track.tags.append(_get_tag(tag))
    for attachment in attachments:
        track.attachments.append(_get_attachment(attachment))
    track.lyrics = lyrics or ''
    track.source_filename = source_filename

    DBSession.add(track)

    return track