Пример #1
0
def artist_network(request, artist_slug):
    results = Artist.objects.filter(slug=artist_slug)
    if results:
        a = results[0]
        primary_examples = a.primary_examples.all()
        featured_examples = a.featured_examples.all()

        network = []
        artist_cache = dict()
        example_cache = dict()

        for example in primary_examples:
            if example.song_title not in example_cache:
                example_cache[example.song_title] = 1
                for ar in example.feat_artist.all():
                    if ar not in artist_cache:
                        artist_cache[ar] = 1
                    else:
                        artist_cache[ar] += 1

        for example in featured_examples:
            if example.song_title not in example_cache:
                example_cache[example.song_title] = 1
                for ar in example.artist.all():
                    if ar not in artist_cache:
                        artist_cache[ar] = 1
                    else:
                        artist_cache[ar] += 1
                for ar in example.feat_artist.exclude(slug=a.slug):
                    if ar is not a:
                        if ar not in artist_cache:
                            artist_cache[ar] = 1
                        else:
                            artist_cache[ar] += 1

        for artist in artist_cache:
            img = check_for_image(artist.slug)
            if 'none' not in img:
                artist_object = {
                  "name": reformat_name(artist.name),
                  "link": "/artists/" + artist.slug,
                  "img":  img,
                  "size": artist_cache[artist]
                }
                network.append(artist_object)

        data = {
            'name': reformat_name(a.name),
            'img': check_for_image(a.slug),
            'link': "/artists/" + a.slug,
            'size': 5,
            'children': network
        }
        return Response(data)
    else:
        return Response({})
Пример #2
0
def song(request, song_slug):
    if request.method == 'POST':
        form = SongForm(request.POST)
        if form.is_valid():
            song = Song.objects.get(slug=song_slug)
            song.title = form.cleaned_data['title']
            song.release_date = form.cleaned_data["release_date"]
            song.release_date_string = form.cleaned_data["release_date_string"]
            song.artist_name = form.cleaned_data["artist_name"]
            song.album = form.cleaned_data["album"]
            song.lyrics = form.cleaned_data["lyrics"]
            song.release_date_verified = form.cleaned_data["release_date_verified"]
            song.save()

    song = get_list_or_404(Song, slug=song_slug)[0]
    published_entries = Entry.objects.filter(publish=True).values_list('headword', flat=True)
    template = loader.get_template('dictionary/song.html')
    same_dates = [
        {
            'title': s.title,
            'artist_name': reformat_name(s.artist_name),
            'artist_slug': s.artist_slug,
            'slug': s.slug
        } for s in Song.objects.filter(release_date=song.release_date).order_by('artist_name') if s != song]
    image = check_for_image(song.artist_slug, 'artists', 'full')
    thumb = check_for_image(song.artist_slug, 'artists', 'thumb')
    form = SongForm(instance=song)
    context = {
        "title": song.title,
        "slug": song.slug,
        "image": image,
        "thumb": thumb,
        "artist_name": reformat_name(song.artist_name),
        "artist_slug": song.artist_slug,
        "primary_artist": [build_artist(a) for a in song.artist.all()],
        "featured_artists": [build_artist(a) for a in song.feat_artist.all()],
        "release_date": song.release_date,
        "release_date_string": song.release_date_string,
        "album": song.album,
        "examples": [build_example(example, published_entries, rf=True) for example in song.examples.all()],
        "same_dates": same_dates,
        "form": None
    }

    if request.user.is_authenticated():
        context['form'] = form

    return HttpResponse(template.render(context, request))
Пример #3
0
def semantic_classes(request):
    template = loader.get_template('dictionary/semantic_classes.html')
    results = SemanticClass.objects.annotate(num_senses=Count('senses')).order_by('-num_senses')
    semantic_class_count = results.count()
    context = {
        'semantic_class_count': semantic_class_count,
        'image': check_for_image('semantic-classes', 'semantic_classes', 'full')
    }
    return HttpResponse(template.render(context, request))
Пример #4
0
def domains(request):
    template = loader.get_template('dictionary/domains.html')
    results = Domain.objects.annotate(num_senses=Count('senses')).order_by('-num_senses')
    domain_count = results.count()
    context = {
        'domain_count': domain_count,
        'image': check_for_image('domains', 'domains', 'full')
    }
    return HttpResponse(template.render(context, request))
