<commit_before>from math import ceil

from django.shortcuts import render
from events.services.EventService import EventService
from datetime import datetime
from django.conf import settings


def home_page(request):
    page = _get_current_page(request)

    service = EventService(settings.API_BASE_ADDRESS)
    events = service.events(page=page)
    date = datetime.now()

    pagination = events['response']['pagination']

    return render(request, 'home.html', {
        'events': _transform_api_response_to_model_list(events['response']['events']),
        'date': date,
        'current_page': page,
        'pages': _page_range(pagination['total'])
    })


def events_page(request, month, day):
    page = _get_current_page(request)

    service = EventService(settings.API_BASE_ADDRESS)
    events = service.events(month, day, page)

    today = datetime.now()

    date = datetime.strptime(
        '{} {} {}'.format(day, month, today.year), '%d %B %Y')

    pagination = events['response']['pagination']

    return render(request, 'home.html', {
        'events': _transform_api_response_to_model_list(events['response']['events']),
        'date': date,
        'current_page': page,
        'pages': _page_range(pagination['total'])
    })


def playlist_page(request):
    date = datetime.now()

    service = EventService(settings.API_BASE_ADDRESS)
    playlist = service.playlist()

    tracks = playlist['response']['tracks']

    spotify_playlist = ",".join([track['spotifyId'].rsplit(':', 1)[1] for track in tracks])

    return render(request, 'playlist.html', {
        'date': date,
        'playlist': tracks,
        'spotify_playlist': spotify_playlist
    })


def about_page(request):
    return render(request, 'about.html')


def _get_current_page(request):
    return int(request.GET.get('page', 1))


def _page_range(total):
    return range(1, 1 + ceil(total / EventService.RESULTS_PER_PAGE))


def _transform_api_response_to_model_list(events):
    return [_api_event_to_event_model(event) for event in events]


def _api_event_to_event_model(event):
    name = event.get('name', None)

    return Event(event["date"], event["description"], event["type"], name)


class Event(object):
    TWITTER_MSG_LEN = 140
    TWITTER_HASH_TAG = "#thisdayinmusic"
    TWITTER_USER = "@today_in_music"<commit_msg>Clarify spotify playlist calculation<commit_after>from math import ceil

from django.shortcuts import render
from events.services.EventService import EventService
from datetime import datetime
from django.conf import settings


def home_page(request):
    page = _get_current_page(request)

    service = EventService(settings.API_BASE_ADDRESS)
    events = service.events(page=page)
    date = datetime.now()

    pagination = events['response']['pagination']

    return render(request, 'home.html', {
        'events': _transform_api_response_to_model_list(events['response']['events']),
        'date': date,
        'current_page': page,
        'pages': _page_range(pagination['total'])
    })


def events_page(request, month, day):
    page = _get_current_page(request)

    service = EventService(settings.API_BASE_ADDRESS)
    events = service.events(month, day, page)

    today = datetime.now()

    date = datetime.strptime(
        '{} {} {}'.format(day, month, today.year), '%d %B %Y')

    pagination = events['response']['pagination']

    return render(request, 'home.html', {
        'events': _transform_api_response_to_model_list(events['response']['events']),
        'date': date,
        'current_page': page,
        'pages': _page_range(pagination['total'])
    })


def playlist_page(request):
    date = datetime.now()

    service = EventService(settings.API_BASE_ADDRESS)
    playlist = service.playlist()

    tracks = playlist['response']['tracks']

    spotify_playlist = _create_spotify_playlist(tracks)

    return render(request, 'playlist.html', {
        'date': date,
        'playlist': tracks,
        'spotify_playlist': spotify_playlist
    })


def _create_spotify_playlist(tracks):
    return ",".join([_remove_spotify_prefix(track) for track in tracks])


def _remove_spotify_prefix(track):
    return track['spotifyId'].rsplit(':', 1)[1]


def about_page(request):
    return render(request, 'about.html')


def _get_current_page(request):
    return int(request.GET.get('page', 1))


def _page_range(total):
    return range(1, 1 + ceil(total / EventService.RESULTS_PER_PAGE))


def _transform_api_response_to_model_list(events):
    return [_api_event_to_event_model(event) for event in events]


def _api_event_to_event_model(event):
    name = event.get('name', None)

    return Event(event["date"], event["description"], event["type"], name)


class Event(object):
    TWITTER_MSG_LEN = 140
    TWITTER_HASH_TAG = "#thisdayinmusic"
    TWITTER_USER = "@today_in_music"
{'fraction_non_alphanumeric': 0.13135680461190036, 'fraction_numerical': 0.0032942145357216387, 'mean_word_length': 4.2012847965738755, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4690104', 'n_tokens_mistral': 1574, 'n_tokens_neox': 1489, 'n_words': 304}