def recent_pastes(request):
    def day(paste):
        return datetime.date(year=paste.created_at.year, month=paste.created_at.month, day=paste.created_at.day)

    # Order by descending creation date to avoid duplicate day results from
    # groupby().
    paste_qs = Paste.get_for_request(request).order_by("-created_at")
    paste_dict = dict([(p.pk, p) for p in paste_qs])

    file_qs = File.objects.filter(paste__in=paste_qs).select_related()
    file_dict = dict([(f.pk, f) for f in file_qs])

    paste_files = collections.defaultdict(list)
    for pk, f in file_dict.iteritems():
        paste_files[f.paste.pk].append(f)

    recent_pastes = []
    for date, paste_list in groupby(paste_qs, day):
        # Sort the pastes again to avoid incorrect ordering within a day, this
        # might be unnecessary if the user can't modify creation dates.
        paste_list = sorted(paste_list, key=lambda p: p.created_at, reverse=True)

        for paste in paste_list:
            paste.file_list = paste_files[paste.pk]

        recent_pastes.append((date, paste_list))

    return {"recent_pastes": recent_pastes}