Пример #5
0
def semantic_class(request, semantic_class_slug):
    template = loader.get_template('dictionary/semantic_class.html')
    semantic_class = get_object_or_404(SemanticClass, slug=semantic_class_slug)
    sense_count = semantic_class.senses.filter(publish=True).order_by('headword').count()
    context = {
        'semantic_class': un_camel_case(semantic_class.name),
        'slug': semantic_class_slug,
        'sense_count': sense_count,
        'image': check_for_image(semantic_class.slug, 'semantic_classes', 'full')
    }
    return HttpResponse(template.render(context, request))
Пример #6
0
def song_artist_network(request, song_slug):
    results = Song.objects.filter(slug=song_slug)
    if results:
        song = results[0]
        network = []
        artist_cache = dict()

        a = song.artist.first()

        for ar in song.feat_artist.all():
            if ar not in artist_cache:
                artist_cache[ar] = 5
            else:
                artist_cache[ar] += 1

        for artist in artist_cache:
            img = check_for_image(artist.slug)
            artist_object = {
              "name": reformat_name(artist.name),
              "link": "/artists/" + artist.slug,
              "img":  img,
              "size": artist_cache[artist]
            }
            network.append(artist_object)

        if a:
            data = {
                'name': reformat_name(a.name),
                'img': check_for_image(a.slug),
                'link': "/artists/" + a.slug,
                'size': 5,
                'children': network
            }
            return Response(data)
        else:
            return Response({})
    else:
        return Response({})
Пример #7
0
def domain(request, domain_slug):
    template = loader.get_template('dictionary/domain.html')
    domain = get_object_or_404(Domain, slug=domain_slug)
    sense_objects = domain.senses.filter(publish=True).order_by('headword')
    published = Entry.objects.filter(publish=True).values_list('slug', flat=True)
    senses = [build_sense_preview(sense, published) for sense in sense_objects]
    senses_data = [{"word": sense.headword, "weight": sense.examples.count()} for sense in sense_objects]
    data = [sense.headword for sense in sense_objects]
    context = {
        'domain': un_camel_case(domain.name),
        'slug': domain_slug,
        'senses': senses,
        'senses_data': json.dumps(senses_data),
        'published_entries': published,
        'image': check_for_image(domain.slug, 'domains', 'full'),
        'data': json.dumps(data)
    }
    return HttpResponse(template.render(context, request))
Пример #8
0
def place(request, place_slug):
    place = get_object_or_404(Place, slug=place_slug)
    template = loader.get_template('dictionary/place.html')

    published = Entry.objects.filter(publish=True).values_list('headword', flat=True)
    entity_results = NamedEntity.objects.filter(pref_label_slug=place_slug)
    examples = []

    artists = collect_place_artists(place, [])

    artists_with_image = [artist for artist in artists if '__none.png' not in artist['image']]
    artists_without_image = [artist for artist in artists if '__none.png' in artist['image']]

    contains = [{'name': abbreviate_place_name(c.name), 'slug': c.slug} for c in place.contains.order_by('name')]
    within = {}
    if ', ' in place.full_name:
        w_name = ', '.join(place.full_name.split(', ')[1:])
        w_slug = slugify(w_name)
        within = {'name': abbreviate_place_name(w_name), 'slug': w_slug}


    # TODO: reorder examples by release_date in case of multiple entities
    if len(entity_results) >= 1:
        for entity in entity_results:
            examples += [build_example(example, published, rf=True) for example in entity.examples.order_by('release_date')]

    context = {
        'place': place.name,
        'place_name_full': place.full_name,
        'slug': place.slug,
        'contains': contains,
        'within': within,
        'num_artists': len(artists),
        'artists_with_image': artists_with_image,
        'artists_without_image': artists_without_image,
        'image': check_for_image(place.slug, 'places', 'full'),
        'examples': sorted(examples, key=itemgetter('release_date'))[:NUM_QUOTS_TO_SHOW],
        'num_examples': len(examples)
    }
    return HttpResponse(template.render(context, request))
Пример #9
0
def artist(request, artist_slug):
    artist_results = get_list_or_404(Artist, slug=artist_slug)
    artist = artist_results[0]
    origin_results = artist.origin.all()
    if origin_results:
        origin = origin_results[0].full_name
        origin_slug = origin_results[0].slug
        long = origin_results[0].longitude
        lat = origin_results[0].latitude
    else:
        origin = ''
        origin_slug = ''
        long = ''
        lat = ''
    published = Entry.objects.filter(publish=True).values_list('slug', flat=True)
    template = loader.get_template('dictionary/artist.html')
    entity_results = NamedEntity.objects.filter(pref_label_slug=artist_slug)

    primary_senses = [
        {
            'headword': sense.headword,
            'slug': sense.slug,
            'xml_id': sense.xml_id,
            'example_count': sense.examples.filter(artist=artist).count(),
            'examples': [build_example(example, published) for example in sense.examples.filter(artist=artist).order_by('release_date')]
        } for sense in artist.primary_senses.filter(publish=True).annotate(num_examples=Count('examples')).order_by('-num_examples')[:5]
    ]

    featured_senses = [
        {
            'headword': sense.headword,
            'slug': sense.slug,
            'xml_id': sense.xml_id,
            'example_count': sense.examples.filter(feat_artist=artist).count(),
            'examples': [build_example(example, published) for example in sense.examples.filter(feat_artist=artist).order_by('release_date')]
        } for sense in artist.featured_senses.filter(publish=True).annotate(num_examples=Count('examples')).order_by('num_examples')[:5]
    ]

    entity_examples = []
    if entity_results:
        entity_examples = [build_example(example, published) for example in entity_results[0].examples.all()]

    image = check_for_image(artist.slug, 'artists', 'full')
    thumb = check_for_image(artist.slug, 'artists', 'thumb')
    name = reformat_name(artist.name)
    primary_sense_count = artist.primary_senses.filter(publish=True).count()
    featured_sense_count = artist.featured_senses.filter(publish=True).count()

    context = {
        'artist': name,
        'slug': artist.slug,
        'origin': origin,
        'origin_slug': origin_slug,
        'longitude': long,
        'latitude': lat,
        'primary_sense_count': primary_sense_count,
        'featured_sense_count': featured_sense_count,
        'primary_senses': primary_senses,
        'featured_senses': featured_senses,
        'entity_examples': entity_examples,
        'entity_example_count': len(entity_examples),
        'image': image,
        'thumb': thumb,
        'also_known_as': [{
            'artist': aka.name,
            'slug': aka.slug
        } for aka in artist.also_known_as.all()]
    }
    return HttpResponse(template.render(context, request))
Пример #10
0
def stats(request):

    LIST_LENGTH = 5

    published_headwords = Entry.objects.filter(publish=True).values_list('headword', flat=True)

    entry_count = Entry.objects.filter(publish=True).count()
    sense_count = Sense.objects.filter(publish=True).count()
    example_count = Example.objects.all().count()
    best_attested_senses = [sense for sense in Sense.objects.annotate(num_examples=Count('examples')).order_by('-num_examples')[:LIST_LENGTH]]
    best_attested_sense_count = best_attested_senses[0].num_examples
    best_attested_domains = [domain for domain in Domain.objects.annotate(num_senses=Count('senses')).order_by('-num_senses')[:LIST_LENGTH]]
    best_attested_semantic_classes = [semantic_class for semantic_class in SemanticClass.objects.annotate(num_senses=Count('senses')).order_by('-num_senses')[:LIST_LENGTH]]
    most_cited_songs = [song for song in Song.objects.annotate(num_examples=Count('examples')).order_by('-num_examples')[:LIST_LENGTH]]
    most_cited_song_count = most_cited_songs[0].num_examples
    most_mentioned_places = [e for e in NamedEntity.objects.filter(entity_type='place').annotate(num_examples=Count('examples')).order_by('-num_examples')[:LIST_LENGTH]]
    most_mentioned_artists = [e for e in NamedEntity.objects.filter(entity_type='artist').annotate(num_examples=Count('examples')).order_by('-num_examples')[:LIST_LENGTH]]
    most_cited_artists = [artist for artist in Artist.objects.annotate(num_cites=Count('primary_examples')).order_by('-num_cites')[:LIST_LENGTH]]
    examples_date_ascending = Example.objects.order_by('release_date')
    examples_date_descending = Example.objects.order_by('-release_date')
    seventies = Example.objects.filter(release_date__range=["1970-01-01", "1979-12-31"]).count()
    eighties = Example.objects.filter(release_date__range=["1980-01-01", "1989-12-31"]).count()
    nineties = Example.objects.filter(release_date__range=["1990-01-01", "1999-12-31"]).count()
    noughties = Example.objects.filter(release_date__range=["2000-01-01", "2009-12-31"]).count()
    twenty_tens = Example.objects.filter(release_date__range=["2010-01-01", "2019-12-31"]).count()
    decade_max = max([seventies, eighties, nineties, noughties, twenty_tens])
    places = [place for place in Place.objects.annotate(num_artists=Count('artists')).order_by('-num_artists')[:LIST_LENGTH]]
    domain_count = best_attested_domains[0].num_senses
    semantic_class_count = best_attested_semantic_classes[0].num_senses
    place_count = count_place_artists(places[0], [0])
    place_mention_count = most_mentioned_places[0].num_examples
    artist_mention_count = most_mentioned_artists[0].num_examples
    artist_cite_count = most_cited_artists[0].num_cites

    template = loader.get_template('dictionary/stats.html')

    WIDTH_ADJUSTMENT = 5

    context = {
        'num_entries': entry_count,
        'num_senses': sense_count,
        'num_examples': example_count,
        'best_attested_senses': [
            {
                'headword': sense.headword,
                'slug': sense.slug,
                'anchor': sense.xml_id,
                'definition': sense.definition,
                'num_examples': sense.num_examples,
                'width': (sense.num_examples / best_attested_sense_count) * 100 - WIDTH_ADJUSTMENT

            } for sense in best_attested_senses
            ],
        'best_attested_domains': [
            {
                'name': domain.name,
                'slug': domain.slug,
                'num_senses': domain.num_senses,
                'width': (domain.num_senses / domain_count) * 100 - WIDTH_ADJUSTMENT

            } for domain in best_attested_domains
            ],
        'best_attested_semantic_classes': [
            {
                'name': semantic_class.name,
                'slug': semantic_class.slug,
                'num_senses': semantic_class.num_senses,
                'width': (semantic_class.num_senses / semantic_class_count) * 100 - WIDTH_ADJUSTMENT

            } for semantic_class in best_attested_semantic_classes
            ],
        'most_cited_songs': [
            {
                'title': song.title,
                'slug': song.slug,
                'artist_name': song.artist_name,
                'artist_slug':song.artist_slug,
                'num_examples': song.num_examples,
                'width': (song.num_examples / most_cited_song_count) * 100 - WIDTH_ADJUSTMENT
            } for song in most_cited_songs
            ],
        'most_mentioned_places': [
            {
                'name': e.name,
                'slug': e.pref_label_slug,
                'pref_label': e.pref_label,
                'entity_type': e.entity_type,
                'num_examples': e.num_examples,
                'width': (e.num_examples / place_mention_count) * 100 - WIDTH_ADJUSTMENT
            } for e in most_mentioned_places
            ],
        'most_cited_artists': [
            {
                'name': artist.name,
                'slug': artist.slug,
                'image': check_for_image(artist.slug, 'artists', 'thumb'),
                'count': artist.num_cites,
                'width': (artist.num_cites / artist_cite_count) * 100 - WIDTH_ADJUSTMENT
            } for artist in most_cited_artists[:LIST_LENGTH+1]
            ],
        'most_mentioned_artists': [
            {
                'name': e.pref_label,
                'slug': e.pref_label_slug,
                'image': check_for_image(e.pref_label_slug, 'artists', 'thumb'),
                'pref_label': e.pref_label,
                'entity_type': e.entity_type,
                'num_examples': e.num_examples,
                'width': (e.num_examples / artist_mention_count) * 100 - WIDTH_ADJUSTMENT
            } for e in most_mentioned_artists
            ],
        'num_artists': len(most_cited_artists),
        'num_places': len(places),
        'best_represented_places': [
            {
                'name': p.name.split(', ')[0],
                'slug': p.slug,
                'num_artists': count_place_artists(p, [0]),
                'width': (count_place_artists(p, [0]) / place_count) * 100 - WIDTH_ADJUSTMENT
            } for p in places
            ],
        'earliest_date': {'example': [build_example(date, published_headwords) for date in examples_date_ascending[:LIST_LENGTH]]},
        'latest_date': {'example': [build_example(date, published_headwords) for date in examples_date_descending[:LIST_LENGTH]]},
        'num_seventies': seventies,
        'seventies_width': (seventies / decade_max) * 100,
        'num_eighties': eighties,
        'eighties_width': (eighties / decade_max) * 100 - WIDTH_ADJUSTMENT,
        'num_nineties': nineties,
        'nineties_width': (nineties / decade_max) * 100 - WIDTH_ADJUSTMENT,
        'num_noughties': noughties,
        'noughties_width': (noughties / decade_max) * 100 - WIDTH_ADJUSTMENT,
        'num_twenty_tens': twenty_tens,
        'twenty_tens_width': (twenty_tens / decade_max) * 100 - WIDTH_ADJUSTMENT
    }
    return HttpResponse(template.render(context, request))
Пример #11
0
def sense_timeline(request, sense_id):
    EXX_THRESHOLD = 30
    published_entries = Entry.objects.filter(publish=True).values_list('headword', flat=True)
    results = Sense.objects.filter(xml_id=sense_id)
    if results:
        sense_object = results[0]
        exx = sense_object.examples.order_by('release_date')
        exx_count = exx.count()
        if exx_count > 30:
            exx = [ex for ex in reduce_ordered_list(exx, EXX_THRESHOLD)]
        events = [build_timeline_example(example, published_entries) for example in exx if check_for_image(example.artist_slug, 'artists', 'full')]
        data = {
            "events": events
        }
        return Response(data)
    else:
        return Response({})
Пример #12
0
def artists_missing_metadata(request):
    BASE_URL = "http://" + request.META['HTTP_HOST']

    primary_results_no_image = [artist for artist in Artist.objects.annotate(num_cites=Count('primary_examples')).order_by('-num_cites')]
    feat_results_no_image = [artist for artist in Artist.objects.annotate(num_cites=Count('featured_examples')).order_by('-num_cites')]

    primary_results_no_origin = [artist for artist in Artist.objects.filter(origin__isnull=True).annotate(num_cites=Count('primary_examples')).order_by('-num_cites')]
    feat_results_no_origin = [artist for artist in Artist.objects.filter(origin__isnull=True).annotate(num_cites=Count('featured_examples')).order_by('-num_cites')]

    if primary_results_no_image or feat_results_no_image:
        data = {
            'primary_artists_no_image': [
                                   {
                                       'name': artist.name,
                                       'slug': artist.slug,
                                       'site_link': BASE_URL + '/artists/' + artist.slug,
                                       'num_cites': artist.num_cites
                                   } for artist in primary_results_no_image if '__none' in check_for_image(artist.slug)][:3],
            'feat_artists_no_image': [
                                   {
                                       'name': artist.name,
                                       'slug': artist.slug,
                                       'site_link': BASE_URL + '/artists/' + artist.slug,
                                       'num_cites': artist.num_cites
                                   } for artist in feat_results_no_image if '__none' in check_for_image(artist.slug)][:3],
            'primary_artists_no_origin': [
                                   {
                                       'name': artist.name,
                                       'slug': artist.slug,
                                       'site_link': BASE_URL + '/artists/' + artist.slug,
                                       'num_cites': artist.num_cites
                                   } for artist in primary_results_no_origin][:3],
            'feat_artists_no_origin': [
                                   {
                                       'name': artist.name,
                                       'slug': artist.slug,
                                       'site_link': BASE_URL + '/artists/' + artist.slug,
                                       'num_cites': artist.num_cites
                                   } for artist in feat_results_no_origin][:3]
        }
        return Response(data)
    else:
        return Response({})